C++:围绕某个点旋转向量
我正在尝试围绕向量上的某个点旋转向量(在 C++ 中):
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
围绕点 (1,1)(即5")旋转 90 度将导致:
rotated around the point (1,1) (which is the "5") 90 degrees would result in:
7 4 1
8 5 2
9 6 3
现在我正在使用:
x = (x * cos(90)) - (y * sin(90))
y = (y * cos(90)) + (x * sin(90))
但我不希望它围绕 (0,0) 旋转
But I don't want it rotated around (0,0)
推荐答案
答案取决于您的坐标系.
The answer depends on your coordinate system.
如果您使用计算机图形矢量实现,其中 (0,0)
是左上角,并且您正在围绕点 (dx,dy)
,那么旋转计算,包括转换回原始坐标系,将是:
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))
物理/数学坐标系,(0,0)
位于底部左侧
如果您使用的是更传统的现实世界坐标系,其中(0,0)
是下左角,然后旋转计算,围绕点(dx, dy)
包括转换回原始坐标系,将是:
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
感谢 mmx 对 Pesto 的帖子,以及 SkeletorFromEterenia 以突出显示我的实施中的错误.
Thanks to mmx for their comment on Pesto's post, and to SkeletorFromEterenia for highlighting an error in my implementation.
相关文章