CurveExtras.js 7.2 KB

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