CurveExtras.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * A bunch of parametric curves
  3. * @author zz85
  4. *
  5. * Formulas collected from various sources
  6. * http://mathworld.wolfram.com/HeartCurve.html
  7. * http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page6.html
  8. * http://en.wikipedia.org/wiki/Viviani%27s_curve
  9. * http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page4.html
  10. * http://www.mi.sanu.ac.rs/vismath/taylorapril2011/Taylor.pdf
  11. * http://prideout.net/blog/?p=44
  12. */
  13. ( function( Curves ) {
  14. // GrannyKnot
  15. function GrannyKnot() {
  16. THREE.Curve.call( this );
  17. }
  18. GrannyKnot.prototype = Object.create( THREE.Curve.prototype );
  19. GrannyKnot.prototype.constructor = GrannyKnot;
  20. GrannyKnot.prototype.getPoint = function( t ) {
  21. t = 2 * Math.PI * t;
  22. var x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t );
  23. var y = - 0.1 * Math.cos( 2 * t ) - 0.27 * Math.sin( 2 * t ) + 0.38 * Math.cos( 4 * t ) + 0.46 * Math.sin( 4 * t );
  24. var z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t );
  25. return new THREE.Vector3( x, y, z ).multiplyScalar( 20 );
  26. };
  27. // HeartCurve
  28. function HeartCurve( s ) {
  29. THREE.Curve.call( this );
  30. this.scale = ( s === undefined ) ? 5 : s;
  31. }
  32. HeartCurve.prototype = Object.create( THREE.Curve.prototype );
  33. HeartCurve.prototype.constructor = HeartCurve;
  34. HeartCurve.prototype.getPoint = function( t ) {
  35. t *= 2 * Math.PI;
  36. var x = 16 * Math.pow( Math.sin( t ), 3 );
  37. var y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t );
  38. var z = 0;
  39. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  40. };
  41. // Viviani's Curve
  42. function VivianiCurve( radius ) {
  43. THREE.Curve.call( this );
  44. this.radius = radius;
  45. }
  46. VivianiCurve.prototype = Object.create( THREE.Curve.prototype );
  47. VivianiCurve.prototype.constructor = VivianiCurve;
  48. VivianiCurve.prototype.getPoint = function( t ) {
  49. t = t * 4 * Math.PI; // normalized to 0..1
  50. var a = this.radius / 2;
  51. var x = a * ( 1 + Math.cos( t ) );
  52. var y = a * Math.sin( t );
  53. var z = 2 * a * Math.sin( t / 2 );
  54. return new THREE.Vector3( x, y, z );
  55. };
  56. // KnotCurve
  57. function KnotCurve() {
  58. THREE.Curve.call( this );
  59. }
  60. KnotCurve.prototype = Object.create( THREE.Curve.prototype );
  61. KnotCurve.prototype.constructor = KnotCurve;
  62. KnotCurve.prototype.getPoint = function( t ) {
  63. t *= 2 * Math.PI;
  64. var R = 10;
  65. var s = 50;
  66. var x = s * Math.sin( t );
  67. var y = Math.cos( t ) * ( R + s * Math.cos( t ) );
  68. var z = Math.sin( t ) * ( R + s * Math.cos( t ) );
  69. return new THREE.Vector3( x, y, z );
  70. };
  71. // HelixCurve
  72. function HelixCurve() {
  73. THREE.Curve.call( this );
  74. }
  75. HelixCurve.prototype = Object.create( THREE.Curve.prototype );
  76. HelixCurve.prototype.constructor = HelixCurve;
  77. HelixCurve.prototype.getPoint = function( t ) {
  78. var a = 30; // radius
  79. var b = 150; // height
  80. var t2 = 2 * Math.PI * t * b / 30;
  81. var x = Math.cos( t2 ) * a;
  82. var y = Math.sin( t2 ) * a;
  83. var z = b * t;
  84. return new THREE.Vector3( x, y, z );
  85. };
  86. // TrefoilKnot
  87. function TrefoilKnot( s ) {
  88. THREE.Curve.call( this );
  89. this.scale = ( s === undefined ) ? 10 : s;
  90. }
  91. TrefoilKnot.prototype = Object.create( THREE.Curve.prototype );
  92. TrefoilKnot.prototype.constructor = TrefoilKnot;
  93. TrefoilKnot.prototype.getPoint = function( t ) {
  94. t *= Math.PI * 2;
  95. var x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t );
  96. var y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t );
  97. var z = Math.sin( 3 * t );
  98. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  99. };
  100. // TorusKnot
  101. function TorusKnot( s ) {
  102. THREE.Curve.call( this );
  103. this.scale = ( s === undefined ) ? 10 : s;
  104. }
  105. TorusKnot.prototype = Object.create( THREE.Curve.prototype );
  106. TorusKnot.prototype.constructor = TorusKnot;
  107. TorusKnot.prototype.getPoint = function( t ) {
  108. var p = 3;
  109. var q = 4;
  110. t *= Math.PI * 2;
  111. var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  112. var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  113. var z = Math.sin( q * t );
  114. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  115. };
  116. // CinquefoilKnot
  117. function CinquefoilKnot( s ) {
  118. THREE.Curve.call( this );
  119. this.scale = ( s === undefined ) ? 10 : s;
  120. }
  121. CinquefoilKnot.prototype = Object.create( THREE.Curve.prototype );
  122. CinquefoilKnot.prototype.constructor = CinquefoilKnot;
  123. CinquefoilKnot.prototype.getPoint = function( t ) {
  124. var p = 2;
  125. var q = 5;
  126. t *= Math.PI * 2;
  127. var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  128. var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  129. var z = Math.sin( q * t );
  130. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  131. };
  132. // TrefoilPolynomialKnot
  133. function TrefoilPolynomialKnot( s ) {
  134. THREE.Curve.call( this );
  135. this.scale = ( s === undefined ) ? 10 : s;
  136. }
  137. TrefoilPolynomialKnot.prototype = Object.create( THREE.Curve.prototype );
  138. TrefoilPolynomialKnot.prototype.constructor = TrefoilPolynomialKnot;
  139. TrefoilPolynomialKnot.prototype.getPoint = function( t ) {
  140. t = t * 4 - 2;
  141. var x = Math.pow( t, 3 ) - 3 * t;
  142. var y = Math.pow( t, 4 ) - 4 * t * t;
  143. var z = 1 / 5 * Math.pow( t, 5 ) - 2 * t;
  144. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  145. };
  146. var scaleTo = function( x, y, t ) {
  147. var r = y - x;
  148. return t * r + x;
  149. };
  150. // FigureEightPolynomialKnot
  151. function FigureEightPolynomialKnot( s ) {
  152. THREE.Curve.call( this );
  153. this.scale = ( s === undefined ) ? 1 : s;
  154. }
  155. FigureEightPolynomialKnot.prototype = Object.create( THREE.Curve.prototype );
  156. FigureEightPolynomialKnot.prototype.constructor = FigureEightPolynomialKnot;
  157. FigureEightPolynomialKnot.prototype.getPoint = function( t ) {
  158. t = scaleTo( - 4, 4, t );
  159. var x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 );
  160. var y = Math.pow( t, 4 ) - 13 * t * t;
  161. var z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 );
  162. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  163. };
  164. // DecoratedTorusKnot4a
  165. function DecoratedTorusKnot4a( s ) {
  166. THREE.Curve.call( this );
  167. this.scale = ( s === undefined ) ? 40 : s;
  168. }
  169. DecoratedTorusKnot4a.prototype = Object.create( THREE.Curve.prototype );
  170. DecoratedTorusKnot4a.prototype.constructor = DecoratedTorusKnot4a;
  171. DecoratedTorusKnot4a.prototype.getPoint = function( t ) {
  172. t *= Math.PI * 2;
  173. var x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  174. var y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  175. var z = 0.35 * Math.sin( 5 * t );
  176. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  177. };
  178. // DecoratedTorusKnot4b
  179. function DecoratedTorusKnot4b( s ) {
  180. THREE.Curve.call( this );
  181. this.scale = ( s === undefined ) ? 40 : s;
  182. }
  183. DecoratedTorusKnot4b.prototype = Object.create( THREE.Curve.prototype );
  184. DecoratedTorusKnot4b.prototype.constructor = DecoratedTorusKnot4b;
  185. DecoratedTorusKnot4b.prototype.getPoint = function( t ) {
  186. var fi = t * Math.PI * 2;
  187. var x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  188. var y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  189. var z = 0.2 * Math.sin( 9 * fi );
  190. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  191. };
  192. // DecoratedTorusKnot5a
  193. function DecoratedTorusKnot5a( s ) {
  194. THREE.Curve.call( this );
  195. this.scale = ( s === undefined ) ? 40 : s;
  196. }
  197. DecoratedTorusKnot5a.prototype = Object.create( THREE.Curve.prototype );
  198. DecoratedTorusKnot5a.prototype.constructor = DecoratedTorusKnot5a;
  199. DecoratedTorusKnot5a.prototype.getPoint = function( t ) {
  200. var fi = t * Math.PI * 2;
  201. var x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  202. var y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  203. var z = 0.2 * Math.sin( 20 * fi );
  204. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  205. };
  206. // DecoratedTorusKnot5c
  207. function DecoratedTorusKnot5c( s ) {
  208. THREE.Curve.call( this );
  209. this.scale = ( s === undefined ) ? 40 : s;
  210. }
  211. DecoratedTorusKnot5c.prototype = Object.create( THREE.Curve.prototype );
  212. DecoratedTorusKnot5c.prototype.constructor = DecoratedTorusKnot5c;
  213. DecoratedTorusKnot5c.prototype.getPoint = function( t ) {
  214. var fi = t * Math.PI * 2;
  215. var x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  216. var y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  217. var z = 0.35 * Math.sin( 15 * fi );
  218. return new THREE.Vector3( x, y, z ).multiplyScalar( this.scale );
  219. };
  220. // export
  221. Curves.GrannyKnot = GrannyKnot;
  222. Curves.HeartCurve = HeartCurve;
  223. Curves.VivianiCurve = VivianiCurve;
  224. Curves.KnotCurve = KnotCurve;
  225. Curves.HelixCurve = HelixCurve;
  226. Curves.TrefoilKnot = TrefoilKnot;
  227. Curves.TorusKnot = TorusKnot;
  228. Curves.CinquefoilKnot = CinquefoilKnot;
  229. Curves.TrefoilPolynomialKnot = TrefoilPolynomialKnot;
  230. Curves.FigureEightPolynomialKnot = FigureEightPolynomialKnot;
  231. Curves.DecoratedTorusKnot4a = DecoratedTorusKnot4a;
  232. Curves.DecoratedTorusKnot4b = DecoratedTorusKnot4b;
  233. Curves.DecoratedTorusKnot5a = DecoratedTorusKnot5a;
  234. Curves.DecoratedTorusKnot5c = DecoratedTorusKnot5c;
  235. } ) ( THREE.Curves = THREE.Curves || {} );