C++: Rotating a vector around a certain point(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.
这篇关于C++:围绕某个点旋转向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++:围绕某个点旋转向量
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01