python 坐标系绕y轴旋转180度矩阵
时间: 2025-06-04 22:56:53 浏览: 13
### Python 中实现坐标系绕 Y 轴旋转 180 度的矩阵变换方法
在三维空间中,绕某个轴旋转的角度可以用对应的旋转矩阵表示。对于绕 Y 轴旋转的情况,其旋转矩阵的形式如下:
\[
R_y(\theta) =
\begin{bmatrix}
\cos \theta & 0 & \sin \theta \\
0 & 1 & 0 \\
-\sin \theta & 0 & \cos \theta
\end{bmatrix}
\]
当旋转角度为 \(180^\circ\)(即 \(\pi\) 弧度)时,代入上述公式可得具体的旋转矩阵[^2]。
以下是基于 Python 的实现代码,使用 NumPy 来构建和应用该旋转矩阵:
```python
import numpy as np
def rotate_around_y(x, y, z, theta):
# 构建绕 Y 轴旋转的矩阵
rotation_matrix = np.array([
[np.cos(theta), 0, np.sin(theta)],
[0, 1, 0],
[-np.sin(theta), 0, np.cos(theta)]
])
# 将输入点转化为列向量形式
point = np.array([[x], [y], [z]])
# 执行矩阵乘法得到旋转后的点
rotated_point = np.dot(rotation_matrix, point)
return rotated_point.flatten()
# 设置旋转角度为 180 度 (π 弧度)
theta = np.pi # 或者可以直接写成 180 * np.pi / 180
# 测试点 (x=1, y=2, z=3)
original_point = (1, 2, 3)
rotated_point = rotate_around_y(*original_point, theta)
print(f"Original Point: {original_point}")
print(f"Rotated Point around Y-axis by 180 degrees: ({rotated_point[0]}, {rotated_point[1]}, {rotated_point[2]})")
```
#### 解析
- 上述函数 `rotate_around_y` 接收一个三维点 `(x, y, z)` 和旋转角度 `\theta`。
- 使用给定的旋转矩阵对点进行变换,并返回新的位置。
- 当设置 `\theta=np.pi` 时,实现了绕 Y 轴旋转 \(180^\circ\) 的效果。
如果需要扩展到齐次坐标系中的变换,则可以进一步修改代码以支持平移或其他复合操作[^4]。
---
阅读全文
相关推荐


















