一、学习目的
(1)掌握多面体的存储方法。
(2)掌握图形的几何变换及投影变换。
(3)掌握三维形体不同投影方法的投影图的生成原理。
(4)掌握多面体投影图绘制的编程方法。
二、学习内容
(1)编程实现一个长方体的正轴测投影图。
(2)编程实现一个长方体的斜平行投影图。
(3)编程实现一个长方体的一点透视图。
三、基本算法
1. 坐标系变换算法
-
旋转矩阵构造
用于将任意投影面对齐到标准坐标系(如XOY平面)。通过绕轴旋转(如绕X/Y/Z轴),结合 罗德里格斯旋转公式 或 欧拉角,生成旋转变换矩阵,使投影面法向量对齐目标轴(如Z轴)。 -
平移变换
将投影面平移到原点,简化投影计算。
2. 投影矩阵算法
-
正轴测投影
通过正交投影矩阵移除深度坐标(Z轴),结合绕轴旋转实现立体效果。 -
斜平行投影
引入 错切矩阵(Shear Matrix),沿某一轴向偏移深度坐标,模拟斜视角。 -
一点透视投影
构造 透视矩阵,引入灭点(Vanishing Point)。
四、具体代码
import numpy as np
import matplotlib.pyplot as pltclass Cube:def __init__(self, size=1):self.vertices = np.array([[0, 0, 0],[size, 0, 0],[size, size, 0],[0, size, 0],[0, 0, size],[size, 0, size],[size, size, size],[0, size, size]])self.edges = [[0, 1], [1, 2], [2, 3], [3, 0],[4, 5], [5, 6], [6, 7], [7, 4],[0, 4], [1, 5], [2, 6], [3, 7]]def orthographic_projection(self):return self.vertices[:, :2]def oblique_parallel_projection(self, alpha=np.pi/4, l=0.5):P = np.array([[1, 0, -l*np.cos(alpha)],[0, 1, -l*np.sin(alpha)],[0, 0, 0]])return np.dot(self.vertices, P.T)def one_point_perspective_projection(self, d=5):P = np.array([[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, -1/d, 0]])homogeneous_vertices = np.hstack((self.vertices, np.ones((8, 1))))projected = np.dot(homogeneous_vertices, P.T)return (projected[:, :2].T / projected[:, 3]).Tdef plot_projection(self, projection, title):projected_vertices = projection()plt.figure()for edge in self.edges:plt.plot(projected_vertices[edge, 0], projected_vertices[edge, 1], 'b')plt.title(title)plt.axis('equal')plt.show()if __name__ == '__main__':cube = Cube()cube.plot_projection(cube.orthographic_projection, 'Orthographic Projection')cube.plot_projection(lambda: cube.oblique_parallel_projection(), 'Oblique Parallel Projection')cube.plot_projection(cube.one_point_perspective_projection, 'One-Point Perspective Projection')
五、运行结果
六、项目简介
# 立方体投影项目
## 项目概述
这个Python项目演示了立方体的三种不同投影方法:正交投影、斜平行投影和一点透视投影。
## 投影方法说明
1. **正交投影(Orthographic Projection)**: 直接忽略Z坐标,将3D点投影到XY平面
2. **斜平行投影(Oblique Parallel Projection)**: 保持平行线,但以一定角度显示
3. **一点透视投影(One-Point Perspective Projection)**: 模拟人眼看到的透视效果
## 运行要求
- Python 3.x
- NumPy
- Matplotlib
## 示例代码
```python
import numpy as np
import matplotlib.pyplot as plt
from cube_projections import Cube
cube = Cube()
cube.plot_projection(cube.orthographic_projection, 'Orthographic Projection')
```
## 预期输出
运行后会显示三个窗口,分别展示三种不同的立方体投影效果图。