我目前正在尝试通过我正在做的 2D 图形模块的一些矢量测试。
\n\n我目前正在努力进行以下测试:
\n\n标准化函数\xe2\x80\x93 您的 Vector 对象应该有一个 \xe2\x80\x98normalise\xe2\x80\x99 函数,该函数不带参数。该函数应返回一个新构造的 Vector 对象,该对象是 \n标准化 \xe2\x80\x98this\xe2\x80\x99 Vector 的结果(从中生成单位 Vector)。
\n\n这是我到目前为止所做的:
\n\n标准化函数 \xe2\x80\x93 您的 Vector 对象应该有一个 \xe2\x80\x98normalise\xe2\x80\x99 函数,该函数不带参数。该函数应返回一个新构造的 Vector 对象,该对象是 \n标准化 \xe2\x80\x98this\xe2\x80\x99 Vector 的结果(从中生成单位 Vector)。
\n\n var Vector = (function () {\nfunction Vector(pX, pY, pZ) {\n this.setX(pX);\n this.setY(pY);\n this.setZ(pZ);\n\n}\nVector.prototype.getX = function () {\n return this.mX;\n};\nVector.prototype.setX = function (pX) {\n this.mX = pX;\n};\nVector.prototype.getY = function () {\n return this.mY;\n};\nVector.prototype.setY = function (pY) {\n this.mY = pY;\n}\nVector.prototype.getZ = function () {\n return this.mZ;\n}\nVector.prototype.setZ = function (pZ) {\n this.mZ = pZ;\n}\n\nVector.prototype.add = function (v) {\n return new Vector(this.getX() + v.getX(), this.getY() + v.getY(), this.getZ() + v.getZ());\n}\n\nVector.prototype.subtract = function (v) {\n return new Vector(this.getX() - v.getX(), this.getY() - v.getY(), this.getZ() - v.getZ());\n}\n\nVector.prototype.multiply = function (scalar) {\n return new Vector(this.getX() * scalar, this.getY() * scalar, this.getZ() * scalar);\n};\n\nVector.prototype.divide = function (scalar) {\n return new Vector(this.getX() / scalar, this.getY() / scalar, this.getZ() /scalar);\n};\n\nVector.prototype.magnitude = function () {\n return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY() + this.getZ() + this.getZ())\n}\n\n\n //this is the vector I have tried for the normalisation\nVector.prototype.normalisedVector = function () {\n var vec = new Vector(this.getX(), this.getY(), this.getZ());\n return new Vector(vec.divide(this.magnitude()));\n}\nreturn Vector;\n }());\nRun Code Online (Sandbox Code Playgroud)\n\n然而我所尝试的方法不起作用,我真的不明白为什么。任何帮助将不胜感激!\n谢谢!
\n\n编辑 - 只是再次阅读它,我不认为我会像它所要求的那样返回单位向量。有谁知道我将如何去做这件事?
\nvar vec = new Vector(this.getX(), this.getY(), this.getZ());
return new Vector(vec.divide(this.magnitude()));
Run Code Online (Sandbox Code Playgroud)
应该
return this.divide(this.magnitude());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11625 次 |
| 最近记录: |