旋转矩阵推导
时间: 2025-08-01 13:07:03 浏览: 13
### 三维与二维旋转矩阵的数学推导过程
旋转矩阵是描述空间中物体旋转的一种数学工具,广泛应用于计算机图形学、机器人学以及游戏开发等领域。旋转矩阵的推导可以从二维空间开始,再扩展到三维空间。
#### 二维坐标系的旋转
在二维空间中,一个点 $ P = (x, y) $ 绕原点逆时针旋转角度 $ \theta $ 后的新坐标 $ P' = (x', y') $ 可以通过三角函数关系得出:
$$
\begin{aligned}
x' &= x\cos\theta - y\sin\theta \\
y' &= x\sin\theta + y\cos\theta
\end{aligned}
$$
将上述变换写成矩阵形式,可以得到二维旋转矩阵:
$$
P' = \begin{bmatrix}
\cos\theta & -\sin\theta \\
\sin\theta & \cos\theta
\end{bmatrix} P
$$
这一矩阵形式便于进行连续旋转操作和与其他变换矩阵的组合[^4]。
#### 三维坐标系的旋转
在三维空间中,旋转可以围绕任意轴进行,但为了简化分析,通常考虑绕坐标轴(X、Y、Z)的旋转。
##### 绕Z轴旋转
在三维空间中,绕Z轴旋转与二维旋转相同,Z坐标保持不变。设点 $ P = (x, y, z) $ 绕Z轴旋转角度 $ \theta $,其变换公式为:
$$
\begin{aligned}
x' &= x\cos\theta - y\sin\theta \\
y' &= x\sin\theta + y\cos\theta \\
z' &= z
\end{aligned}
$$
对应的旋转矩阵为:
$$
R_z(\theta) = \begin{bmatrix}
\cos\theta & -\sin\theta & 0 \\
\sin\theta & \cos\theta & 0 \\
0 & 0 & 1
\end{bmatrix}
$$
##### 绕X轴旋转(俯仰角 Pitch)
绕X轴旋转时,Y和Z坐标发生变化,X保持不变。设旋转角度为 $ \theta $,则:
$$
\begin{aligned}
y' &= y\cos\theta - z\sin\theta \\
z' &= y\sin\theta + z\cos\theta \\
x' &= x
\end{aligned}
$$
对应的旋转矩阵为:
$$
R_x(\theta) = \begin{bmatrix}
1 & 0 & 0 \\
0 & \cos\theta & -\sin\theta \\
0 & \sin\theta & \cos\theta
\end{bmatrix}
$$
##### 绕Y轴旋转(偏航角 Yaw)
绕Y轴旋转时,X和Z坐标发生变化,Y保持不变。设旋转角度为 $ \theta $,则:
$$
\begin{aligned}
x' &= x\cos\theta + z\sin\theta \\
z' &= -x\sin\theta + z\cos\theta \\
y' &= y
\end{aligned}
$$
对应的旋转矩阵为:
$$
R_y(\theta) = \begin{bmatrix}
\cos\theta & 0 & \sin\theta \\
0 & 1 & 0 \\
-\sin\theta & 0 & \cos\theta
\end{bmatrix}
$$
#### 任意点的旋转
在实际应用中,旋转中心可能不在原点。此时需要先将物体平移到原点,执行旋转后再平移回原位置。设旋转中心为点 $ (x, y) $,则变换过程为:
$$
v' = T(x, y) \cdot R \cdot T(-x, -y) \cdot v
$$
其中 $ T(x, y) $ 为平移矩阵,$ R $ 为旋转矩阵[^3]。
#### 示例代码:二维旋转矩阵计算
```python
import numpy as np
def rotation_matrix_2d(theta):
theta = np.radians(theta)
return np.array([
[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]
])
# 示例:绕原点旋转30度
R = rotation_matrix_2d(30)
print("二维旋转矩阵:")
print(R)
```
#### 示例代码:三维绕Z轴旋转矩阵计算
```python
def rotation_matrix_z(theta):
theta = np.radians(theta)
return np.array([
[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]
])
# 示例:绕Z轴旋转45度
R_z = rotation_matrix_z(45)
print("绕Z轴旋转矩阵:")
print(R_z)
```
---
阅读全文
相关推荐




















