日期:2013-12-01  浏览次数:20595 次

  声明:切务用于商业用途,只供学习和参考

二维矢量
//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);
            &nbs