// toxiclibs r43 - http://github.com/hapticdata/toxiclibsjs
var toxi=toxi||{};(function(){if(typeof window!=="undefined")if(!window.Int32Array){window.Int32Array=Array;window.Float32Array=Array}if(typeof module!=="undefined"&&typeof module.exports!=="undefined")module.exports=toxi})();toxi.extend=function(d,e){d.prototype=new e;d.constructor=d;d.prototype.parent=e.prototype};toxi.MathUtils={};toxi.MathUtils.SQRT2=Math.sqrt(2);toxi.MathUtils.SQRT3=Math.sqrt(3);toxi.MathUtils.LOG2=Math.log(2);toxi.MathUtils.PI=3.141592653589793;toxi.MathUtils.INV_PI=1/toxi.MathUtils.PI;
toxi.MathUtils.HALF_PI=toxi.MathUtils.PI/2;toxi.MathUtils.THIRD_PI=toxi.MathUtils.PI/3;toxi.MathUtils.QUARTER_PI=toxi.MathUtils.PI/4;toxi.MathUtils.TWO_PI=toxi.MathUtils.PI*2;toxi.MathUtils.THREE_HALVES_PI=toxi.MathUtils.TWO_PI-toxi.MathUtils.HALF_PI;toxi.MathUtils.PI_SQUARED=toxi.MathUtils.PI*toxi.MathUtils.PI;toxi.MathUtils.EPS=1.1920928955078125E-7;toxi.MathUtils.DEG2RAD=toxi.MathUtils.PI/180;toxi.MathUtils.RAD2DEG=180/toxi.MathUtils.PI;toxi.MathUtils.SHIFT23=8388608;
toxi.MathUtils.INV_SHIFT23=1/toxi.MathUtils.SHIFT23;toxi.MathUtils.SIN_A=-4/(toxi.MathUtils.PI*toxi.MathUtils.PI);toxi.MathUtils.SIN_B=4/toxi.MathUtils.PI;toxi.MathUtils.SIN_P=0.225;toxi.MathUtils.abs=Math.abs;toxi.MathUtils.ceilPowerOf2=function(d){for(var e=1;e<d;)e<<=1;return e};toxi.MathUtils.clip=function(d,e,g){return d<e?e:d>g?g:d};toxi.MathUtils.clipNormalized=function(d){if(d<0)return 0;else if(d>1)return 1;return d};toxi.MathUtils.cos=Math.cos;
toxi.MathUtils.degrees=function(d){return d*this.RAD2DEG};toxi.MathUtils.fastCos=function(d){return toxi.MathUtils.fastSin(d+(d>toxi.MathUtils.HALF_PI?-toxi.MathUtils.THREE_HALVES_PI:toxi.MathUtils.HALF_PI))};toxi.MathUtils.fastSin=function(d){d=toxi.MathUtils.SIN_B*d+toxi.MathUtils.SIN_A*d*Math.abs(d);return toxi.MathUtils.SIN_P*(d*Math.abs(d)-d)+d};toxi.MathUtils.flipCoin=function(){return Math.random()<0.5};toxi.MathUtils.floor=function(d){var e=parseInt(d,10);d<0&&d!=e&&e--;return e};
toxi.MathUtils.floorPowerOf2=function(d){return parseInt(Math.pow(2,parseInt(Math.log(d)/toxi.MathUtils.LOG2,10)),10)};toxi.MathUtils.max=function(d,e,g){if(!g)return Math.max(d,e);return d>e?d>g?d:g:e>g?e:g};toxi.MathUtils.min=function(d,e,g){if(!g)return Math.min(d,e);return d<e?d<g?d:g:e<g?e:g};toxi.MathUtils.normalizedRandom=function(){return Math.random()*2-1};toxi.MathUtils.radians=function(d){return d*toxi.MathUtils.DEG2RAD};
toxi.MathUtils.random=function(d,e){if(!d&&!e)return Math.random();else if(!e){e=d;return Math.random()*e}return Math.random()*(e-d)+d};toxi.MathUtils.reduceAngle=function(d){d%=toxi.MathUtils.TWO_PI;if(Math.abs(d)>toxi.MathUtils.PI)d-=toxi.MathUtils.TWO_PI;if(Math.abs(d)>toxi.MathUtils.HALF_PI)d=toxi.MathUtils.PI-d;return d};toxi.MathUtils.sign=function(d){return d<0?-1:d>0?1:0};
toxi.MathUtils.sin=function(d){d=toxi.MathUtils.reduceAngle(d);if(Math.abs(d)<=toxi.MathUtils.QUARTER_PI)return toxi.MathUtils.fastSin(d);return toxi.MathUtils.fastCos(toxi.MathUtils.HALF_PI-d)};toxi.BezierInterpolation=function(d,e){this.c1=d;this.c2=e};toxi.BezierInterpolation.prototype={interpolate:function(d,e,g){var h=g*g,i=1-g,m=i*i;return d*m*i+3*(this.c1*(e-d)+d)*g*m+3*(this.c2*(e-d)+e)*h*i+e*h*g},setCoefficients:function(d,e){this.c1=d;this.c2=e}};
toxi.CircularInterpolation=function(d){if(d===undefined)this.isFlipped=false};toxi.CircularInterpolation.prototype={interpolate:function(d,e,g){if(this.isFlipped)return d-(e-d)*(Math.sqrt(1-g*g)-1);else{g=1-g;return d+(e-d)*Math.sqrt(1-g*g)}},setFlipped:function(d){this.isFlipped=d}};toxi.CosineInterpolation=function(){};toxi.CosineInterpolation.prototype={interpolate:function(d,e,g){return e+(d-e)*(0.5+0.5*Math.cos(g*Math.PI))}};
toxi.DecimatedInterpolation=function(d,e){if(d===undefined)throw Error("steps was not passed to constructor");this.numSteps=d;this.strategy=e===undefined?new toxi.LinearInterpolation:e};toxi.DecimatedInterpolation.prototype={interpolate:function(d,e,g){return this.strategy.interpolate(d,e,Math.floor(g*this.numSteps)/this.numSteps)}};toxi.ExponentialInterpolation=function(d){this.exponent=d===undefined?2:d};
toxi.ExponentialInterpolation.prototype={interpolate:function(d,e,g){return d+(e-d)*Math.pow(g,this.exponent)}};toxi.Interpolation2D={};toxi.Interpolation2D.bilinear=function(d,e,g,h,i,m,n,o,p,r){var t,u,B;if(d instanceof Object){t=d.x;d=d.y;u=e.x;e=e.y;B=g.x;g=g.y;h=h;i=i;m=m;n=n}else{t=d;d=e;u=g;e=h;B=i;g=m;h=n;i=o;m=p;n=r}r=1/((B-u)*(g-e));u=(t-u)*r;t=(B-t)*r;e=d-e;d=g-d;return h*t*d+i*u*d+m*t*e+n*u*e};toxi.LinearInterpolation=function(){};
toxi.LinearInterpolation.prototype={interpolate:function(d,e,g){return d+(e-d)*g}};toxi.SigmoidInterpolation=function(d){if(d===undefined)d=2;this.setSharpness(d)};toxi.SigmoidInterpolation.prototype={getSharpness:function(){return this.sharpness},interpolate:function(d,e,g){g=(g*2-1)*this.sharpPremult;g=1/(1+Math.exp(-g));return d+(e-d)*g},setSharpness:function(d){this.sharpness=d;this.sharpPremult=5*d}};toxi.ThresholdInterpolation=function(d){this.threshold=d};
toxi.ThresholdInterpolation.prototype={interpolate:function(d,e,g){return g<this.threshold?d:e}};toxi.ZoomLensInterpolation=function(d,e){this.leftImpl=new toxi.CircularInterpolation;this.rightImpl=new toxi.CircularInterpolation;this.lensPos=d||0.5;this.lensStrength=e||1;this.absStrength=Math.abs(this.lensStrength);this.leftImpl.setFlipped(this.lensStrength>0);this.rightImpl.setFlipped(this.lensStrength<0)};
toxi.ZoomLensInterpolation.prototype={interpolate:function(d,e,g){var h=d+(e-d)*g;h+=g<this.lensPos?(this.leftImpl.interpolate(d,d+(e-d)*this.lensPos,g/this.lensPos)-h)*this.absStrength:(this.rightImpl.interpolate(d+(e-d)*this.lensPos,e,(g-this.lensPos)/(1-this.lensPos))-h)*this.absStrength;return h},setLensPos:function(d,e){this.lensPos+=(toxi.MathUtils.clipNormalized(d)-this.lensPos)*e},setLensStrength:function(d,e){this.lensStrength+=(toxi.MathUtils.clip(d,-1,1)-this.lensStrength)*e;this.absStrength=
toxi.MathUtils.abs(this.lensStrength);this.leftImpl.setFlipped(this.lensStrength>0);this.rightImpl.setFlipped(this.lensStrength<0)}};toxi.Range=function(d,e){this.min=d;this.max=e};toxi.Range.prototype.toString=function(){return"{ min: "+this.min+", max: "+this.max+"}"};
toxi.ScaleMap=function(d,e,g,h){if(arguments.length==1&&arguments[0].input!==undefined&&arguments[0].output!==undefined){var i=arguments[0];g=i.output.min;h=i.output.max;e=i.input.max;d=i.input.min}this.mapFunction=new toxi.LinearInterpolation;this.setInputRange(d,e);this.setOutputRange(g,h)};
toxi.ScaleMap.prototype={getClippedValueFor:function(d){return this.mapFunction.interpolate(0,this.mapRange,toxi.MathUtils.clipNormalized((d-this._in.min)/this._interval))+this._out.min},getInputMedian:function(){return(this._in.min+this._in.max)*0.5},getInputRange:function(){return this._in},getMappedMedian:function(){return this.getMappedValueFor(0.5)},getMappedValueFor:function(d){return this.mapFunction.interpolate(0,this.mapRange,(d-this._in.min)/this._interval)+this._out.min},getOutputMedian:function(){return(this._out.min+
this._out.max)*0.5},getOutputRange:function(){return this._out},setInputRange:function(d,e){this._in=new toxi.Range(d,e);this._interval=e-d},setMapFunction:function(d){this.mapFunction=d},setOutputRange:function(d,e){this._out=new toxi.Range(d,e);this.mapRange=e-d},toString:function(){return"toxi.ScaleMap, inputRange: "+this._in.toString()+" outputRange: "+this._out.toString()}};
toxi.AbstractWave=function(d,e,g,h){if(d!==undefined||e!==undefined||g!==undefined||h!==undefined){this.setPhase(d);this.frequency=e;if(g===undefined)g=1;if(h===undefined)h=1;this.amp=g;this.offset=h}};
toxi.AbstractWave.prototype={cyclePhase:function(d){if(d===undefined)d=0;this.phase=(this.phase+d)%toxi.AbstractWave.TWO_PI;if(this.phase<0)this.phase+=toxi.AbstractWave.TWO_PI;return this.phase},getClass:function(){return"toxi.AbstractWave"},pop:function(){if(this.stateStack===undefined||this.stateStack!==undefined&&this.stateStack.length<=0){console.log(this.toString());console.log("no wave states on stack")}else{var d=this.stateStack.pop();this.phase=d.phase;this.frequency=d.frequency;this.amp=
d.amp;this.offset=d.offset}},push:function(){if(this.stateStack===undefined)this.stateStack=[];this.stateStack.push(new toxi.WaveState(this.phase,this.frequency,this.amp,this.offset))},reset:function(){this.phase=this.origPhase},setPhase:function(d){this.phase=d;this.cyclePhase();this.origPhase=d},toString:function(){return this.getClass()+" phase:"+this.phase+" frequency: "+this.frequency+" amp: "+this.amp+" offset: "+this.offset},update:function(){console.log(this.getClass()+" this should be overridden")}};
toxi.AbstractWave.PI=3.141592653589793;toxi.AbstractWave.TWO_PI=2*toxi.AbstractWave.PI;toxi.AbstractWave.hertzToRadians=function(d,e){return d/e*toxi.AbstractWave.TWO_PI};toxi.AbstractWave.radiansToHertz=function(d,e){return d/toxi.AbstractWave.TWO_PI*e};toxi.AMFMSineWave=function(d,e,g,h,i){if(typeof g=="number"){toxi.AbstractWave.apply(this,[d,e,1,g]);this.amod=h;this.fmod=i}else{toxi.AbstractWave.apply(this,[d,e]);this.amod=g;this.fmod=h}};toxi.extend(toxi.AMFMSineWave,toxi.AbstractWave);
toxi.AMFMSineWave.prototype.getClass=function(){return"AMFMSineWave"};toxi.AMFMSineWave.prototype.pop=function(){this.parent.pop.call(this);this.amod.pop();this.fmod.pop()};toxi.AMFMSineWave.prototype.push=function(){this.parent.push.call(this);this.amod.push();this.fmod.push()};toxi.AMFMSineWave.prototype.reset=function(){this.parent.reset.call(this);this.fmod.reset();this.amod.reset()};
toxi.AMFMSineWave.prototype.update=function(){this.amp=this.amod.update();this.value=this.amp*Math.sin(this.phase)+this.offset;this.cyclePhase(this.frequency+this.fmod.update());return this.value};toxi.ConstantWave=function(d){toxi.AbstractWave.apply(this);this.value=d};toxi.extend(toxi.ConstantWave,toxi.AbstractWave);toxi.ConstantWave.prototype.getClass=function(){return"ConstantWave"};toxi.ConstantWave.prototype.update=function(){return this.value};
toxi.FMHarmonicSquareWave=function(d,e,g,h,i){this.maxHarmonics=3;if(typeof g=="number"){if(i===undefined)i=new toxi.ConstantWave(0);toxi.AbstractWave.apply(this,[d,e,g,h]);this.fmod=i}else{toxi.AbstractWave.apply(this,[d,e]);this.fmod=g}};toxi.extend(toxi.FMHarmonicSquareWave,toxi.AbstractWave);toxi.FMHarmonicSquareWave.prototype.getClass=function(){return"FMHarmonicSquareWave"};toxi.FMHarmonicSquareWave.prototype.pop=function(){this.parent.pop.call(this);this.fmod.pop()};
toxi.FMHarmonicSquareWave.prototype.push=function(){this.parent.push.call(this);this.fmod.push()};toxi.FMHarmonicSquareWave.prototype.reset=function(){this.parent.reset.call(this);this.fmod.reset()};toxi.FMHarmonicSquareWave.prototype.update=function(){this.value=0;for(var d=1;d<=this.maxHarmonics;d+=2)this.value+=1/d*Math.sin(d*this.phase);this.value*=this.amp;this.value+=this.offset;this.cyclePhase(this.frequency+this.fmod.update());return this.value};
toxi.FMSawtoothWave=function(d,e,g,h,i){if(typeof g=="number"){toxi.AbstractWave.apply(this,[d,e,g,h]);this.fmod=i}else{toxi.AbstractWave.apply(this,[d,e]);this.fmod=g}};toxi.extend(toxi.FMSawtoothWave,toxi.AbstractWave);toxi.FMSawtoothWave.prototype.getClass=function(){return"FMSawtoothWave"};toxi.FMSawtoothWave.prototype.pop=function(){this.parent.pop.call(this);this.fmod.pop()};toxi.FMSawtoothWave.prototype.push=function(){this.parent.push.call(this);this.fmod.push()};
toxi.FMSawtoothWave.prototype.reset=function(){this.parent.reset.call(this);this.fmod.reset()};toxi.FMSawtoothWave.prototype.update=function(){this.value=(this.phase/toxi.AbstractWave.TWO_PI*2-1)*this.amp+this.offset;this.cyclePhase(this.frequency+this.fmod.update());return this.value};toxi.FMSineWave=function(d,e,g,h,i){if(typeof g=="number"){toxi.AbstractWave.apply(this,[d,e,g,h]);this.fmod=i}else{toxi.AbstractWave.apply(this,[d,e]);this.fmod=g}};toxi.extend(toxi.FMSineWave,toxi.AbstractWave);
toxi.FMSineWave.prototype.getClass=function(){return"FMSineWave"};toxi.FMSineWave.prototype.pop=function(){this.parent.pop.call(this);this.fmod.pop()};toxi.FMSineWave.prototype.push=function(){this.parent.push.call(this);this.fmod.push()};toxi.FMSineWave.prototype.reset=function(){this.parent.reset.call(this);this.fmod.reset()};toxi.FMSineWave.prototype.update=function(){this.value=Math.sin(this.phase)*this.amp+this.offset;this.cyclePhase(this.frequency+this.fmod.update());return this.value};
toxi.FMSquareWave=function(d,e,g,h,i){if(typeof g=="number")if(i===undefined)toxi.AbstractWave.apply(this,[d,e,g,h,new toxi.ConstantWave(0)]);else{toxi.AbstractWave.apply(this,[d,e,g,h]);this.fmod=i}else{toxi.AbstractWave.apply(this,[d,e]);this.fmod=g}};toxi.extend(toxi.FMSquareWave,toxi.AbstractWave);toxi.FMSquareWave.prototype.getClass=function(){return"FMSquareWave"};toxi.FMSquareWave.prototype.pop=function(){this.parent.pop.call(this);this.fmod.pop()};
toxi.FMSquareWave.prototype.push=function(){this.parent.push.call(this);this.fmod.push()};toxi.FMSquareWave.prototype.reset=function(){this.parent.reset.call(this);this.fmod.reset()};toxi.FMSquareWave.prototype.update=function(){this.value=(this.phase/toxi.AbstractWave.TWO_PI<0.5?1:-1)*this.amp+this.offset;this.cyclePhase(this.frequency+this.fmod.update());return this.value};
toxi.FMTriangleWave=function(d,e,g,h,i){if(typeof g=="number")if(i!==undefined){toxi.AbstractWave.apply(this,[d,e,g,h]);this.fmod=i}else toxi.AbstractWave.apply(this,[d,e,g,h,new toxi.ConstantWave(0)]);else toxi.AbstractWave.apply(this,[d,e,1,0])};toxi.extend(toxi.FMTriangleWave,toxi.AbstractWave);toxi.FMTriangleWave.prototype.getClass=function(){return"FMTriangleWave"};toxi.FMTriangleWave.prototype.pop=function(){this.parent.pop.call(this);this.fmod.pop()};
toxi.FMTriangleWave.prototype.push=function(){this.parent.push.call(this);this.fmod.push()};toxi.FMTriangleWave.prototype.reset=function(){this.parent.reset.call(this);this.fmod.reset()};toxi.FMTriangleWave.prototype.update=function(){this.value=2*this.amp*(Math.abs(toxi.AbstractWave.PI-this.phase)*toxi.MathUtils.INV_PI-0.5)+this.offset;this.cyclePhase(this.frequency+this.fmod.update());return this.value};toxi.SineWave=function(d,e,g,h){toxi.AbstractWave.apply(this,[d,e,g,h])};
toxi.extend(toxi.SineWave,toxi.AbstractWave);toxi.SineWave.prototype.getClass=function(){return"SineWave"};toxi.SineWave.prototype.pop=function(){this.parent.pop.call(this)};toxi.SineWave.prototype.push=function(){this.parent.push.call(this)};toxi.SineWave.prototype.update=function(){this.value=Math.sin(this.phase)*this.amp+this.offset;this.cyclePhase(this.frequency);return this.value};toxi.WaveState=function(d,e,g,h){this.phase=d;this.frequency=e;this.amp=g;this.offset=h};
toxi.SinCosLUT=function(d){if(!d)d=toxi.SinCosLUT.DEFAULT_PRECISION;this.precision=d;this.period=360/this.precision;this.quadrant=this.period>>2;this.deg2rad=Math.PI/180*this.precision;this.rad2deg=180/Math.PI/this.precision;this.sinLUT=[];for(d=0;d<this.period;d++)this.sinLUT[d]=Math.sin(d*this.deg2rad)};
toxi.SinCosLUT.prototype={cos:function(d){for(;d<0;)d+=toxi.MathUtils.TWO_PI;return this.sinLUT[(d*this.rad2deg+this.quadrant)%this.period]},getPeriod:function(){return this.period},getPrecision:function(){return this.precision},getSinLUT:function(){return this.sinLUT},sin:function(d){for(;d<0;)d+=toxi.MathUtils.TWO_PI;return this.sinLUT[d*this.rad2deg%this.period]}};toxi.SinCosLUT.DEFAULT_PRECISION=0.25;toxi.SinCosLUT.DEFAULT_INSTANCE=undefined;
toxi.SinCosLUT.getDefaultInstance=function(){if(toxi.SinCosLUT.DEFAULT_INSTANCE===undefined)toxi.SinCosLUT.DEFAULT_INSTANCE=new toxi.SinCosLUT;return toxi.SinCosLUT.DEFAULT_INSTANCE};
toxi.UnitTranslator={INCH_MM:25.4,POINT_POSTSCRIPT:72,millisToPixels:function(d,e){return Math.floor(d/this.INCH_MM*e)},millisToPoints:function(d){return d/this.INCH_MM*this.POINT_POSTSCRIPT},pixelsToInch:function(d,e){return d/e},pixelsToMillis:function(d,e){return this.pixelsToInch(d,e)*this.INCH_MM},pixelsToPoints:function(d,e){return this.pixelsToInch(d,e)*this.POINT_POSTSCRIPT},pointsToMillis:function(d){return d/this.POINT_POSTSCRIPT*this.INCH_MM},pointsToPixels:function(d,e){return this.millisToPixels(this.pointsToMillis(d),
e)},squareInchToMillis:function(d){return d*this.INCH_MM*this.INCH_MM},squarePointsToInch:function(d){return d/(this.POINT_POSTSCRIPT*this.POINTPOSCRIPT)},squarePointsToMillis:function(d){return this.squareInchToMillis(this.squarePointsToInch(d))}};toxi.Vec2D=function(d,e){if(d instanceof Object&&d.x!==undefined&&d.y!==undefined){e=d.y;d=d.x}else{if(d===undefined)d=0;if(e===undefined)e=0}this.x=d;this.y=e};
toxi.Vec2D.Axis={X:{getVector:function(){return toxi.Vec2D.X_AXIS},toString:function(){return"toxi.Vec2D.Axis.X"}},Y:{getVector:function(){return toxi.Vec2D.Y_AXIS},toString:function(){return"toxi.Vec2D.Axis.Y"}}};
(function(){var d=function(e,g){if(e instanceof Object){g=e.y;e=e.x}else if(e!==undefined&&g===undefined)g=e;else if(e===undefined)e=0;else if(g===undefined)g=0;return{x:e,y:g}};toxi.Vec2D.prototype={abs:function(){this.x=Math.abs(this.x);this.y=Math.abs(this.y);return this},add:function(e,g){var h=new toxi.Vec2D(e,g);h.x+=this.x;h.y+=this.y;return h},addSelf:function(e,g){var h=d(e,g);this.x+=h.x;this.y+=h.y;return this},angleBetween:function(e,g){if(g===undefined){this.dot(e);return Math.acos(this.dot(e))}var h=
g?this.getNormalized().dot(e.getNormalized()):this.dot(e);return Math.acos(h)},clear:function(){this.x=this.y=0;return this},compareTo:function(e){if(this.x==e.x&&this.y==e.y)return 0;return this.magSquared()-e.magSquared()},constrain:function(e,g){if(e instanceof Object&&g instanceof Object){this.x=toxi.MathUtils.clip(this.x,e.x,g.x);this.y=toxi.MathUtils.clip(this.y,e.y,g.y)}else if(e.x!==undefined&&e.y!==undefined&&e.width!==undefined&&e.height!==undefined){this.x=toxi.MathUtils.clip(this.x,e.x,
e.x+e.width);this.y=toxi.MathUtils.clip(this.y,e.y,e.y+e.height)}return this},copy:function(){return new toxi.Vec2D(this)},cross:function(e){return this.x*e.y-this.y*e.x},distanceTo:function(e){if(e!==undefined){var g=this.x-e.x;e=this.y-e.y;return Math.sqrt(g*g+e*e)}else return NaN},distanceToSquared:function(e){if(e!==undefined){var g=this.x-e.x;e=this.y-e.y;return g*g+e*e}else return NaN},dot:function(e){return this.x*e.x+this.y*e.y},equals:function(e){if(e instanceof Object)return this.x==e.x&&
this.y==e.y;return false},equalsWithTolerance:function(e,g){if(toxi.MathUtils.abs(this.x-e.x)<g)if(toxi.MathUtils.abs(this.y-e.y)<g)return true;return false},floor:function(){this.x=toxi.MathUtils.floor(this.x);this.y=toxi.MathUtils.floor(this.y);return this},frac:function(){this.x-=toxi.MathUtils.floor(this.x);this.y-=toxi.MathUtils.floor(this.y);return this},getAbs:function(){return(new toxi.Vec2D(this)).abs()},getComponent:function(e){if(typeof e=="number")e=e===0?toxi.Vec2D.Axis.X:toxi.Vec2D.Axis.Y;
if(e==toxi.Vec2D.Axis.X)return this.x;else if(e==toxi.Vec2D.Axis.Y)return this.y;return 0},getConstrained:function(e){return(new toxi.Vec2D(this)).constrain(e)},getFloored:function(){return(new toxi.Vec2D(this)).floor()},getFrac:function(){return(new toxi.Vec2D(this)).frac()},getInverted:function(){return new toxi.Vec2D(-this.x,-this.y)},getLimited:function(e){if(this.magSquared()>e*e)return this.getNormalizedTo(e);return new toxi.Vec2D(this)},getNormalized:function(){return(new toxi.Vec2D(this)).normalize()},
getNormalizedTo:function(e){return(new toxi.Vec2D(this)).normalizeTo(e)},getPerpendicular:function(){return(new toxi.Vec2D(this)).perpendicular()},getReciprocal:function(){return(new toxi.Vec2D(this)).reciprocal()},getReflected:function(e){return(new toxi.Vec2D(this)).reflect(e)},getRotated:function(e){return(new toxi.Vec2D(this)).rotate(e)},getSignum:function(){return(new toxi.Vec2D(this)).signum()},heading:function(){return Math.atan2(this.y,this.x)},interpolateTo:function(e,g,h){return h===undefined?
new toxi.Vec2D(this.x+(e.x-this.x)*g,this.y+(e.y-this.y)*g):new toxi.Vec2D(h.interpolate(this.x,e.x,g),h.interpolate(this.y,e.y,g))},interpolateToSelf:function(e,g,h){if(h===undefined){this.x+=(e.x-this.x)*g;this.y+=(e.y-this.y)*g}else{this.x=h.interpolate(this.x,e.x,g);this.y=h.interpolate(this.y,e.y,g)}return this},invert:function(){this.x*=-1;this.y*=-1;return this},isInCircle:function(e,g){return this.sub(e).magSquared()<=g*g},isInRectangle:function(e){if(this.x<e.x||this.x>e.x+e.width)return false;
if(this.y<e.y||this.y>e.y+e.height)return false;return true},isInTriangle:function(e,g,h){e=this.sub(e).normalize();g=this.sub(g).normalize();h=this.sub(h).normalize();var i=Math.acos(e.dot(g));i+=Math.acos(g.dot(h));i+=Math.acos(h.dot(e));return Math.abs(i-toxi.MathUtils.TWO_PI)<=0.0050},isMajorAxis:function(e){var g=Math.abs(this.x),h=Math.abs(this.y),i=1-e;if(g>i)return h<e;else if(h>i)return g<e;return false},isZeroVector:function(){return Math.abs(this.x)<toxi.MathUtils.EPS&&Math.abs(this.y)<
toxi.MathUtils.EPS},jitter:function(e,g){var h=d(e,g);this.x+=toxi.MathUtils.normalizedRandom()*h.x;this.y+=toxi.MathUtils.normalizedRandom()*h.y;return this},limit:function(e){if(this.magSquared()>e*e)return this.normalize().scaleSelf(e);return this},magnitude:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},magSquared:function(){return this.x*this.x+this.y*this.y},max:function(e){return new toxi.Vec2D(toxi.MathUtils.max(this.x,e.x),toxi.MathUtils.max(this.y,e.y))},maxSelf:function(e){this.x=
toxi.MathUtils.max(this.x,e.x);this.y=toxi.MathUtils.max(this.y,e.y);return this},min:function(e){return new toxi.Vec2D(toxi.MathUtils.min(this.x,e.x),toxi.MathUtils.min(this.y,e.y))},minSelf:function(e){this.x=toxi.MathUtils.min(this.x,e.x);this.y=toxi.MathUtils.min(this.y,e.y);return this},normalize:function(){var e=this.x*this.x+this.y*this.y;if(e>0){e=1/Math.sqrt(e);this.x*=e;this.y*=e}return this},normalizeTo:function(e){var g=Math.sqrt(this.x*this.x+this.y*this.y);if(g>0){g=e/g;this.x*=g;this.y*=
g}return this},perpendicular:function(){var e=this.x;this.x=-this.y;this.y=e;return this},positiveHeading:function(){var e=Math.sqrt(this.x*this.x+this.y*this.y);if(this.y>=0)return Math.acos(this.x/e);return Math.acos(-this.x/e)+toxi.MathUtils.PI},reciprocal:function(){this.x=1/this.x;this.y=1/this.y;return this},reflect:function(e){return this.set(e.scale(this.dot(e)*2).subSelf(this))},rotate:function(e){var g=Math.cos(e);e=Math.sin(e);var h=g*this.x-e*this.y;this.y=e*this.x+g*this.y;this.x=h;return this},
roundToAxis:function(){if(Math.abs(this.x)<0.5)this.x=0;else{this.x=this.x<0?-1:1;this.y=0}if(Math.abs(this.y)<0.5)this.y=0;else{this.y=this.y<0?-1:1;this.x=0}return this},scale:function(e,g){var h=d(e,g);return new toxi.Vec2D(this.x*h.x,this.y*h.y)},scaleSelf:function(e,g){var h=d(e,g);this.x*=h.x;this.y*=h.y;return this},set:function(e,g){var h=d(e,g);this.x=h.x;this.y=h.y;return this},setComponent:function(e,g){if(typeof e=="number")e=e===0?toxi.Vec2D.Axis.X:toxi.Vec2D.Axis.Y;if(e===toxi.Vec2D.Axis.X)this.x=
g;else if(e===toxi.Vec2D.Axis.Y)this.y=g;return this},signum:function(){this.x=this.x<0?-1:this.x===0?0:1;this.y=this.y<0?-1:this.y===0?0:1;return this},sub:function(e,g){var h=d(e,g);return new toxi.Vec2D(this.x-h.x,this.y-h.y)},subSelf:function(e,g){var h=d(e,g);this.x-=h.x;this.y-=h.y;return this},tangentNormalOfEllipse:function(e,g){var h=this.sub(e);return(new toxi.Vec2D(h.x/(g.x*g.x),h.y/(g.y*g.y))).normalize()},toArray:function(){return[this.x,this.y]},toCartesian:function(){var e=this.x*Math.cos(this.y);
this.y=this.x*Math.sin(this.y);this.x=e;return this},toPolar:function(){var e=Math.sqrt(this.x*this.x+this.y*this.y);this.y=Math.atan2(this.y,this.x);this.x=e;return this},toString:function(){return"{x:"+this.x+", y:"+this.y+"}"}};toxi.Vec2D.X_AXIS=new toxi.Vec2D(1,0);toxi.Vec2D.Y_AXIS=new toxi.Vec2D(0,1);toxi.Vec2D.ZERO=new toxi.Vec2D;toxi.Vec2D.MIN_VALUE=new toxi.Vec2D(Number.MIN_VALUE,Number.MIN_VALUE);toxi.Vec2D.MAX_VALUE=new toxi.Vec2D(Number.MAX_VALUE,Number.MAX_VALUE);toxi.Vec2D.fromTheta=
function(e){return new toxi.Vec2D(Math.cos(e),Math.sin(e))};toxi.Vec2D.max=function(e,g){return new toxi.Vec2D(toxi.MathUtils.max(e.x,g.x),toxi.MathUtils.max(e.y,g.y))};toxi.Vec2D.min=function(e,g){return new toxi.Vec2D(toxi.MathUtils.min(e.x,g.x),toxi.MathUtils.min(e.y,g.y))};toxi.Vec2D.randomVector=function(){return(new toxi.Vec2D(Math.random()*2-1,Math.random()*2-1)).normalize()}})();
toxi.Vec3D=function(d,e,g){if(d instanceof Object&&d.x!==undefined&&d.y!==undefined&&d.z!==undefined){this.x=d.x;this.y=d.y;this.z=d.z}else if(d===undefined)this.z=this.y=this.x=0;else{this.x=d;this.y=e;this.z=g}};
toxi.Vec3D.prototype={abs:function(){this.x=Math.abs(this.x);this.y=Math.abs(this.y);this.z=Math.abs(this.z);return this},add:function(d,e,g){if(d instanceof Object)return new toxi.Vec3D(this.x+d.x,this.y+d.y,this.z+d.z);return new toxi.Vec3D(this.x+d,this.y+e,this.z+g)},addSelf:function(d,e,g){if(d!==undefined&&e!==undefined&&g!==undefined){this.x+=d;this.y+=e;this.z+=g}else{this.x+=d.x;this.y+=d.y;this.z+=d.z}return this},angleBetween:function(d,e){var g;g=e?this.getNormalized().dot(d.getNormalized()):
this.dot(d);return Math.acos(g)},clear:function(){this.x=this.y=this.z=0;return this},compareTo:function(d){if(this.x==d.x&&this.y==d.y&&this.z==d.z)return 0;return this.magSquared()-d.magSqaured()},constrain:function(d){if(d instanceof AABB){d.getMax();d.getMin()}this.x=Math.clip(this.x,this.min.x,this.max.x);this.y=toxi.MathUtils.clip(this.y,this.min.y,this.max.y);this.z=toxi.MathUtils.clip(this.z,this.min.z,this.max.z);return this},copy:function(){return new toxi.Vec3D(this)},cross:function(d){return new toxi.Vec3D(this.y*
d.z-d.y*this.z,this.z*d.x-d.z*this.x,this.x*d.y-d.x*this.y)},crossInto:function(d){var e=d.x,g=d.y;d=d.z;result.x=this.y*d-g*this.z;result.y=this.z*e-d*this.x;result.z=this.x*g-e*this.y;return result},crossSelf:function(d){var e=this.y*d.z-d.y*this.z,g=this.z*d.x-d.z*this.x;this.z=d.y-d.x*this.y;this.y=g;this.x=e;return this},distanceTo:function(d){if(d!==undefined){var e=this.x-d.x,g=this.y-d.y;d=this.z-d.z;return Math.sqrt(e*e+g*g+d*d)}return NaN},distanceToSquared:function(d){if(d!==undefined){var e=
this.x-d.x,g=this.y-d.y;d=this.z-d.z;return e*e+g*g+d*d}return NaN},dot:function(d){return this.x*d.x+this.y*d.y+this.z*d.z},equals:function(d){if(d instanceof Object)return this.x==d.x&&this.y==d.y&&this.z==d.z;return false},equalsWithTolerance:function(d,e){if(Math.abs(this.x-d.x)<e)if(Math.abs(this.y-d.y)<e)if(Math.abs(this.z-d.z)<e)return true;return false},floor:function(){this.x=Math.floor(this.x);this.y=Math.floor(this.y);this.z=Math.floor(this.z);return this},frac:function(){this.x-=Math.floor(this.x);
this.y-=Math.floor(this.y);this.z-=Math.floor(this.z);return this},getAbs:function(){return(new toxi.Vec3D(this)).abs()},getComponent:function(d){if(d instanceof Number)d=d===toxi.Vec3D.Axis.X?0:d===toxi.Vec3D.Axis.Y?1:2;switch(d){case 0:return this.x;case 1:return this.y;case 2:return this.z}},getConstrained:function(d){return(new toxi.Vec3D(this)).constrain(d)},getFloored:function(){return(new toxi.Vec3D(this)).floor()},getFrac:function(){return(new toxi.Vec3D(this)).frac()},getInverted:function(){return new toxi.Vec3D(-this.x,
-this.y,-this.z)},getLimited:function(d){if(this.magSquared()>d*d)return this.getNormalizedTo(d);return new toxi.Vec3D(this)},getNormalized:function(){return(new toxi.Vec3D(this)).normalize()},getNormalizedTo:function(d){return(new toxi.Vec3D(this)).normalizeTo(d)},getReciprocal:function(){return this.copy().reciprocal()},getReflected:function(d){return this.copy().reflect(d)},getRotatedAroundAxis:function(d,e){return(new toxi.Vec3D(this)).rotateAroundAxis(d,e)},getRotatedX:function(d){return(new toxi.Vec3D(this)).rotateX(d)},
getRotatedY:function(d){return(new toxi.Vec3D(this)).rotateY(d)},getRotatedZ:function(d){return(new toxi.Vec3D(this)).rotateZ(d)},getSignum:function(){return(new toxi.Vec3D(this)).signum()},headingXY:function(){return Math.atan2(this.y,this.x)},headingXZ:function(){return Math.atan2(this.z,this.x)},headingYZ:function(){return Math.atan2(this.y,this.z)},immutable:function(){return this},interpolateTo:function(d,e,g){if(g===undefined)return new toxi.Vec3D(this.x+(d.x-this.x)*e,this.y+(d.y-this.y)*e,
this.z+(d.z-z)*e);return new toxi.Vec3D(g.interpolate(this.y,d.y,e),g.interpolate(this.y,d.y,e),g.interpolate(this.z,d.z,e))},interpolateToSelf:function(d,e,g){if(g===undefined){this.x+=(d.x-this.x)*e;this.y+=(d.y-this.y)*e;this.z+=(d.z-this.z)*e}else{this.x=g.interpolate(this.x,d.x,e);this.y=g.interpolate(this.y,d.y,e);this.z=g.interpolate(this.z,d.z,e)}return this},invert:function(){this.x*=-1;this.y*=-1;this.z*=-1;return this},isInAABB:function(d,e){if(e){var g=e.x;if(this.x<d.x-g||this.x>d.x+
g)return false;g=e.y;if(this.y<d.y-g||this.y>d.y+g)return false;g=e.y;if(this.z<d.z-g||this.y>d.z+g)return false}return true},isMajorAxis:function(d){var e=toxi.MathUtils.abs(this.x),g=toxi.MathUtils.abs(this.y),h=toxi.MathUtils.abs(this.z),i=1-d;if(e>i){if(g<d)return h<d}else if(g>i){if(e<d)return h<d}else if(h>i)if(e<d)return g<d;return false},isZeroVector:function(){return Math.abs(this.x)<toxi.MathUtils.EPS&&Math.abs(this.y)<toxi.MathUtils.EPS&&toxi.MathUtils.abs(this.z)<toxi.MathUtils.EPS},jitter:function(d,
e,g){if(e===undefined||g===undefined)e=g=d;this.x+=toxi.MathUtils.normalizedRandom()*d;this.y+=toxi.MathUtils.normalizedRandom()*e;this.z+=toxi.MathUtils.normalizedRandom()*g;return this},limit:function(d){if(this.magSquared()>d*d)return this.normalize().scaleSelf(d);return this},magnitude:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},magSquared:function(){return this.x*this.x+this.y*this.y+this.z*this.z},maxSelf:function(d){this.x=Math.max(this.x,d.x);this.y=Math.max(this.y,
d.y);this.z=Math.max(this.z,d.z);return this},minSelf:function(d){this.x=Math.min(this.x,d.x);this.y=Math.min(this.y,d.y);this.z=Math.min(this.z,d.z);return this},modSelf:function(d,e,g){if(e===undefined||g===undefined)e=g=d;this.x%=d;this.y%=e;this.z%=g;return this},normalize:function(){var d=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);if(d>0){d=1/d;this.x*=d;this.y*=d;this.z*=d}return this},normalizeTo:function(d){var e=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);if(e>0){e=d/e;
this.x*=e;this.y*=e;this.z*=e}return this},reciprocal:function(){this.x=1/this.x;this.y=1/this.y;this.z=1/this.z;return this},reflect:function(d){return this.set(d.scale(this.dot(d)*2).subSelf(this))},rotateAroundAxis:function(d,e){var g=d.x,h=d.y,i=d.z,m=g*this.x,n=g*this.y,o=g*this.z,p=h*this.x,r=h*this.y,t=h*this.z,u=i*this.x,B=i*this.y,C=i*this.z;si=Math.sin(e);co=Math.cos(e);o=h*(m+r+C)+(this.y*(g*g+i*i)-h*(m+C))*co+(u-o)*si;n=i*(m+r+C)+(this.z*(g*g+h*h)-i*(m+r))*co+(-p+n)*si;this.x=g*(m+r+C)+
(this.x*(h*h+i*i)-g*(r+C))*co+(-B+t)*si;this.y=o;this.z=n;return this},rotateX:function(d){var e=Math.cos(d);d=Math.sin(d);var g=e*this.z-d*this.y;this.y=d*this.z+e*this.y;this.z=g;return this},rotateY:function(d){var e=Math.cos(d);d=Math.sin(d);var g=e*this.x-d*this.z;this.z=d*this.x+e*this.z;this.x=g;return this},rotateZ:function(d){var e=Math.cos(d);d=Math.sin(d);var g=e*this.x-d*this.y;this.y=d*this.x+e*this.y;this.x=g;return this},roundToAxis:function(){if(Math.abs(this.x)<0.5)this.x=0;else{this.x=
this.x<0?-1:1;this.y=this.z=0}if(Math.abs(this.y)<0.5)this.y=0;else{this.y=this.y<0?-1:1;this.x=this.z=0}if(Math.abs(this.z)<0.5)this.z=0;else{this.z=this.z<0?-1:1;this.x=this.y=0}return this},scale:function(d,e,g){if(d instanceof toxi.Vec3D)return new toxi.Vec3D(this.x*d.x,this.y*d.y,this.z*d.z);else if(e===undefined||g===undefined)e=g=d;return new toxi.Vec3D(this.x*d,this.y*e,this.z*g)},scaleSelf:function(d,e,g){if(d instanceof Object){this.x*=d.x;this.y*=d.y;this.z*=d.z;return true}else if(e===
undefined||g===undefined)e=g=d;this.x*=d;this.y*=e;this.z*=g;return this},set:function(d,e,g){if(d instanceof Object){this.x=d.x;this.y=d.y;this.z=d.z;return this}else if(e===undefined||g===undefined)e=g=d;this.x=d;this.y=e;this.z=g;return this},setXY:function(d){this.x=d.x;this.y=d.y;return this},shuffle:function(d){for(var e,g=0;g<d;g++)switch(Math.floor(Math.random()*3)){case 0:e=this.x;this.x=this.y;this.z=e;break;case 1:e=this.x;this.x=this.z;this.z=e;break;case 2:e=this.y;this.y=this.z;this.z=
e}return this},signum:function(){this.x=this.x<0?-1:this.x===0?0:1;this.y=this.y<0?-1:this.y===0?0:1;this.z=this.z<0?-1:this.z===0?0:1;return this},sub:function(d,e,g){if(d instanceof Object)return new toxi.Vec3D(this.x-d.x,this.y-d.y,this.z-d.z);else if(e===undefined||g===undefined)e=g=d;return new toxi.Vec3D(this.x-d,this.y-e,this.z-g)},subSelf:function(d,e,g){if(d instanceof Object){this.x-=d.x;this.y-=d.y;this.z-=d.z;return this}else if(e===undefined||g===undefined)e=g=d;this.x-=d;this.y-=e;this.z-=
g;return this},to2DXY:function(){return new toxi.Vec2D(this.x,this.y)},to2DXZ:function(){return new toxi.Vec2D(this.x,this.z)},to2DYZ:function(){return new toxi.Vec2D(this.y,this.z)},toArray:function(){return[this.x,this.y,this.z]},toArray4:function(d){var e=this.toArray();e[3]=d;return e},toCartesian:function(){var d=this.x*Math.cos(this.z),e=this.x*Math.sin(this.z),g=d*Math.sin(this.y);this.x=d*Math.cos(this.y);this.y=e;this.z=g;return this},toSpherical:function(){var d=Math.abs(this.x)<=toxi.MathUtils.EPS?
toxi.MathUtils.EPS:this.x,e=this.z,g=Math.sqrt(d*d+this.y*this.y+e*e);this.z=Math.asin(this.y/g);this.y=Math.atan(e/d)+(d<0?Math.PI:0);this.x=g;return this},toString:function(){return"[ x: "+this.x+", y: "+this.y+", z: "+this.z+"]"}};toxi.Vec3D.MIN_VALUE=new toxi.Vec3D(Number.MIN_VALUE,Number.MIN_VALUE,Number.MIN_VALUE);toxi.Vec3D.MAX_VALUE=new toxi.Vec3D(Number.MAX_VALUE,Number.MAX_VALUE,Number.MAX_VALUE);toxi.Vec3D.fromXYTheta=function(d){return new toxi.Vec3D(Math.cos(d),Math.sin(d),0)};
toxi.Vec3D.fromXZTheta=function(d){return new toxi.Vec3D(Math.cos(d),0,Math.sin(d))};toxi.Vec3D.fromYZTheta=function(d){return new toxi.Vec3D(0,Math.cos(d),Math.sin(d))};toxi.Vec3D.max=function(d,e){return new toxi.Vec3D(Math.max(d.x(),e.x()),Math.max(d.y(),e.y()),Math.max(d.z(),e.z()))};toxi.Vec3D.min=function(d,e){return new toxi.Vec3D(Math.min(d.x(),e.x()),Math.min(d.y(),e.y()),Math.min(d.z(),e.z()))};
toxi.Vec3D.randomVector=function(){return(new toxi.Vec3D(Math.random()*2-1,Math.random()*2-1,Math.random()*2-1)).normalize()};toxi.Vec3D.ZERO=new toxi.Vec3D(0,0,0);toxi.Vec3D.X_AXIS=new toxi.Vec3D(1,0,0);toxi.Vec3D.Y_AXIS=new toxi.Vec3D(0,1,0);toxi.Vec3D.Z_AXIS=new toxi.Vec3D(0,0,1);
toxi.Vec3D.Axis={X:{getVector:function(){return toxi.Vec3D.X_AXIS},toString:function(){return"toxi.Vec3D.Axis.X"}},Y:{getVector:function(){return toxi.Vec3D.Y_AXIS},toString:function(){return"toxi.Vec3D.Axis.Y"}},Z:{getVector:function(){return toxi.Vec3D.Z_AXIS},toString:function(){return"toxi.Vec3D.Axis.Z"}}};toxi.Vec2D.prototype.bisect=function(d){var e=this.sub(d);d=this.add(d);d=e.dot(d);return new toxi.Vec3D(e.x,e.y,-d/2)};
toxi.Vec2D.prototype.to3DXY=function(){return new toxi.Vec3D(this.x,this.y,0)};toxi.Vec2D.prototype.to3DXZ=function(){return new toxi.Vec3D(this.x,0,this.y)};toxi.Vec2D.prototype.to3DYZ=function(){return new toxi.Vec3D(0,this.x,this.y)};toxi.Polygon2D=function(){this.vertices=[];var d,e;if(arguments.length>1){d=0;for(e=arguments.length;d<e;d++)this.add(arguments[d].copy())}else if(arguments.length==1){var g=arguments[0];if(g instanceof Array){d=0;for(e=g.length;d<e;d++)this.add(g[d].copy())}}};
toxi.Polygon2D.prototype={add:function(d){this.vertices.indexOf(d)<0&&this.vertices.push(d)},containsPoint:function(d){var e=this.vertices.length,g=0,h=e-1,i=false,m=d.x;d=d.y;for(g=0;g<e;g++){var n=this.vertices[g];h=this.vertices[h];if(n.y<d&&h.y>=d||h.y<d&&n.y>=d)if(n.x+(d-n.y)/(h.y-n.y)*(h.x-n.x)<m)i=!i;h=g}return i},containsPoly:function(d){for(var e=0,g=d.vertices.length;e<g;e++)if(!this.containsPoint(d.vertices[e]))return false;return true},flipVertexOrder:function(){this.vertices.reverse();
return this},getArea:function(){for(var d=0,e=this.vertices.length,g=0;g<e;g++){var h=this.vertices[g],i=this.vertices[(g+1)%e];d+=h.x*i.y;d-=h.y*i.x}d*=0.5;return d},getCentroid:function(){for(var d=new toxi.Vec2D,e=this.vertices.length,g=0;g<e;g++){var h=this.vertices[g],i=this.vertices[(g+1)%e],m=h.x*i.y-i.x*h.y;d.x+=(h.x+i.x)*m;d.y+=(h.y+i.y)*m}return d.scale(1/(this.getArea()*6))},getCircumference:function(){for(var d=0,e=0,g=this.vertices.length;e<g;e++)d+=this.vertices[e].distanceTo(this.vertices[(e+
1)%g]);return d},getEdges:function(){for(var d=this.vertices.length,e=[],g=0;g<d;g++)e[g]=new toxi.Line2D(this.vertices[g],this.vertices[(g+1)%d]);return e},getNumPoints:function(){return this.vertices.length},isClockwise:function(){if(this.vertices.length>2)return toxi.Triangle2D.isClockwise(this.vertices[0],this.vertices[1],this.vertices[2]);return false},intersectsPoly:function(d){if(this.containsPoly(d))return true;else{var e=this.getEdges();d=d.getEdges();for(var g=0,h=e.length;g<h;g++)for(var i=
0,m=d.length,n=e[g];i<m;i++)if(n.intersectLine(d[i]).getType()==toxi.Line2D.LineIntersection.Type.INTERSECTING)return true;return false}},rotate:function(d){for(var e=0,g=this.vertices.length;e<g;e++)this.vertices[e].rotate(d)},scale:function(){var d,e;if(arguments.length==1){e=arguments[0];if(e instanceof toxi.Vec2D){d=e.x;e=e.y}else e=d=e}else if(arguments.length==2){d=arguments[0];e=arguments[1]}else throw"Invalid argument(s) passed.";for(var g=0,h=this.vertices.length;g<h;g++)this.vertices[g].scaleSelf(d,
e);return this},translate:function(){var d,e;if(arguments.length==1&&arguments[0]instanceof toxi.Vec2D){d=arg.x;e=arg.y}else if(arguments.length==2){d=arguments[0];e=arguments[1]}else throw"Invalid argument(s) passed.";for(var g=0,h=this.vertices.length;g<h;g++)this.vertices[g].addSelf(d,e);return this},smooth:function(d,e){for(var g=this.getCentroid(),h=this.vertices.length,i=[],m=0,n=h-1,o=1;m<h;m++){var p=this.vertices[m],r=this.vertices[n].sub(p).addSelf(this.vertices[o].sub(p)).addSelf(p.sub(g).scaleSelf(e));
i.push(p.add(r.scaleSelf(d)));n++;if(n==h)n=0;o++;if(o==h)o=0}this.vertices=i;return this},toString:function(){for(var d="",e=0;e<this.vertices.length;e++){d+=this.vertices[e];if(e<this.vertices.length-1)d+=", "}return d}};toxi.BernsteinPolynomial=function(d){this.resolution=d;for(var e=Array(d),g=Array(d),h=Array(d),i=Array(d),m=0,n=1/(d-1),o=0;o<d;o++){var p=1-m,r=p*p,t=m*m;e[o]=p*r;g[o]=3*m*r;h[o]=3*t*p;i[o]=m*t;m+=n}this.b0=e;this.b1=g;this.b2=h;this.b3=i};
toxi.Spline2D=function(d,e){if(arguments.length===0){this.setTightness(toxi.Spline2D.DEFAULT_TIGHTNESS);this.pointList=[]}else if(arguments.length>=3){this.pointList=d.slice(0);this.bernstein=e;this.setTightness(tightness)}else if(arguments.length==1){this.pointList=d;this.numP=d.length;this.coeffA=[];this.delta=[];this.bi=[];for(var g=0;g<this.numP;g++){this.coeffA[g]=new toxi.Vec2D;this.delta[g]=new toxi.Vec2D;this.bi[g]=0}}this.numP=this.pointList.length;this.bi=[]};
toxi.Spline2D.prototype={add:function(d){this.pointList.push(d.copy());return this},computeVertices:function(d){this.updateCoefficients();if(this.bernstein===undefined||this.bernstein.resolution!=d)this.bernstein=new toxi.BernsteinPolynomial(d);d=this.bernstein;this.vertices=[];this.findCPoints();for(var e=new toxi.Vec2D,g=new toxi.Vec2D,h=0;h<this.numP-1;h++){var i=this.points[h],m=this.points[h+1];e.set(this.delta[h]).addSelf(i);g.set(m).subSelf(this.delta[h+1]);for(var n=0;n<d.resolution;n++)this.vertices.push(new toxi.Vec2D(i.x*
d.b0[n]+e.x*d.b1[n]+g.x*d.b2[n]+m.x*d.b3[n],i.y*d.b0[n]+e.y*d.b1[n]+g.y*d.b2[n]+m.y*d.b3[n]))}return this.vertices},findCPoints:function(){this.bi[1]=-0.25;var d;this.coeffA[1].set((this.points[2].x-this.points[0].x-this.delta[0].x)*this.tightness,(this.points[2].y-this.points[0].y-this.delta[0].y)*this.tightness);for(d=2;d<this.numP-1;d++){this.bi[d]=-1/(this.invTightness+this.bi[d-1]);this.coeffA[d].set(-(this.points[d+1].x-this.points[d-1].x-this.coeffA[d-1].x)*this.bi[d],-(this.points[d+1].y-
this.points[d-1].y-this.coeffA[d-1].y)*this.bi[d])}for(d=this.numP-2;d>0;d--)this.delta[d].set(this.coeffA[d].x+this.delta[d+1].x*this.bi[d],this.coeffA[d].y+this.delta[d+1].y*this.bi[d])},getDecimatedVertices:function(d,e){if(e===undefined)e=true;if(this.vertices===undefined||this.vertices.length<2)this.computeVertices(toxi.Spline2D.DEFAULT_RES);for(var g=this.getEstimatedArcLength(),h=[],i=d/g,m=0,n=0;n<1;n+=i){for(var o=n*g;o>=this.arcLenIndex[m];)m++;this.uniform.push(this.vertices[m-1].interpolateTo(this.vertices[m],
(o-this.arcLenIndex[m-1])/(this.arcLenIndex[m]-this.arcLenIndex[m-1])))}e&&h.push(this.vertices[this.vertices.length-1]);return h},getEstimatedArcLength:function(){if(this.arcLenIndex===undefined||this.arcLenIndex!==undefined&&this.arcLenIndex.length!=this.vertices.length)this.arcLenIndex=[];for(var d=0,e=1;e<this.arcLenIndex.length;e++){d+=this.vertices[e-1].distanceTo(this.vertices[e]);this.arcLenIndex[e]=d}return d},getNumPoints:function(){return this.numP},getPointList:function(){return this.pointList},
getTightness:function(){return this.tightness},setPointList:function(d){this.pointList=d.slice(0);return this},setTightness:function(d){this.tightness=d;this.invTightness=1/this.tightness;return this},updateCoefficients:function(){this.numP=this.pointList.length;if(this.points===undefined||this.points!==undefined&&this.points.length!=this.numP){this.coeffA=[];this.delta=[];this.bi=[];for(var d=0;d<this.numP;d++){this.coeffA[d]=new toxi.Vec2D;this.delta[d]=new toxi.Vec2D}this.setTightness(this.tightness)}this.points=
this.pointList.slice(0)}};toxi.Spline2D.DEFAULT_TIGHTNESS=0.25;toxi.Spline2D.DEFAULT_RES=16;
toxi.Ellipse=function(d,e,g,h){this.radius=new toxi.Vec2D;if(arguments.length===0){toxi.Vec2D.apply(this,[0,0]);this.setRadii(1,1)}else if(d instanceof toxi.Vec2D){toxi.Vec2D.apply(this,[d.x,d.y]);e instanceof toxi.Vec2D?this.setRadii(e.x,e.y):this.setRadii(e,g)}else if(h===undefined)if(g===undefined){toxi.Vec2D.apply(this,[0,0]);this.setRadii(d,e)}else{toxi.Vec2D.apply(this,[d,e]);this.setRadii(g,g)}else{console.log("yup");toxi.Vec2D.apply(this,[d,e]);this.setRadii(g,h)}};
toxi.extend(toxi.Ellipse,toxi.Vec2D);toxi.Ellipse.prototype.containsPoint=function(d){var e=this.getFoci();return d.distanceTo(e[0])+d.distanceTo(e[1])<2*toxi.MathUtils.max(this.radius.x,this.radius.y)};toxi.Ellipse.prototype.getArea=function(){return toxi.MathUtils.PI*radius.x*radius.y};toxi.Ellipse.prototype.getCircumference=function(){return Math.sqrt(0.5*this.radius.magSquared())*toxi.MathUtils.TWO_PI};
toxi.Ellipse.prototype.getFoci=function(){var d=[];if(this.radius.x>this.radius.y){d[0]=this.sub(this.focus,0);d[1]=this.add(this.focus,0)}else{d[0]=this.sub(0,this.focus);d[1]=this.add(0,this.focus)}return d};toxi.Ellipse.prototype.getRadii=function(){return this.radius.copy()};toxi.Ellipse.prototype.setRadii=function(d,e){if(d instanceof toxi.Vec2D){e=d.y;d=d.x}this.radius.set(d,e);this.focus=this.radius.magnitude();return this};
toxi.Ellipse.prototype.toPolygon2D=function(d){for(var e=new toxi.Polygon2D,g=toxi.MathUtils.TWO_PI/d,h=0;h<d;h++){var i=toxi.Vec2D.fromTheta(h*g).scaleSelf(this.radius).addSelf(this);e.add(i)}return e};
toxi.Rect=function(d,e,g,h){if(arguments.length==2)if(d instanceof toxi.Vec2D){this.x=d.x;this.y=d.y;this.width=e.x-this.x;this.height=e.y-this.y}else throw Error("toxi.Rect received incorrect parameters");else if(arguments.length==4){this.x=d;this.y=e;this.width=g;this.height=h}else if(arguments.length==1){var i=arguments[0];if(i.x!==undefined&&i.y!==undefined&&i.width&&i.height){this.x=i.x;this.y=i.y;this.width=i.width;this.height=i.height}}else if(arguments.length>0)throw Error("toxi.Rect received incorrect parameters");
};toxi.Rect.fromCenterExtent=function(d,e){return new toxi.Rect(d.sub(e),d.add(e))};
toxi.Rect.prototype={containsPoint:function(d){var e=d.x;d=d.y;if(e<this.x||e>=this.x+this.width)return false;if(d<this.y||d>=this.y+this.height)return false;return true},copy:function(){return new toxi.Rect(this.x,this.y,this.width,this.height)},getArea:function(){return this.width*this.height},getAspect:function(){return this.width/this.height},getBottom:function(){return this.y+this.height},getBottomRight:function(){return new toxi.Vec2D(this.x+this.width,this.y+this.height)},getCentroid:function(){return new toxi.Vec2D(this.x+
this.width*0.5,this.y+this.height*0.5)},getDimensions:function(){return new toxi.Vec2D(this.width,this.height)},getEdge:function(d){switch(d){case 0:d=new toxi.Line2D(new toxi.Vec2D(this.x,this.y),new toxi.Vec2D(this.x+this.width,this.y));break;case 1:d=new toxi.Line2D(new toxi.Vec2D(this.x+this.width,this.y),new toxi.Vec2D(this.x+this.width,this.y+this.height));break;case 2:d=new toxi.Line2D(new toxi.Vec2D(this.x,this.y+this.height),new toxi.Vec2D(this.x+this.width,this.y+this.height));break;case 3:d=
new toxi.Line2D(new toxi.Vec2D(this.x,this.y),new toxi.Vec2D(this.x,this.y+this.height));break;default:throw Error("edge ID needs to be 0...3");}return d},getLeft:function(){return this.x},getRight:function(){return this.x+this.width},getTop:function(){return this.y},getTopLeft:function(){return new toxi.Vec2D(this.x,this.y)},intersectsRay:function(d,e,g){var h=d.getDirection().reciprocal(),i=h.x<0,m=h.y<0,n=this.getTopLeft(),o=this.getBottomRight(),p=i?o:n,r=(p.x-d.x)*h.x;p=i?n:o;i=(p.x-d.x)*h.x;
p=m?o:n;var t=(p.y-d.y)*h.y;p=m?n:o;h=(p.y-d.y)*h.y;if(!(r>h||t>i)){if(t>r)r=t;if(h<i)i=h;if(r<g&&i>e)return d.getPointAtDistance(r)}},intersectsRect:function(d){return!(this.x>d.x+d.width||this.x+this.width<d.x||this.y>d.y+d.height||this.y+this.height<d.y)},scale:function(d){var e=this.getCentroid();this.width*=d;this.height*=d;this.x=e.x-this.width*0.5;this.y=e.y-this.height*0.5;return this},set:function(d,e,g,h){if(arguments.length==-1){this.y=d.y;this.width=d.width;this.height=d.height;this.x=
d.x}else if(arguments.length===4){this.x=d;this.y=e;this.width=g;this.height=h}else throw Error("toxi.Rect set() received wrong parameters");},setDimenions:function(d){this.width=d.x;this.height=d.y;return this},setPosition:function(d){this.x=d.x;this.y=d.y;return this},toPolygon2D:function(){var d=new toxi.Polygon2D;d.add(new toxi.Vec2D(this.x,this.y));d.add(new toxi.Vec2D(this.x+this.width,this.y));d.add(new toxi.Vec2D(this.x+this.width,this.y+this.height));d.add(new toxi.Vec2D(this.x,this.y+this.height));
return d},toString:function(){return"rect: {x: "+this.x+", y: "+this.y+", width: "+this.width+", height: "+this.height+"}"},union:function(d){var e=MathUtils.max(this.x+this.width,d.x+d.width);this.x=MathUtils.min(this.x,d.x);this.width=e-this.x;e=MathUtils.max(this.y+this.height,d.y+d.height);this.y=MathUtils.min(this.y,d.y);this.height=e-this.y;return this}};
toxi.Circle=function(d,e,g){if(arguments.length==1)d instanceof toxi.Circle?toxi.Ellipse.apply(this,[d,d.radius.x]):toxi.Ellipse.apply(this,[0,0,d]);else if(arguments.length==2)toxi.Ellipse.apply(this,[d,e]);else{console.log("hit");toxi.Ellipse.apply(this,[d,e,g,g])}};toxi.extend(toxi.Circle,toxi.Ellipse);toxi.Circle.from2Points=function(d,e){var g=d.interpolateTo(e,0.5),h=g.distanceTo(d);return new toxi.Circle(g,h)};
toxi.Circle.from3Points=function(d,e,g){var h,i=e.sub(d),m=g.sub(e);if(toxi.MathUtils.abs(i.x)<=1.0E-7&&toxi.MathUtils.abs(m.y)<=1.0E-7){e=(new toxi.Vec2D(e.x+g.x,d.y+e.y)).scaleSelf(0.5);d=e.distanceTo(d);h=new toxi.Circle(e,d)}else{i=i.y/i.x;m=m.y/m.x;if(toxi.MathUtils.abs(i-m)>1.0E-7&&i!==0){g=(i*m*(d.y-g.y)+m*(d.x+e.x)-i*(e.x+g.x))/(2*(m-i));e=new toxi.Vec2D(g,-(g-(d.x+e.x)/2)/i+(d.y+e.y)/2);d=e.distanceTo(d);h=new toxi.Circle(e,d)}}return h};
toxi.Circle.prototype.containsPoint=function(d){return this.distanceToSquared(d)<=this.radius.x*this.radius.x};toxi.Circle.prototype.getCircumference=function(){return toxi.MathUtils.TWO_PI*this.radius.x};toxi.Circle.prototype.getRadius=function(){return this.radius.x};toxi.Circle.prototype.getTangentPoints=function(d){var e=this.interpolateTo(d,0.5);return this.intersectsCircle(new toxi.Circle(e,e.distanceTo(d)))};
toxi.Circle.prototype.intersectsCircle=function(d){var e,g=d.sub(this),h=g.magnitude(),i=this.radius.x;d=d.radius.x;if(h<=i+d&&h>=Math.abs(i-d)){d=(i*i-d*d+h*h)/(2*h);h=1/h;e=this.add(g.scale(d*h));i=Math.sqrt(i*i-d*d);g.perpendicular().scaleSelf(i*h);h=e.add(g);g=e.sub(g);e=[h,g]}return e};toxi.Circle.prototype.setRadius=function(d){this.setRadii(d,d);return this};toxi.CircleIntersector=function(d){this.circle=d;this.isec=undefined};
toxi.CircleIntersector.prototype={getIntersectionData:function(){return this.isec},intersectsRay:function(d){this.isec.clear();var e=circle.sub(d),g=e.magSquared();e=e.dot(d.getDirection());var h=circle.getRadius();g=h*h-(g-e*e);if(g>=0){this.isec.isIntersection=true;this.isec.dist=e-Math.sqrt(g);this.isec.pos=d.getPointAtDistance(isec.dist);this.isec.normal=this.isec.pos.sub(this.circle).normalize()}return this.isec.isIntersection}};
toxi.Cone=function(d,e,g,h,i){toxi.Vec3D.apply(this,[d]);this.dir=e.getNormalized();this.radiusNorth=g;this.radiusSouth=h;this.length=i};toxi.extend(toxi.Cone,toxi.Vec3D);
toxi.Cone.prototype.toMesh=function(){var d={mesh:undefined,steps:NaN,thetaOffset:0,topClosed:true,bottomClosed:true};if(arguments.length==1)if(arguments[0]instanceof Object){var e=arguments[0];d.mesh=e.mesh;d.steps=e.steps||e.resolution||e.res;d.thetaOffset=optiontsObject.thetaOffset||d.thetaOffset;d.topClosed=e.topClosed||d.topClosed;d.bottomClosed=e.bottomClosed||d.bottomClosed}else d.steps=arguments[0];else if(arguments.length==2){d.steps=arguments[0];d.thetaOffset=arguments[1]}else if(arguments.length==
5){d.mesh=arguments[0];d.steps=arguments[1];d.thetaOffset=arguments[2];d.topClosed=arguments[3];d.bottomClosed=arguments[4]}var g=this.add(0.01,0.01,0.01).cross(this.dir.getNormalized()).normalize(),h=this.dir.scale(this.length*0.5);e=this.sub(h);h=this.add(h);var i=[],m=[],n=Math.PI*2/d.steps,o=0;for(o=0;o<d.steps;o++){var p=g.getRotatedAroundAxis(this.dir,o*n+d.thetaOffset);i[o]=p.scale(this.radiusSouth).addSelf(e);m[o]=p.scale(this.radiusNorth).addSelf(h)}g=d.steps*2+2;o=d.steps*2+(d.topClosed?
d.steps:0)+(d.bottomClosed?d.steps:0);g=d.mesh||new toxi.TriangleMesh("cone",g,o);o=0;for(j=1;o<d.steps;o++,j++){if(j==d.steps)j=0;g.addFace(i[o],m[o],i[j],undefined,undefined,undefined,undefined);g.addFace(i[j],m[o],m[j],undefined,undefined,undefined,undefined);d.bottomClosed&&g.addFace(e,i[o],i[j],undefined,undefined,undefined,undefined);d.topClosed&&g.addFace(m[o],h,m[j],undefined,undefined,undefined,undefined)}return g};toxi.Line2D=function(d,e){this.a=d;this.b=e};
toxi.Line2D.prototype={closestPointTo:function(d){var e=this.b.sub(this.a);d=d.sub(this.a).dot(e)/e.magSquared();if(d<0)return this.a.copy();else if(d>1)return this.b.copy();return this.a.add(e.scaleSelf(d))},copy:function(){return new toxi.Line2D(this.a.copy(),this.b.copy())},equals:function(d){if(this==d)return true;if(!(d instanceof toxi.Line2D))return false;return(this.a.equals(d.a)||this.a.equals(d.b))&&(this.b.equals(d.b)||this.b.equals(d.a))},getDirection:function(){return this.b.sub(this.a).normalize()},
getLength:function(){return this.a.distanceTo(this.b)},getLengthSquared:function(){return this.a.distanceToSquared(this.b)},getMidPoint:function(){return this.a.add(this.b).scaleSelf(0.5)},getNormal:function(){return this.b.sub(this.a).perpendicular()},getTheta:function(){return this.a.angleBetween(this.b,true)},hasEndPoint:function(d){return this.a.equals(d)||this.b.equals(d)},intersectLine:function(d){var e;e=(d.b.y-d.a.y)*(this.b.x-this.a.x)-(d.b.x-d.a.x)*(this.b.y-this.a.y);var g=(d.b.x-d.a.x)*
(this.a.y-d.a.y)-(d.b.y-d.a.y)*(this.a.x-d.a.x);d=(this.b.x-this.a.x)*(this.a.y-d.a.y)-(this.b.y-this.a.y)*(this.a.x-d.a.x);if(e!==0){g=g/e;e=d/e;e=g>=0&&g<=1&&e>=0&&e<=1?new toxi.Line2D.LineIntersection(toxi.Line2D.LineIntersection.Type.INTERSECTING,this.a.interpolateTo(this.b,g)):new toxi.Line2D.LineIntersection(toxi.Line2D.LineIntersection.Type.NON_INTERSECTING,undefined)}else e=new toxi.Line2D.LineIntersection(toxi.Line2D.LineIntersection.Type.COINCIDENT,undefined);return e},offsetAndGrowBy:function(d,
e,g){var h=this.getMidPoint(),i=this.getDirection(),m=i.getPerpendicular();g!==undefined&&h.sub(g).dot(m)<0&&m.invert();m.normalizeTo(d);this.a.addSelf(m);this.b.addSelf(m);i.scaleSelf(e);this.a.subSelf(i);this.b.addSelf(i);return this},scale:function(d){d=(1-d)*0.5;var e=a.interpolateTo(this.b,d);this.b.interpolateToSelf(this.a,d);this.a.set(e);return this},set:function(d,e){this.a=d;this.b=e;return this},splitIntoSegments:function(d,e,g){return toxi.Line2D.splitIntoSegments(this.a,this.b,e,d,g)},
toRay2D:function(){return new toxi.Ray2D(this.a.copy(),this.b.sub(this.a).normalize())}};toxi.Line2D.splitIntoSegments=function(d,e,g,h,i){if(h===undefined)h=[];i&&h.push(d.copy());i=d.distanceTo(e);if(i>g){var m=d.copy();for(d=e.sub(d).limit(g);i>g;){m.addSelf(d);h.push(m.copy());i-=g}}h.push(e.copy());return h};toxi.Line2D.LineIntersection=function(d,e){this.type=d;this.pos=e};
toxi.Line2D.LineIntersection.prototype={getPos:function(){return this.pos.copy()},getType:function(){return this.type},toString:function(){return"type: "+this.type+" pos: "+this.pos}};toxi.Line2D.LineIntersection.Type={COINCIDENT:0,PARALLEL:1,NON_INTERSECTING:2,INTERSECTING:3};toxi.Triangle=function(d,e,g){if(arguments.length==3){this.a=d;this.b=e;this.c=g}};
toxi.Triangle.createEquilateralFrom=function(d,e){var g=d.interpolateTo(e,0.5),h=e.sub(d),i=d.cross(h.normalize());g.addSelf(i.normalizeTo(h.magnitude()*toxi.MathUtils.SQRT3/2));return new toxi.Triangle(d,e,g)};toxi.Triangle.isClockwiseInXY=function(d,e,g){return(e.x-d.x)*(g.y-d.y)-(g.x-d.x)*(e.y-d.y)<0};toxi.Triangle.isClockwiseInXZ=function(d,e,g){return(e.x-d.x)*(g.z-d.z)-(g.x-d.x)*(e.z-d.z)<0};toxi.Triangle.isClockwiseInYZ=function(d,e,g){return(e.y-d.y)*(g.z-d.z)-(g.y-d.y)*(e.z-d.z)<0};
toxi.Triangle.prototype={closestPointOnSurface:function(d){var e=this.b.sub(this.a),g=this.c.sub(this.a),h=this.c.sub(this.b),i=d.sub(this.a),m=d.sub(this.b),n=d.sub(this.c);d=a.sub(this.p);var o=b.sub(this.p),p=c.sub(this.p),r=i.dot(e);i=i.dot(g);if(r<=0&&i<=0)return this.a;var t=m.dot(this.a.sub(this.b)),u=n.dot(this.a.sub(this.c)),B=m.dot(h);n=n.dot(this.b.sub(this.c));if(t<=0&&B<=0)return this.b;if(u<=0&&n<=0)return this.c;var C=e.cross(g);m=C.dot(d.crossSelf(o));if(m<=0&&r>=0&&t>=0)return this.a.add(e.scaleSelf(r/
(r+t)));e=C.dot(o.crossSelf(p));if(e<=0&&B>=0&&n>=0)return this.b.add(h.scaleSelf(B/(B+n)));h=C.dot(p.crossSelf(d));if(h<=0&&i>=0&&u>=0)return this.a.add(g.scaleSelf(i/(i+u)));g=e/(e+h+m);h=h/(e+h+m);e=1-g-h;return this.a.scale(g).addSelf(this.b.scale(h)).addSelf(this.c.scale(e))},computeCentroid:function(){return this.centroid=this.a.add(this.b).addSelf(this.c).scaleSelf(1/3)},computeNormal:function(){return this.normal=this.a.sub(this.c).crossSelf(this.a.sub(this.b)).normalize()},containsPoint:function(d){if(d.equals(this.a)||
d.equals(this.b)||d.equals(this.c))return true;var e=d.sub(this.a).normalize(),g=d.sub(this.b).normalize();d=d.sub(this.c).normalize();var h=Math.acos(e.dot(g));h+=Math.acos(g.dot(d));h+=Math.acos(d.dot(e));return toxi.MathUtils.abs(h-toxi.MathUtils.TWO_PI)<=0.0050},flipVertexOrder:function(){this.a=this.c;this.c=this.t;return this},fromBarycentric:function(d){return new toxi.Vec3D(this.a.x*d.x()+this.b.x*d.y()+this.c.x*d.z(),this.a.y*d.x()+this.b.y*d.y()+this.c.y*d.z(),this.a.z*d.x()+this.b.z*d.y()+
this.c.z*d.z())},getBoundingBox:function(){var d=toxi.Vec3D.min(toxi.Vec3D.min(this.a,this.b),this.c),e=toxi.Vec3D.max(toxi.Vec3D.max(this.a,this.b),this.c);return toxi.AABB.fromMinMax(d,e)},getClosestPointTo:function(d){var e=new toxi.Line3D(this.a,this.b),g=e.closestPointTo(d),h=e.set(this.b,this.c).closestPointTo(d);e=e.set(this.c,this.a).closestPointTo(d);var i=d.sub(g).magSquared(),m=d.sub(h).magSquared();d=d.sub(e).magSquared();i=i;g=g;if(m<i){i=m;g=h}if(d<i)g=e;return g},isClockwiseInXY:function(){return toxi.Triangle.isClockwiseInXY(this.a,
this.b,this.c)},isClockwiseInXZ:function(){return toxi.Triangle.isClockwiseInXY(this.a,this.b,this.c)},isClockwiseInYZ:function(){return toxi.Triangle.isClockwiseInXY(this.a,this.b,this.c)},set:function(d,e,g){this.a=d;this.b=e;this.c=g},toBarycentric:function(d){var e=b.sub(this.a).cross(this.c.sub(this.a)),g=e.getNormalized(),h=g.dot(e);e=g.dot(this.b.sub(d).cross(this.c.sub(d)))/h;d=g.dot(this.c.sub(d).cross(this.a.sub(d)))/h;return new toxi.Vec3D(e,d,1-e-d)},toString:function(){return"Triangle: "+
this.a+","+this.b+","+this.c}};toxi.Triangle2D=function(d,e,g){if(arguments.length===3){this.a=d.copy();this.b=e.copy();this.c=g.copy()}};toxi.Triangle2D.createEquilateralFrom=function(d,e){var g=d.interpolateTo(e,0.5),h=d.sub(e),i=h.getPerpendicular();g.addSelf(i.normalizeTo(h.magnitude()*toxi.MathUtils.SQRT3/2));return new toxi.Triangle2D(d,e,g)};toxi.Triangle2D.isClockwise=function(d,e,g){return(e.x-d.x)*(g.y-d.y)-(g.x-d.x)*(e.y-d.y)<0};
toxi.Triangle2D.prototype={adjustTriangleSizeBy:function(d,e,g){if(arguments.length==1)g=e=d;this.computeCentroid();var h=(new toxi.Line2D(this.a.copy(),this.b.copy())).offsetAndGrowBy(d,1E5,this.centroid),i=(new toxi.Line2D(this.b.copy(),this.c.copy())).offsetAndGrowBy(e,1E5,this.centroid),m=(new toxi.Line2D(this.c.copy(),this.a.copy())).offsetAndGrowBy(g,1E5,this.centroid);this.a=h.intersectLine(m).getPos();this.b=h.intersectLine(i).getPos();this.c=i.intersectLine(m).getPos();this.computeCentroid();
return this},computeCentroid:function(){return this.centroid=this.a.add(this.b).addSelf(this.c).scaleSelf(1/3)},containsPoint:function(d){var e=d.sub(this.a).normalize(),g=d.sub(this.b).normalize();d=d.sub(this.c).normalize();var h=Math.acos(e.dot(g));h+=Math.acos(g.dot(d));h+=Math.acos(d.dot(e));return toxi.MathUtils.abs(h-toxi.MathUtils.TWO_PI)<=0.01},copy:function(){return new toxi.Triangle2D(this.a.copy(),this.b.copy(),this.c.copy())},flipVertexOrder:function(){var d=this.a;this.a=this.c;this.c=
d;return this},getArea:function(){return this.b.sub(this.a).cross(this.c.sub(this.a))*0.5},getBounds:function(){return new toxi.Rect(toxi.Vec2D.min(toxi.Vec2D.min(a,b),c),toxi.Vec2D.max(toxi.Vec2D.max(a,b),c))},getCircumCircle:function(){var d=a.bisect(this.b).cross(this.b.bisect(this.c));d=new toxi.Vec2D(d.x/d.z,d.y/d.z);var e=this.a.distanceTo(this.b),g=this.b.distanceTo(this.c),h=this.c.distanceTo(this.a);return new toxi.Circle(d,e*g*h/Math.sqrt((e+g+h)*(-e+g+h)*(e-g+h)*(e+g-h)))},getCircumference:function(){return this.a.distanceTo(this.b)+
this.b.distanceTo(this.c)+this.c.distanceTo(this.a)},getClosestPointTo:function(d){var e=new toxi.Line2D(this.a,this.b),g=e.closestPointTo(d),h=e.set(this.b,this.c).closestPointTo(d);e=e.set(this.c,this.a).closestPointTo(d);var i=d.sub(g).magSquared(),m=d.sub(h).magSquared();d=d.sub(e).magSquared();i=i;g=g;if(m<i){i=m;g=h}if(d<i)g=e;return g},intersectsTriangle:function(d){if(this.containsPoint(d.a)||this.containsPoint(d.b)||this.containsPoint(d.c))return true;if(d.containsPoint(this.a)||d.containsPoint(this.b)||
d.containsPoint(this.c))return true;var e=[new toxi.Line2D(this.a,this.b),new toxi.Line2D(this.b,this.c),new toxi.Line2D(this.c,this.a)];d=[new toxi.Line2D(d.a,d.b),new toxi.Line2D(d.b,d.c),new toxi.Line2D(d.c,d.a)];for(var g=0,h=e.length;g<h;g++)for(var i=e[g],m=0,n=d.length;m<n;m++){var o=i.intersectLine(d[m]).getType();if(o!=toxi.Line2D.LineIntersection.Type.NON_INTERSECTING&&o!=toxi.Line2D.LineIntersection.Type.PARALLEL)return true}return false},isClockwise:function(){return toxi.Triangle2D.isClockwise(this.a,
this.b,this.c)},set:function(d,e,g){this.a=d;this.b=e;this.c=g},toPolygon2D:function(){var d=new toxi.Polygon2D;d.add(this.a.copy());d.add(this.b.copy());d.add(this.c.copy());return d},toString:function(){return"Triangle2D: "+this.a+","+this.b+","+this.c}};toxi.IsectData2D=function(d){if(d!==undefined){this.isIntersection=d.isIntersection;this.dist=d.dist;this.pos=d.pos.copy();this.dir=d.dir.copy();this.normal=d.normal.copy()}else this.clear()};
toxi.IsectData2D.prototype={clear:function(){this.isIntersection=false;this.dist=0;this.pos=new toxi.Vec2D;this.dir=new toxi.Vec2D;this.normal=new toxi.Vec2D},toString:function(){var d="isec: "+this.isIntersection;if(this.isIntersection)d+=" at:"+this.pos+" dist:"+this.dist+" normal:"+this.normal;return d}};toxi.IsectData3D=function(d){if(d!==undefined){this.isIntersection=d.isIntersection;this.dist=d.dist;this.pos=d.pos.copy();this.dir=d.dir.copy();this.normal=d.normal.copy()}else this.clear()};
toxi.IsectData3D.prototype={clear:function(){this.isIntersection=false;this.dist=0;this.pos=new toxi.Vec3D;this.dir=new toxi.Vec3D;this.normal=new toxi.Vec3D},toString:function(){var d="isec: "+this.isIntersection;if(this.isIntersection)d+=" at:"+this.pos+" dist:"+this.dist+" normal:"+this.normal;return d}};
(function(){toxi.Matrix4x4=function(e,g,h,i,m,n,o,p,r,t,u,B,C,E,F,D){this.temp=[];if(arguments.length===0){this.matrix=[];this.matrix[0]=[1,0,0,0];this.matrix[1]=[0,1,0,0];this.matrix[2]=[0,0,1,0];this.matrix[3]=[0,0,0,1]}else if(V11 instanceof Number)this.matrix=[[e,g,h,i],[m,n,o,p],[r,t,u,B],[C,E,F,D]];else if(e instanceof Array){if(e.length!=9&&e.length!=16)throw Error("toxi.Matrix4x4: Array length must == 9 or 16");if(e.length==16){this.matrix=[];this.matrix[0]=e.slice(0,4);this.matrix[1]=e.slice(4,
8);this.matrix[2]=e.slice(8,12);this.matrix[3]=e.slice(12)}else{this.matrix[0]=e.slice(0,3);this.matrix[0][3]=NaN;this.matrix[1]=e.slice(3,6);this.matrix[1][3]=NaN;this.matrix[2]=e.slice(6,9);this.matrix[2][3]=NaN;this.matrix[3]=[NaN,NaN,NaN,NaN]}}else if(e instanceof toxi.Matrix4x4){var A=0;if(e.length==16)for(A=0;A<4;A++)this.matrix[A]=[e.matrix[A][0],e.matrix[A][1],e.matrix[A][2],e.matrix[A][3]];else{for(A=0;A<3;A++)this.matrix[A]=[e.matrix[A][0],e.matrix[A][1],e.matrix[A][2],NaN];this.matrix[3]=
[NaN,NaN,NaN,NaN]}}else console.error("toxi.Matrix4x4: incorrect parameters used to construct new instance")};toxi.Matrix4x4.prototype={add:function(e){return(new toxi.Matrix4x4(this)).addSelf(e)},addSelf:function(e){for(var g=0;g<4;g++){var h=this.matrix[g],i=e.matrix[g];h[0]+=i[0];h[1]+=i[1];h[2]+=i[2];h[3]+=i[3]}return this},applyTo:function(e){return this.applyToSelf(new toxi.Vec3D(e))},applyToSelf:function(e){for(var g=0;g<4;g++){var h=this.matrix[g];this.temp[g]=e.x*h[0]+e.y*h[1]+e.z*h[2]+h[3]}e.set(this.temp[0],
this.temp[1],this.temp[2]).scaleSelf(1/this.temp[3]);return e},copy:function(){return new toxi.Matrix4x4(this)},getInverted:function(){return(new toxi.Matrix4x4(this)).invert()},getRotatedAroundAxis:function(e,g){return(new toxi.Matrix4x4(this)).rotateAroundAxis(e,g)},getRotatedX:function(e){return(new toxi.Matrix4x4(this)).rotateX(e)},getRotatedY:function(e){return(new toxi.Matrix4x4(this)).rotateY(e)},getRotatedZ:function(e){return(new toxi.Matrix4x4(this)).rotateZ(e)},getTransposed:function(){return(new toxi.Matrix4x4(this)).transpose()},
identity:function(){var e=this.matrix[0];e[1]=e[2]=e[3]=0;e=this.matrix[1];e[0]=e[2]=e[3]=0;e=this.matrix[2];e[0]=e[1]=e[3]=0;e=this.matrix[3];e[0]=e[1]=e[2]=0;this.matrix[0][0]=1;this.matrix[1][1]=1;this.matrix[2][2]=1;this.matrix[3][3]=1;return this},invert:function(){var e=[],g=[],h=[],i=this.toArray(),m=0;for(m=0;m<4;m++){var n=m<<2;g[m]=i[n];g[m+4]=i[n+1];g[m+8]=i[n+2];g[m+12]=i[n+3]}e[0]=g[10]*g[15];e[1]=g[11]*g[14];e[2]=g[9]*g[15];e[3]=g[11]*g[13];e[4]=g[9]*g[14];e[5]=g[10]*g[13];e[6]=g[8]*
g[15];e[7]=g[11]*g[12];e[8]=g[8]*g[14];e[9]=g[10]*g[12];e[10]=g[8]*g[13];e[11]=g[9]*g[12];m=g[0];i=g[1];n=g[2];var o=g[3],p=g[4],r=g[5],t=g[6],u=g[7];h[0]=e[0]*r+e[3]*t+e[4]*u;h[0]-=e[1]*r+e[2]*t+e[5]*u;h[1]=e[1]*p+e[6]*t+e[9]*u;h[1]-=e[0]*p+e[7]*t+e[8]*u;h[2]=e[2]*p+e[7]*r+e[10]*u;h[2]-=e[3]*p+e[6]*r+e[11]*u;h[3]=e[5]*p+e[8]*r+e[11]*t;h[3]-=e[4]*p+e[9]*r+e[10]*t;h[4]=e[1]*i+e[2]*n+e[5]*o;h[4]-=e[0]*i+e[3]*n+e[4]*o;h[5]=e[0]*m+e[7]*n+e[8]*o;h[5]-=e[1]*m+e[6]*n+e[9]*o;h[6]=e[3]*m+e[6]*i+e[11]*o;h[6]-=
e[2]*m+e[7]*i+e[10]*o;h[7]=e[4]*m+e[9]*i+e[10]*n;h[7]-=e[5]*m+e[8]*i+e[11]*n;e[0]=n*u;e[1]=o*t;e[2]=i*u;e[3]=o*r;e[4]=i*t;e[5]=n*r;e[6]=m*u;e[7]=o*p;e[8]=m*t;e[9]=n*p;e[10]=m*r;e[11]=i*p;m=g[8];i=g[9];n=g[10];o=g[11];p=g[12];r=g[13];t=g[14];u=g[15];h[8]=e[0]*r+e[3]*t+e[4]*u;h[8]-=e[1]*r+e[2]*t+e[5]*u;h[9]=e[1]*p+e[6]*t+e[9]*u;h[9]-=e[0]*p+e[7]*t+e[8]*u;h[10]=e[2]*p+e[7]*r+e[10]*u;h[10]-=e[3]*p+e[6]*r+e[11]*u;h[11]=e[5]*p+e[8]*r+e[11]*t;h[11]-=e[4]*p+e[9]*r+e[10]*t;h[12]=e[2]*n+e[5]*o+e[1]*i;h[12]-=
e[4]*o+e[0]*i+e[3]*n;h[13]=e[8]*o+e[0]*m+e[7]*n;h[13]-=e[6]*n+e[9]*o+e[1]*m;h[14]=e[6]*i+e[11]*o+e[3]*m;h[14]-=e[10]*o+e[2]*m+e[7]*i;h[15]=e[10]*n+e[4]*m+e[9]*i;h[15]-=e[8]*i+e[11]*n+e[5]*m;e=1/(g[0]*h[0]+g[1]*h[1]+g[2]*h[2]+g[3]*h[3]);for(k=m=0;m<4;m++){g=this.matrix[m];for(i=0;i<4;i++)g[i]=h[k++]*e}return this},multiply:function(e){if(typeof e=="number")return(new toxi.Matrix4x4(this)).multiply(e);return(new toxi.Matrix4x4(this)).multiplySelf(e)},multiplySelf:function(e){var g=0,h;if(typeof e==
"number"){for(g=0;g<4;g++){h=matrix[g];h[0]*=e;h[1]*=e;h[2]*=e;h[3]*=e}return this}var i=e.matrix[0],m=e.matrix[1],n=e.matrix[2];e=e.matrix[3];for(g=0;g<4;g++){h=this.matrix[g];for(var o=0;o<4;o++)this.temp[o]=h[0]*i[o]+h[1]*m[o]+h[2]*n[o]+h[3]*e[o];h[0]=this.temp[0];h[1]=this.temp[1];h[2]=this.temp[2];h[3]=this.temp[3]}return this},rotateAroundAxis:function(e,g){var h,i,m,n,o,p,r,t;h=e.x;i=e.y;m=e.z;n=Math.sin(g);o=Math.cos(g);p=1-o;r=p*h;t=p*i;d.set(r*h+o,r*i+n*m,r*m-n*i,0,r*i-n*m,t*i+o,t*m+n*h,
0,r*m+n*i,t*m-n*h,p*m*m+o,0,0,0,0,1);return this.multiplySelf(d)},rotateX:function(e){d.identity();d.matrix[1][1]=d[2][2]=Math.cos(e);d.matrix[2][1]=Math.sin(e);d.matrix[1][2]=-d.matrix[2][1];return this.multiplySelf(d)},rotateY:function(e){d.identity();d.matrix[0][0]=d.matrix[2][2]=Math.cos(e);d.matrix[0][2]=Math.sin(e);d.matrix[2][0]=-d.matrix[0][2];return this.multiplySelf(d)},rotateZ:function(e){d.identity();d.matrix[0][0]=d.matrix[1][1]=Math.cos(e);d.matrix[1][0]=Math.sin(e);d.matrix[0][1]=-d.matrix[1][0];
return this.multiplySelf(d)},scale:function(e,g,h){return(new toxi.Matrix4x4(this)).scaleSelf(e,g,h)},scaleSelf:function(e,g,h){if(e instanceof Object){g=e.y;h=e.z;e=e.x}else if(g===undefined||h===undefined)h=g=e;d.identity();d.matrix[0][0]=e;d.matrix[1][1]=g;d.matrix[2][2]=h;return this.multiplySelf(d)},set:function(e,g,h,i,m,n,o,p,r,t,u,B,C,E,F,D){var A;if(typeof e=="number"){A=this.matrix[0];A[0]=e;A[1]=g;A[2]=h;A[3]=i;A=this.matrix[1];A[0]=m;A[1]=n;A[2]=o;A[3]=p;A=this.matrix[2];A[0]=r;A[1]=t;
A[2]=u;A[3]=B;A=this.matrix[3];A[0]=C;A[1]=E;A[2]=F;A[3]=D}else for(e=0;e<4;e++){A=this.matrix[e];g=A.matrix[e];A[0]=g[0];A[1]=g[1];A[2]=g[2];A[3]=g[3]}return this},sub:function(e){return(new toxi.Matrix4x4(this)).subSelf(e)},subSelf:function(e){for(var g=0;g<4;g++){var h=this.matrix[g],i=e.matrix[g];h[0]-=i[0];h[1]-=i[1];h[2]-=i[2];h[3]-=i[3]}return this},toArray:function(e){if(e===undefined)e=[];for(var g=0,h=0;g<4;g++)for(var i=this.matrix[g],m=0;m<4;m++)e[h++]=i[m];return e},toFloatArray:function(e){return toArray(e)},
toString:function(){return"| "+this.matrix[0][0]+" "+this.matrix[0][1]+" "+this.matrix[0][2]+" "+this.matrix[0][3]+" |\n| "+this.matrix[1][0]+" "+this.matrix[1][1]+" "+this.matrix[1][2]+" "+this.matrix[1][3]+" |\n| "+this.matrix[2][0]+" "+this.matrix[2][1]+" "+this.matrix[2][2]+" "+this.matrix[2][3]+" |\n| "+this.matrix[3][0]+" "+this.matrix[3][1]+" "+this.matrix[3][2]+" "+this.matrix[3][3]+" |"},toTransposedFloatArray:function(e){if(e===undefined)e=[];for(var g=0,h=0;g<4;g++)for(var i=0;i<4;i++)e[h++]=
this.matrix[i][g];return e},translate:function(e,g,h){return(new toxi.Matrix4x4(this)).translateSelf(e,g,h)},translateSelf:function(e,g,h){if(e instanceof Object){g=e.y;h=e.z;e=e.x}d.identity();d.matrix[0][3]=e;d.matrix[1][3]=g;d.matrix[2][3]=h;return this.multiplySelf(d)},transpose:function(){return this.set(this.matrix[0][0],this.matrix[1][0],this.matrix[2][0],this.matrix[3][0],this.matrix[0][1],this.matrix[1][1],this.matrix[2][1],this.matrix[3][1],this.matrix[0][2],this.matrix[1][2],this.matrix[2][2],
this.matrix[3][2],this.matrix[0][3],this.matrix[1][3],this.matrix[2][3],this.matrix[3][3])}};var d=new toxi.Matrix4x4})();toxi.Quaternion=function(d,e,g,h){if(arguments.length==4){this.w=d;this.x=e;this.y=g;this.z=h}else if(arguments.length==2){this.x=e.x;this.y=e.y;this.z=e.z;this.w=d}else if(arguments.length==1){this.w=q.w;this.x=q.x;this.y=q.y;this.z=q.z}};
toxi.Quaternion.prototype={add:function(d){return new toxi.Quaternion(this.x+d.x,this.y+d.y,this.z+d.z,this.w+d.w)},addSelf:function(d){this.x+=d.x;this.y+=d.y;this.z+=d.z;return this},copy:function(){return new Quaternion(this.w,this.x,this.y,this.z)},dot:function(d){return this.x*d.x+this.y*d.y+this.z*d.z+this.w*d.w},getConjugate:function(){var d=new toxi.Quaternion;d.x=-this.x;d.y=-this.y;d.z=-this.z;d.w=w;return d},identity:function(){this.w=1;this.z=this.y=this.x=0;return this},interpolateTo:function(d,
e,g){return arguments.length==3?this.copy().interpolateTo(d,g.interpolate(0,1,e)):this.copy().interpolateToSelf(d,e)},interpolateToSelf:function(d,e,g){if(arguments.length==3)e=g.interpolate(0,1,e);var h,i,m=toxi.MathUtils.clip(this.dot(d),-1,1);if(1-m>=toxi.MathUtils.EPS){i=Math.acos(m);var n=1/Math.sin(i);h=Math.sin(i*(1-e))*n;i=Math.sin(i*e)*n}else{h=1-e;i=e}if(m<0){this.w=h*this.w-i*d.w;this.x=h*this.x-i*d.x;this.y=h*this.y-i*d.y;this.z=h*this.z-i*d.z}else{this.w=h*w+i*d.w;this.x=h*x+i*d.x;this.y=
h*y+i*d.y;this.z=h*z+i*d.z}return this},magnitude:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w)},multiply:function(d){var e=new toxi.Quaternion;e.w=this.w*d.w-x*d.x-y*d.y-z*d.z;e.x=this.w*d.x+x*d.w+y*d.z-z*d.y;e.y=this.w*d.y+y*d.w+z*d.x-x*d.z;e.z=this.w*d.z+z*d.w+x*d.y-y*d.x;return e},normalize:function(){var d=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);if(d>toxi.MathUtils.EPS){d=1/d;this.x*=d;this.y*=d;this.z*=d;this.w*=d}return this},
scale:function(d){return new toxi.Quaternion(this.x*d,this.y*d,this.z*d,this.w*d)},scaleSelf:function(d){this.x*=d;this.y*=d;this.z*=d;this.w*=d;return this},set:function(d,e,g,h){if(arguments.length==4){this.w=d;this.x=e;this.y=g;this.z=h}else if(arguments.length==2){this.w=d;this.x=v.x;this.y=v.y;this.z=v.z}else{this.w=q.w;this.x=q.x;this.y=q.y;this.z=q.z}return this},sub:function(d){return new toxi.Quaternion(this.x-d.x,this.y-d.y,this.z-d.z,this.w-d.w)},subSelf:function(d){this.x-=d.x;this.y-=
d.y;this.z-=d.z;this.w-=d.w;return this},toArray:function(){return[this.w,this.x,this.y,this.z]},toAxisAngle:function(){var d=[],e=Math.sqrt(1-this.w*this.w);e=e<toxi.MathUtils.EPS?1:1/e;d[0]=Math.acos(this.w)*2;d[1]=this.x*e;d[2]=this.y*e;d[3]=this.z*e;return d},toMatrix4x4:function(d){if(d===undefined)d=new toxi.Matrix4x4;var e=this.x+this.x,g=this.y+this.y,h=this.z+this.z,i=this.x*e,m=this.x*g,n=this.x*h,o=this.y*g,p=this.y*h,r=this.z*h;e=this.w*e;g=this.w*g;h=this.w*h;return d.set(1-(o+r),m-h,
n+g,0,m+h,1-(i+r),p-e,0,n-g,p+e,1-(i+o),0,0,0,0,1)},toString:function(){return"{axis: ["+this.x+","+this.y+","+this.z+"], w: "+this.w+"}"}};toxi.Quaternion.DOT_THRESHOLD=0.9995;toxi.Quaternion.createFromAxisAngle=function(d,e){e*=0.5;var g=toxi.MathUtils.sin(e),h=toxi.MathUtils.cos(e);return new toxi.Quaternion(h,d.getNormalizedTo(g))};
toxi.Quaternion.createFromEuler=function(d,e,g){d*=0.5;e*=0.5;g*=0.5;var h=toxi.MathUtils.sin(d);d=toxi.MathUtils.cos(d);var i=toxi.MathUtils.sin(e);e=toxi.MathUtils.cos(e);var m=toxi.MathUtils.sin(g);g=toxi.MathUtils.cos(g);var n=d*e,o=h*i,p=new toxi.Quaternion;p.x=m*n-g*o;p.y=g*h*e+m*d*i;p.z=g*d*i-m*h*e;p.w=g*n+m*o;return p};
toxi.Quaternion.createFromMatrix=function(d){var e=0,g=[],h=d.matrix[0][0]+d.matrix[1][1]+d.matrix[2][2];if(h>0){e=Math.sqrt(h+1);g[3]=e*0.5;e=0.5/e;g[0]=d.matrix[1][2]-d.matrix[2][1]*e;g[1]=d.matrix[2][0]-d.matrix[0][2]*e;g[2]=d.matrix[0][1]-d.matrix[1][0]*e}else{e=[1,2,0];var i=h=0,m=0;if(d.matrix[1][1]>d.matrix[0][0])h=1;if(d.matrix[2][2]>d.matrix[h][h])h=2;i=e[h];m=e[i];e=Math.sqrt(d.matrix[h][h]-(d.matrix[i][i]+d.matrix[m][m])+1);g[h]=e*0.5;e=0.5/e;g[3]=(d.matrix[i][m]-d.matrix[m][i])*e;g[i]=
(d.matrix[h][i]+d.matrix[i][h])*e;g[m]=(d.matrix[h][m]+d.matrix[m][h])*e}return new toxi.Quaternion(g[3],g[0],g[1],g[2])};toxi.Quaternion.getAlignmentQuat=function(d,e){var g=d.getNormalized(),h=e.cross(g),i=h.magnitude()+1.0E-4;g=Math.atan2(i,e.dot(g));return this.createFromAxisAngle(h,g)};toxi.Vertex=function(d,e){toxi.Vec3D.apply(this,[d]);this.id=e;this.normal=new toxi.Vec3D};toxi.extend(toxi.Vertex,toxi.Vec3D);toxi.Vertex.prototype.addFaceNormal=function(d){this.normal.addSelf(d)};
toxi.Vertex.prototype.clearNormal=function(){this.normal.clear()};toxi.Vertex.prototype.computeNormal=function(){this.normal.normalize()};toxi.Vertex.prototype.toString=function(){return this.id+": p: "+this.parent.toString.call(this)+" n:"+this.normal.toString()};
toxi.Face=function(d,e,g,h,i,m){this.a=d;this.b=e;this.c=g;this.normal=this.a.sub(this.c).crossSelf(this.a.sub(this.b)).normalize();this.a.addFaceNormal(this.normal);this.b.addFaceNormal(this.normal);this.c.addFaceNormal(this.normal);if(h!==undefined){this.uvA=h;this.uvB=i;this.uvC=m}};
toxi.Face.prototype={computeNormal:function(){this.normal=this.a.sub(this.c).crossSelf(this.a.sub(this.b)).normalize()},flipVertexOrder:function(){var d=this.a;this.a=this.b;this.b=d;this.normal.invert()},getCentroid:function(){return this.a.add(this.b).addSelf(this.c).scale(1/3)},getClass:function(){return"toxi.Face"},getVertices:function(d){if(d!==undefined){d[0]=this.a;d[1]=this.b;d[2]=this.c}else d=[this.a,this.b,this.c];return d},toString:function(){return this.getClass()+" "+this.a+", "+this.b+
", "+this.c},toTriangle:function(){return new toxi.Triangle3D(this.a.copy(),this.b.copy(),this.c.copy())}};toxi.Mesh3D=function(){};toxi.TriangleMesh=function(d){if(d===undefined)d="untitled";this.setName(d);this.matrix=new toxi.Matrix4x4;this.vertices=[];this.__verticesObject={};this.faces=[];this.numFaces=this.numVertices=0;this.uniqueVertexID=-1;return this};toxi.TriangleMesh.DEFAULT_NUM_VERTICES=1E3;toxi.TriangleMesh.DEFAULT_NUM_FACES=3E3;toxi.TriangleMesh.DEFAULT_STRIDE=4;
toxi.TriangleMesh.prototype={addFace:function(d,e,g,h,i,m,n){if(n===undefined)if(m===undefined){if(i===undefined)h=undefined;n=m=i=undefined}else{n=m;m=i;i=h}d=this.__checkVertex(d);e=this.__checkVertex(e);g=this.__checkVertex(g);if(!(d.id===e.id||d.id===g.id||e.id===g.id)){if(h!==undefined){var o=d.sub(g).crossSelf(d.sub(e));if(h.dot(o)<0){h=d;d=e;e=h}}this.faces.push(new toxi.Face(d,e,g,i,m,n));this.numFaces++}return this},addMesh:function(d){for(var e=d.getFaces().length,g=0;g<e;g++){var h=d.getFaces()[g];
this.addFace(h.a,h.b,h.c)}return this},center:function(d){this.computeCentroid();d=d!==undefined?d.sub(this.centroid):this.centroid.getInverted();for(var e=this.vertices.length,g=0;g<e;g++)this.vertices[g].addSelf(d);this.getBoundingBox();return this.bounds},__checkVertex:function(d){var e=d.toString(),g=this.__verticesObject[e];if(g===undefined){g=this.createVertex(d,this.uniqueVertexID++);this.__verticesObject[e]=g;this.vertices.push(g);this.numVertices++}else window.numDupes++;return g},clear:function(){this.vertices=
[];this.faces=[];this.bounds=undefined;this.numFaces=this.numVertices=0;return this},computeCentroid:function(){this.centroid.clear();for(var d=this.vertices.length,e=0;e<d;e++)this.centroid.addSelf(this.vertices[e]);return this.centroid.scaleSelf(1/this.numVertices).copy()},computeFaceNormals:function(){for(var d=this.faces.length,e=0;e<d;e++)this.faces[e].computeNormal()},computeVertexNormals:function(){var d=this.vertices.length,e=0;for(e=0;e<d;e++)this.vertices[e].clearNormal();d=this.faces.length;
for(e=0;e<d;e++){var g=this.faces[e];g.a.addFaceNormal(g);g.b.addFaceNormal(g);g.c.addFaceNormal(g)}d=this.vertices.length;for(e=0;e<d;e++)this.vertices[e].computeNormal();return this},copy:function(){for(var d=new toxi.TriangleMesh(this.name+"-copy",this.numVertices,this.numFaces),e=this.faces.length,g=0;g<e;g++){var h=this.faces[g];d.addFace(h.a,h.b,h.c,h.normal,h.uvA,h.uvB,h.uvC)}return d},createVertex:function(d,e){return new toxi.Vertex(d,e)},faceOutwards:function(){this.computeCentroid();for(var d=
this.faces.length,e=0;e<d;e++){var g=this.faces[e];g.getCentroid().sub(this.centroid).dot(g.normal)<0&&g.flipVertexOrder()}return this},flipVertexOrder:function(){for(var d=this.faces.length,e=0;e<d;e++){var g=this.faces[e],h=g.a;g.a=g.b;g.b=h;g.normal.invert()}return this},flipYAxis:function(){this.transform((new Matrix4x4).scaleSelf(1,-1,1));this.flipVertexOrder();return this},getBoundingBox:function(){for(var d=toxi.Vec3D.MAX_VALUE.copy(),e=toxi.Vec3D.MIN_VALUE.copy(),g=this.vertices.length,h=
0;h<g;h++){this.minBounds.minSelf(v);this.maxBounds.maxSelf(v)}return this.bounds=toxi.AABB.fromMinMax(d,e)},getBoundingSphere:function(){var d=0;this.computeCentroid();for(var e=this.vertices.length,g=0;g<e;g++)d=toxi.MathUtils.max(d,this.vertices[g].distanceToSquared(this.centroid));return new toxi.Sphere(this.centroid,Math.sqrt(d))},getClosestVertexToPoint:function(d){for(var e,g=Number.MAX_VALUE,h=this.vertices.length,i=0;i<h;i++){var m=this.vertices[i],n=m.distanceToSquared(d);if(n<g){e=m;g=
n}}return e},getFaceNormalsAsArray:function(d,e,g){if(arguments.length===0){d=undefined;e=0;g=toxi.TriangleMesh.DEFAULT_STRIDE}else if(arguments.length==1&&arguments[0]instanceof Object){var h=arguments[0];d=h.normals;e=h.offset;g=h.stride}g=toxi.MathUtils.max(g,3);if(d===undefined)d=[];h=e;for(var i=this.faces.length,m=0;m<i;m++){var n=this.faces[m];d[h]=n.normal.x;d[h+1]=n.normal.y;d[h+2]=n.normal.z;h+=g;d[h]=n.normal.x;d[h+1]=n.normal.y;d[h+2]=n.normal.z;h+=g;d[h]=n.normal.x;d[h+1]=n.normal.y;
d[h+2]=n.normal.z;h+=g}return d},getFaces:function(){return this.faces},getFacesAsArray:function(){for(var d=[],e=0,g=this.faces.length,h=0;h<g;h++){var i=this.faces[h];d[e++]=i.a.id;d[e++]=i.b.id;d[e++]=i.c.id}return d},getIntersectionData:function(){return this.intersector.getIntersectionData()},getMeshAsVertexArray:function(d,e,g){if(d===undefined)d=undefined;if(g===undefined)g=toxi.TriangleMesh.DEFAULT_STRIDE;g=toxi.MathUtils.max(g,3);if(d===undefined)d=[];e=0;for(var h=this.faces.length,i=0;i<
h;++i){var m=this.faces[i];d[e]=m.a.x;d[e+1]=m.a.y;d[e+2]=m.a.z;e+=g;d[e]=m.b.x;d[e+1]=m.b.y;d[e+2]=m.b.z;e+=g;d[e]=m.c.x;d[e+1]=m.c.y;d[e+2]=m.c.z;e+=g}return d},getNumFaces:function(){return this.numFaces},getNumVertices:function(){return this.numVertices},getRotatedAroundAxis:function(d,e){return this.copy().rotateAroundAxis(d,e)},getRotatedX:function(d){return this.copy().rotateX(d)},getRotatedY:function(d){return this.copy().rotateY(d)},getRotatedZ:function(d){return this.copy().rotateZ(d)},
getScaled:function(d){return this.copy().scale(d)},getTranslated:function(d){return this.copy().translate(d)},getUniqueVerticesAsArray:function(){for(var d=[],e=0,g=this.vertices.length,h=0;e<g;h++){var i=this.vertices[h];d[e++]=i.x;d[e++]=i.y;d[e++]=i.z}return d},getVertexAtPoint:function(d){for(var e,g=0;g<this.vertices.length;g++)if(this.vertices[g].equals(d))e=g;return this.vertices[e]},getVertexIndex:function(d){for(var e=this.vertices.length,g=0;g<e;g++)if(this.vertices[g].equals(d))matchedVertex=
g;return matchedVertex},getVertexForID:function(d){for(var e,g=this.vertices.length,h=0;h<g;h++){var i=this.vertices[h];if(i.id==d){e=i;break}}return e},getVertexNormalsAsArray:function(d,e,g){if(e===undefined)e=0;if(g===undefined)g=toxi.TriangleMesh.DEFAULT_STRIDE;g=toxi.MathUtils.max(g,3);if(d===undefined)d=[];e=e;for(var h=this.faces.length,i=0;i<h;i++){var m=this.faces[i];d[e]=m.a.normal.x;d[e+1]=m.a.normal.y;d[e+2]=m.a.normal.z;e+=g;d[e]=m.b.normal.x;d[e+1]=m.b.normal.y;d[e+2]=m.b.normal.z;e+=
g;d[e]=m.c.normal.x;d[e+1]=m.c.normal.y;d[e+2]=m.c.normal.z;e+=g}return d},getVertices:function(){return this.vertices},handleSaveAsSTL:function(){console.log("toxi.TriangleMesh.handleSaveAsSTL() currently not implemented")},intersectsRay:function(d){for(var e=this.intersector.getTriangle(),g=this.faces.length,h=0;h<g;h++){e.a=f.a;e.b=f.b;e.c=f.c;if(this.intersector.intersectsRay(d))return true}return false},perforateFace:function(d,e){var g=d.getCentroid(),h=1-e,i=d.a.interpolateTo(g,h),m=d.b.interpolateTo(g,
h);g=d.c.interpolateTo(g,h);this.removeFace(d);this.addFace(d.a,m,i);this.addFace(d.a,d.b,m);this.addFace(d.b,g,m);this.addFace(d.b,d.c,g);this.addFace(d.c,i,g);this.addFace(d.c,d.a,i);return new toxi.Triangle3D(i,m,g)},pointTowards:function(d){return this.transform(toxi.Quaternion.getAlignmentQuat(d,toxi.Vec3D.Z_AXIS).toMatrix4x4(),true)},removeFace:function(d){for(var e=-1,g=this.faces.length,h=0;h<g;h++)if(this.faces[h]==d){e=h;break}e>-1&&this.faces.splice(e,1)},rotateAroundAxis:function(d,e){return this.transform(this.matrix.identity().rotateAroundAxis(d,
e))},rotateX:function(d){return this.transform(this.matrix.identity().rotateX(d))},rotateY:function(d){return this.transform(this.matrix.identity().rotateY(d))},rotateZ:function(d){return this.transform(this.matrix.identity().rotateZ(d))},saveAsOBJ:function(){console.log("toxi.TriangleMesh.saveAsOBJ() currently not implemented")},saveAsSTL:function(){console.log("toxi.TriangleMesh.saveAsSTL() currently not implemented")},scale:function(d){return this.transform(this.matrix.identity().scaleSelf(d))},
setName:function(d){this.name=d;return this},toString:function(){return"toxi.TriangleMesh: "+this.name+" vertices: "+this.getNumVertices()+" faces: "+this.getNumFaces()},toWEMesh:function(){console.log("toxi.TriangleMesh.toWEMesh() currently not implemented")},transform:function(d,e){if(e===undefined)e=true;for(var g=this.vertices.length,h=0;h<g;h++){var i=this.vertices[h];i.set(d.applyTo(i))}e&&this.computeFaceNormals();return this},updateVertex:function(d,e){var g=this.getVertexIndex(d);if(g>-1){this.vertices.splice(v,
1);this.vertices[g].set(e);this.vertices.push(v)}return this}};toxi.Sphere=function(d,e){if(d===undefined){toxi.Vec3D.apply(this,[new toxi.Vec3D]);this.radius=1}else if(d instanceof toxi.Vec3D){toxi.Vec3D.apply(this,[d]);this.radius=d instanceof toxi.Sphere?d.radius:e}else{toxi.Vec3D.apply(this,[new toxi.Vec3D]);this.radius=d}};toxi.extend(toxi.Sphere,toxi.Vec3D);toxi.Sphere.prototype.containsPoint=function(d){return this.sub(d).magSquared()<=this.radius*this.radius};
toxi.Sphere.prototype.intersectRay=function(d){var e,g,h=d.sub(this);g=h.magSquared();d=-h.dot(d.getDirection());g=this.radius*this.radius-(g-d*d);if(g>=0){g=Math.sqrt(g);e=d+g;g=d-g;if(!(e<0&&g<0))if(e>0&&g>0){if(e>g){d=e;e=g;g=d}}else if(g>0){d=e;e=g;g=d}e=[e,g]}return e};toxi.Sphere.prototype.intersectSphereTriangle=function(d,e){e.set(d.closestPointOnSurface(this));return e.sub(this).magSquared()<=this.radius*this.radius};toxi.Sphere.prototype.tangentPlaneNormalAt=function(d){return this.sub(d).normalize()};
toxi.Sphere.prototype.toMesh=function(){var d={mesh:undefined,resolution:0};if(arguments.length===1)if(arguments[0]instanceof Object){d.mesh=arguments[0].mesh;d.resolution=arguments[0].res||arguments[0].resolution}else d.resolution=arguments[0];else{d.mesh=arguments[0];d.resolution=arguments[1]}return(new toxi.SurfaceMeshBuilder(new toxi.SphereFunction(this))).createMesh(d.mesh,d.resolution,1)};toxi.VertexSelector=function(d){this.mesh=d;this.selection=[]};
toxi.VertexSelector.prototype={addSelection:function(d){this.checkMeshIdentity(d.getMesh());this.selection.addAll(d.getSelection());return this},checkMeshIdentity:function(d){if(d!=this.mesh)throw Error("The given selector is not using the same mesh instance");},clearSelection:function(){this.selection.clear();return this},getMesh:function(){return this.mesh},getSelection:function(){return this.selection},invertSelection:function(){for(var d=[],e=mesh.getVertices(),g=e.length,h=0;h<g;h++){var i=e[h];
selection.contains(i)||d.add(i)}this.selection=d;return this},selectSimilar:function(d){for(var e=d.length,g=0;g<e;g++)this.selection.add(this.mesh.getClosestVertexToPoint(d[g]));return this},selectVertices:function(){return this},setMesh:function(d){this.mesh=d;this.clearSelection()},size:function(){return this.selection.size()},subtractSelection:function(d){this.checkMeshIdentity(d.getMesh());this.selection.removeAll(d.getSelection());return this}};
toxi.BoxSelector=function(d,e){toxi.VertexSelector.apply(this,[d]);this.box=e};toxi.extend(toxi.BoxSelector,toxi.VertexSelector);toxi.BoxSelector.prototype.selectVertices=function(){this.clearSelection();for(var d=this.mesh.getVertices(),e=d.length,g=0;g<e;g++){var h=d[g];this.box.containsPoint(h)&&this.selection.add(h)}return this};toxi.DefaultSelector=function(d){toxi.VertexSelector.apply(this,[d])};toxi.extend(toxi.DefaultSelector,toxi.VertexSelector);
toxi.DefaultSelector.prototype.selectVertices=function(){this.clearSelection();this.selection.addAll(this.mesh.getVertices());return this};toxi.PlaneSelector=function(d,e,g,h){toxi.VertexSelector.apply(this,[d]);this.plane=e;this.classifier=g;this.tolerances=h===undefined?1.0E-4:h};toxi.extend(toxi.PlaneSelector,toxi.VertexSelector);
toxi.PlaneSelector.prototype.selectVertices=function(){this.clearSelection();for(var d=this.mesh.getVertices(),e=d.length,g=0;g<e;g++){var h=d[g];this.plane.classifyPoint(h,this.tolerance)==this.classifier&&this.selection.add(h)}return this};toxi.SphereFunction=function(d){if(d===undefined)this.sphere=new toxi.Sphere(new toxi.Vec3D,1);this.sphere=d instanceof toxi.Sphere?d:new toxi.Sphere(new toxi.Vec3D,d);this.phiRange=toxi.MathUtils.PI;this.thetaRange=toxi.MathUtils.TWO_PI};
toxi.SphereFunction.prototype={computeVertexFor:function(d,e,g){e-=toxi.MathUtils.HALF_PI;var h=toxi.MathUtils.cos(e),i=toxi.MathUtils.cos(g);e=toxi.MathUtils.sin(e);g=toxi.MathUtils.sin(g);h=toxi.MathUtils.sign(h)*toxi.MathUtils.abs(h);d.x=h*toxi.MathUtils.sign(i)*toxi.MathUtils.abs(i);d.y=toxi.MathUtils.sign(e)*toxi.MathUtils.abs(e);d.z=h*toxi.MathUtils.sign(g)*toxi.MathUtils.abs(g);return d.scaleSelf(this.sphere.radius).addSelf(this.sphere)},getPhiRange:function(){return this.phiRange},getPhiResolutionLimit:function(d){return d},
getThetaRange:function(){return this.thetaRange},getThetaResolutionLimit:function(d){return d},setMaxPhi:function(d){this.phiRange=toxi.MathUtils.min(d/2,toxi.MathUtils.PI)},setMaxTheta:function(d){this.thetaRange=toxi.MathUtils.min(d,toxi.MathUtils.TWO_PI)}};toxi.SphericalHarmonics=function(d){this.m=d};
toxi.SphericalHarmonics.prototype={computeVertexFor:function(d,e,g){var h=0;h+=Math.pow(toxi.MathUtils.sin(this.m[0]*g),this.m[1]);h+=Math.pow(toxi.MathUtils.cos(this.m[2]*g),this.m[3]);h+=Math.pow(toxi.MathUtils.sin(this.m[4]*e),this.m[5]);h+=Math.pow(toxi.MathUtils.cos(this.m[6]*e),this.m[7]);var i=toxi.MathUtils.sin(g);d.x=h*i*toxi.MathUtils.cos(e);d.y=h*toxi.MathUtils.cos(g);d.z=h*i*toxi.MathUtils.sin(e);return d},getPhiRange:function(){return toxi.MathUtils.TWO_PI},getPhiResolutionLimit:function(d){return d},
getThetaRange:function(){return toxi.MathUtils.PI},getThetaResolutionLimit:function(d){return d}};toxi.SuperEllipsoid=function(d,e){this.p1=d;this.p2=e};
toxi.SuperEllipsoid.prototype={computeVertexFor:function(d,e,g){e-=toxi.MathUtils.HALF_PI;var h=toxi.MathUtils.cos(e),i=toxi.MathUtils.cos(g);e=toxi.MathUtils.sin(e);g=toxi.MathUtils.sin(g);h=toxi.MathUtils.sign(h)*Math.pow(toxi.MathUtils.abs(h),this.p1);d.x=h*toxi.MathUtils.sign(i)*Math.pow(Math.abs(i),this.p2);d.y=toxi.MathUtils.sign(e)*Math.pow(Math.abs(e),this.p1);d.z=h*toxi.MathUtils.sign(g)*Math.pow(toxi.MathUtils.abs(g),this.p2);return d},getPhiRange:function(){return toxi.MathUtils.TWO_PI},
getPhiResolutionLimit:function(d){return d/2},getThetaRange:function(){return toxi.MathUtils.TWO_PI},getThetaResolutionLimit:function(d){return d}};toxi.SurfaceMeshBuilder=function(d){this.func=d};
toxi.SurfaceMeshBuilder.prototype={createMesh:function(){var d={mesh:undefined,resolution:0,size:1,isClosed:true};if(arguments.length===1)if(arguments[0]instanceof Object){var e=arguments[0];d.mesh=e.mesh;d.resolution=e.res||e.resoultion;if(e.size!==undefined)d.size=e.size;if(e.isClosed!==undefined)d.isClosed=e.isClosed}else d.resolution=arguments[0];else if(arguments.length>2){d.mesh=arguments[0];d.resolution=arguments[1];d.size=arguments[2];if(arguments.length==4)d.isClosed=arguments[3]}e=d.mesh;
if(e===undefined||e===null)e=new toxi.TriangleMesh;for(var g=new toxi.Vec3D,h=new toxi.Vec3D,i=new toxi.Vec3D,m=new toxi.Vec3D,n=new toxi.Vec3D,o=new toxi.Vec3D,p=this.func.getPhiResolutionLimit(d.resolution),r=this.func.getPhiRange(),t=this.func.getThetaResolutionLimit(d.resolution),u=this.func.getThetaRange(),B=1/(1==d.resolution%2?d.resolution-0:d.resolution),C=0;C<p;C++)for(var E=C*r*B,F=(C+1)*r*B,D=0;D<=t;D++){var A=D*u/d.resolution,G=this.func;g=G.computeVertexFor(g,F,A).scaleSelf(d.size);h=
G.computeVertexFor(h,E,A).scaleSelf(d.size);h.distanceTo(g)<1.0E-4&&h.set(g);if(D>0){if(D==t&&d.isClosed){g.set(n);h.set(o)}e.addFace(i,m,g);e.addFace(m,h,g)}else{n.set(g);o.set(h)}i.set(g);m.set(h)}return e},getFunction:function(){return this.func},setFunction:function(d){this.func=d}};toxi.AxisAlignedCylinder=function(d,e,g){this.pos=d===undefined?undefined:d.copy();this.setRadius(e);this.setLength(g)};
toxi.AxisAlignedCylinder.prototype={containsPoint:function(){throw Error("AxisAlignedCylinder.containsPoint(): not implmented");},getLength:function(){return this.length},getMajorAxis:function(){throw Error("AxisAlignedCylinder.getMajorAxis(): not implemented");},getPosition:function(){return this.pos.copy()},getRadius:function(){return this.radius},setLength:function(d){this.length=d},setPosition:function(d){this.pos.set(d)},setRadius:function(d){this.radius=d;this.radiusSquared=d*d},toMesh:function(){var d=
{mesh:undefined,steps:12,thetaOffset:0};if(arguments.length==1&&typeof arguments[0]=="object")for(var e in arguments[0])d[e]=arguments[0][e];else if(arguments.length==2){d.steps=arguments[0];d.thetaOffset=arguments[1]}return(new toxi.Cone(this.pos,this.getMajorAxis().getVector(),this.radius,this.radius,this.length)).toMesh(d.mesh,d.steps,d.thetaOffset,true,true)}};
toxi.AABB=function(d,e){if(d===undefined){toxi.Vec3D.apply(this);this.setExtent(new toxi.Vec3D)}else if(typeof d=="number"){toxi.Vec3D.apply(this,[new toxi.Vec3D]);this.setExtent(d)}else if(d instanceof toxi.Vec3D){toxi.Vec3D.apply(this,[d]);if(e===undefined&&d instanceof toxi.AABB)this.setExtent(d.getExtent());else typeof e=="number"?this.setExtent(new toxi.Vec3D(e,e,e)):this.setExtent(e)}};toxi.extend(toxi.AABB,toxi.Vec3D);
toxi.AABB.fromMinMax=function(d,e){var g=toxi.Vec3D.min(d),h=toxi.Vec3D.max(e);return new toxi.AABB(g.interpolateTo(h,0.5),h.sub(g).scaleSelf(0.5))};toxi.AABB.prototype.containsPoint=function(d){return d.isInAABB(this)};toxi.AABB.prototype.copy=function(){return new toxi.AABB(this)};toxi.AABB.prototype.getExtent=function(){return this.extent.copy()};toxi.AABB.prototype.getMax=function(){return this.max.copy()};toxi.AABB.prototype.getMin=function(){return this.min.copy()};
toxi.AABB.prototype.getNormalForPoint=function(d){d=d.sub(this);var e=this.extent.sub(d.getAbs());d=d.getSignum();var g=toxi.Vec3D.X_AXIS.scale(d.x),h=e.x;if(e.y<h){h=e.y;g=toxi.Vec3D.Y_AXIS.scale(d.y)}if(e.z<h)g=toxi.Vec3D.Z_AXIS.scale(d.z);return g};toxi.AABB.prototype.includePoint=function(d){this.min.minSelf(d);this.max.maxSelf(d);this.set(this.min.interpolateTo(this.max,0.5));this.extent.set(this.max.sub(this.min).scaleSelf(0.5));return this};
toxi.AABB.prototype.intersectsBox=function(d){var e=d.sub(this);return Math.abs(e.x)<=this.extent.x+d.extent.x&&Math.abs(e.y)<=this.extent.y+d.extent.y&&Math.abs(e.z)<=this.extent.z+d.extent.z};
toxi.AABB.prototype.intersectsRay=function(d,e,g){var h=d.getDirection().reciprocal(),i=h.x<0,m=h.y<0,n=h.z<0,o=i?this.max:this.min,p=(o.x-d.x)*h.x;o=i?this.min:this.max;i=(o.x-d.x)*h.x;o=m?this.max:this.min;var r=(o.y-d.y)*h.y;o=m?this.min:this.max;o=(o.y-d.y)*h.y;if(p>o||r>i)return null;if(r>p)p=r;if(o<i)i=o;o=n?max:min;m=(o.z-d.z)*h.z;o=n?min:max;h=(o.z-d.z)*h.z;if(p>h||m>i)return null;if(m>p)p=m;if(h<i)i=h;if(p<g&&i>e)return d.getPointAtDistance(p)};
toxi.AABB.prototype.intersectsSphere=function(d,e){if(arguments.length==1)e=d.radius;var g,h=0;if(d.x<this.min.x){g=d.x-this.min.x;h=g*g}else if(d.x>this.max.x){g=d.x-this.max.x;h+=g*g}if(d.y<this.min.y){g=d.y-this.min.y;h+=g*g}else if(d.y>this.max.y){g=d.y-this.max.y;h+=g*g}if(d.z<this.min.z){g=d.z-this.min.z;h+=g*g}else if(d.z>this.max.z){g=d.z-this.max.z;h+=g*g}return h<=e*e};
toxi.AABB.prototype.intersectsTriangle=function(d){var e,g,h,i,m,n;e=d.a.sub(this);g=d.b.sub(this);d=d.c.sub(this);h=g.sub(e);i=d.sub(g);m=e.sub(d);n=h.getAbs();if(this.testAxis(h.z,-h.y,n.z,n.y,e.y,e.z,d.y,d.z,this.extent.y,this.extent.z))return false;if(this.testAxis(-h.z,h.x,n.z,n.x,e.x,e.z,d.x,d.z,this.extent.x,this.extent.z))return false;if(this.testAxis(h.y,-h.x,n.y,n.x,g.x,g.y,d.x,d.y,this.extent.x,this.extent.y))return false;n=i.getAbs();if(this.testAxis(i.z,-i.y,n.z,n.y,e.y,e.z,d.y,d.z,this.extent.y,
this.extent.z))return false;if(this.testAxis(-i.z,i.x,n.z,n.x,e.x,e.z,d.x,d.z,this.extent.x,this.extent.z))return false;if(this.testAxis(i.y,-i.x,n.y,n.x,e.x,e.y,g.x,g.y,this.extent.x,this.extent.y))return false;n=m.getAbs();if(this.testAxis(m.z,-m.y,n.z,n.y,e.y,e.z,g.y,g.z,this.extent.y,this.extent.z))return false;if(this.testAxis(-m.z,m.x,n.z,n.x,e.x,e.z,g.x,g.z,this.extent.x,this.extent.z))return false;if(this.testAxis(m.y,-m.x,n.y,n.x,g.x,g.y,d.x,d.y,this.extent.x,this.extent.y))return false;
if(toxi.MathUtils.min(e.x,g.x,d.x)>this.extent.x||toxi.MathUtils.max(e.x,g.x,d.x)<-this.extent.x)return false;if(toxi.MathUtils.min(e.y,g.y,d.y)>this.extent.y||toxi.MathUtils.max(e.y,g.y,d.y)<-this.extent.y)return false;if(toxi.MathUtils.min(e.z,g.z,d.z)>this.extent.z||toxi.MathUtils.max(e.z,g.z,d.z)<-this.extent.z)return false;g=h.cross(i);e=-g.dot(e);if(!this.planeBoxOverlap(g,e,extent))return false;return true};
toxi.AABB.prototype.planeBoxOverlap=function(d,e,g){var h=new toxi.Vec3D,i=new toxi.Vec3D;if(d.x>0){h.x=-g.x;i.x=g.x}else{h.x=g.x;i.x=-g.x}if(d.y>0){h.y=-g.y;i.y=g.y}else{h.y=g.y;i.y=-g.y}if(d.z>0){h.z=-g.z;i.z=g.z}else{h.z=g.z;i.z=-g.z}if(d.dot(h)+e>0)return false;if(d.dot(i)+e>=0)return true;return false};
toxi.AABB.prototype.set=function(d,e,g){if(d instanceof toxi.AABB){this.extent.set(d.extent);return toxi.Vec3D.set.apply(this,[d])}if(d instanceof toxi.Vec3D){e=d.y;g=d.z;d=d.a}this.x=d;this.y=e;this.z=g;this.updateBounds();return this};toxi.AABB.prototype.setExtent=function(d){this.extent=d.copy();return this.updateBounds()};toxi.AABB.prototype.testAxis=function(d,e,g,h,i,m,n,o,p,r){i=d*i+e*m;e=d*n+e*o;if(i<e){d=i;e=e}else{d=e;e=i}g=g*p+h*r;return d>g||e<-g};
toxi.AABB.prototype.toMesh=function(d){if(d===undefined)d=new toxi.TriangleMesh("aabb",8,12);var e=new toxi.Vec3D(this.min.x,this.max.y,this.max.z),g=new toxi.Vec3D(this.max.x,this.max.y,this.max.z),h=new toxi.Vec3D(this.max.x,this.min.y,this.max.z),i=new toxi.Vec3D(this.min.x,this.min.y,this.max.z),m=new toxi.Vec3D(this.min.x,this.max.y,this.min.z),n=new toxi.Vec3D(this.max.x,this.max.y,this.min.z),o=new toxi.Vec3D(this.max.x,this.min.y,this.min.z),p=new toxi.Vec3D(this.min.x,this.min.y,this.min.z);
d.addFace(e,g,i,undefined,undefined,undefined,undefined);d.addFace(g,h,i,undefined,undefined,undefined,undefined);d.addFace(n,m,o,undefined,undefined,undefined,undefined);d.addFace(m,p,o,undefined,undefined,undefined,undefined);d.addFace(m,n,e,undefined,undefined,undefined,undefined);d.addFace(n,g,e,undefined,undefined,undefined,undefined);d.addFace(o,p,i,undefined,undefined,undefined,undefined);d.addFace(o,i,h,undefined,undefined,undefined,undefined);d.addFace(m,e,p,undefined,undefined,undefined,
undefined);d.addFace(e,i,p,undefined,undefined,undefined,undefined);d.addFace(g,n,o,undefined,undefined,undefined,undefined);d.addFace(g,o,h,undefined,undefined,undefined,undefined);return d};toxi.AABB.prototype.toString=function(){return"<aabb> pos: "+this.parent.toString()+" ext: "+this.extent.toString()};toxi.AABB.prototype.updateBounds=function(){if(this.extent!==undefined){this.min=this.sub(this.extent);this.max=this.add(this.extent)}return this};
toxi.BezierPatch=function(d){this.points=d===undefined?[]:d;for(d=0;d<4;d++)for(var e=0;e<4;e++)this.points[d][e]=new toxi.Vec3D};
toxi.BezierPatch.prototype={computePointAt:function(d,e){return this.computePointAt(d,e,this.points)},set:function(d,e,g){this.points[e][d].set(g);return this},toMesh:function(d,e){var g,h;if(e===undefined){g=new toxi.TriangleMesh;h=d}else{g=d;h=e}for(var i=Array(h+1),m=Array(h+1),n=1/h,o=0;o<=h;o++){for(var p=0;p<=h;p++){var r=this.computePointAt(p*n,o*n,this.points);if(p>0&&o>0){g.addFace(r,i[p-1],m[p-1]);g.addFace(r,m[p-1],m[p])}i[p]=r}p=m;m=i;i=p}return g}};
toxi.BezierPatch.computePointAt=function(d,e,g){var h=1-d,i=h*h*3*d,m=h*h*h,n=d*d,o=1-e,p=o*o*o,r=e*e*e;h=h*n*3;d=d*n;n=o*e*e*3;e=o*o*3*e;o=g[0];var t=g[1],u=g[2];g=g[3];return new toxi.Vec3D(m*(o[0].x*p+o[1].x*e+o[2].x*n+o[3].x*r)+i*(t[0].x*p+t[1].x*e+t[2].x*n+t[3].x*r)+h*(u[0].x*p+u[1].x*e+u[2].x*n+u[3].x*r)+d*(g[0].x*p+g[1].x*e+g[2].x*n+g[3].x*r),m*(o[0].y*p+o[1].y*e+o[2].y*n+o[3].y*r)+i*(t[0].y*p+t[1].y*e+t[2].y*n+t[3].y*r)+h*(u[0].y*p+u[1].y*e+u[2].y*n+u[3].y*r)+d*(g[0].y*p+g[1].y*e+g[2].y*n+
g[3].y*r),m*(o[0].z*p+o[1].z*e+o[2].z*n+o[3].z*r)+i*(t[0].z*p+t[1].z*e+t[2].z*n+t[3].z*r)+h*(u[0].z*p+u[1].z*e+u[2].z*n+u[3].z*r)+d*(g[0].z*p+g[1].z*e+g[2].z*n+g[3].z*r))};toxi.XAxisCylinder=function(d,e,g){toxi.AxisAlignedCylinder.apply(this,[d,e,g])};toxi.extend(toxi.XAxisCylinder,toxi.AxisAlignedCylinder);toxi.XAxisCylinder.prototype.containsPoint=function(d){if(toxi.MathUtils.abs(d.x-this.pos.x)<this.length*0.5){var e=d.y-this.pos.y;d=d.z-this.pos.z;if(Math.abs(d*d+e*e)<this.radiusSquared)return true}return false};
toxi.XAxisCylinder.prototype.getMajorAxis=function(){return toxi.Vec3D.Axis.X};toxi.YAxisCylinder=function(d,e,g){toxi.AxisAlignedCylinder.apply(this,[d,e,g])};toxi.extend(toxi.YAxisCylinder,toxi.AxisAlignedCylinder);toxi.YAxisCylinder.prototype.containsPoint=function(d){if(toxi.MathUtils.abs(d.y-this.pos.y)<this.length*0.5){var e=d.x-this.pos.x;d=d.z-this.pos.z;if(Math.abs(d*d+e*e)<this.radiusSquared)return true}return false};toxi.YAxisCylinder.prototype.getMajorAxis=function(){return toxi.Vec3D.Axis.Y};
toxi.ZAxisCylinder=function(d,e,g){toxi.AxisAlignedCylinder.apply(this,[d,e,g])};toxi.extend(toxi.ZAxisCylinder,toxi.AxisAlignedCylinder);toxi.ZAxisCylinder.prototype.containsPoint=function(d){if(MathUtils.abs(d.z-this.pos.z)<this.length*0.5){var e=d.x-this.pos.x;d=d.y-this.pos.y;if(Math.abs(e*e+d*d)<this.radiusSquared)return true}return false};toxi.ZAxisCylinder.prototype.getMajorAxis=function(){return toxi.Vec3D.Axis.Z};toxi.Line3D=function(d,e){this.a=d;this.b=e};
toxi.Line3D.prototype={closestLineTo:function(d){var e=d.a.sub(d.b);if(e.isZeroVector())return new toxi.Line3D.LineIntersection(toxi.Line3D.LineIntersection.Type.NON_INTERSECTING);var g=this.b.sub(this.a);if(g.isZeroVector())return new toxi.Line3D.LineIntersection(toxi.Line3D.LineIntersection.Type.NON_INTERSECTING);var h=this.a.sub(d.a),i=h.x*e.x+h.y*e.y+h.z*e.z,m=e.x*g.x+e.y*g.y+e.z*g.z;h=h.x*g.x+h.y*g.y+h.z*g.z;var n=e.x*e.x+e.y*e.y+e.z*e.z,o=(g.x*g.x+g.y*g.y+g.z*g.z)*n-m*m;if(Math.abs(o)<toxi.MathUtils.EPS)return new toxi.Line3D.LineIntersection(toxi.Line3D.LineIntersection.Type.NON_INTERSECTING);
h=(i*m-h*n)/o;i=(i+m*h)/n;g=this.a.add(g.scaleSelf(h));d=d.a.add(e.scaleSelf(i));return new toxi.Line3D.LineIntersection(toxi.Line3D.LineIntersection.Type.INTERSECTING,new toxi.Line3D(g,d),h,i)},closestPointTo:function(d){var e=this.b.sub(this.a);d=d.sub(this.a).dot(e)/e.magSquared();if(d<0)return this.a.copy();else if(d>1)return this.b.copy();return this.a.add(e.scaleSelf(d))},copy:function(){return new toxi.Line3D(this.a.copy(),this.b.copy())},equals:function(d){if(this==d)return true;if(typeof d!=
toxi.Line3D)return false;return(this.a.equals(d.a)||this.a.equals(l.b))&&(this.b.equals(l.b)||this.b.equals(l.a))},getDirection:function(){return this.b.sub(this.a).normalize()},getLength:function(){return this.a.distanceTo(this.b)},getLengthSquared:function(){return this.a.distanceToSquared(this.b)},getMidPoint:function(){return this.a.add(this.b).scaleSelf(0.5)},getNormal:function(){return this.b.cross(this.a)},hasEndPoint:function(d){return this.a.equals(d)||this.b.equals(d)},offsetAndGrowBy:function(d,
e,g){var h=this.getMidPoint(),i=this.getDirection(),m=this.a.cross(i).normalize();g!==undefined&&h.sub(g).dot(m)<0&&m.invert();m.normalizeTo(d);this.a.addSelf(m);this.b.addSelf(m);i.scaleSelf(e);this.a.subSelf(i);this.b.addSelf(i);return this},set:function(d,e){this.a=d;this.b=e;return this},splitIntoSegments:function(d,e,g){return toxi.Line3D.splitIntoSegments(this.a,this.b,e,d,g)},toString:function(){return this.a.toString()+" -> "+this.b.toString()}};
toxi.Line3D.splitIntoSegments=function(d,e,g,h,i){if(h===undefined)h=[];i&&h.push(d.copy());i=d.distanceTo(e);if(i>g){var m=d.copy();for(d=e.sub(d).limit(g);i>g;){m.addSelf(d);h.push(m.copy());i-=g}}h.push(e.copy());return h};toxi.Line3D.LineIntersection=function(d,e,g,h){this.type=d;if(g===undefined)g=0;if(h===undefined)h=0;this.line=e;this.coeff=[g,h]};
toxi.Line3D.LineIntersection.prototype={getCoefficient:function(){return this.coeff},getLength:function(){if(this.line!==undefined)return this.line.getLength()},getLine:function(){if(this.line!==undefined)return this.line.copy()},getType:function(){return this.type},isIntersectionInside:function(){return this.type==toxi.Line3D.LineIntersection.Type.INTERSECTING&&this.coeff[0]>=0&&this.coeff[0]<=1&&this.coeff[1]>=0&&this.coeff[1]<=1},toString:function(){return"type: "+this.type+" line: "+this.line}};
toxi.Line3D.LineIntersection.Type={NON_INTERSECTING:0,INTERSECTING:1};toxi.Ray2D=function(d,e,g){if(arguments.length==3){toxi.Vec2D.apply(this,[d,e]);this.dir=g.getNormalized()}else if(arguments.length==2){toxi.Vec2D.apply(this,[d]);this.dir=e.getNormalized()}else if(arguments.length===0){toxi.Vec2D.apply(this);this.dir=toxi.Vec2D.Y_AXIS.copy()}};toxi.extend(toxi.Ray2D,toxi.Vec2D);toxi.Ray2D.prototype.getDirection=function(){return this.dir.copy()};
toxi.Ray2D.prototype.getDistanceToPoint=function(d){d=d.sub(this);return d.distanceTo(this.dir.scale(d.dot(this.dir)))};toxi.Ray2D.prototype.getPointAtDistance=function(d){return this.add(this.dir.scale(d))};toxi.Ray2D.prototype.setDirection=function(d){this.dir.set(d).normalize();return this};toxi.Ray2D.prototype.toLine2DWithPointAtDistance=function(d){return new toxi.Line2D(this,this.getPointAtDistance(d))};
toxi.Ray2D.prototype.toString=function(){return"origin: "+toxi.Vec2D.prototype.toString.apply(this)+" dir: "+this.dir};toxi.Ray3D=function(d,e,g,h){var i,m;if(arguments.length==4){i=new toxi.Vec3D(d,e,g);m=h}else if(arguments.length==2){i=d;m=e}else{i=new toxi.Vec3D;m=toxi.Vec3D.Y_AXIS.copy()}toxi.Vec3D.apply(this,[i]);this.dir=m};toxi.extend(toxi.Ray3D,toxi.Vec3D);toxi.Ray3D.prototype.getDirection=function(){return this.dir.copy()};
toxi.Ray3D.prototype.getDistanceToPoint=function(d){d=d.sub(this);return d.distanceTo(this.dir.scale(d.dot(this.dir)))};toxi.Ray3D.prototype.getPointAtDistance=function(d){return this.add(this.dir.scale(d))};toxi.Ray3D.prototype.setDirection=function(d){this.dir.set(d).normalize();return this};toxi.Ray3D.prototype.toLine3DWithPointAtDistance=function(d){return new toxi.Line3D(this,this.getPointAtDistance(d))};
toxi.Ray3D.prototype.toString=function(){return"origin: "+this.parent.toString.call(this)+" dir: "+this.dir};toxi.color=toxi.color||{};
(function(){var d=function(e){var g="0000";if(e>=0&&e<=15)g="000"+e.toString(16);else if(e>=16&&e<=255)g="00"+e.toString(16);else if(e>=256&&e<=4095)g="0"+e.toString(16);else if(e>=4096&&e<=65535)g=e.toString(16);return g};toxi.color.TColor=function(e){this.rgb=Array(3);this.hsv=Array(3);this.cmyk=Array(4);this._alpha=1;if(e!==undefined){this.cmyk=e.toCMYKAArray().splice(0,4);this.hsv=e.toHSVAArray().splice(0,3);this.rgb=e.toRGBAArray().splice(0,3);this._alpha=e._alpha}};toxi.color.TColor.prototype=
{add:function(e){return this.copy().addSelf(e)},addSelf:function(e){this.rgb[0]=toxi.MathUtils.min(this.rgb[0]+e.rgb[0],1);this.rgb[1]=toxi.MathUtils.min(this.rgb[1]+e.rgb[1],1);this.rgb[2]=toxi.MathUtils.min(this.rgb[2]+e.rgb[2],1);return this.setRGB(rgb)},adjustConstrast:function(e){return this.hsv[2]<0.5?this.darken(e):this.lighten(e)},adjustHSV:function(e,g,h){return this.setHSV([this.hsv[0]+e,this.hsv[1]+g,this.hsv[2]+h])},adjustRGB:function(e,g,h){return this.setRGB([this.rgb[0]+e,this.rgb[1]+
g,this.rgb[2]+h])},alpha:function(){return this._alpha},analog:function(e,g){this.rotateRYB(toxi.MathUtils.degrees(e)*toxi.MathUtils.normalizedRandom());this.hsv[1]+=g*toxi.MathUtils.normalizedRandom();this.hsv[2]+=g*toxi.MathUtils.normalizedRandom();return this.setHSV(this.hsv)},black:function(){return this.cmyk[3]},blend:function(e,g){if(g===undefined)g=0.5;var h=e.toRGBAArray();this.rgb[0]+=(h[0]-this.rgb[0])*g;this.rgb[1]+=(h[1]-this.rgb[1])*g;this.rgb[2]+=(h[2]-this.rgb[2])*g;this._alpha+=(e.alpha()-
this._alpha)*g;return this.setRGB(this.rgb)},blue:function(){return this.rgb[2]},brightness:function(){return this.hsv[2]},complement:function(){return this.rotateRYB(180)},copy:function(){return new toxi.color.TColor(this)},cyan:function(){return this.cmyk[0]},darken:function(e){this.hsv[2]=toxi.MathUtils.clip(this.hsv[2]-e,0,1);return this.setHSV(this.hsv)},desaturate:function(e){this.hsv[1]=toxi.MathUtils.clip(this.hsv[1]-e,0,1);return this.setHSV(this.hsv)},differenceTo:function(e){return toxi.color.TColor.newRGB(Math.abs(this.rgb[0]-
e.rgb[0]),Math.abs(this.rgb[1]-e.rgb[1]),Math.abs(this.rgb[2]-e.rgb[2]))},distanceToCMYK:function(e){var g=e.toCMYKAArray();e=this.cmyk[0]-g[0];var h=this.cmyk[1]-g[1],i=this.cmyk[2]-g[2];g=this.cmyk[3]-g[3];return Math.sqrt(e*e+h*h+i*i+g*g)},distanceToHSV:function(e){var g=this.hsv[0]*toxi.MathUtils.TWO_PI,h=e.hue()*toxi.MathUtils.TWO_PI;g=new toxi.Vec3D(Math.cos(g)*this.hsv[1],Math.sin(g)*this.hsv[1],this.hsv[2]);e=new toxi.Vec3D(Math.cos(h)*e.saturation(),Math.sin(h)*e.saturation(),e.brightness());
return g.distanceTo(e)},distanceToRGB:function(e){var g=e.toRGBAArray();e=this.rgb[0]-g[0];var h=this.rgb[1]-g[1];g=this.rgb[2]-g[2];return Math.sqrt(e*e+h*h+g*g)},equals:function(e){if(e!==undefined&&e instanceof toxi.color.TColor){var g=e.rgb[0]-this.rgb[0],h=e.rgb[1]-this.rgb[1],i=e.rgb[2]-this.rgb[2];e=e.alpha()-this._alpha;return Math.sqrt(g*g+h*h+i*i+e*e)<toxi.color.TColor.EPS}return false},getAnalog:function(e,g){return(new toxi.color.TColor(this)).analog(e,g)},getBlended:function(e,g){return(new toxi.color.TColor(this)).blend(e,
g)},getComplement:function(){return(new toxi.color.TColor(this)).complement()},getComponentValue:function(e){return e.getComponentValueFor(this)},getDarkened:function(e){return(new toxi.color.TColor(this)).darken(e)},getDesaturated:function(e){return(new toxi.color.TColor(this)).desaturate(e)},getDifferenceTo:function(e){return this.copy().differenceTo(e)},getInverted:function(){return(new toxi.color.TColor(this)).invert()},getLightened:function(e){return(new toxi.color.TColor(this)).lighten(e)},
getRotatedRYB:function(e){return(new toxi.color.TColor(this)).rotateRYB(e)},getSaturated:function(e){return(new toxi.color.TColor(this)).saturate(e)},green:function(){return this.rgb[1]},hue:function(){return this.hsv[0]},invert:function(){this.rgb[0]=1-this.rgb[0];this.rgb[1]=1-this.rgb[1];this.rgb[2]=1-this.rgb[2];return this.setRGB(this.rgb)},isBlack:function(){return this.rgb[0]<=toxi.color.TColor.BLACK_POINT&&this.rgb[0]==this.rgb[1]&&this.rgb[0]==this.rgb[2]},isGrey:function(){return this.hsv[1]<
toxi.color.TColor.GREY_THRESHOLD},isWhite:function(){return this.rgb[0]>=toxi.color.TColor.WHITE_POINT&&this.rgb[0]==this.rgb[1]&&this.rgb[0]==this.rgb[2]},lighten:function(e){this.hsv[2]=toxi.MathUtils.clip(this.hsv[2]+e,0,1);return this.setHSV(this.hsv)},luminance:function(){return this.rgb[0]*0.299+this.rgb[1]*0.587+this.rgb[2]*0.114},magenta:function(){return this.cmyk[1]},red:function(){return this.rgb[0]},rotateRYB:function(e){parseInt(toxi.MathUtils.degrees(e),10);var g=this.hsv[0]*360,h=0,
i,m;e%=360;var n=0;for(h=0;h<toxi.color.TColor.RYB_WHEEL.length-1;h++){i=toxi.color.TColor.RYB_WHEEL[h];m=toxi.color.TColor.RYB_WHEEL[h+1];if(m.y<i.y)m.y+=360;if(i.y<=g&&g<=m.y){n=i.x+(m.x-i.x)*(g-i.y)/(m.y-i.y);break}}n=(n+e)%360;for(h=0;h<toxi.color.TColor.RYB_WHEEL.length-1;h++){i=toxi.color.TColor.RYB_WHEEL[h];m=toxi.color.TColor.RYB_WHEEL[h+1];if(m.y<i.y)m.y+=360;if(i.x<=n&&n<=m.x){g=i.y+(m.y-i.y)*(n-i.x)/(m.x-i.x);break}}this.hsv[0]=g%360/360;return this.setHSV(this.hsv)},saturate:function(e){this.hsv[1]=
toxi.MathUtils.clip(this.hsv[1]+e,0,1);return this.setHSV(this.hsv)},saturation:function(){return this.hsv[1]},setAlpha:function(e){this._alpha=e;return this},setARGB:function(e){this.setRGB((e>>16&255)*toxi.TColor.INV8BIT,(e>>8&255)*toxi.TColor.INV8BIT,(e&255)*toxi.TColor.INV8BIT);this._alpha=(e>>>24)*toxi.TColor.INV8BIT;return this},setBlack:function(e){this.cmyk[3]=e;return this.setCMYK(cmyk)},setBlue:function(e){this.rgb[2]=e;return this.setRGB(this.rgb)},setBrightness:function(e){this.hsv[2]=
toxi.MathUtils.clip(e,0,1);return this.setHSV(this.hsv)},setCMYK:function(e,g,h,i){if(e instanceof Array){g=e[1];h=e[2];i=e[3];e=e[0]}this.cmyk[0]=e;this.cmyk[1]=g;this.cmyk[2]=h;this.cmyk[3]=i;this.rgb=toxi.color.TColor.cmykToRGB(this.cmyk[0],this.cmyk[1],this.cmyk[2],this.cmyk[3]);this.hsv=toxi.color.TColor.rgbToHSV(this.rgb[0],this.rgb[1],this.rgb[2]);return this},setCyan:function(e){this.cmyk[0]=e;return this.setCMYK(this.cmyk)},setGreen:function(e){this.rgb[1]=e;return this.setRGB(this.rgb)},
setHSV:function(e,g,h){if(e instanceof Array){g=e[1];h=e[2];e=e[0]}e=[e,g,h];this.hsv[0]=e[0]%1;this.hsv[0]<0&&this.hsv[0]++;this.hsv[1]=toxi.MathUtils.clip(e[1],0,1);this.hsv[2]=toxi.MathUtils.clip(e[2],0,1);this.rgb=toxi.color.TColor.hsvToRGB(this.hsv[0],this.hsv[1],this.hsv[2]);this.cmyk=toxi.color.TColor.rgbToCMYK(this.rgb[0],this.rgb[1],this.rgb[2]);return this},setHue:function(e){e%=1;e<0&&e++;this.hsv[0]=e;this.setHSV(this.hsv)},setMagenta:function(e){this.cmyk[1]=e;return this.setCMYK(this.cmyk)},
setRed:function(e){this.rgb[0]=e;return this.setRGB(this.rgb)},setRGB:function(e,g,h){if(e instanceof Array){g=e[1];h=e[2];e=e[0]}this.rgb[0]=toxi.MathUtils.clip(e,0,1);this.rgb[1]=toxi.MathUtils.clip(g,0,1);this.rgb[2]=toxi.MathUtils.clip(h,0,1);this.cmyk=toxi.color.TColor.rgbToCMYK(this.rgb[0],this.rgb[1],this.rgb[2]);this.hsv=toxi.color.TColor.rgbToHSV(this.rgb[0],this.rgb[1],this.rgb[2]);return this},setSaturation:function(e){this.hsv[1]=toxi.MathUtils.clip(e,0,1);return this.setHSV(this.hsv)},
setYellow:function(e){this.cmyk[2]=e;return this.setCMYK(this.cmyk)},sub:function(e){return this.copy().subSelf(e)},subSelf:function(e){this.rgb[0]=toxi.MathUtils.max(this.rgb[0]-e.rgb[0],0);this.rgb[1]=toxi.MathUtils.max(this.rgb[1]-e.rgb[1],0);this.rgb[2]=toxi.MathUtils.max(this.rgb[2]-e.rgb[2],0);return this.setRGB(this.rgb)},toARGB:function(){var e=parseInt(this.rgb[0]*255,10),g=parseInt(this.rgb[1]*255,10),h=parseInt(this.rgb[2]*255,10),i=parseInt(this._alpha*255,10);return e<<16|g<<8|h|i<<24},
toCMYKAArray:function(e){if(e===undefined)e=[];e[0]=this.cmyk[0];e[1]=this.cmyk[1];e[2]=this.cmyk[2];e[3]=this._alpha;return e},toHex:function(){var e=d(this.toARGB());if(e.length>6)e=e.substring(2);return e},toHSVAArray:function(e){if(e===undefined)e=[];e[0]=this.hsv[0];e[1]=this.hsv[1];e[2]=this.hsv[2];e[3]=this._alpha;return e},toRGBAArray:function(e,g){if(e===undefined){e=[];g=0}e[g++]=this.rgb[0];e[g++]=this.rgb[1];e[g++]=this.rgb[2];e[g]=this._alpha;return e},toString:function(){return"toxi.color.TColor: rgb: "+
this.rgb[0]+", "+this.rgb[1]+", "+this.rgb[2]+" hsv: "+this.hsv[0]+","+this.hsv[1]+","+this.hsv[2]+" cmyk: "+this.cmyk[0]+", "+this.cmyk[1]+","+this.cmyk[2]+","+this.cmyk[3]+" alpha: "+this._alpha},yellow:function(){return this.cmyk[2]}};toxi.color.TColor.INV60DEGREES=60/360;toxi.color.TColor.INV8BIT=1/255;toxi.color.TColor.EPS=0.0010;toxi.color.TColor.BLACK_POINT=0.08;toxi.color.TColor.WHITE_POINT=1;toxi.color.TColor.GREY_THRESHOLD=0.01;toxi.color.TColor.cmykToRGB=function(e,g,h,i,m){if(m===undefined)m=
[0,0,0];m[0]=1-Math.min(1,e+i);m[1]=1-Math.min(1,g+i);m[2]=1-Math.min(1,h+i);return m};toxi.color.TColor.hexToRGB=function(e,g){if(g===undefined)g=[];e=e.charAt(0)=="#"?e.substring(1,7):e;g[0]=parseInt(e.substring(0,2),16)*toxi.color.TColor.INV8BIT;g[1]=parseInt(e.substring(2,4),16)*toxi.color.TColor.INV8BIT;g[2]=parseInt(e.substring(4,6),16)*toxi.color.TColor.INV8BIT;return g};toxi.color.TColor.hsvToRGB=function(e,g,h,i){if(i===undefined)i=[];if(g===0)i[0]=i[1]=i[2]=h;else{e/=toxi.color.TColor.INV60DEGREES;
var m=parseInt(e,10),n=e-m;e=h*(1-g);var o=h*(1-g*n);g=h*(1-g*(1-n));if(m===0){i[0]=h;i[1]=g;i[2]=e}else if(m==1){i[0]=o;i[1]=h;i[2]=e}else if(m==2){i[0]=e;i[1]=h;i[2]=g}else if(m==3){i[0]=e;i[1]=o;i[2]=h}else if(m==4){i[0]=g;i[1]=e;i[2]=h}else{i[0]=h;i[1]=e;i[2]=o}}return i};toxi.color.TColor.labToRGB=function(e,g,h,i){if(i===undefined)i=[];e=(e+16)/116;g=g/500+e;var m=e-h/200;h=0;i[0]=g;i[1]=e;i[2]=m;for(h=0;h<3;h++){e=Math.pow(i[h],3);i[h]=e>0.008856?e:(i[h]-16/116)/7.787}g=i[0]*0.95047;e=i[1];
m=i[2]*1.08883;i[0]=g*3.2406+e*-1.5372+m*-0.4986;i[1]=g*-0.9689+e*1.8758+m*0.0415;i[2]=g*0.0557+e*-0.204+m*1.057;e=1/2.4;for(h=0;h<3;h++)i[h]=i[h]>0.0031308?1.055*Math.pow(i[h],e)-0.055:12.92*i[h];return i};toxi.color.TColor.newARGB=function(e){return toxi.color.TColor.newRGBA((e>>16&255)*toxi.color.TColor.INV8BIT,(e>>8&255)*toxi.color.TColor.INV8BIT,(e&255)*toxi.color.TColor.INV8BIT,(e>>>24)*toxi.color.TColor.INV8BIT)};toxi.color.TColor.newCMYK=function(e,g,h,i){return toxi.color.TColor.newCMYKA(e,
g,h,i,1)};toxi.color.TColor.newCMYKA=function(e,g,h,i,m){var n=new toxi.color.TColor;n.setCMYK([e,g,h,i]);n.setAlpha(toxi.MathUtils.clip(m,0,1));return n};toxi.color.TColor.newGray=function(e){return toxi.color.TColor.newGrayAlpha(e,1)};toxi.color.TColor.newGrayAlpha=function(e,g){var h=new toxi.color.TColor;h.setRGB([e,e,e]);h.setAlpha(g);return h};toxi.color.TColor.newHex=function(e){var g=new toxi.color.TColor;g.setRGB(toxi.color.TColor.hexToRGB(e));g.setAlpha(1);return g};toxi.color.TColor.newHSV=
function(e,g,h){return toxi.color.TColor.newHSVA(e,g,h,1)};toxi.color.TColor.newHSVA=function(e,g,h,i){var m=new toxi.color.TColor;m.setHSV(e,g,h);m.setAlpha(toxi.MathUtils.clip(i,0,1));return m};toxi.color.TColor.newRandom=function(){return toxi.color.TColor.newRGBA(Math.random(),Math.random(),Math.random(),1)};toxi.color.TColor.newRGB=function(e,g,h){return toxi.color.TColor.newRGBA(e,g,h,1)};toxi.color.TColor.newRGBA=function(e,g,h,i){var m=new toxi.color.TColor;m.setRGB([e,g,h]);m.setAlpha(toxi.MathUtils.clip(i,
0,1));return m};toxi.color.TColor.rgbToCMYK=function(e,g,h,i){if(i===undefined)i=[];i[0]=1-e;i[1]=1-g;i[2]=1-h;i[3]=toxi.MathUtils.min(i[0],i[1],i[2]);i[0]=toxi.MathUtils.clip(i[0]-i[3],0,1);i[1]=toxi.MathUtils.clip(i[1]-i[3],0,1);i[2]=toxi.MathUtils.clip(i[2]-i[3],0,1);i[3]=toxi.MathUtils.clip(i[3],0,1);return i};toxi.color.TColor.rgbToHex=function(e,g,h){return d(toxi.MathUtils.clip(e,0,1)*255)+d(toxi.MathUtils.clip(g,0,1)*255)+d(toxi.MathUtils.clip(h,0,1)*255)};toxi.color.TColor.rgbToHSV=function(e,
g,h,i){if(i===undefined)i=[];var m=0,n=0,o=toxi.MathUtils.max(e,g,h),p=o-toxi.MathUtils.min(e,g,h);if(o!==0)n=p/o;if(n!==0)m=e==o?(g-h)/p:g==o?2+(h-e)/p:4+(e-g)/p;m*=toxi.color.TColor.INV60DEGREES;if(m<0)m+=1;i[0]=m;i[1]=n;i[2]=o;return i};toxi.color.TColor.RED=toxi.color.TColor.newRGB(1,0,0);toxi.color.TColor.RYB_WHEEL=[new toxi.Vec2D(0,0),new toxi.Vec2D(15,8),new toxi.Vec2D(30,17),new toxi.Vec2D(45,26),new toxi.Vec2D(60,34),new toxi.Vec2D(75,41),new toxi.Vec2D(90,48),new toxi.Vec2D(105,54),new toxi.Vec2D(120,
60),new toxi.Vec2D(135,81),new toxi.Vec2D(150,103),new toxi.Vec2D(165,123),new toxi.Vec2D(180,138),new toxi.Vec2D(195,155),new toxi.Vec2D(210,171),new toxi.Vec2D(225,187),new toxi.Vec2D(240,204),new toxi.Vec2D(255,219),new toxi.Vec2D(270,234),new toxi.Vec2D(285,251),new toxi.Vec2D(300,267),new toxi.Vec2D(315,282),new toxi.Vec2D(330,298),new toxi.Vec2D(345,329),new toxi.Vec2D(360,0)];toxi.color.TColor.GREEN=toxi.color.TColor.newRGB(0,1,0);toxi.color.TColor.BLUE=toxi.color.TColor.newRGB(0,0,1);toxi.color.TColor.CYAN=
toxi.color.TColor.newRGB(0,1,1);toxi.color.TColor.MAGENTA=toxi.color.TColor.newRGB(1,0,1);toxi.color.TColor.YELLOW=toxi.color.TColor.newRGB(1,1,0);toxi.color.TColor.BLACK=toxi.color.TColor.newRGB(0,0,0);toxi.color.TColor.WHITE=toxi.color.TColor.newRGB(1,1,1)})();toxi.physics2d=toxi.physics2d||{};toxi.physics2d.removeItemFrom=function(d,e){var g=e.indexOf(d);if(g>-1)return e.splice(g,1)};
toxi.physics2d.VerletParticle2D=function(d,e,g){this.force=new toxi.Vec2D;if(d instanceof toxi.Vec2D)if(d instanceof toxi.physics2d.VerletParticle2D){e=d.y;g=d.weight;d=d.x;this.isLocked=d.isLocked}else{if(e===undefined){e=d.y;g=1}else{g=e;e=d.y}d=d.x}toxi.Vec2D.apply(this,[d,e]);this.isLocked=false;this.prev=new toxi.Vec2D(this);this.temp=new toxi.Vec2D;g=g||1;this.setWeight(g)};toxi.extend(toxi.physics2d.VerletParticle2D,toxi.Vec2D);
toxi.physics2d.VerletParticle2D.prototype.addBehavior=function(d,e){if(this.behaviors===undefined)this.behaviors=[];if(d===undefined)throw{name:"TypeError",message:"behavior was undefined"};e=e===undefined?1:e;d.configure(e);this.behaviors.push(d);return this};toxi.physics2d.VerletParticle2D.prototype.addConstraint=function(d){if(this.constraints===undefined)this.constraints=[];this.constraints.push(d);return this};
toxi.physics2d.VerletParticle2D.prototype.addForce=function(d){this.force.addSelf(d);return this};toxi.physics2d.VerletParticle2D.prototype.addVelocity=function(d){this.prev.subSelf(d);return this};toxi.physics2d.VerletParticle2D.prototype.applyBehaviors=function(){if(this.behaviors!==undefined){var d=0;for(d=0;d<this.behaviors.length;d++)this.behaviors[d].applyBehavior(this)}};
toxi.physics2d.VerletParticle2D.prototype.applyConstraints=function(){if(this.constraints!==undefined){var d=0;for(d=0;d<this.constraints.length;d++)this.constraints[d].applyConstraint(this)}};toxi.physics2d.VerletParticle2D.prototype.clearForce=function(){this.force.clear();return this};toxi.physics2d.VerletParticle2D.prototype.clearVelocity=function(){this.prev.set(this);return this};toxi.physics2d.VerletParticle2D.prototype.getInvWeight=function(){return this.invWeight};
toxi.physics2d.VerletParticle2D.prototype.getPreviousPosition=function(){return this.prev};toxi.physics2d.VerletParticle2D.prototype.getVelocity=function(){return this.sub(this.prev)};toxi.physics2d.VerletParticle2D.prototype.getWeight=function(){return this.weight};toxi.physics2d.VerletParticle2D.prototype.lock=function(){this.isLocked=true;return this};toxi.physics2d.VerletParticle2D.prototype.removeAllBehaviors=function(){this.behaviors=[];return this};
toxi.physics2d.VerletParticle2D.prototype.removeAllConstraints=function(){this.constraints=[];return this};toxi.physics2d.VerletParticle2D.prototype.removeBehavior=function(d){return toxi.physics2d.removeItemFrom(d,this.behaviors)};toxi.physics2d.VerletParticle2D.prototype.removeConstraint=function(d){return toxi.physics2d.removeItemFrom(d,this.constraints)};toxi.physics2d.VerletParticle2D.prototype.scaleVelocity=function(d){this.prev.interpolateToSelf(this,1-d);return this};
toxi.physics2d.VerletParticle2D.prototype.setPreviousPosition=function(d){this.prev.set(d);return this};toxi.physics2d.VerletParticle2D.prototype.setWeight=function(d){this.weight=d;this.invWeight=d!==0?1/d:0};toxi.physics2d.VerletParticle2D.prototype.unlock=function(){this.clearVelocity();this.isLocked=false;return this};
toxi.physics2d.VerletParticle2D.prototype.update=function(){if(!this.isLocked){this.applyBehaviors();this.temp.set(this);this.addSelf(this.sub(this.prev).addSelf(this.force.scale(this.weight)));this.prev.set(this.temp);this.force.clear();this.applyConstraints()}};toxi.physics2d.VerletSpring2D=function(d,e,g,h){this.a=d;this.b=e;this.restLength=g;this.strength=h};toxi.physics2d.VerletSpring2D.EPS=1.0E-6;
toxi.physics2d.VerletSpring2D.prototype={getRestLength:function(){return this.restLength},getStrength:function(){return this.strength},lockA:function(d){this.isALocked=d;return this},lockB:function(d){this.isALocked=d;return this},setRestLength:function(d){this.restLength=d;this.restLengthSquared=d*d;return this},setStrength:function(d){this.strength=d;return this},update:function(d){var e=this.b.sub(this.a),g=e.magnitude()+toxi.physics2d.VerletSpring2D.EPS;g=(g-this.restLength)/(g*(this.a.invWeight+
this.b.invWeight))*this.strength;if(!this.a.isLocked&&!this.isALocked){this.a.addSelf(e.scale(g*this.a.invWeight));d&&this.a.applyConstraints()}if(!this.b.isLocked&&!this.isBLocked){this.b.addSelf(e.scale(-g*this.b.invWeight));d&&this.b.applyConstraints()}}};toxi.physics2d.AttractionBehavior=function(d,e,g,h){if(arguments.length<3)throw{name:"IncorrectParameters",message:"Constructor received incorrect Parameters"};this.jitter=h||0;this.attractor=d;this.strength=g;this.setRadius(e)};
toxi.physics2d.AttractionBehavior.prototype={applyBehavior:function(d){var e=this.attractor.sub(d),g=e.magSquared();if(g<this.radiusSquared){e=e.normalizeTo(1-g/this.radiusSquared).jitter(this.jitter).scaleSelf(this.attrStrength);d.addForce(e)}},configure:function(d){this.timeStep=d;this.setStrength(this.strength)},getAttractor:function(){return this.attractor},getJitter:function(){return this.jitter},getRadius:function(){return this.radius},getStrength:function(){return this.strength},setAttractor:function(d){this.attractor=
d},setJitter:function(d){this.jitter=d},setRadius:function(d){this.radius=d;this.radiusSquared=d*d},setStrength:function(d){this.strength=d;this.attrStrength=d*this.timeStep}};toxi.physics2d.ConstantForceBehavior=function(d){this.force=d;this.scaleForce=new toxi.Vec2D;this.timeStep=0};
toxi.physics2d.ConstantForceBehavior.prototype={applyBehavior:function(d){d.addForce(this.scaledForce)},configure:function(d){this.timeStep=d;this.setForce(this.force)},getForce:function(){return this.force},setForce:function(d){this.force=d;this.scaledForce=this.force.scale(this.timeStep)},toString:function(){return"behavior force: "+this.force+" scaledForce: "+this.scaledForce+" timeStep: "+this.timeStep}};
toxi.physics2d.GravityBehavior=function(d){toxi.physics2d.ConstantForceBehavior.apply(this,[d])};toxi.extend(toxi.physics2d.GravityBehavior,toxi.physics2d.ConstantForceBehavior);toxi.physics2d.GravityBehavior.prototype.configure=function(d){this.timeStep=d;this.scaledForce=this.force.scale(d*d)};
toxi.physics2d.AngularConstraint=function(d,e){if(arguments.length>1){this.theta=e;this.rootPos=new toxi.Vec2D(d)}else{this.rootPos=new toxi.Vec2D;this.theta=d}if(parseInt(this.theta,10)!=this.theta)this.theta=toxi.MathUtils.radians(this.theta)};toxi.physics2d.AngularConstraint.prototype.applyConstraint=function(d){var e=d.sub(this.rootPos),g=toxi.MathUtils.floor(e.heading()/this.theta)*this.theta;d.set(this.rootPos.add(toxi.Vec2D.fromTheta(g).scaleSelf(e.magnitude())))};
toxi.physics2d.AxisConstraint=function(d,e){this.axis=d;this.constraint=e};toxi.physics2d.AxisConstraint.prototype.applyConstraint=function(d){d.setComponent(this.axis,this.constraint)};toxi.physics2d.CircularConstraint=function(d,e){if(arguments.length==1)this.circle=d;else{console.log("a: "+d);this.circle=new toxi.Circle(d,e)}};toxi.physics2d.CircularConstraint.prototype.applyConstraint=function(d){this.circle.containsPoint(d)&&d.set(this.circle.add(d.sub(this.circle).normalizeTo(this.circle.getRadius())))};
toxi.physics2d.MaxConstraint=function(d,e){this.axis=d;this.threshold=e};toxi.physics2d.MaxConstraint.prototype.applyConstraint=function(d){d.getComponent(this.axis)>this.threshold&&d.setComponent(this.axis,this.threshold)};toxi.physics2d.MinConstraint=function(d,e){this.axis=d;this.threshold=e};toxi.physics2d.MinConstraint.prototype.applyConstraint=function(d){d.getComponent(this.axis)<this.threshold&&d.setComponent(this.axis,this.threshold)};
toxi.physics2d.RectConstraint=function(d,e){if(arguments.length==1)this.rect=d.copy();else if(arguments.length>1)this.rect=new toxi.Rect(d,e);this.intersectRay=new toxi.Ray2D(this.rect.getCentroid(),new toxi.Vec2D)};
toxi.physics2d.RectConstraint.prototype={applyConstraint:function(d){this.rect.containsPoint(d)&&d.set(this.rect.intersectsRay(this.intersectRay.setDirection(this.intersectRay.sub(d)),0,Number.MAX_VALUE))},getBox:function(){return this.rect.copy()},setBox:function(d){this.rect=d.copy();this.intersectRay.set(this.rect.getCentroid())}};toxi.physics2d.ParticlePath2D=function(d){toxi.Spline2D.call(this,d);this.particles=[]};toxi.extend(toxi.physics2d.ParticlePath2D,toxi.Spline2D);
(function(){toxi.physics2d.ParticlePath2D.prototype.createParticles=function(d,e,g,h){this.particles=[];this.computeVertices(e);e=0;g=this.getDecimatedVertices(g,true);for(e=0;e<g;e++){var i=this.createSingleParticle(v,h);this.particles.push(i);d.addParticle(i)}return this.particles}})();
toxi.physics2d.ParticleString2D=function(){var d={physics:undefined,plist:undefined,pos:undefined,step:undefined,num:undefined,mass:undefined,strength:undefined},e=false;if(arguments.length===0)throw Error("Incorrect Parameters");else if(arguments.length==1){var g=arguments[0],h;for(h in g)d[h]=g[h]}else{d.physics=arguments[0];if(arguments.length==6){d.pos=arguments[1];d.step=arguments[2];d.num=arguments[3];d.mass=arguments[4];d.strength=arguments[5]}else{d.plist=arguments[1];d.strength=arguments[2]}}if(d.num!==
undefined&&d.pos!==undefined&&d.step!==undefined&&d.mass!==undefined)e=true;if(!e&&d.plist===undefined)throw Error("Incorrect Parameters, please supply plist or num, pos, step & mass");this.physics=d.physics;this.links=[];var i;g=0;if(e){var m=d.pos.copy(),n=d.step,o=d.mass,p=n.magnitude();this.particles=[];h=d.strength;for(g=0;g<d.num;g++){e=new toxi.physics2d.VerletParticle2D(m.copy(),o);this.particles.push(e);this.physics.particles.push(e);if(i!==undefined){i=this.createSpring(i,e,p,h);this.links.push(i);
this.physics.addSpring(i)}i=e;m.addSelf(n)}}else{h=d.strength;this.particles=d.plist||[];for(g=0;g<this.particles.length;g++){e=this.particles[g];this.physics.addParticle(e);if(i!==undefined){i=this.createSpring(i,e,i.distanceTo(e),h);this.links.push(i);this.physics.addSpring(i)}i=e}}};
toxi.physics2d.ParticleString2D.prototype={clear:function(){for(var d=0,e=this.links.length;d<e;d++)this.physics.removeSpringElements(s);this.particles.clear();this.links.clear()},createSpring:function(d,e,g,h){return new toxi.physics2d.VerletSpring2D(d,e,g,h)},getHead:function(){return this.particles[0]},getNumParticles:function(){return this.particles.length},getTail:function(){return this.particles[this.particles.length-1]}};
toxi.physics2d.PullBackString2D=function(d,e,g){toxi.physics2d.VerletSpring2D.apply(this,[d,e,0,g]);d.lock();this.setRestLength(0.5)};toxi.extend(toxi.physics2d.PullBackString2D,toxi.physics2d.VerletSpring2D);toxi.physics2d.PullBackString2D.prototype.update=function(d){this.b.distanceToSquared(this.a)>this.restLengthSquared&&this.parent.update.call(this,d)};
toxi.physics2d.VerletConstrainedSpring2D=function(d,e,g,h,i){toxi.physics2d.VerletSpring2D.call(this,d,e,g,h);this.limit=i===undefined?Number.MAX_VALUE:i};toxi.extend(toxi.physics2d.VerletConstrainedSpring2D,toxi.physics2d.VerletSpring2D);
toxi.physics2d.VerletConstrainedSpring2D.update=function(d){var e=this.b.sub(this.a),g=e.magnitude()+toxi.physics2d.VerletSpring2D.EPS;g=(g-this.restLength)/(g*(this.a.invWeight+this.b.invWeight))*this.strength;if(!this.a.isLocked&&!this.isALocked){this.a.addSelf(e.scale(g*this.a.invWeight).limit(this.limit));d&&this.a.applyConstraints()}if(!this.b.isLocked&&!this.isBLocked){this.b.subSelf(e.scale(g*this.b.invWeight).limit(this.limit));d&&this.b.applyConstraints()}};
toxi.physics2d.VerletMinDistanceSpring2D=function(d,e,g,h){toxi.physics2d.VerletSpring2D.call(this,d,e,g,h);this.setRestLength(g)};toxi.extend(toxi.physics2d.VerletMinDistanceSpring2D,toxi.physics2d.VerletSpring2D);toxi.physics2d.VerletMinDistanceSpring2D.prototype.update=function(d){this.b.distanceToSquared(this.a)<this.restLengthSquared&&this.parent.update.call(this,d)};
toxi.physics2d.VerletPhysics2D=function(d){var e={numIterations:50,drag:0,timeStep:1},g;if(arguments.length==1&&(arguments[0].gravity||arguments[0].numIterations||arguments[0].timeStep||arguments[0].drag)){g=arguments[0];if(g.gravity!==undefined)d=g.gravity;if(g.numIterations!==undefined)e.numIterations=g.gravity;if(g.drag!==undefined)e.drag=g.drag;if(g.timeStep!==undefined)e.timeStep=g.timeStep}this.behaviors=[];this.particles=[];this.springs=[];this.numIterations=e.numIterations;this.timeStep=e.timeStep;
this.setDrag(e.drag);if(d)if(d instanceof toxi.physics2d.GravityBehavior)this.addBehavior(d);else d instanceof Object&&d.hasOwnProperty("x")&&d.hasOwnProperty("y")&&this.addBehavior(new toxi.physics2d.GravityBehavior(new toxi.Vec2D(d)))};toxi.physics2d.VerletPhysics2D.addConstraintToAll=function(d,e){for(var g=0;g<e.length;g++)e[g].addConstraint(d)};toxi.physics2d.VerletPhysics2D.removeConstraintFromAll=function(d,e){for(var g=0;g<e.length;g++)e[g].removeConstraint(d)};
toxi.physics2d.VerletPhysics2D.prototype={addBehavior:function(d){d.configure(this.timeStep);this.behaviors.push(d)},addParticle:function(d){this.particles.push(d);return this},addSpring:function(d){this.getSpring(d.a,d.b)===undefined&&this.springs.push(d);return this},clear:function(){this.particles=[];this.springs=[];return this},constrainToBounds:function(){var d,e=0;for(e=0;e<this.particles.length;e++){d=this.particles[e];d.bounds!==undefined&&d.constrain(d.bounds)}if(this.worldBounds!==undefined)for(e=
0;e<this.particles.length;e++){d=this.particles[e];d.constrain(this.worldBounds)}},getCurrentBounds:function(){var d=new toxi.Vec2D(Number.MAX_VALUE,Number.MAX_VALUE),e=new toxi.Vec2D(Number.MIN_VALUE,Number.MIN_VALUE),g=0,h;for(g=0;g<this.particles.length;g++){h=this.particles[g];d.minSelf(h);e.maxSelf(h)}return new toxi.Rect(d,e)},getDrag:function(){return 1-this.drag},getNumIterations:function(){return this.numIterations},getSpring:function(d,e){var g=0;for(g=0;g<this.springs.length;g++){var h=
this.springs[g];if(h.a===d&&h.b===e||h.a===e&&h.b===e)return h}},getTimeStep:function(){return this.timeStep},getWorldBounds:function(){return this.worldBounds},removeBehavior:function(d){return toxi.physics2d.removeItemFrom(d,this.behaviors)},removeParticle:function(d){return toxi.physics2d.removeItemFrom(d,this.particles)},removeSpring:function(d){return toxi.physics2d.removeItemFrom(d,this.springs)},removeSpringElements:function(d){if(this.removeSpring(d)!==undefined)return this.removeParticle(d.a)&&
this.removeParticle(d.b);return false},setDrag:function(d){this.drag=1-d},setNumIterations:function(d){this.numIterations=d},setTimeStep:function(d){this.timeStep=d;var e=0;for(e=0;e<this.behaviors.length;e++)this.behaviors[e].configure(d)},setWorldBounds:function(d){this.worldBounds=d;return this},update:function(){this.updateParticles();this.updateSprings();this.constrainToBounds();return this},updateParticles:function(){var d=0,e=0,g,h;for(d=0;d<this.behaviors.length;d++){g=this.behaviors[d];for(e=
0;e<this.particles.length;e++){h=this.particles[e];g.applyBehavior(h)}}for(e=0;e<this.particles.length;e++){h=this.particles[e];h.scaleVelocity(this.drag);h.update()}},updateSprings:function(){var d=0,e=0;for(d=this.numIterations;d>0;d--)for(e=0;e<this.springs.length;e++)this.springs[e].update(d===1)}};

