通过矩阵javascript 2D

时间:2016-12-11 21:13:12

标签: javascript matrix vector vector-multiplication

我有一个矩阵对象,它应该将矩阵乘以向量。

我有一个向量translationVector这是函数的参数。

我在对象顶部定义了一个矩阵this

这是我的矩阵对象,所讨论的函数位于底部。

/*global it, describe, expect, Vector, Matrix*/
/// <reference path="../js/vector.js"/>
/// <reference path="../js/matrix.js"/>
/// <reference path="../Scripts/jasmine.js"/>

var Matrix = (function () {
function Matrix(pX0, pX1, pX2, pY0, pY1, pY2, pZ0, pZ1, pZ2) {
    this.setX0(pX0);
    this.setX1(pX1);
    this.setX2(pX2);
    this.setY0(pY0);
    this.setY1(pY1);
    this.setY2(pY2);
    this.setZ0(pZ0);
    this.setZ1(pZ1);
    this.setZ2(pZ2);

    this.matrix = [
        [pX0, pX1, pX2],
        [pY0, pY1, pY2],
        [pZ0, pZ1, pZ2]
    ];

}
Matrix.prototype.getX0 = function () {
    return this.mX0;
};
Matrix.prototype.setX0 = function (pX0) {
    this.mX0 = pX0;
};
Matrix.prototype.getX1 = function () {
    return this.mX1;
};
Matrix.prototype.setX1 = function (pX1) {
    this.mX1 = pX1;
};
Matrix.prototype.getX2 = function () {
    return this.mX2;
};
Matrix.prototype.setX2 = function (pX2) {
    this.mX2 = pX2;
};

Matrix.prototype.getY0 = function () {
    return this.mY0;
};
Matrix.prototype.setY0 = function (pY0) {
    this.mY0 = pY0;
};
Matrix.prototype.getY1 = function () {
    return this.mY1;
};
Matrix.prototype.setY1 = function (pY1) {
    this.mY1 = pY1;
};
Matrix.prototype.getY2 = function () {
    return this.mY2;
};
Matrix.prototype.setY2 = function (pY2) {
    this.mY2 = pY2;
};


Matrix.prototype.getZ0 = function () {
    return this.mZ0;
};
Matrix.prototype.setZ0 = function (pZ0) {
    this.mZ0 = pZ0;
};
Matrix.prototype.getZ1 = function () {
    return this.mZ0;
};
Matrix.prototype.setZ1 = function (pZ1) {
    this.mZ1 = pZ1;
};
Matrix.prototype.getZ2 = function () {
    return this.mZ2;
};
Matrix.prototype.setZ2 = function (pZ2) {
    this.mZ2 = pZ2;
};

Matrix.prototype.getElement = function (pRow, pColumn) {
    return this.matrix[pRow][pColumn];
};

Matrix.createIdentity = function () {
    return new Matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
};
Matrix.createTranslation = function (translationVector) {

    return new Matrix(1, 0, translationVector.getX(),
        0, 1, translationVector.getY(), 0, 0, 1);
};
Matrix.createScale = function (scaleVector) {

    return new Matrix(scaleVector.getX(), 0, 0,
        0, scaleVector.getY(), 0, 0, 0, 1);
};
Matrix.createRotation = function (rotation) {

    return new Matrix(Math.cos(rotation), -Math.sin(rotation), 0,
        Math.sin(rotation), Math.cos(rotation), 0, 0, 0, 1);
};

//Matrix.prototype.multiply = function (secMatrix) {

//};

Matrix.multiplyVector = function (translationVector) {

    return new Vector(Matrix.createTranslation(translationVector.getX()));
};

//Matrix.prototype.multiplyVector = function (translationVector) {
//    var vector, secondVector;
//    this.matrix = Matrix.createTranslation(translationVector);
//    secondVector = new Vector(this.multiplyVector(vector));

//    return new Vector(Matrix.createTranslation(secondVector.getX(),
//        secondVector.getY(), secondVector.getZ()));
//};

return Matrix;

}());

