Dominic Sore
Dominic Sore

Reputation: 385

Unexpected vector rotation value

So I have a vector (split into two seperate values for learning purposes).

I have it set to to:

var px = 6*( Math.cos( originalAngle * (Math.PI/180)));
var py = 6*( Math.sin( originalAngle * (Math.PI/180)));

Which sets the X coordinate (px) to 6 and sets the Y coordinate (py) to 0.

I then use the rotation matix to try rotate the vector by 90 degrees:

var a = 90*(Math.PI/180);

var nx = px*Math.cos(a) - py * Math.sin(a);
var ny = px*Math.sin(a) + py * Math.cos(a);

I would expect the new coordinates to be X(nx): 0 and Y(ny): 6.

But my actual result is X(nx): 3.6739403974420594e-16 and Y(ny): Y: 6.

So, I'm not sure why X is return that value.

EDIT* My fiddle is here

Upvotes: 0

Views: 68

Answers (1)

WilliamSF
WilliamSF

Reputation: 284

This is a rounding error due to approximations made when computing the cos and sin. 3.6739403974420594e-16 is almost 0 but if you need to obtain exactly 0 you could set a threshold for what is small enough to be zero:

if(nx<1e-10){
    nx=0
}
if(ny<1e-10){
    ny=0
}

Upvotes: 1

Related Questions