as笔记。oop与算法。---5

      闪了一下 2004-7-13 18:39
/*///////vector3d方法列表/////
1.toString()
2.reset(x,y,z)//重新赋值例如:myv.reset(2,3,-1);
3.getclone()//复制.例如myv2=myv1.getclone();
4.eqV(v)//是否相等例如if(v1.eqv(v2)){};
5.addV(v)和addVNew(v),minusV(v),minusVNew(v);//加法和减法v1.addV(v2);v3=v1.addVNew(v2);
6. scaleV(n)和scaleVNew(n)//伸长.见上
7.getLength()和setLength(n)//a=v1.getLength();v1.setLength(30);
8.dot(v)//内积x=v1.dot(v2)判断垂直if(v1.dot(v2)==0)
9.angleBetween(v)//得到夹角例如x=v1.angleBetween(v2);

以下是3d特有的.

10.cross(v)//叉乘积,返回得到的与两向量垂直的向量v3=v1.cross(v2);
11.getPers(n),pers(n)和persNew(n)//得到透视比例.透视.和返回透视向量.
注意n都是眼睛与屏幕的距离.一般正数.默认300

以下是有关旋转的.全部以rot开头.后面的是所绕轴名.最后有cs的是接受余正弦参数的
12.rotX(n),rotY(n),rotZ(n),rotXY(n),rotXYZ(n)和rotXCS(n),rotYCS(n),rotZCS(n),rotXYCS(n),rotXYZCS(n)
*/
class vector3d {
var x:Number;
var y:Number;
var z:Number;
// 构造函数
function vector3d(getx:Number, gety:Number, getz:Number) {
x = getx;
y = gety;
z = getz;
}
// 用于返回属性值的方法
function toString():String {
return ("["+x+","+y+","+z+"]");
}
//改变属性值的方法.
function reset(getx:Number, gety:Number, getz:Number):Void {
x = getx;
y = gety;
z = getz;
}
//克隆向量.与mx不同//!!!!!!
function getclone():vector3d {
return new vector3d(x, y, z);
}
//比较相等否.
function eqV(getVector:vector3d):Boolean {
return (x == getVector.x && y == getVector.y && z == getVector.z);
}
//向量加法.
function addV(getVector:vector3d):Void {
x += getVector.x;
y += getVector.y;
z += getVector.z;
}
//向量加法得到新向量.
function addVNew(getVector:vector3d):vector3d {
return new vector3d(x+getVector.x, y+getVector.y, z+getVector.z);
}
//向量减法暂时不写.应该可以替代.留空.用缩放系数-1替代.因为flash加法并不快.
//例如v1-v2写为v1.addV(v2.scaleNew(-1))//果然麻烦.还是写一个.
//向量减法
function minusV(getVector:vector3d):Void {
x -= getVector.x;
y -= getVector.y;
z -= getVector.z;
}
//向量减法得到新向量.
function minusVNew(getVector:vector3d):vector3d {
return new vector3d(x-getVector.x, y-getVector.y, z-getVector.z);
}
//向量缩放
function scaleV(n:Number):Void {
x *= n;
y *= n;
z *= n;
}
//向量缩放得到新向量.
function scaleVNew(n:Number):vector3d {
return new vector3d(x*n, y*n, z*n);
}
//得到向量的长度
function getLength():Number {
return Math.sqrt(x*x+y*y+z*z);
}
//设置向量长度
function setLength(len:Number):Void {
var r = this.getLength();
if (r) {
this.scaleV(len/r);
} else {
this.x = len;
}
}
/*下面一部分是2维的代码3维代码不能简单更改得到.

//得到向量角度
function getAngle():Number {
return (180/Math.PI)*Math.atan2(y, x);
}
//设置向量角度
function setAngle(angle:Number):Void {
var r = this.getLength();
var tem = angle*(Math.PI/180);
x = r*Math.cos(tem);
y = r*Math.sin(tem);
}
//旋转.
function rotate(angle:Number):Void {
var r = this.getLength();
var tem = angle*(Math.PI/180);
var ca = Math.cos(tem);
var sa = Math.sin(tem);
var rx = x*ca-y*sa;
var ry = x*sa+y*ca;
x = rx;
y = ry;
}
//旋转并得到新向量.
function rotateNew(angle:Number):vector {
var v = new vector(x, y);
v.rotate(angle);
return v;
}

*/
//内积
function dot(v:vector3d):Number {
return x*v.x+y*v.y+z*v.z;
}
//法向量计算省略.3d的不要法向量.
//垂直验证省略.用(v1.dot(v2)==0)判断垂直.
//向量夹角.得到向量夹角绝对值.
function angleBetween(v:vector3d):Number {
var tem = this.dot(v);
return (180/Math.PI)*Math.acos(tem/(this.getLength()*v.getLength()));
}
//叉积
function cross(v:vector3d):vector3d {
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 vector3d(cx, cy, cz);
}
//得到透视比例
function getPers(n:Number):Number {
if (n == undefined) {
n = 300;
}
//默认值为300.以后搞平行投影默认是0
return n/(n+z);
}
//得到投影
function pers(p:Number):Void {
if (p == undefined) {
p = getPers();
}
x *= p;
y *= p;
z = 0;
}
//将投影作为新对象返回.
function persNew(p:Number):vector3d {
if (p == undefined) {
p = getPers();
}
return new vector3d(x*p, y*p, 0);
}
///////////后面部分有关旋转/////////////////
//x轴角度例如:rotX(90)//旋转90度
function rotX(n:Number):Void {
var tem = Math.PI/180*n;
var ca = Math.cos(tem);
var sa = Math.sin(tem);
var temY = y*ca-z*sa;
var temZ = y*sa+z*ca;
y = temY;
z = temZ;
}
//x轴正余弦例如rotXCS(Math.cos(90),Math.sin(90))CS的意思是余弦在前.
function rotXCS(ca:Number, sa:Number):Void {
var temY = y*ca-z*sa;
var temZ = y*sa+z*ca;
y = temY;
z = temZ;
}
//y轴角度例如:rotY(90)//旋转90度
function rotY(n:Number):Void {
var tem = Math.PI/180*n;
var ca = Math.cos(tem);
var sa = Math.sin(tem);
var temX = x*ca-z*sa;
var temZ = x*-sa+z*ca;
x = temX;
z = temZ;
}
//y轴正余弦例如rotYCS(Math.cos(90),Math.sin(90))CS的意思是余弦在前.
function rotYCS(ca:Number, sa:Number):Void {
var temX = y*ca-z*sa;
var temZ = y*-sa-z*ca;
x = temX;
z = temZ;
}
//z轴角度例如:rotz(90)//旋转90度
function rotZ(n:Number):Void {
var tem = Math.PI/180*n;
var ca = Math.cos(tem);
var sa = Math.sin(tem);
var temX = x*ca-y*sa;
var temY = x*sa+y*ca;
x = temX;
y = temY;
}
//y轴正余弦例如rotYCS(Math.cos(90),Math.sin(90))CS的意思是余弦在前.
function rotZCS(ca:Number, sa:Number):Void {
var temX = x*ca-y*sa;
var temy = x*sa+y*ca;
x = temX;
y = temy;
}
//绕xy旋转
function rotXY(a:Number, b:Number):Void {
var tema = (Math.PI/180)*a;
var temb = (Math.PI/180)*b;
var ca = Math.cos(tema);
var sa = Math.sin(tema);
var cb = Math.cos(temb);
var sb = Math.sin(temb);
//绕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 rotXYCS(ca:Number, sa:Number, cb:Number, sb:Number):Void {
//绕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 rotXYZ(a:Number, b:Number, c:Number):Void {
var tem = Math.PI/180;
var tema = tem*a;
var temb = tem*b;
var temc = tem*c;
var ca = Math.cos(tema);
var sa = Math.sin(tema);
var cb = Math.cos(temb);
var sb = Math.sin(temb);
var cc = Math.cos(temc);
var sc = Math.sin(temc);
//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 rotXYZCS(ca:Number, sa:Number, cb:Number, sb:Number, cc:Number, sc:Number):Void {
//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;
}
}

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}
}