我很难得到这个以使矩阵乘以translationVector并返回单个向量。

我得到的错误是undefined is not a contructor。这可能是因为我没有正确应用翻译并返回矢量。

当我取消注释最底层的功能并评论当前正在使用的功能时,我得到undefined is not an object

对于其中任何一种,我不确定如何修复它们。

这些是需要传递的测试:

describe("Multiply vector", function () {
    describe("Translation", function () {
        var vector, translationVector, matrix, secondVector;
        vector = new Vector(30, 40, 1);
        translationVector = new Vector(10, 20, 1);
        matrix = Matrix.createTranslation(translationVector);
        secondVector = matrix.multiplyVector(vector);
        it("X Set", function () {
            expect(secondVector.getX()).toEqual(40);
        });

        it("Y Set", function () {
            expect(secondVector.getY()).toEqual(60);
        });

        it("Z Set", function () {
            expect(secondVector.getZ()).toEqual(1);
        });
    });
    describe("Rotation", function () {
        var vector, rotation, matrix, secondVector;
        vector = new Vector(30, 40, 1);
        rotation = Math.PI / 2;
        matrix = Matrix.createRotation(rotation);
        secondVector = matrix.multiplyVector(vector);
        it("X Set", function () {
            expect(secondVector.getX()).toBeCloseTo(-40, 1);
        });

        it("Y Set", function () {
            expect(secondVector.getY()).toBeCloseTo(30, 1);
        });

        it("Z Set", function () {
            expect(secondVector.getZ()).toBeCloseTo(1, 1);
        });
    });
    describe("Scale", function () {
        var vector, scaleVector, matrix, secondVector;
        vector = new Vector(30, 40, 1);
        scaleVector = new Vector(2, 2, 1);
        matrix = Matrix.createScale(scaleVector);
        secondVector = matrix.multiplyVector(vector);
        it("X Set", function () {
            expect(secondVector.getX()).toEqual(60);
        });

        it("Y Set", function () {
            expect(secondVector.getY()).toEqual(80);
        });

        it("Z Set", function () {
            expect(secondVector.getZ()).toEqual(1);
        });
    });
});

我正确地进行了翻译吗?任何帮助将不胜感激。

矢量对象:

var Vector = (function () {
function Vector(pX, pY, pZ) {
    this.setX(pX);
    this.setY(pY);
    this.setZ(pZ);

}
Vector.prototype.getX = function () {
    return this.mX;
};
Vector.prototype.setX = function (pX) {
    this.mX = pX;
};
Vector.prototype.getY = function () {
    return this.mY;
};
Vector.prototype.setY = function (pY) {
    this.mY = pY;
};
Vector.prototype.getZ = function () {
    return this.mZ;
};
Vector.prototype.setZ = function (pZ) {
    this.mZ = pZ;
};

Vector.prototype.add = function (v) {
    return new Vector(this.getX() + v.getX(), this.getY() + v.getY());
};
Vector.prototype.subtract = function (s) {
    return new Vector(this.getX() - s.getX(), this.getY() - s.getY());
};
Vector.prototype.multiply = function (scalar) {
    return new Vector(this.getX() * scalar, this.getY() * scalar);
};
Vector.prototype.divide = function (scalar) {
    return new Vector(this.getX() / scalar, this.getY() / scalar);
};
Vector.prototype.magnitude = function () {
    return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY());
};
Vector.prototype.normalise = function () {
    return new Vector(this.getX() / this.magnitude(), this.getY() / this.magnitude());
};

Vector.prototype.dotProduct = function (secondVector) {
    var thirdVector;
    thirdVector = new Vector(40, -30, 0);
    // need to make it return a scalar instead of a vector
    return new Vector(this.getX() * secondVector.getX() +
        this.getY() * secondVector.getY(),
        this.getX() * thirdVector.getX() +
        this.getY() * thirdVector.getY()
        );
};
Vector.prototype.interpolate = function (secondVector, interpolation) {
    return new Vector(this.getX() + interpolation *
        (secondVector.getX() - this.getX()),
        (this.getY() + interpolation * (secondVector.getY() - this.getY())));
};
Vector.prototype.rotate = function (rotation) {
    return new Vector((this.getX() * Math.cos(rotation)) -
        (Math.sin(rotation) * this.getY()),
        (Math.cos(rotation) * this.getY()) + (Math.sin(rotation) * this.getX()));
};

Vector.prototype.angleBetween = function (secondVector) {
    var Vec1, Vec2, Vec3, Dot;


    Vec1 = this.magnitude();
    Vec2 = secondVector.magnitude();

    Vec3 = this.getX() * this.getY() + secondVector.getX() * secondVector.getY();

    Dot = this.dotProduct(this) * this.dotProduct(secondVector);

    return Math.acos(Vec3 / Vec1 * Vec2 * Math.PI / 2, 1);
};

Vector.prototype.limitTo = function (Scalar) {

    return this.normalise().multiply(Math.min(this.magnitude(), Scalar));

};
return Vector;

}());

