Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D Euclidean vector rotations

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?

like image 685
deceleratedcaviar Avatar asked Jan 24 '11 08:01

deceleratedcaviar


People also ask

How do you rotate a 2d vector?

Normally rotating vectors involves matrix math, but there's a really simple trick for rotating a 2D vector by 90° clockwise: just multiply the X part of the vector by -1, and then swap X and Y values.

How do you rotate a vector by 45 degrees?

If we represent the point (x,y) by the complex number x+iy, then we can rotate it 45 degrees clockwise simply by multiplying by the complex number (1−i)/√2 and then reading off their x and y coordinates.

What is a 2d rotation?

2D Rotation is a process of rotating an object with respect to an angle in a two dimensional plane. Consider a point object O has to be rotated from one angle to another in a 2D plane. Let- Initial coordinates of the object O = (Xold, Yold)


2 Answers

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

like image 132
Sebastian Paaske Tørholm Avatar answered Nov 04 '22 08:11

Sebastian Paaske Tørholm


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; 
like image 31
Caspar Kleijne Avatar answered Nov 04 '22 08:11

Caspar Kleijne