class Vector { public var x,y,z; // constructor function Vector (x:Number, y:Number, z:Number) { this.x= x; this.y = y; this.z = z; } public function angle () { var angle = Math.atan2 (this.y,this.x); return angle; } public function addV (addition) { var copy=this.copy(); copy.x+=addition.x; copy.y+=addition.y; copy.z+=addition.z; return copy; } public function sub (subtraction) { var x = this.x - subtraction.x; var y = this.y - subtraction.y; var z = this.z - subtraction.z; return new Vector(x,y,z); } public function mult (multiplication) { var x = this.x * multiplication.x; var y = this.y * multiplication.y; var z = this.y * multiplication.z; return new Vector(x,y,z); } public function div (division) { var x = this.x / division.x; var y = this.y / division.y; var z = this.z / division.z; return new Vector(x,y,z); } public function length () { var sumpow2 = Math.pow(Math.abs(this.x),2)+Math.pow (Math.abs(this.y),2); if (this.z) sumpow2 += Math.pow (Math.abs(this.z),2); return Math.sqrt(sumpow2); } public function equals (p) { if (this.x == p.x and this.y == p.y) return true; else return false; } public function round () { var x = Math.round(this.x); var y = Math.round(this.y); return new Vector(x,y); } public function floor () { var x = Math.floor(this.x); var y = Math.floor(this.y); return new Vector(x,y); } public function ceil () { var x = Math.ceil(this.x); var y = Math.ceil(this.y); return new Vector(x,y); } public function normalizeAngle (angle) { // makes sure the angle is between 0 and 6.28 radians while (angle < 0) { angle += Math.PI*2; } if (angle > 0) { angle = angle % (Math.PI*2); } return angle; } public function to2D () { // calculates screen x- and y-values for given 3d-pos var screenWidth = 1000; // determines verdwijnpunt, bigger means further away, size of the screen is a good value //var width = 500; //var height = 500; var eyeheight = 2; var y= this.y; var x0= screenWidth/4; var y0; if (_root.settings.v3d.horizon) y0 = _root.settings.v3d.horizon; else y0 = screenWidth/8; var x2= this.x/y*screenWidth; var y2 = (eyeheight-this.z)/y*screenWidth; // return new 2d-point var newPoint = new Vector ((x0+x2), (y0+y2)); return newPoint; } public function trace () { if (this.z==null) trace ("vector="+this.x+","+this.y); else trace ("vector="+this.x+","+this.y+","+this.z); } public function rotateAroundAxis (angle,axis) { // note: this function has been rewritten // it does not return a vector but changes 'this' itself // this is almost 50% faster // so the other vector-functions should be rewritten too! //var t1=getTimer(); //trace ("test="+c.roundTo(100.45,2)); var newX = this.x, newY = this.y, newZ = this.z; switch (axis) { case "x_pos" : newY = Math.cos(angle) * y - Math.sin(angle)* z; newZ = Math.sin(angle) * y + Math.cos(angle)* z; break; case "z_pos" : newX = Math.cos(angle) * x - Math.sin(angle)* y; newY = Math.sin(angle) * x + Math.cos(angle)* y; break; case "y_pos" : newZ = Math.cos(angle) * z - Math.sin(angle) * x; newX = Math.sin(angle) * z + Math.cos(angle)* x; break; } //_root.stap5+=(getTimer()-t1); return new Vector (newX,newY,newZ); } public function rotate (angle) { //var t1=getTimer(); //_root.stap6+=(getTimer()-t1); if (angle.x!=0) this.rotateAroundAxis (angle.x,"x_pos"); if (angle.y!=0) this.rotateAroundAxis (angle.y,"y_pos"); if (angle.z!=0) this.rotateAroundAxis (angle.z,"z_pos"); } public function copy () { return new Vector (this.x,this.y,this.z); } public function load (v) { this.x=v.x; this.y=v.y; this.z=v.z; } public function toPolar () { var phi,theta var r = this.length(); theta = Math.asin (this.z/r); var rXY = r * Math.cos(theta); phi = Math.acos (this.x/rXY); return new Polar (r,phi,theta); } }