C++: Rotating a vector around a certain point

The answer depends on your coordinate system.

Computer graphics coordinate system, with (0,0) at Top left

If you are using a computer graphics vector implementation where (0,0) is the top left corner and you are rotating around the point (dx, dy), then the rotation calculation, including the translation back into the original coordinate system, would be:

x_rotated =      ((x - dx) * cos(angle)) - ((dy - y) * sin(angle)) + dx
y_rotated = dy - ((dy - y) * cos(angle)) + ((x - dx) * sin(angle))

Physics/Maths coordinate system, with (0,0) at Bottom left

If you are using a more traditional real world coordinate system, where (0,0) is the bottom left corner, then the rotation calculation, around the point (dx, dy) including the translation back into the original coordinate system, would be:

x_rotated = ((x - dx) * cos(angle)) - ((y - dy) * sin(angle)) + dx
y_rotated = ((x - dx) * sin(angle)) + ((y - dy) * cos(angle)) + dy

Thanks to mmx for their comment on Pesto‘s post, and to SkeletorFromEterenia for highlighting an error in my implementation.

Leave a Comment