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