//原作者:Robert Penner
//AS2.0改写:wiyiflash
//欢迎访问:http://www.wiyiflash.com/bbs
//-------------------------------------------------------
//Vector3D类提供了Flash里的3D空间内的矢量运算功能
//AS2.0改写:wiyiflash
//欢迎访问:http://www.wiyiflash.com/bbs
//-------------------------------------------------------
//Vector3D类提供了Flash里的3D空间内的矢量运算功能
import ExtMath; class Vector3D { var x:Number; var y:Number; var z:Number; // 构造函数 public function Vector3D(x:Number, y:Number, z:Number) { this.x = x; this.y = y; this.z = z; } // 用于返回属性值的方法 public function toString():String { return ("["+x+","+y+","+z+"]"); } //改变属性值的方法 public function reset(x:Number, y:Number, z:Number):Void { with (this) { __constructor__(x, y, z); } } //克隆向量.与mx不同//!!!!!! public function getClone():Vector3D { with (this) { return new __constructor__(x, y, z); } } //比较相等否 public function equals(v:Vector3D):Boolean { return (x == v.x && y == v.y && z == v.z); } //向量加法 public function plus(v:Vector3D):Void { x += v.x; y += v.y; z += v.z; } //向量加法得到新向量 public function plusNew(v:Vector3D):Vector3D { with (this) { return new __constructor__(x+v.x, y+v.y, z+v.z); } } //向量减法 public function minus(v:Vector3D):Void { x -= v.x; y -= v.y; z -= v.z; } //向量减法得到新向量 public function minusNew(v:Vector3D):Vector3D { with (this) { return new Vector3D(x-v.x, y-v.y, z-v.z); } } //向量缩放 public function scale(n:Number):Void { x *= n; y *= n; z *= n; } //向量缩放得到新向量 public function scaleNew(n:Number):Vector3D { with (this) { return new __constructor__(x*n, y*n, z*n); } } //得到向量的长度 public function getLength():Number { return Math.sqrt(x*x+y*y+z*z); } //设置向量长度 public function setLength(len:Number):Void { var r = this.getLength(); if (r) { scale(len/r); } else { x = len; } } //点积 public function dot(v:Vector3D):Number { return x*v.x+y*v.y+z*v.z; } //叉积 public function cross(v:Vector3D):Vector3D { with (this) { var cx = y*v.z-z*v.y; var cy = z*v.x-x*v.z; var cz = x*v.y-y*v.x; return new __constructor__(cx, cy, cz); } } public function angleBetween(v:Vector3D):Number { var dp = dot(v); var cosAngle = dp/(getLength()*v.getLength()); return ExtMath.acosD(cosAngle); } //透视比例 public function getPerspective(viewDist:Number):Number { if (viewDist == undefined) { viewDist = 300; } if (viewDist == Infinity) { return 1; } return viewDist/(viewDist+z); } //投影 function persProject(p:Number) { with (this) { if (p == undefined) { p = getPerspective(); } x *= p; y *= p; z = 0; } } //将投影作为新对象返回 function persProjectNew(p:Number) { with (this) { if (p == undefined) { p = getPerspective(); } return new __constructor__(p*x, p*y, 0); } } //rotate 相关 public function rotateX(angle:Number):Void { with (ExtMath) { var ca = cosD(angle); var sa = sinD(angle); } with (this) { var tempY = y*ca-z*sa; var tempZ = y*sa+z*ca; y = tempY; z = tempZ; } } function rotateXTrig(ca:Number, sa:Number):Void { with (this) { var tempY = y*ca-z*sa; var tempZ = y*sa+z*ca; y = tempY; z = tempZ; } } function rotateY(angle:Number):Void { with (ExtMath) { var ca = cosD(angle); var sa = sinD(angle); } with (this) { var tempX = x*ca+z*sa; var tempZ = x*-sa+z*ca; x = tempX; z = tempZ; } } function rotateYTrig(ca:Number, sa:Number):Void { with (this) { var tempX = x*ca+z*sa; var tempZ = x*-sa+z*ca; x = tempX; z = tempZ; } } function rotateZ(angle:Number):Void { with (ExtMath) { var ca = cosD(angle); var sa = sinD(angle); } with (this) { var tempX = x*ca-y*sa; var tempY = x*sa+y*ca; x = tempX; y = tempY; } } function rotateZTrig(ca:Number, sa:Number):Void { with (this) { var tempX = x*ca-y*sa; var tempY = x*sa+y*ca; x = tempX; y = tempY; } } //绕xy旋转 function rotateXY(a:Number, b:Number):Void { with (ExtMath) { var ca = cosD(a); var sa = sinD(a); var cb = cosD(b); var sb = sinD(b); } with (this) { //绕x var rz = y*sa+z*ca; y = y*ca-z*sa; //绕y z = x*-sb+rz*cb; x = x*cb+rz*sb; } } //接受四个参数的绕xy旋转先x后y.先c后s. function rotateXYTrig(ca:Number, sa:Number, cb:Number, sb:Number):Void { with (this) { //绕x var rz = y*sa+z*ca; y = y*ca-z*sa; //绕y z = x*-sb+rz*cb; x = x*cb+rz*sb; } } //绕xyz轴旋转 function rotateXYZ(a:Number, b:Number, c:Number):Void { with (ExtMath) { var ca = cosD(a); var sa = sinD(a); var cb = cosD(b); var sb = sinD(b); var cc = cosD(c); var sc = sinD(c); } with (this) { //x轴 var ry = y*ca-z*sa; var rz = y*sa+z*ca; //y轴 var rx = x*cb+rz*sb; z = x*-sb+rz*cb; //z轴 x = rx*cc-ry*sc; y = rx*sc+ry*cc; } } function rotateXYZTrig(ca:Number, sa:Number, cb:Number, sb:Number, cc:Number, sc:Number):Void { with (this) { //x轴 var ry = y*ca-z*sa; var rz = y*sa+z*ca; //y轴 var rx = x*cb+rz*sb; z = x*-sb+rz*cb; //z轴 x = rx*cc-ry*sc; y = rx*sc+ry*cc; } } } |
[From 维艺--Flash论坛]