站内搜索:     
站点首页破釜学院网页制作Flash特别应用 → 二维、三维矢量类
正在加载相关信息.....
Web 站内搜索
二维、三维矢量类
】【打印】【加入收藏】【关闭收藏到新浪ViVi】【收藏到365KEY】 浏览字号:
日期:2006-09-01 人气: 出处:闪吧 作者: mcf
 声明:切务用于商业用途,只供学习和参考

二维矢量
//Vector 构造函数
_global.Vector = function(x, y) {
        this.x = x;
        this.y = y;
};
//对矢量重新赋值
Vector.prototype.reset = function(x, y) {
        this.constructor(x, y);
}
//向量相加,原向量改变;
Vector.prototype.plus = function(v) {
        this.x += v.x;
        this.y += v.y;
};
//向量向量相加,返回新向量,原向量不变
Vector.prototype.plusNew = function(v) {
        with (this) {
                return new constructor(x+v.x, y+v.y);
        }
};
//向量相减,原向量改变;
Vector.prototype.minus = function(v) {
        this.x -= v.x;
        this.y -= v.y;
};
//向量向量相减,返回新向量,原向量不变
Vector.prototype.minusNew = function(v) {
        with (this) {
                return new constructor(x-v.x, y-v.y);
        }
};
//向量求逆,返回新向量
Vector.prototype.negateNew = function() {
        with (this) {
                return new constructor(x=-x, y=-y);
        }
};
//向量求逆,改变原向量
Vector.prototype.negate = function() {
        with (this) {
                x = -x;
                y = -y;
        }
};
//向量缩放,改变原向量
Vector.prototype.scale = function(s) {
        this.x *= s;
        this.y *= s;
};
//向量缩放,返回新向量
Vector.prototype.scaleNew = function(s) {
        with (this) {
                return new constructor(x*s, y*s);
        }
};
//向量的长度
Vector.prototype.getLength = function() {
        with (this) {
                return Math.sqrt(x*x+y*y);
        }
};
//设置向量的长度
Vector.prototype.setLength = function(length) {
        var l = this.getLength();
        if (l) {
                this.x *= (length/l);
                this.y *= (length/l);
        } else {
                (this.x=length);
        }
};
//向量的角度
Vector.prototype.getAngle = function() {
        return Math.atan2(this.y, this.x)*180/Math.PI;
};
//角度制▲函数
cosD = function (angle) {
        return Math.cos(angle*Math.PI/180);
};
sinD = function (angle) {
        return Math.sin(angle*Math.PI/180);
};
//向量的旋转,原向量改变
Vector.prototype.rotate = function(angle) {
        var ca = cosD(angle);
        var sa = sinD(angle);
        with (this) {
                var rx = x*ca-y*sa;
                var ry = x*sa+y*ca;
                x = rx;
                y = ry;
        }
};
//向量旋转,返回新向量
Vector.prototype.rotateNew1 = function(angle) {
        var ca = cosD(angle);
        var sa = sinD(angle);
        with (this) {
                var rx = x*ca-y*sa;
                var ry = x*sa+y*ca;
                return new constructor(rx, ry);
        }
}
//向量旋转,返回新向量的第二种方法;
Vector.prototype.rotateNew = function(angle) {
        var v = new this.constructor(this.x, this.y);
        v.rotate(angle);
        return v;
};
//点积
Vector.prototype.dot = function(v) {
        with (this) {
                return x*v.x+y*v.y;
        }
};
//向量夹角
Vector.prototype.angleBetween = function(v) {
        var d = this.dot(v);
        var r1 = this.getLength();
        var r2 = v.getLength();
        return Math.acos(d/(r1*r2))*180/Math.PI;
};





三维矢量

_global.Vector3d = function(x, y, z) {
        this.x = x;
        this.y = y;
        this.z = z;
};
Vector3d.prototype.reset = function(x, y, z) {
        return this.constructor(x, y, z);
};
//向量相加
Vector3d.prototype.plus = function(v) {
        this.x += v.x;
        this.y += v.y;
        this.z += v.z;
};
Vector3d.prototype.plusNew = function(v) {
        with (this) {
                return new constructor(x+v.x, y+v.y, z+v.z);
        }
};
// 向量相减
Vector3d.prototype.minus = function(v) {
        this.x -= v.x;
        this.y -= v.y;
        this.z -= v.z;
};
Vector3d.prototype.minusNew = function(v) {
        with (this) {
                return new constructor(x-v.x, y-v.y, z-v.z);
        }
};
//向量逆转
Vector3d.prototype.negate = function() {
        this.x *= -1;
        this.y *= -1;
        this.z *= -1;
};
Vector.prototype.negateNw = function() {
        wirh(this);
        return new constructor(-x, -y, -z);
};
//向量缩放
Vector3d.prototype.scale = function(s) {
        this.x *= s;
        this.y*s;
        this.z*s;
};
Vector3d.prototype.scaleNew = function(s) {
        with (this) {
                return new constructor(x*s, y*s, z*s);
        }
};
//向量的长度
Vector3d.prototype.getLength = function() {
        with (this) {
                return Math.sqrt(x*x+y*y+z*z);
        }
};
Vector3d.prototype.setLength = function(len) {
        var r = this.getLength();
        if (r) {
                with (this) {
                        return new constructor(x*len/r, y*len/r, z*len/r);
                }
        } else {
                this.x = len;
        }
};
//向量的点积
Vector3d.prototype.dot = function(v) {
        with (this) {
                return x*v.x+y*v.y+z*v.z;
        }
};
//向量的叉积
Vector3d.prototype.cross = function(v) {
        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);
        }
};
//向量的夹角
Vector3d.prototype.angleBetween = function(v) {
        var dp = this.dot(v);
        var r1 = this.getLength();
        var r2 = v.getLength();
        return Math.acos(dp/(r1*r2))*180/Math.PI;
};
Vector3d.prototype.angleBetween1 = function(v) {
        var a = this.cross(v);
        var dp = a.getLength();
        var r1 = this.getLength();
        var r2 = v.getLength();
        return Math.asin(dp/(r1*r2))*180/Math.PI;
};

