C++:围绕某个点旋转向量
我试图围绕向量上的某个点旋转向量(在 C++ 中):
1 2 3
4 5 6
7 8 9
围绕点 (1,1) (即“5”)旋转 90 度会导致:
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) 旋转
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
答案取决于您的坐标系。
计算机图形坐标系,
(0,0)
位于顶部左侧如果您使用的是计算机图形矢量实现,其中
(0,0)
是左上角,并且您围绕点(dx, dy)
旋转,那么旋转计算(包括平移回原始坐标系)将是:物理/数学坐标系,
(0,0)
位于底部左侧如果您使用的是更传统的现实世界坐标系,其中
(0,0)
是左下角,那么围绕点(dx, dy)
的旋转计算(包括平移回原始坐标系)将是:感谢 mmx 对 Pesto 的帖子,以及 SkeletorFromEterenia 突出显示我的实现中的错误。
The answer depends on your coordinate system.
Computer graphics coordinate system, with
(0,0)
at Top leftIf 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:Physics/Maths coordinate system, with
(0,0)
at Bottom leftIf 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:Thanks to mmx for their comment on Pesto's post, and to SkeletorFromEterenia for highlighting an error in my implementation.
解决方法是将矢量平移到旋转中心为(0,0)的坐标系。 应用旋转矩阵并将矢量平移回原始坐标系。
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.
假设您使用的是标准向量实现,其中 (0,0) 为左上角,并且您围绕点 (x_origin, y_origin) 旋转,则应该这样做:
请注意,y 是 y_origin - y 因为 y 值随着下降而增加。
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:
Note that the y's are
y_origin - y
because the y value increases as you go down.我发现 Mark Booth 的答案是错误的(将 (0,1,0) 旋转 0 度,用他的公式得到 (0,-1,0)),我最终得到:
这可以进一步缩短当然可以,但我想让它尽可能简单。
I found the answer from Mark 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:
This can be further shortened of course, but I want to make it as simple as possible.
您将需要使用平移矩阵来围绕不同点移动旋转。
You will need to use a translation matrix to move rotate about a different point.