CurveExtras.js 9.9 KB

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