矩阵变换在坐标系中的应用

264 阅读1分钟

前言

以三维坐标系为例,应用矩阵变换进行坐标平移、缩放、旋转。

原理

对于坐标变换,需要引入齐次坐标的概念。

齐次坐标是将一个原本是n维的向量用一个n+1维向量来表示。

借用F.S. Hill Jr.的一句话来解释为何要引入齐次坐标,“齐次坐标表示是计算机图形学的重要手段之一,它既能够用来明确区分向量和点,同时也更易用于进行仿射(线性)几何变换。”。

其中的几何变换,正是我们需要应用的场景。

这里我们用的是行向量,左乘行变换,右乘列变换。

平移

平移变换的计算,乘一个平移矩阵,可以表示为如下方式:

[xyz1][100001000010xyz1]=[x+xy+yz+z1]\begin{bmatrix} x & y & z & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ x' & y' & z' & 1 \end{bmatrix} = \begin{bmatrix} x+x' & y+y' & z+z' & 1 \end{bmatrix}

原坐标(x, y, z)分别平移x', y', z'后,坐标变为(x+x', y+y', z+z')。

缩放(整体)

缩放变换的计算,乘一个缩放矩阵,可以表示为如下方式:

[xyz1][100001000010000s]=[xsyszs1]\begin{bmatrix} x & y & z & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & s \end{bmatrix} = \begin{bmatrix} \frac{x}{s} & \frac{y}{s} & \frac{z}{s} & 1 \end{bmatrix}

原坐标(x, y, z)缩放s倍后,坐标变为(x/s, y/s, z/s)。

旋转(绕坐标轴)

旋转变换的计算,乘一个旋转矩阵,x, y, z坐标轴分别表示为:

绕x轴

[xyz1][10000cosθsinθ00sinθcosθ00001]=[xycosθzsinθysinθ+zcosθ1]\begin{bmatrix} x & y & z & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & cos\theta & sin\theta & 0 \\ 0 & -sin\theta & cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} x & y \cdot cos\theta - z \cdot sin\theta & y \cdot sin\theta + z \cdot cos\theta & 1 \end{bmatrix}

绕y轴

[xyz1][cosθ0sinθ00100sinθ0cosθ00001]=[xcosθ+zsinθyzcosθxsinθ1]\begin{bmatrix} x & y & z & 1 \end{bmatrix} \cdot \begin{bmatrix} cos\theta & 0 & -sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ sin\theta & 0 & cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} x \cdot cos\theta + z \cdot sin\theta & y & z \cdot cos\theta - x \cdot sin\theta & 1 \end{bmatrix}

绕z轴

[xyz1][cosθsinθ00sinθcosθ0000100001]=[xcosθysinθycosθ+xsinθz1]\begin{bmatrix} x & y & z & 1 \end{bmatrix} \cdot \begin{bmatrix} cos\theta & sin\theta & 0 & 0 \\ -sin\theta & cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} x \cdot cos\theta - y \cdot sin\theta & y \cdot cos\theta + x \cdot sin\theta & z & 1 \end{bmatrix}

仿真

使用MathLabTool进行三维坐标系仿真