我必须按照以下方式创建一个Identity矩阵,
“您的Matrix对象应具有不带任何参数的'createIdentity'函数。该函数应返回一个新构造的Matrix对象,该对象的单元格设置为一个单位矩阵。此函数的作用类似于C#中的静态方法。您的JavaScript对象,请不要将函数添加到Matrix原型,而应直接添加到Matrix对象。(例如Matrix.createIdentity = function(){等。)”
到目前为止,我所做的是:
var Matrix = (function () {
function Matrix(pX0, pX1, pX2, pY0, pY1, pY2, pZ0, pZ1, 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 () {
var Test = new Matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
Test = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
];
return new Test();
};
//Matrix.createIdentity = function () {
// this.Matrix = [
// [this.Matrix.setX0(1), this.Matrix.setX1(0), this.Matrix.setX2(0)],
// [this.Matrix.setY0(0), this.Matrix.setY1(1), this.Matrix.setY2(0)],
// [this.Matrix.setZ0(0), this.Matrix.setZ1(0), this.Matrix.setZ2(1)]
// ];
// return new Matrix();
//};
这是它必须通过的测试,该测试在茉莉花中:
describe("Create Identity", function () {
var matrix = Matrix.createIdentity();
it("Element (0,0) Set", function () {
expect(matrix.getElement(0, 0)).toEqual(1);
});
it("Element (0,1) Set", function () {
expect(matrix.getElement(0, 1)).toEqual(0);
});
it("Element (0,2) Set", function () {
expect(matrix.getElement(0, 2)).toEqual(0);
});
it("Element (1,0) Set", function () {
expect(matrix.getElement(1, 0)).toEqual(0);
});
it("Element (1,1) Set", function () {
expect(matrix.getElement(1, 1)).toEqual(1);
});
it("Element (1,2) Set", function () {
expect(matrix.getElement(1, 2)).toEqual(0);
});
it("Element (2,0) Set", function () {
expect(matrix.getElement(2, 0)).toEqual(0);
});
it("Element (2,1) Set", function () {
expect(matrix.getElement(2, 1)).toEqual(0);
});
it("Element (2,2) Set", function () {
expect(matrix.getElement(2, 2)).toEqual(1);
});
});
我看不到我如何不满足给出的描述的要求。我尝试了几种方法通过测试,但我似乎无法通过测试。我究竟做错了什么?
您的功能应该简单地是:
Matrix.createIdentity = function () {
return new Matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
}
现有函数返回一个对象,即Array,该对象不是Matrix
且不具有该getElement
方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句