// // @auteur : YopSolo // @mail : mail@yopsolo.fr // @site : http://www.yopsolo.fr // @date : 12/2004 // class Vector3D { private var x; private var y; private var z; // -- CONSTRUCTEUR function Vector3D(x:Number, y:Number, z:Number) { this.x = x; this.y = y; this.z = z; } // -- SETTEUR function reset(x:Number, y:Number, z:Number):Void { this.x = x; this.y = y; this.z = z; } // -- DEBUG function toString():String { var rx = Math.round(this.x*1000)/1000; var ry = Math.round(this.y*1000)/1000; var rz = Math.round(this.y*1000)/1000; return "["+rx+","+ry+","+rz+"]"; } // -- CLONAGE function clone():Vector3D { return new Vector3D(this.x, this.y, this.z); } // -- TESTS function egal(v:Vector3D):Boolean { return (this.x == v.x && this.y == v.y && this.z == v.z); } // -- OPERATIONS SUR LES VECTEURS // Addition function plus(v:Vector3D):Void { this.x += v.x; this.y += v.y; this.z += v.z; } function plusN(v:Vector3D):Vector3D { var nX = this.x+v.x; var nY = this.y+v.y; var nZ = this.z+v.z; return new Vector3D(nX, nY, nZ); } // Soustration function moins(v:Vector3D):Void { this.x -= v.x; this.y -= v.y; this.z -= v.z; } function moinsN(v:Vector3D):Vector3D { var nX = this.x-v.x; var nY = this.y-v.y; var nZ = this.z-v.z; return new Vector3D(nX, nY, nZ); } // Produit function produit(v:Vector3D):Number { return this.x*v.x+this.y*v.y+this.z*v.z; } // -- TRANSFORMATIONS function inv() { this.x = -this.x; this.y = -this.y; this.z = -this.z; } function invN(v:Vector3D):Vector3D { var nX = -this.x; var nY = -this.y; var nZ = -this.z; return new Vector3D(nX, nY, nZ); } function scale(s:Number):Void { this.x *= s; this.y *= s; this.z *= s; } function scaleN(s:Number):Vector3D { var nX = this.x*s; var nY = this.y*s; var nZ = this.z*s; return new Vector3D(nX, nY, nZ); } // -- TAILLE function getLen():Number { with (this) { return Math.sqrt(x*x+y*y+z*z); } } function setLen(len:Number):Void { var t = this.getLen(); if (t) { this.scale(len/t); } else { this.x = len; } } // rappel, le resultat est une normale aux 2 vecteurs parents // test avec 2,0,0 cross 0,2,0 = 0,0,4 // test avec 0,2,0 cross 2,0,0 = 0,0,-4 function cross(v:Vector3D):Vector3D { with (this) { var cx = y*vz-z*vy; var cy = z*vx-x*vz; var cz = x*vy-y*vx; return new Vector3D(cx, cy, cz); } } // focale function getPerspective(dist:Number):Number { if (dist == undefined) { dist = 300; } return dist/(this.z+dist); } // projection function prj(p:Number):Void { with (this) { if (p == undefined) { p = getPerspective(); } x *= p; y *= p; z = 0; } } function prjN(p:Number):Vector3D { with (this) { if (p == undefined) { p = getPerspective(); } return new Vector3D(x*p, y*p, 0); } } // -- TRIGO/Vecteur3D // TRANSLATIONS function translate(a:Number, b:Number, c:Number):Void { with (this) { x += a; y += b; z += c; } } function translateN(a:Number, b:Number, c:Number):Vector3D { with (this) { var tx = x+a; var ty = y+b; var tz = z+c; } return new Vector3D(tx, ty, tz); } // ROTATION-X function rotateX(a:Number):Void { var ca:Number = Trigo.cosD(a); var sa:Number = Trigo.sinD(a); with (this) { var tmpY = y*ca-z*sa; var tmpZ = y*sa+z*ca; y = tmpY; z = tmpZ; } } // ROTATION-Y function rotateY(a:Number):Void { var ca:Number = Trigo.cosD(a); var sa:Number = Trigo.sinD(a); with (this) { var tmpX = x*ca+z*sa; var tmpZ = z*ca-x*sa; x = tmpX; z = tmpZ; } } // ROTATIONZ function rotateZ(a:Number):Void { var ca:Number = Trigo.cosD(a); var sa:Number = Trigo.sinD(a); with (this) { var tmpX = x*ca-y*sa; var tmpY = x*sa+y*ca; x = tmpX; y = tmpY; } } // Angle formé par 2 vecteurs, attention au vecteur nul, car cela renverrai un NaN // normal car on cherche l'angle formé entre un point (vecteur nul) et une droite function theta(v:Vector3D):Number { // produit vectoriel var pdt = this.produit(v); // cos de l'angle var ca = pdt/(this.getLen()*v.getLen()); // cos inverse en degres return Trigo.acosD(ca); } }