views:

2887

answers:

6

Hi All,

I have a very simple question.

I am trying to rotate a vector around a certain point on the vector(in C++):

1 2 3
4 5 6
7 8 9

rotated around the point (1,1) (which is the "5") 90 degrees would result in:

7 4 1
8 5 2
9 6 3

Right now I am using:

x = (x * cos(90)) - (y * sin(90))
y =(y * cos(90)) + (x * sin(90))

But I don't want it rotated around (0,0)

Thanks!

+3  A: 

The solution is to translate the vector to a coordinate system in which the center of rotation is (0,0). Apply the rotation matrix and translate the vector back to the original coordinate system.

dx = x of rotation center
dy = y of rotation center

V2 = V - [dx, dy, 0]
V3 = V2 * rotation matrix
Result = V3 + [dx, dy, 0]

Mehrdad Afshari
A: 

You will need to use a translation matrix to move rotate about a different point.

Ben S
http://mathworld.wolfram.com/RotationMatrix.html
Can Berk Güder
+2  A: 

Assuming you're using a standard vector implementation where (0,0) would be the top left corner and you're rotating around the point (x_origin, y_origin), this should do it:

x = ((x - x_origin) * cos(angle)) - ((y_origin - y) * sin(angle))
y = ((y_origin - y) * cos(angle)) - ((x - x_origin) * sin(angle))

Note that the y's are y_origin - y because the y value increases as you go down.

Pesto
You'd need to translate it back after the transform. The resulting (x, y) is not in the original coordinate system.
Mehrdad Afshari
A: 

Done.

Thanks for the help. Here is the code:

ja = 0-(TRANSLATION_SIZE-i)*SIN90;
ia = (j-TRANSLATION_SIZE)*SIN90;

_net.AddArc(networkNodes[slice-1][j][i],networkNodes[slice][ja+TRANSLATION_SIZE][(-ia)+TRANSLATION_SIZE]);
Erik Ahlswede
+1  A: 

As the commenter to Pesto suggested, including the translation back into the original coordinate system would be:

x = x_origin +
    ((x - x_origin) * cos(angle)) - ((y_origin - y) * sin(angle))
y = y_origin +  
    ((y_origin - y) * cos(angle)) - ((x - x_origin) * sin(angle))
Mark Booth
A: 

ja = 0-(TRANSLATION_SIZE-i)*SIN90; ia = (j-TRANSLATION_SIZE)*SIN90;

_net.AddArc(networkNodes[slice-1][j][i],networkNodes[slice][ja+TRANSLATION_SIZE][(-ia)+TRANSLATION_SIZE]);


Should this be standard C++? I'd need such for all platforms.

Mike