C++: Rotating a vector around a certain point

2019-01-12 03:00发布

问题:

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)

回答1:

As Mehrdad Afshari commented on Pesto's post, including the translation back into the original coordinate system would be:

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


回答2:

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]


回答3:

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.



回答4:

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



回答5:

I found the answer from Marc Booth to be wrong (rotate (0,1,0) by 0 degrees and you get (0,-1,0) with his formula), and I ended up with:

double cs = cos_deg(new_degrees);
double sn = sin_deg(new_degrees);

double translated_x = x - x_origin;
double translated_y = y - y_origin;

double result_x = translated_x * cs - translated_y * sn;
double result_y = translated_x * sn + translated_y * cs;

result_x += x_origin;
result_y += y_origin;

This can be further simplified of course, but I want to make it as simple as possible.