如何编写绕坐标系中心旋转点的公式?

问题描述 投票:0回答:1

在处理中,我有一个 3D 坐标系,其中 x、y 和 z 维度从 0 到 1000。我在这些坐标内随机放置一个点,并希望它绕中心旋转(x、y 和 z = 500) )按给定角度。我想重新计算点坐标,而不是依赖translate()或rotateX()/rotateY()/rotateZ()方法。

x、y 和 z 坐标的数学公式是什么?如何用代码表达公式? (我所做的搜索并没有给出针对我的案例的答案)。

processing trigonometry
1个回答
1
投票

要围绕 3D 空间中的任意中心旋转点,可以使用旋转矩阵。这些矩阵允许您围绕 x、y 和 z 轴执行特定角度的旋转。在处理中,要围绕中心 (500, 500, 500) 旋转点 (x, y, z),您需要应用平移,执行旋转,然后平移回原始位置。

步骤包括:

通过减去中心坐标将点移动到原点。 对所需的轴使用合适的旋转矩阵。 通过添加中心坐标将点移回其原始位置。 以下是围绕中心 (500, 500, 500) 围绕 x 轴旋转点 (x, y, z) 角度 theta 的示例:

float x = 300; // Example point coordinates`
float y = 700;
float z = 400;

float centerX = 500;
float centerY = 500;
float centerZ = 500;

float theta = radians(45); // Angle in radians

// Move the point to the origin
x -= centerX;
y -= centerY;
z -= centerZ;

// Perform rotation around x-axis
float newY = y * cos(theta) - z * sin(theta);
float newZ = y * sin(theta) + z * cos(theta);

// Move the point back to the original position
x += centerX;
newY += centerY;
newZ += centerZ;

// Updated point coordinates
println("New Coordinates:");
println("x: " + x);
println("y: " + newY);
println("z: " + newZ);

通过调整此代码并为 y 轴和 z 轴使用不同的旋转矩阵,您也可以绕这些轴执行旋转。

绕 y 轴旋转角度 theta:

|  cosθ   0  sinθ |
|   0       1    0   |
| -sinθ  0  cosθ |

绕 z 轴旋转角度 theta:

|  cosθ  -sinθ  0  |
|  sinθ   cosθ  0  |
|   0        0      1 |
© www.soinside.com 2019 - 2024. All rights reserved.