CurveExtras.js 10 KB

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