原理公式

平移、旋转、缩放本质上是三种不同的运算,齐次坐标把它们统一为矩阵乘法。 2D 齐次坐标把点记作 (x, y, 1),变换矩阵为 3×3:

$$\text{平移 } T(t_x,t_y)=\begin{bmatrix} 1&0&t_x\\0&1&t_y\\0&0&1\end{bmatrix},\quad \text{旋转 } R(\theta)=\begin{bmatrix}\cos\theta&-\sin\theta&0\\\sin\theta&\cos\theta&0\\0&0&1\end{bmatrix},\quad \text{缩放 } S(s_x,s_y)=\begin{bmatrix}s_x&0&0\\0&s_y&0\\0&0&1\end{bmatrix}$$

复合变换(先旋转、再缩放、再平移)就是矩阵连乘:

$$p' = T \cdot S \cdot R \cdot p$$

推导要点:矩阵乘法不可交换(T·S·R ≠ R·S·T),连乘顺序即变换施加顺序, 且从右往左读。3D 同理,只是矩阵扩为 4×4,多一维 z 与透视除法。

重难点核心代码(code/demo11_transforms.py)

Python
import numpy as np

theta = np.radians(45)
R = np.array([[np.cos(theta), -np.sin(theta), 0],
              [np.sin(theta),  np.cos(theta), 0], [0, 0, 1]])   # 旋转
S = np.array([[1.5, 0, 0], [0, 0.5, 0], [0, 0, 1]])             # 缩放
T = np.array([[1, 0, 3], [0, 1, 2], [0, 0, 1]])                 # 平移

p = np.array([[0], [0], [1]])        # 点用齐次坐标 (x, y, 1)
p_new = T @ S @ R @ p                # 复合变换 = 矩阵连乘

齐次变换矩阵
齐次变换矩阵