CurveExtras.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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, optionalTarget ) {
  21. var point = optionalTarget || new THREE.Vector3();
  22. t = 2 * Math.PI * t;
  23. var x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t );
  24. 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 );
  25. var z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t );
  26. return point.set( x, y, z ).multiplyScalar( 20 );
  27. };
  28. // HeartCurve
  29. function HeartCurve( scale ) {
  30. THREE.Curve.call( this );
  31. this.scale = ( scale === undefined ) ? 5 : scale;
  32. }
  33. HeartCurve.prototype = Object.create( THREE.Curve.prototype );
  34. HeartCurve.prototype.constructor = HeartCurve;
  35. HeartCurve.prototype.getPoint = function ( t, optionalTarget ) {
  36. var point = optionalTarget || new THREE.Vector3();
  37. t *= 2 * Math.PI;
  38. var x = 16 * Math.pow( Math.sin( t ), 3 );
  39. var y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t );
  40. var z = 0;
  41. return point.set( x, y, z ).multiplyScalar( this.scale );
  42. };
  43. // Viviani's Curve
  44. function VivianiCurve( scale ) {
  45. THREE.Curve.call( this );
  46. this.scale = ( scale === undefined ) ? 70 : scale;
  47. }
  48. VivianiCurve.prototype = Object.create( THREE.Curve.prototype );
  49. VivianiCurve.prototype.constructor = VivianiCurve;
  50. VivianiCurve.prototype.getPoint = function ( t, optionalTarget ) {
  51. var point = optionalTarget || new THREE.Vector3();
  52. t = t * 4 * Math.PI; // normalized to 0..1
  53. var a = this.scale / 2;
  54. var x = a * ( 1 + Math.cos( t ) );
  55. var y = a * Math.sin( t );
  56. var z = 2 * a * Math.sin( t / 2 );
  57. return point.set( x, y, z );
  58. };
  59. // KnotCurve
  60. function KnotCurve() {
  61. THREE.Curve.call( this );
  62. }
  63. KnotCurve.prototype = Object.create( THREE.Curve.prototype );
  64. KnotCurve.prototype.constructor = KnotCurve;
  65. KnotCurve.prototype.getPoint = function ( t, optionalTarget ) {
  66. var point = optionalTarget || new THREE.Vector3();
  67. t *= 2 * Math.PI;
  68. var R = 10;
  69. var s = 50;
  70. var x = s * Math.sin( t );
  71. var y = Math.cos( t ) * ( R + s * Math.cos( t ) );
  72. var z = Math.sin( t ) * ( R + s * Math.cos( t ) );
  73. return point.set( x, y, z );
  74. };
  75. // HelixCurve
  76. function HelixCurve() {
  77. THREE.Curve.call( this );
  78. }
  79. HelixCurve.prototype = Object.create( THREE.Curve.prototype );
  80. HelixCurve.prototype.constructor = HelixCurve;
  81. HelixCurve.prototype.getPoint = function ( t, optionalTarget ) {
  82. var point = optionalTarget || new THREE.Vector3();
  83. var a = 30; // radius
  84. var b = 150; // height
  85. var t2 = 2 * Math.PI * t * b / 30;
  86. var x = Math.cos( t2 ) * a;
  87. var y = Math.sin( t2 ) * a;
  88. var z = b * t;
  89. return point.set( x, y, z );
  90. };
  91. // TrefoilKnot
  92. function TrefoilKnot( scale ) {
  93. THREE.Curve.call( this );
  94. this.scale = ( scale === undefined ) ? 10 : scale;
  95. }
  96. TrefoilKnot.prototype = Object.create( THREE.Curve.prototype );
  97. TrefoilKnot.prototype.constructor = TrefoilKnot;
  98. TrefoilKnot.prototype.getPoint = function ( t, optionalTarget ) {
  99. var point = optionalTarget || new THREE.Vector3();
  100. t *= Math.PI * 2;
  101. var x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t );
  102. var y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t );
  103. var z = Math.sin( 3 * t );
  104. return point.set( x, y, z ).multiplyScalar( this.scale );
  105. };
  106. // TorusKnot
  107. function TorusKnot( scale ) {
  108. THREE.Curve.call( this );
  109. this.scale = ( scale === undefined ) ? 10 : scale;
  110. }
  111. TorusKnot.prototype = Object.create( THREE.Curve.prototype );
  112. TorusKnot.prototype.constructor = TorusKnot;
  113. TorusKnot.prototype.getPoint = function ( t, optionalTarget ) {
  114. var point = optionalTarget || new THREE.Vector3();
  115. var p = 3;
  116. var q = 4;
  117. t *= Math.PI * 2;
  118. var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  119. var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  120. var z = Math.sin( q * t );
  121. return point.set( x, y, z ).multiplyScalar( this.scale );
  122. };
  123. // CinquefoilKnot
  124. function CinquefoilKnot( scale ) {
  125. THREE.Curve.call( this );
  126. this.scale = ( scale === undefined ) ? 10 : scale;
  127. }
  128. CinquefoilKnot.prototype = Object.create( THREE.Curve.prototype );
  129. CinquefoilKnot.prototype.constructor = CinquefoilKnot;
  130. CinquefoilKnot.prototype.getPoint = function ( t, optionalTarget ) {
  131. var point = optionalTarget || new THREE.Vector3();
  132. var p = 2;
  133. var q = 5;
  134. t *= Math.PI * 2;
  135. var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  136. var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  137. var z = Math.sin( q * t );
  138. return point.set( x, y, z ).multiplyScalar( this.scale );
  139. };
  140. // TrefoilPolynomialKnot
  141. function TrefoilPolynomialKnot( scale ) {
  142. THREE.Curve.call( this );
  143. this.scale = ( scale === undefined ) ? 10 : scale;
  144. }
  145. TrefoilPolynomialKnot.prototype = Object.create( THREE.Curve.prototype );
  146. TrefoilPolynomialKnot.prototype.constructor = TrefoilPolynomialKnot;
  147. TrefoilPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) {
  148. var point = optionalTarget || new THREE.Vector3();
  149. t = t * 4 - 2;
  150. var x = Math.pow( t, 3 ) - 3 * t;
  151. var y = Math.pow( t, 4 ) - 4 * t * t;
  152. var z = 1 / 5 * Math.pow( t, 5 ) - 2 * t;
  153. return point.set( x, y, z ).multiplyScalar( this.scale );
  154. };
  155. var scaleTo = function ( x, y, t ) {
  156. var r = y - x;
  157. return t * r + x;
  158. };
  159. // FigureEightPolynomialKnot
  160. function FigureEightPolynomialKnot( scale ) {
  161. THREE.Curve.call( this );
  162. this.scale = ( scale === undefined ) ? 1 : scale;
  163. }
  164. FigureEightPolynomialKnot.prototype = Object.create( THREE.Curve.prototype );
  165. FigureEightPolynomialKnot.prototype.constructor = FigureEightPolynomialKnot;
  166. FigureEightPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) {
  167. var point = optionalTarget || new THREE.Vector3();
  168. t = scaleTo( - 4, 4, t );
  169. var x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 );
  170. var y = Math.pow( t, 4 ) - 13 * t * t;
  171. var z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 );
  172. return point.set( x, y, z ).multiplyScalar( this.scale );
  173. };
  174. // DecoratedTorusKnot4a
  175. function DecoratedTorusKnot4a( scale ) {
  176. THREE.Curve.call( this );
  177. this.scale = ( scale === undefined ) ? 40 : scale;
  178. }
  179. DecoratedTorusKnot4a.prototype = Object.create( THREE.Curve.prototype );
  180. DecoratedTorusKnot4a.prototype.constructor = DecoratedTorusKnot4a;
  181. DecoratedTorusKnot4a.prototype.getPoint = function ( t, optionalTarget ) {
  182. var point = optionalTarget || new THREE.Vector3();
  183. t *= Math.PI * 2;
  184. var x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  185. var y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  186. var z = 0.35 * Math.sin( 5 * t );
  187. return point.set( x, y, z ).multiplyScalar( this.scale );
  188. };
  189. // DecoratedTorusKnot4b
  190. function DecoratedTorusKnot4b( scale ) {
  191. THREE.Curve.call( this );
  192. this.scale = ( scale === undefined ) ? 40 : scale;
  193. }
  194. DecoratedTorusKnot4b.prototype = Object.create( THREE.Curve.prototype );
  195. DecoratedTorusKnot4b.prototype.constructor = DecoratedTorusKnot4b;
  196. DecoratedTorusKnot4b.prototype.getPoint = function ( t, optionalTarget ) {
  197. var point = optionalTarget || new THREE.Vector3();
  198. var fi = t * Math.PI * 2;
  199. var x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  200. var y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  201. var z = 0.2 * Math.sin( 9 * fi );
  202. return point.set( x, y, z ).multiplyScalar( this.scale );
  203. };
  204. // DecoratedTorusKnot5a
  205. function DecoratedTorusKnot5a( scale ) {
  206. THREE.Curve.call( this );
  207. this.scale = ( scale === undefined ) ? 40 : scale;
  208. }
  209. DecoratedTorusKnot5a.prototype = Object.create( THREE.Curve.prototype );
  210. DecoratedTorusKnot5a.prototype.constructor = DecoratedTorusKnot5a;
  211. DecoratedTorusKnot5a.prototype.getPoint = function ( t, optionalTarget ) {
  212. var point = optionalTarget || new THREE.Vector3();
  213. var fi = t * Math.PI * 2;
  214. var x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  215. var y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  216. var z = 0.2 * Math.sin( 20 * fi );
  217. return point.set( x, y, z ).multiplyScalar( this.scale );
  218. };
  219. // DecoratedTorusKnot5c
  220. function DecoratedTorusKnot5c( scale ) {
  221. THREE.Curve.call( this );
  222. this.scale = ( scale === undefined ) ? 40 : scale;
  223. }
  224. DecoratedTorusKnot5c.prototype = Object.create( THREE.Curve.prototype );
  225. DecoratedTorusKnot5c.prototype.constructor = DecoratedTorusKnot5c;
  226. DecoratedTorusKnot5c.prototype.getPoint = function ( t, optionalTarget ) {
  227. var point = optionalTarget || new THREE.Vector3();
  228. var fi = t * Math.PI * 2;
  229. var x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  230. var y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  231. var z = 0.35 * Math.sin( 15 * fi );
  232. return point.set( x, y, z ).multiplyScalar( this.scale );
  233. };
  234. // export
  235. Curves.GrannyKnot = GrannyKnot;
  236. Curves.HeartCurve = HeartCurve;
  237. Curves.VivianiCurve = VivianiCurve;
  238. Curves.KnotCurve = KnotCurve;
  239. Curves.HelixCurve = HelixCurve;
  240. Curves.TrefoilKnot = TrefoilKnot;
  241. Curves.TorusKnot = TorusKnot;
  242. Curves.CinquefoilKnot = CinquefoilKnot;
  243. Curves.TrefoilPolynomialKnot = TrefoilPolynomialKnot;
  244. Curves.FigureEightPolynomialKnot = FigureEightPolynomialKnot;
  245. Curves.DecoratedTorusKnot4a = DecoratedTorusKnot4a;
  246. Curves.DecoratedTorusKnot4b = DecoratedTorusKnot4b;
  247. Curves.DecoratedTorusKnot5a = DecoratedTorusKnot5a;
  248. Curves.DecoratedTorusKnot5c = DecoratedTorusKnot5c;
  249. } )( THREE.Curves = THREE.Curves || {} );