C++:围绕某个点旋转向量

发布于 2024-07-14 20:23:11 字数 294 浏览 5 评论 0原文

我试图围绕向量上的某个点旋转向量(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云淡月浅 2024-07-21 20:23:11

答案取决于您的坐标系。

计算机图形坐标系,(0,0) 位于顶部左侧

如果您使用的是计算机图形矢量实现,其中 (0,0)左上角,并且您围绕点(dx, dy)旋转,那么旋转计算(包括平移回原始坐标系)将是:

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) 的旋转计算(包括平移回原始坐标系)将是:

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

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

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.

错爱 2024-07-21 20:23:11

解决方法是将矢量平移到旋转中心为(0,0)的坐标系。 应用旋转矩阵并将矢量平移回原始坐标系。

dx = x of rotation center  
dy = y of rotation center

V2 = V - [dx, dy, 0]  
V3 = V2 * rotation matrix  
Result = V3 + [dx, dy, 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.

dx = x of rotation center  
dy = y of rotation center

V2 = V - [dx, dy, 0]  
V3 = V2 * rotation matrix  
Result = V3 + [dx, dy, 0]
鹤舞 2024-07-21 20:23:11

假设您使用的是标准向量实现,其中 (0,0) 为左上角,并且您围绕点 (x_origin, y_origin) 旋转,则应该这样做:

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

请注意,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:

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.

靖瑶 2024-07-21 20:23:11

我发现 Mark Booth 的答案是错误的(将 (0,1,0) 旋转 0 度,用他的公式得到 (0,-1,0)),我最终得到:

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;

这可以进一步缩短当然可以,但我想让它尽可能简单。

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:

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 shortened of course, but I want to make it as simple as possible.

独闯女儿国 2024-07-21 20:23:11

您将需要使用平移矩阵来围绕不同点移动旋转。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文