//透视比例
Vector3d.prototype.getPerspective = function(viewDist) {
        if (viewDiat == undefined) {
                viewDist = 300;
        }
        return viewDist/(viewDist+this.z);
};
//向量的投影到xy平面上
Vector3d.prototype.persProject = function(p) {
        if (p == undefined) {
                p = this.getPerspective();
        }
        this.x *= p;
        this.y *= p;
        this.z = 0;
};
Vector3d.prototype.persProjectNew = function(p) {
        if (p=undefined) {
                p = this.getPerspective();
        }
        with (this) {
                return new constructor(x*p, y*p, 0);
        }
};
//向量的旋转
//绕x轴
Vector3d.prototype.rotateX = function(angle) {
        var ca = Math.cos(angle*Math.PI/180);
        var sa = Math.sin(angle*Math.PI/180);
        with (this) {
                y = y*ca-z*sa;
                z = y*sa+z*ca;
                x = x;
        }
};
Vector3d.prototype.rotateXTrig = function(ca, sa) {
        with (this) {
                y = y*ca-z*sa;
                z = y*sa+z*ca;
                x = x;
        }
};
//绕y轴
Vector3d.prototype.rotateY = function(angle) {
        var ca = Math.cos(angle*Math.PI/180);
        var sa = Math.sin(angle*Math.PI/180);
        with (this) {
                var tempz = z*ca-x*sa;
                var tempx = z*sa+x*ca;
                z = tempz;
                x = tempx;
        }
};
Vector3d.prototype.rotateYTrig = function(ca, sa) {
        with (this) {
                var tempz = z*ca-x*sa;
                var tempx = z*sa+x*ca;
                z = tempz;
                x = tempx;
        }
};
//绕着Z轴转
Vector3d.prototype.rotateZ = function(angle) {
        var ca = Math.cos(angle*Math.PI/180);
        var sa = Math.sin(angle*Math.PI/180);
        with (this) {
                var tempx = x*ca-y*sa;
                var tempy = x*sa+y*ca;
                x = tempx;
                y = tempy;
        }
};
Vector3d.prototype.rotateZTrig = function(ca, sa) {
        with (this) {
                var tempx = x*ca-y*sa;
                var tempy = x*sa+y*ca;
                x = tempx;
                y = tempy;
        }
};
//绕xy旋转
Vector3d.prototype.rotateXY = function(a, b) {
        var sa = Math.sin(a*Math.PI/180);
        var ca = Math.cos(a*Math.PI/180);
        var sb = Math.sin(b*Math.PI/180);
        var cb = Math.cos(b*Math.PI/180);
        with (this) {
                //绕x轴
                var rz = y*sa+z*ca;
                y = y*ca-z*sa;
                //绕y轴转
                z = rz*cb-x*sb;
                x = rz*sb+x*cb;
        }
};
Vector3d.prototype.rotateXYZ = function(a, b, c) {
        var sa = Math.sin(a*Math.PI/180), ca = Math.cos(a*Math.PI/180);
        var sb = Math.sin(b*Math.PI/180), cb = Math.cos(b*Math.PI/180);
        var sc = Math.sin(c*Math.PI/180), cc = Math.cos(c*Math.PI/180);
        with (this) {
                //绕x轴
                var ry = y*ca-z*sa;
                var rz = z*sa+z*ca;
                //绕y轴
                var rx = rz*sb+x*cb;
                z = rz*cb-x*sb;
                //绕z轴
                x = rx*cc-ry*sc;
                y = rx*sc+ry*cc;
        }
};
Vector3d.prototype.rotateXYZTrig = function(ca, sa, cb, sb, cc, sc) {
        with (this) {
                var ry = y*ca-z*sa;
                var rz = y*sa+z*ca;
                var rx = rz*sb+x*cb;
                z = rz*cb-x*sb;
                x = rx*cc-ry*sc;
                y = rx*sc+ry*cc;
        }
};

这一部分和二维的是相似的,就不作太多的文字说明了,如有错漏,望见谅,希望大家都能作出漂亮的flash作品

选自:《Flash Mx编程与创意实现》


>>>> 进入论坛交流 <<<<