2D Euclidean vector rotations

deceleratedcaviar picture deceleratedcaviar · Jan 24, 2011 · Viewed 96.8k times · Source

I have a euclidean vector a sitting at the coordinates (0, 1). I want to rotate a by 90 degrees (clockwise) around the origin: (0, 0).

If I have a proper understanding of how this should work, the resultant (x, y) coordinates after the rotation should be (1, 0). If I were to rotate it by 45 degrees (still clockwise) instead, I would have expected the resultant coordinates to be (0.707, 0.707).

theta = deg2rad(angle);

cs = cos(theta);
sn = sin(theta);

x = x * cs - y * sn;
y = x * sn + y * cs;

Using the above code, with an angle value of 90.0 degrees, the resultant coordinates are: (-1, 1). And I am so damn confused. The examples seen in the following links represent the same formula shown above surely?

What have I done wrong? Or have I misunderstood how a vector is to be rotated?

Answer

Sebastian Paaske Tørholm picture Sebastian Paaske Tørholm · Jan 24, 2011

Rotating a vector 90 degrees is particularily simple.

(x, y) rotated 90 degrees around (0, 0) is (-y, x).

If you want to rotate clockwise, you simply do it the other way around, getting (y, -x).