CurveExtras.js 5.9 KB

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