CurveExtras.js 7.9 KB

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