CurveExtras.js 8.7 KB

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