admin管理员组文章数量:1302869
I am currently trying to pass some vector tests for a module in 2D graphics I am doing.
I am currently struggling with the following test:
Normalise function – your Vector object should have a ‘normalise’ function that takes no parameters. The function should return a newly constructed Vector object that is the result of normalising the ‘this’ Vector (making a unit Vector from it).
This is what I have done so far:
Normalise function – your Vector object should have a ‘normalise’ function that takes no parameters. The function should return a newly constructed Vector object that is the result of normalising the ‘this’ Vector (making a unit Vector from it).
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(), this.getZ() + v.getZ());
}
Vector.prototype.subtract = function (v) {
return new Vector(this.getX() - v.getX(), this.getY() - v.getY(), this.getZ() - v.getZ());
}
Vector.prototype.multiply = function (scalar) {
return new Vector(this.getX() * scalar, this.getY() * scalar, this.getZ() * scalar);
};
Vector.prototype.divide = function (scalar) {
return new Vector(this.getX() / scalar, this.getY() / scalar, this.getZ() /scalar);
};
Vector.prototype.magnitude = function () {
return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY() + this.getZ() + this.getZ())
}
//this is the vector I have tried for the normalisation
Vector.prototype.normalisedVector = function () {
var vec = new Vector(this.getX(), this.getY(), this.getZ());
return new Vector(vec.divide(this.magnitude()));
}
return Vector;
}());
However what I have tried isn't working and I really don't get why. Any help would be greatly appreciated! Thank you!
EDIT - just reading through it again, I don't think I am returning a unit vector like it asks. Does anyone know how I would go about doing this?
I am currently trying to pass some vector tests for a module in 2D graphics I am doing.
I am currently struggling with the following test:
Normalise function – your Vector object should have a ‘normalise’ function that takes no parameters. The function should return a newly constructed Vector object that is the result of normalising the ‘this’ Vector (making a unit Vector from it).
This is what I have done so far:
Normalise function – your Vector object should have a ‘normalise’ function that takes no parameters. The function should return a newly constructed Vector object that is the result of normalising the ‘this’ Vector (making a unit Vector from it).
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(), this.getZ() + v.getZ());
}
Vector.prototype.subtract = function (v) {
return new Vector(this.getX() - v.getX(), this.getY() - v.getY(), this.getZ() - v.getZ());
}
Vector.prototype.multiply = function (scalar) {
return new Vector(this.getX() * scalar, this.getY() * scalar, this.getZ() * scalar);
};
Vector.prototype.divide = function (scalar) {
return new Vector(this.getX() / scalar, this.getY() / scalar, this.getZ() /scalar);
};
Vector.prototype.magnitude = function () {
return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY() + this.getZ() + this.getZ())
}
//this is the vector I have tried for the normalisation
Vector.prototype.normalisedVector = function () {
var vec = new Vector(this.getX(), this.getY(), this.getZ());
return new Vector(vec.divide(this.magnitude()));
}
return Vector;
}());
However what I have tried isn't working and I really don't get why. Any help would be greatly appreciated! Thank you!
EDIT - just reading through it again, I don't think I am returning a unit vector like it asks. Does anyone know how I would go about doing this?
Share Improve this question edited Nov 15, 2016 at 1:22 lucycopp asked Nov 15, 2016 at 1:13 lucycopplucycopp 2131 gold badge5 silver badges18 bronze badges 15-
new Vector(vec.divide(this.magnitude()));
--- theVector
constructor accepts 3 arguments. And you're passing just one.return this.divide(this.magnitude())
should just work. – zerkms Commented Nov 15, 2016 at 1:14 - "Does anyone know how I would go about doing this?" --- you were given at least 3 answers: one above from me and 2 below. – zerkms Commented Nov 15, 2016 at 1:24
- I mean to return it as a unit vector – lucycopp Commented Nov 15, 2016 at 1:25
- The answers below just return it as a dimensional vector, i need a unit vector, I think it would be easier for me to delete this question it's getting a bit to confusing – lucycopp Commented Nov 15, 2016 at 1:26
- "The answers below just return it as a dimensional vector, i need a unit vector," --- the answers below surely return a unit vector. – zerkms Commented Nov 15, 2016 at 1:27
2 Answers
Reset to default 5var vec = new Vector(this.getX(), this.getY(), this.getZ());
return new Vector(vec.divide(this.magnitude()));
should be
return this.divide(this.magnitude());
new Vector()
requires 3 arguments, X
, Y
, and Z
. So you should divide each coordinate of the current vector by this.magnitude()
and use those as the arguments.
Vector.prototype.normalizeVector = function() {
var mag = this.magnitude();
return new Vector(this.mX/mag, this.mY/mag, this.mZ/mag);
};
本文标签: Normalise a vector JavaScriptStack Overflow
版权声明:本文标题:Normalise a vector JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741717606a2394206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论