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())); --- the Vector 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
 |  Show 10 more ments

2 Answers 2

Reset to default 5
var 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