C++:围绕某个点旋转向量 [英] C++: Rotating a vector around a certain point

查看:24
本文介绍了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

感谢 mmxPesto 的帖子,以及 SkeletorFromEterenia 以突出显示我的实施中的错误.

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

这篇关于C++:围绕某个点旋转向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