收集一些变换矩阵

下面的内容主要是收集一些二维/三维图形变换的矩阵,并尝试理解一下为什么。

二维图形变换

显然,对一个二维图形做几何变换,就是对图形上的每一个点做相应的几何变换。
虽然有时候实际上我们并不需要全部做,比如一个直线段我们只需要把端点变换过去然后连起来就可以了。

常见的二维图形几何变换有:

  • 平移
  • 缩放
  • 旋转

二维齐次变换矩阵

我们引入一个概念叫二位齐次变换矩阵,它可以用来进行二位图像的各种变换,如下:

$$
T_{2D}=\left(
\begin{matrix}
a & d & g \\
b & e & h \\
c & f & i \\
\end{matrix}
\right)
$$

我们可以从变换功能上把它分成四个部分:

$$
\left(
\begin{matrix}
a & d \\
b & e \\
\end{matrix}
\right),\left(
\begin{matrix}
c & f \\
\end{matrix}
\right),\left(
\begin{matrix}
g \\
h \\
\end{matrix}
\right),\left(
\begin{matrix}
i \\
\end{matrix}
\right)
$$

其中:$\left(\begin{matrix}
a & d \\
b & e \\
\end{matrix}\right)$是对图形进行比例、旋转、对称、错切等变换;$\left(\begin{matrix}
c & f \\
\end{matrix}\right)$是对图形进行平移变换;$\left(\begin{matrix}
g \\
h \\
\end{matrix}\right)$是对图形进行投影变换;$\left(\begin{matrix}
i
\end{matrix}\right)$是对图形整体做比例变换。

平移

平移会用到的位置就是上述的$\left(\begin{matrix}
c & f \\
\end{matrix}\right)$位置,然后其余部分按照单位矩阵补上就OK了,也就是:

$$
\boldsymbol{p’}=\left(
\begin{matrix}
x’ & y’ & 1 \\
\end{matrix}
\right)=\left(
\begin{matrix}
x & y & 1 \\
\end{matrix}
\right)\left(
\begin{matrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
T_x & T_y & 1 \\
\end{matrix}
\right)=\left(
\begin{matrix}
x+T_x & y+T_y & 1
\end{matrix}
\right)
$$




$(x+T_x, y+T_y)$




$(x,y)$



缩放

缩放也很简单,只需要用到二维齐次变换矩阵中的$a,e$就可以了。

$$
\boldsymbol{p’}=\left(
\begin{matrix}
x’ & y’ & 1 \\
\end{matrix}
\right)=\left(
\begin{matrix}
x & y & 1 \\
\end{matrix}
\right)\left(
\begin{matrix}
S_x & 0 & 0 \\
0 & S_y & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)=\left(
\begin{matrix}
xS_x & yS_y & 1
\end{matrix}
\right)
$$




$(xS_x, yS_y)$




$(x,y)$



旋转

旋转时,我们假设逆时针旋转了$\theta$,很容易可以得到如下关系:

(用$x’=t\sin(\alpha+\theta)$这种方式或者 比较麻烦的方式)

$$
\boldsymbol{p’}=\left(
\begin{matrix}
x’ & y’ & 1 \\
\end{matrix}
\right)=\left(
\begin{matrix}
x & y & 1 \\
\end{matrix}
\right)\left(
\begin{matrix}
\cos{\theta} & \sin{\theta} & 0 \\
-\sin{\theta} & \cos{\theta} & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)=\left(
\begin{matrix}
x\cos{\theta}-y\sin{\theta} & x\sin{\theta}+y\cos{\theta} & 1
\end{matrix}
\right)
$$




$(x\cos\theta-y\sin\theta, x\sin\theta+y\cos\theta)$




$(x,y)$




$\theta$



对称

首先我们会讨论一下关于坐标轴或者某些简单的直线对称的变换矩阵,然后再通过变换的组合来完成对任意直线的对称变换。

关于坐标轴对称

当一个图形关于$x$轴对称时,我们只需要将$y$变为原来的相反数即可,因此有以下矩阵:

$$
\left(
\begin{matrix}
1 & 0 & 0 \\
0 & -1 & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
$$

关于$y$对称则是:

$$
\left(
\begin{matrix}
-1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
$$

关于原点对称

也就是两个坐标都取相反数:

$$
\left(
\begin{matrix}
-1 & 0 & 0 \\
0 & -1 & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
$$

关于$y=x$或者$y=-x$对称

关于$y=x$对称时,实际上是将$x$和$y$对调,即$y’=x,x’=y$,因此:

$$
\left(
\begin{matrix}
0 & 1 & 0 \\
1 & 0 & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
$$

那么如果是$y=-x$,我们有:

$$
\left(
\begin{matrix}
0 & -1 & 0 \\
-1 & 0 & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
$$

关于直线$y=kx+b$对称

上面提到的几个都比较容易理解…不过当对一条长得比较随意的直线对称的时候就没那么好直接写出来了。

我们可以把变换分解一下。

我们可以通过平移,旋转和简单的对称来组合成一个比较复杂的变化。

比如这条$y=kx+b$,截距是$b$,我们可以先将点在$y$轴上挪动$-y$,然后关于$y=kx$对称,结束后再往回挪动即可。

因此有:

$$
\left(
\begin{matrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & -b & 1 \\
\end{matrix}
\right)
\left(
\begin{matrix}
? & ? & 0 \\
? & ? & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
\left(
\begin{matrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & b & 1 \\
\end{matrix}
\right)
$$

那么中间的一部分就是关于$y=kx$对称的矩阵,我们要如何得到它呢。

$y=kx$时我们假设直线与$x$轴的夹角为$\theta$,(从$x$轴开始两个方向旋转,$\theta$的范围是$(-\frac{\pi}{2},\frac{\pi}{2})$)

我们有$k=\tan{\theta}$,即$\theta=\arctan{k}$,可以知道$y=kx$是$x$轴旋转了$\theta$后得到的直线。

因此我么可以先将点旋转$-\theta$,再关于$x$轴对称,最后再旋转$\theta$即可,有:

$$
\left(
\begin{matrix}
\cos{\theta} & -\sin{\theta} & 0 \\
\sin{\theta} & \cos{\theta} & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
\left(
\begin{matrix}
1 & 0 & 0 \\
0 & -1 & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
\left(
\begin{matrix}
\cos{\theta} & \sin{\theta} & 0 \\
-\sin{\theta} & \cos{\theta} & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
$$

$$
\left(
\begin{matrix}
\cos{2\theta} & \sin{2\theta} & 0 \\
\sin{2\theta} & -\cos{2\theta} & 0 \\
0 & 0 & 1 \\
\end{matrix}
\right)
$$

是一个美美的式子呢。

我们再代回刚才的$?$号矩阵,可以得到:

$$
\left(
\begin{matrix}
\cos{2\theta} & \sin{2\theta} & 0 \\
\sin{2\theta} & -\cos{2\theta} & 0 \\
-b\sin{2\theta} & b(\cos{2\theta}+1) & 1 \\
\end{matrix}
\right)
$$

这就是我们的对称矩阵了,其中$\theta=\arctan{k}$

三维图形变换