|
|
function Vector(){}function Matrix(){}function Line(){}function Plane(){}var Sylvester={version:"0.1.3",precision:1e-6};Vector.prototype={e:function(a){return 1>a||a>this.elements.length?null:this.elements[a-1]},dimensions:function(){return this.elements.length},modulus:function(){return Math.sqrt(this.dot(this))},eql:function(a){var b=this.elements.length,c=a.elements||a;if(b!=c.length)return!1;do if(Math.abs(this.elements[b-1]-c[b-1])>Sylvester.precision)return!1;while(--b);return!0},dup:function(){return Vector.create(this.elements)},map:function(a){var b=[];return this.each(function(c,d){b.push(a(c,d))}),Vector.create(b)},each:function(a){var b,c=this.elements.length,d=c;do b=d-c,a(this.elements[b],b+1);while(--c)},toUnitVector:function(){var a=this.modulus();return 0===a?this.dup():this.map(function(b){return b/a})},angleFrom:function(a){var b=a.elements||a,c=this.elements.length;if(c!=b.length)return null;var d=0,e=0,f=0;if(this.each(function(a,c){d+=a*b[c-1],e+=a*a,f+=b[c-1]*b[c-1]}),e=Math.sqrt(e),f=Math.sqrt(f),e*f===0)return null;var g=d/(e*f);return-1>g&&(g=-1),g>1&&(g=1),Math.acos(g)},isParallelTo:function(a){var b=this.angleFrom(a);return null===b?null:b<=Sylvester.precision},isAntiparallelTo:function(a){var b=this.angleFrom(a);return null===b?null:Math.abs(b-Math.PI)<=Sylvester.precision},isPerpendicularTo:function(a){var b=this.dot(a);return null===b?null:Math.abs(b)<=Sylvester.precision},add:function(a){var b=a.elements||a;return this.elements.length!=b.length?null:this.map(function(a,c){return a+b[c-1]})},subtract:function(a){var b=a.elements||a;return this.elements.length!=b.length?null:this.map(function(a,c){return a-b[c-1]})},multiply:function(a){return this.map(function(b){return b*a})},x:function(a){return this.multiply(a)},dot:function(a){var b=a.elements||a,c=0,d=this.elements.length;if(d!=b.length)return null;do c+=this.elements[d-1]*b[d-1];while(--d);return c},cross:function(a){var b=a.elements||a;if(3!=this.elements.length||3!=b.length)return null;var c=this.elements;return Vector.create([c[1]*b[2]-c[2]*b[1],c[2]*b[0]-c[0]*b[2],c[0]*b[1]-c[1]*b[0]])},max:function(){var a,b=0,c=this.elements.length,d=c;do a=d-c,Math.abs(this.elements[a])>Math.abs(b)&&(b=this.elements[a]);while(--c);return b},indexOf:function(a){var b,c=null,d=this.elements.length,e=d;do b=e-d,null===c&&this.elements[b]==a&&(c=b+1);while(--d);return c},toDiagonalMatrix:function(){return Matrix.Diagonal(this.elements)},round:function(){return this.map(function(a){return Math.round(a)})},snapTo:function(a){return this.map(function(b){return Math.abs(b-a)<=Sylvester.precision?a:b})},distanceFrom:function(a){if(a.anchor)return a.distanceFrom(this);var b=a.elements||a;if(b.length!=this.elements.length)return null;var c,d=0;return this.each(function(a,e){c=a-b[e-1],d+=c*c}),Math.sqrt(d)},liesOn:function(a){return a.contains(this)},liesIn:function(a){return a.contains(this)},rotate:function(a,b){var c,d,e,f,g;switch(this.elements.length){case 2:return c=b.elements||b,2!=c.length?null:(d=Matrix.Rotation(a).elements,e=this.elements[0]-c[0],f=this.elements[1]-c[1],Vector.create([c[0]+d[0][0]*e+d[0][1]*f,c[1]+d[1][0]*e+d[1][1]*f]));case 3:if(!b.direction)return null;var h=b.pointClosestTo(this).elements;return d=Matrix.Rotation(a,b.direction).elements,e=this.elements[0]-h[0],f=this.elements[1]-h[1],g=this.elements[2]-h[2],Vector.create([h[0]+d[0][0]*e+d[0][1]*f+d[0][2]*g,h[1]+d[1][0]*e+d[1][1]*f+d[1][2]*g,h[2]+d[2][0]*e+d[2][1]*f+d[2][2]*g]);default:return null}},reflectionIn:function(a){if(a.anchor){var b=this.elements.slice(),c=a.pointClosestTo(b).elements;return Vector.create([c[0]+(c[0]-b[0]),c[1]+(c[1]-b[1]),c[2]+(c[2]-(b[2]||0))])}var d=a.elements||a;return this.elements.length!=d.length?null:this.map(function(a,b){return d[b-1]+(d[b-1]-a)})},to3D:function(){var a=this.dup();switch(a.elements.length){case 3:break;case 2:a.elements.push(0);break;default:return null}return a},inspect:function(){return"["+this.elements.join(", ")+"]"},setElements:function(a){return this.elements=(a.elements||a).slice(),this}},Vector.create=function(a){v
|