1

行列をベクトルで乗算する行列オブジェクトがあります。

これは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 = 関数 (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);

};

ベクトルを正しく返すことができないか、乗算が実際には乗算されないようです。

どんな助けでも大歓迎です。

4

0 に答える 0