CurveExtras.js 7.8 KB

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