123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- /*
- * A bunch of parametric curves
- * @author zz85
- *
- * Formulas collected from various sources
- * http://mathworld.wolfram.com/HeartCurve.html
- * http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page6.html
- * http://en.wikipedia.org/wiki/Viviani%27s_curve
- * http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page4.html
- * http://www.mi.sanu.ac.rs/vismath/taylorapril2011/Taylor.pdf
- * http://prideout.net/blog/?p=44
- */
- // Lets define some curves
- THREE.Curves = {};
- THREE.Curves.GrannyKnot = THREE.Curve.create( function() {},
- function(t) {
- t = 2 * Math.PI * t;
- var x = -0.22 * Math.cos(t) - 1.28 * Math.sin(t) - 0.44 * Math.cos(3 * t) - 0.78 * Math.sin(3 * t);
- 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);
- var z = 0.7 * Math.cos(3 * t) - 0.4 * Math.sin(3 * t);
- return new THREE.Vector3(x, y, z).multiplyScalar(20);
- }
- );
- THREE.Curves.HeartCurve = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 5 : s;
- },
- function(t) {
- t *= 2 * Math.PI;
- var tx = 16 * Math.pow(Math.sin(t), 3);
- var ty = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t), tz = 0;
- return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
- }
- );
- // Viviani's Curve
- THREE.Curves.VivianiCurve = THREE.Curve.create(
- function(radius) {
- this.radius = radius;
- },
- function(t) {
- t = t * 4 * Math.PI; // Normalized to 0..1
- var a = this.radius / 2;
- var tx = a * (1 + Math.cos(t)),
- ty = a * Math.sin(t),
- tz = 2 * a * Math.sin(t / 2);
- return new THREE.Vector3(tx, ty, tz);
- }
- );
- THREE.Curves.KnotCurve = THREE.Curve.create(
- function() {
- },
- function(t) {
- t *= 2 * Math.PI;
- var R = 10;
- var s = 50;
- var tx = s * Math.sin(t),
- ty = Math.cos(t) * (R + s * Math.cos(t)),
- tz = Math.sin(t) * (R + s * Math.cos(t));
- return new THREE.Vector3(tx, ty, tz);
- }
- );
- THREE.Curves.HelixCurve = THREE.Curve.create(
- function() {
- },
- function(t) {
- var a = 30; // radius
- var b = 150; //height
- var t2 = 2 * Math.PI * t * b / 30;
- var tx = Math.cos(t2) * a,
- ty = Math.sin(t2) * a,
- tz = b * t;
- return new THREE.Vector3(tx, ty, tz);
- }
- );
- THREE.Curves.TrefoilKnot = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 10 : s;
- },
- function(t) {
- t *= Math.PI * 2;
- var tx = (2 + Math.cos(3 * t)) * Math.cos(2 * t),
- ty = (2 + Math.cos(3 * t)) * Math.sin(2 * t),
- tz = Math.sin(3 * t);
- return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
- }
- );
- THREE.Curves.TorusKnot = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 10 : s;
- },
- function(t) {
- var p = 3,
- q = 4;
- t *= Math.PI * 2;
- var tx = (2 + Math.cos(q * t)) * Math.cos(p * t),
- ty = (2 + Math.cos(q * t)) * Math.sin(p * t),
- tz = Math.sin(q * t);
- return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
- }
- );
- THREE.Curves.CinquefoilKnot = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 10 : s;
- },
- function(t) {
- var p = 2,
- q = 5;
- t *= Math.PI * 2;
- var tx = (2 + Math.cos(q * t)) * Math.cos(p * t),
- ty = (2 + Math.cos(q * t)) * Math.sin(p * t),
- tz = Math.sin(q * t);
- return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
- }
- );
- THREE.Curves.TrefoilPolynomialKnot = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 10 : s;
- },
- function(t) {
- t = t * 4 - 2;
- var tx = Math.pow(t, 3) - 3 * t,
- ty = Math.pow(t, 4) - 4 * t * t,
- tz = 1 / 5 * Math.pow(t, 5) - 2 * t;
- return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
- }
- );
- // var scaleTo = function(x, y) {
- // var r = y - x;
- // return function(t) {
- // t * r + x;
- // };
- // }
- var scaleTo = function(x, y, t) {
- var r = y - x;
- return t * r + x;
- }
- THREE.Curves.FigureEightPolynomialKnot = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 1 : s;
- },
- function(t) {
- t = scaleTo(-4, 4, t);
- var tx = 2 / 5 * t * (t * t - 7) * (t * t - 10),
- ty = Math.pow(t, 4) - 13 * t * t,
- tz = 1 / 10 * t * (t * t - 4) * (t * t - 9) * (t * t - 12);
- return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
- }
- );
- THREE.Curves.DecoratedTorusKnot4a = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 40 : s;
- },
- function(t) {
- t *= Math.PI * 2;
- var
- x = Math.cos(2 * t) * (1 + 0.6 * (Math.cos(5 * t) + 0.75 * Math.cos(10 * t))),
- y = Math.sin(2 * t) * (1 + 0.6 * (Math.cos(5 * t) + 0.75 * Math.cos(10 * t))),
- z = 0.35 * Math.sin(5 * t);
- return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
- }
- );
- THREE.Curves.DecoratedTorusKnot4b = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 40 : s;
- },
- function(t) {
- var fi = t * Math.PI * 2;
- var x = Math.cos(2 * fi) * (1 + 0.45 * Math.cos(3 * fi) + 0.4 * Math.cos(9 * fi)),
- y = Math.sin(2 * fi) * (1 + 0.45 * Math.cos(3 * fi) + 0.4 * Math.cos(9 * fi)),
- z = 0.2 * Math.sin(9 * fi);
- return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
- }
- );
- THREE.Curves.DecoratedTorusKnot5a = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 40 : s;
- },
- function(t) {
- var fi = t * Math.PI * 2;
- var x = Math.cos(3 * fi) * (1 + 0.3 * Math.cos(5 * fi) + 0.5 * Math.cos(10 * fi)),
- y = Math.sin(3 * fi) * (1 + 0.3 * Math.cos(5 * fi) + 0.5 * Math.cos(10 * fi)),
- z = 0.2 * Math.sin(20 * fi);
- return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
- }
- );
- THREE.Curves.DecoratedTorusKnot5c = THREE.Curve.create(
- function(s) {
- this.scale = (s === undefined) ? 40 : s;
- },
- function(t) {
- var fi = t * Math.PI * 2;
- var x = Math.cos(4 * fi) * (1 + 0.5 * (Math.cos(5 * fi) + 0.4 * Math.cos(20 * fi))),
- y = Math.sin(4 * fi) * (1 + 0.5 * (Math.cos(5 * fi) + 0.4 * Math.cos(20 * fi))),
- z = 0.35 * Math.sin(15 * fi);
- return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
- }
- );
|