I have a the a dotProduct function an a magnitude function. I am struggling to combine these to get the radians between of two vectors.
What I have so far is:
Vector.prototype.angleBetween = function (secondVector) {
var Vec1, Vec2, Vec3, Dot;
Vec1 = this.magnitude();
Vec2 = secondVector.magnitude();
Vec3 = Vec1.getX() * Vec2.getX() + Vec1.getY() * Vec2.getY();
Dot = Vec3 / Vec1 * Vec2;
return new Math.cos(Dot);
};
I know I need to do the Dotproduct of the two vectors / magnitude of vec1 * vec2.
It must pass this jasmine test:
describe("Angle between", function () {
var secondVector, angleBetween;
secondVector = new Vector(-40, 30, 0);
angleBetween = secondVector.angleBetween(vector);
it("Result is PI/2", function () {
expect(angleBetween).toBeCloseTo(Math.PI / 2, 1);
});
});
Where am I going wrong on this?
Working Function:
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);
};