59

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?

hiddensunset4
  • 5,825
  • 3
  • 39
  • 61

5 Answers5

111

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).

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
  • 47
    +1. To anyone rotating a 2D vector for a computer screen: this answer assumes the y axis is pointing up as in math. If it is points down as on computer screens, then clockwise and counterclockwise are reversed. `(-y, x)` is clockwise and `(y, -x)` is counterclockwise. – Jordan Miner Jun 18 '16 at 21:05
92

you should remove the vars from the function:

x = x * cs - y * sn; // now x is something different than original vector x
y = x * sn + y * cs;

create new coordinates becomes, to avoid calculation of x before it reaches the second line:

px = x * cs - y * sn; 
py = x * sn + y * cs;
Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
  • 4
    Oh god, I needed fresh eyes... again something so obvious... Thanks mate (works a beaut, 2 hours later... haha) – hiddensunset4 Jan 24 '11 at 08:59
  • when you execute x = x * cs - y * sn;, it gives a different value to x in y = x * sn + y * cs, so the x will "derail" – Caspar Kleijne Jan 24 '11 at 08:59
  • 2
    @Daniel: The x in the second statement had had its value changed by the time you used it to calculate the value for y. So, essentially, you calculated the x coordinate for rotating (0,1) (which is -1). Then you stored this in the x coordinate giving (-1,1) and then you calculated the y coordinate for rotating (-1,1) (which should actually be -1, so I'm not sure how you got (-1,1) rather than (-1,-1) ). The correct answer, by the way, isn't (1,0), it's (-1,0) since rotation by positive angles is counterclockwise when view from above. – Keith Irwin Jan 24 '11 at 09:05
  • 2
    This YouTube [series](https://www.youtube.com/watch?v=kjBOesZCoqc&t=8s) will give you a deep an intuitive understanding for rotation/change of basis! – CPayne Nov 07 '17 at 04:53
25

Rotate by 90 degress around 0,0:

x' = -y
y' = x

Rotate by 90 degress around px,py:

x' = -(y - py) + px
y' = (x - px) + py
Altivo
  • 490
  • 6
  • 13
7

Sounds easier to do with the standard classes:

std::complex<double> vecA(0,1);
std::complex<double> i(0,1); // 90 degrees
std::complex<double> r45(sqrt(2.0),sqrt(2.0));
vecA *= i;
vecA *= r45;

Vector rotation is a subset of complex multiplication. To rotate over an angle alpha, you multiply by std::complex<double> { cos(alpha), sin(alpha) }

MSalters
  • 173,980
  • 10
  • 155
  • 350
5

You're calculating the y-part of your new coordinate based on the 'new' x-part of the new coordinate. Basically this means your calculating the new output in terms of the new output...

Try to rewrite in terms of input and output:

vector2<double> multiply( vector2<double> input, double cs, double sn ) {
  vector2<double> result;
  result.x = input.x * cs - input.y * sn;
  result.y = input.x * sn + input.y * cs;
  return result;
}

Then you can do this:

vector2<double> input(0,1);
vector2<double> transformed = multiply( input, cs, sn );

Note how choosing proper names for your variables can avoid this problem alltogether!

xtofl
  • 40,723
  • 12
  • 105
  • 192