乘法函数现在如何显示:

Matrix.prototype.multiplyVector = function (translationVector) {
    var X, Y, Z, NewVec;

    X = this.getX0() * translationVector.getX() +
        this.getX1() * translationVector.getY() +
        this.getX2() * translationVector.getZ();

    Y = this.getY0() * translationVector.getX() +
        this.getY1() * translationVector.getY() +
        this.getY2() * translationVector.getZ();

    Z = this.getZ0() * translationVector.getX() +
        this.getZ1() * translationVector.getY() +
        this.getZ2() * translationVector.getZ();

    NewVec = new Vector(X, Y, Z);

    return new NewVec(X, Y, Z);

};

修改

完成测试后我可以看到它在secondVector = matrix.multiplyVector(vector);上中断 行:

describe("Multiply vector", function () {
    describe("Translation", function () {
        var vector, translationVector, matrix, secondVector;
        vector = new Vector(30, 40, 1);
        translationVector = new Vector(10, 20, 1);
        matrix = Matrix.createTranslation(translationVector);
        secondVector = matrix.multiplyVector(vector);
        it("X Set", function () {
            expect(secondVector.getX()).toEqual(40);
        });

        it("Y Set", function () {
            expect(secondVector.getY()).toEqual(60);
        });

        it("Z Set", function () {
            expect(secondVector.getZ()).toEqual(1);
        });
    });

我的multiplyVector函数已更新:

Matrix.prototype.multiplyVector = function(translationVector){

  //  var X, matrix, secondVector, vector;
    var X, Y, Z, matrix, secondVector, vector;


    //X = [this.getX0() * translationVector.getX() +
    //    this.getY0() * translationVector.getX() +
    //    this.getZ0() * translationVector.getX()],
    //    [this.getX1() * translationVector.getY() +
    //    this.getY1() * translationVector.getY() +
    //    this.getZ1() * translationVector.getY()],


    //    [this.getX2() * translationVector.getZ() +
    //    this.getY2() * translationVector.getZ() +
    //    this.getZ2() * translationVector.getZ()];



    X = this.getX0() * translationVector.getX() +
        this.getX1() * translationVector.getY() +
        this.getX2() * translationVector.getZ();

    Y = this.getY0() * translationVector.getX() +
        this.getY1() * translationVector.getY() +
        this.getY2() * translationVector.getZ();

    Z = this.getZ0() * translationVector.getX() +
        this.getZ1() * translationVector.getY() +
        this.getZ2() * translationVector.getZ();



    secondVector = new Vector(0, 0, 0);


    matrix = Matrix.createTranslation(translationVector);
    secondVector = matrix.multiplyVector(vector);

    //   NewVec = Matrix.multiplyVector(traslationVector);


  //  NewVec = new Vector(X, Y, Z);

    return new Vector(secondVector);

};

我似乎无法正确返回一个向量,或者乘法实际上并没有实现乘法。

任何帮助都将不胜感激。

0 个答案:

没有答案