【计算机图形学】小白谈计算机图形学(四)二维三维图形变换—1

353 阅读9分钟

小白谈计算机图形学(四)二维三维图形变换—1

窗口与视图

  • 窗口::在计算机图形学中,将在用户坐标系中需要进行观 察和处理的一个坐标区域称为窗口(Window)。即用户在 用户域中指定的任意区域W。 比如下图可以是屏幕上的一个窗口,窗口里的位置都是相对不变的。
    在这里插入图片描述
  • 视图:将窗口映射到显示设备上的坐标区域称为视图区 (Viewport),视图区可以在屏幕上随意改变位置,如下图中的京香照片。

二维图形的几何变换

平移变换

T x , T y T_x,T_y Tx​,Ty​为平移矢量
{ x ′ = x + T x y ′ = y + T y \left\{ \begin{aligned} & x'=x+T_x\\ &y'=y+T_y \end{aligned} \right. {​x′=x+Tx​y′=y+Ty​​

在这里插入图片描述

比例变换

S x , S y S_x,S_y Sx​,Sy​为比例系数
{ x ′ = x S x y ′ = y S y \left\{ \begin{aligned} & x'=xS_x\\ &y'=yS_y \end{aligned} \right. {​x′=xSx​y′=ySy​​

旋转变换

α \alpha α是 p p p点的原始角度位置与水平线的夹角, θ \theta θ是旋转角
在这里插入图片描述
{ x ′ = x c o s θ − y s i n θ y ′ = y s i n θ + y c o s θ \left\{ \begin{aligned} & x'=xcos\theta-ysin\theta\\ &y'=ysin\theta+ycos\theta \end{aligned} \right. {​x′=xcosθ−ysinθy′=ysinθ+ycosθ​

二维图形变换的矩阵表示

  • 图形变换就是要变换图形的顶点坐标,同时保持图形的原拓扑关系不变。
  • 二维图形可看成是一个点集,点的坐标可用行向量 ( x , y ) (x,y) (x,y)或列向量表示 ,图形点集可表示成 m ∗ 2 m*2 m∗2或 2 ∗ m 2*m 2∗m ,图形的变换 ⇒ \Rightarrow ⇒点的变换。

三种变换

  • 平移(Translation)变换
    [ x ′ , y ′ ] = [ x , y ] + [ T x , T y ] [x',y']=[x,y]+[T_x,T_y] [x′,y′]=[x,y]+[Tx​,Ty​]

设 T = [ T x , T y ] T=[T_x,T_y] T=[Tx​,Ty​],则 p ′ = p + T p'=p+T p′=p+T

  • 比例(Scale)变换
    [ x ′ , y ′ ] = [ x , y ] [ S x 0 0 S y ] \begin{gathered} [x',y']=[x,y]\begin{bmatrix} S_x & 0 \\ 0 & S_y \end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y][Sx​0​0Sy​​]​
    设S= [ x ′ , y ′ ] = [ x , y ] [ S x 0 0 S y ] \begin{gathered} [x',y']=[x,y]\begin{bmatrix} S_x & 0 \\ 0 & S_y \end{bmatrix} \end{gathered} [x′,y′]=[x,y][Sx​0​0Sy​​]​,则 p ′ = p ∗ S p'=p*S p′=p∗S。
  • 旋转(Rotate)变换
    [ x ′ , y ′ ] = [ x , y ] [ c o s θ s i n θ − s i n θ c o s θ ] \begin{gathered} [x',y']=[x,y]\begin{bmatrix} cos\theta & sin\theta \\ -sin\theta & cos\theta \end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y][cosθ−sinθ​sinθcosθ​]​

齐次坐标变换

  • 定义:用n+1维的向量表示一个 n 维向量的方法
  • 如:n维向量 ( p 1 , p 2 , . . . , p n ) (p_1,p_2,...,p_n) (p1​,p2​,...,pn​)表示为 ( h p 1 , h p 2 , . . . , h p n , h ) (hp_1,hp_2,...,hp_n,h) (hp1​,hp2​,...,hpn​,h),其中h称为哑坐标,当 h = 1 h=1 h=1时称为 “规格化坐标” ,前 n n n个坐标就是普通坐标系下的 n n n维坐标。
  • 使用原因;对于图形来说,没有实质性的差别,但是却给后面的矩阵运算提供了可行性和方便性。

原二维线性变换

将 x ′ = a 1 x + b 1 y + c 1 与 y ′ = a 2 x + b 2 y + c 2 x'=a_1x+b_1y+c_1与y'=a_2x+b_2y+c_2 x′=a1​x+b1​y+c1​与y′=a2​x+b2​y+c2​改写成:
[ x ′ , y ′ ] = [ x , y , 1 ] [ a 1 a 2 b 1 b 2 c 1 c 2 ] \begin{gathered} [x',y']=[x,y,1]\begin{bmatrix} a_1 & a_2 \\ b_1 & b_2 \\c_1&c_2\end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y,1]⎣⎡​a1​b1​c1​​a2​b2​c2​​⎦⎤​​

齐次坐标法

[ x ′ , y ′ ] = [ x , y , 1 ] [ a 1 a 2 0 b 1 b 2 0 c 1 c 2 1 ] \begin{gathered} [x',y']=[x,y,1]\begin{bmatrix} a_1 & a_2 &0\\ b_1 & b_2 &0\\c_1&c_2&1\end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y,1]⎣⎡​a1​b1​c1​​a2​b2​c2​​001​⎦⎤​​

  • 平移(Translation)变换
    [ x ′ , y ′ ] = [ x , y , 1 ] [ 1 0 0 0 1 0 T x T y 1 ] \begin{gathered} [x',y']=[x,y,1]\begin{bmatrix} 1 & 0 &0\\ 0 & 1 &0\\T_x&T_y&1\end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y,1]⎣⎡​10Tx​​01Ty​​001​⎦⎤​​
  • 比例(Scale)变换
    S x = S y S_x=S_y Sx​=Sy​均匀比例变换, S x ≠ S y S_x\not=S_y Sx​​=Sy​非均匀比例变换
    [ x ′ , y ′ ] = [ x , y , 1 ] [ S x 0 0 0 S y 0 0 0 1 ] \begin{gathered} [x',y']=[x,y,1]\begin{bmatrix} S_x & 0 &0\\ 0 & S_y &0\\0&0&1\end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y,1]⎣⎡​Sx​00​0Sy​0​001​⎦⎤​​
    整体比例变换时,若 s > 1 s>1 s>1,整体缩小;若 0 < s < 1 0<s<1 0<s<1, 整体放大;若 s < 0 s<0 s<0,发生关于原点的对称变换。
    [ x ′ , y ′ ] = [ x , y , 1 ] [ 1 0 0 0 1 0 0 0 s ] \begin{gathered} [x',y']=[x,y,1]\begin{bmatrix} 1 & 0 &0\\ 0 & 1 &0\\0&0&s\end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y,1]⎣⎡​100​010​00s​⎦⎤​​
  • 旋转(Rotate)变换
    顺时针:
    [ x ′ , y ′ ] = [ x , y , 1 ] [ c o s α − s i n α 0 s i n α c o s α 0 0 0 1 ] \begin{gathered} [x',y']=[x,y,1]\begin{bmatrix} cos\alpha & -sin\alpha &0\\ sin\alpha & cos\alpha &0\\0&0&1\end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y,1]⎣⎡​cosαsinα0​−sinαcosα0​001​⎦⎤​​
    逆时针:
    [ x ′ , y ′ ] = [ x , y , 1 ] [ c o s α s i n α 0 − s i n α c o s α 0 0 0 1 ] \begin{gathered} [x',y']=[x,y,1]\begin{bmatrix} cos\alpha & sin\alpha &0\\ -sin\alpha & cos\alpha &0\\0&0&1\end{bmatrix} \quad \end{gathered} [x′,y′]=[x,y,1]⎣⎡​cosα−sinα0​sinαcosα0​001​⎦⎤​​
  • 镜像对称
    在这里插入图片描述
对称方式变换矩阵新坐标点
对称于y轴[ − 1 0 0 0 1 0 0 0 1 ] \begin{gathered}\begin{bmatrix}-1&0&0\\0& 1&0\\0&0&1\end{bmatrix}\quad\end{gathered} ⎣⎡​−100​010​001​⎦⎤​​x ′ = − x y ′ = y x'=-x\\y'=y x′=−xy′=y
对称于x轴[ 1 0 0 0 − 1 0 0 0 1 ] \begin{gathered}\begin{bmatrix}1 & 0 &0\\ 0 & -1&0\\0&0&1\end{bmatrix}\quad\end{gathered} ⎣⎡​100​0−10​001​⎦⎤​​x ′ = x y ′ = − y x'=x\\y'=-y x′=xy′=−y
对称于原点[ − 1 0 0 0 − 1 0 0 0 1 ] \begin{gathered}\begin{bmatrix}-1 & 0 &0\\ 0 & -1&0\\0&0&1\end{bmatrix}\quad\end{gathered} ⎣⎡​−100​0−10​001​⎦⎤​​x ′ = − x y ′ = − y x'=-x\\y'=-y x′=−xy′=−y
对称于直线y=x[ 0 1 0 1 0 0 0 0 1 ] \begin{gathered}\begin{bmatrix}0 & 1 &0\\ 1 & 0&0\\0&0&1\end{bmatrix}\quad\end{gathered} ⎣⎡​010​100​001​⎦⎤​​x ′ = y y ′ = x x'=y\\y'=x x′=yy′=x
对称于直线y=-x[ 0 − 1 0 − 1 0 0 0 0 1 ] \begin{gathered}\begin{bmatrix}0 & -1 &0\\ -1 & 0&0\\0&0&1\end{bmatrix}\quad\end{gathered} ⎣⎡​0−10​−100​001​⎦⎤​​x ′ = − y y ′ = − x x'=-y\\y'=-x x′=−yy′=−x
  • 错切变换:哪个错切变哪个
    [ 1 b 0 c 1 0 0 0 1 ] \begin{gathered} \begin{bmatrix} 1 & b &0\\ c&1&0\\ 0&0&1 \end{bmatrix} \quad\end{gathered} ⎣⎡​1c0​b10​001​⎦⎤​​

沿x轴方向关于y错切: x = x + c y x=x+cy x=x+cy
在这里插入图片描述
沿y轴方向关于x错切: y = y + b x y=y+bx y=y+bx
在这里插入图片描述

复合变换

作一次以上的几何变换时,可以将复杂变换转化成变换矩阵相乘。 不可以交换律可以结合律。平移可交换律

例题:任意直线的对称变换

以沿x轴平移使之过原点再旋转使直线与x轴重合为例

1. α ≠ 9 0 ∘ 时 1.\alpha\not=90^{\circ}时 1.α​=90∘时

在这里插入图片描述

  • 平移: 沿 x x x轴平移 L L L使之过原点,变换矩阵为: T 1 = [ 1 0 0 0 1 0 C A 0 1 ] T_1=\begin{gathered} \begin{bmatrix} 1 & 0 &0\\ 0&1&0\\ \frac{C}{A}&0&1 \end{bmatrix} \quad\end{gathered} T1​=⎣⎡​10AC​​010​001​⎦⎤​​
  • 旋转:顺时针旋转 α \alpha α角使 L L L与 x x x轴重合,变换矩阵: R 1 = [ c o s α − s i n α 0 s i n α c o s α 0 0 0 1 ] R_1=\begin{gathered} \begin{bmatrix} cos\alpha & -sin\alpha &0\\ sin\alpha & cos\alpha &0\\0&0&1 \end{bmatrix} \quad\end{gathered} R1​=⎣⎡​cosαsinα0​−sinαcosα0​001​⎦⎤​​
  • 对称:沿 x x x轴对称,变换矩阵为: S 1 = [ 1 0 0 0 − 1 0 0 0 1 ] S_1=\begin{gathered}\begin{bmatrix}1 & 0 &0\\ 0 & -1&0\\0&0&1\end{bmatrix}\quad\end{gathered} S1​=⎣⎡​100​0−10​001​⎦⎤​​
  • 反旋转:逆时针转 α \alpha α,使 L L L返回原位置,变换矩阵为: R 1 = [ c o s α s i n α 0 − s i n α c o s α 0 0 0 1 ] R_1=\begin{gathered} \begin{bmatrix} cos\alpha & sin\alpha &0\\ -sin\alpha & cos\alpha &0\\0&0&1 \end{bmatrix} \quad\end{gathered} R1​=⎣⎡​cosα−sinα0​sinαcosα0​001​⎦⎤​​
  • 反平移:沿 x x x轴平移到原 L L L的初始位置,变换矩阵为: T 2 = [ 1 0 0 0 1 0 − C A 0 1 ] T_2=\begin{gathered} \begin{bmatrix} 1 & 0 &0\\ 0&1&0\\ -\frac{C}{A}&0&1 \end{bmatrix} \quad\end{gathered} T2​=⎣⎡​10−AC​​010​001​⎦⎤​​
  • 总变换矩阵为: C H = [ c o s 2 α s i n 2 α 0 s i n 2 α − c o s 2 α 0 − C A ( c o s 2 α − 1 ) C A s i n 2 α 1 ] C_H=\begin{gathered} \begin{bmatrix} cos2\alpha & sin2\alpha &0\\ sin2\alpha&-cos2\alpha&0\\ -\frac{C}{A}(cos2\alpha-1)&\frac{C}{A}sin2\alpha&1 \end{bmatrix} \quad\end{gathered} CH​=⎣⎡​cos2αsin2α−AC​(cos2α−1)​sin2α−cos2αAC​sin2α​001​⎦⎤​​
  • 用直线的 A , B , C A,B,C A,B,C三个参数替换总变换矩阵中的 α \alpha α,有如下关系:
    { s i n 2 α = 2 A B A 2 + B 2 c o s 2 α = B 2 − A 2 A 2 + B 2 \left\{ \begin{aligned} & sin2\alpha=\frac{2AB}{A^2+B^2}\\ &cos2\alpha=\frac{B^2-A^2}{A^2+B^2} \end{aligned} \right. ⎩⎪⎪⎨⎪⎪⎧​​sin2α=A2+B22AB​cos2α=A2+B2B2−A2​​
    新的总变换矩阵为: C H = [ B 2 − A 2 A 2 + B 2 2 A B A 2 + B 2 0 2 A B A 2 + B 2 A 2 − B 2 A 2 + B 2 0 − 2 A C A 2 + B 2 2 B C A 2 + B 2 1 ] C_H=\begin{gathered} \begin{bmatrix} \frac{B^2-A^2}{A^2+B^2} & \frac{2AB}{A^2+B^2} &0\\ \frac{2AB}{A^2+B^2}&\frac{A^2-B^2}{A^2+B^2}&0\\ -\frac{2AC}{A^2+B^2}&\frac{2BC}{A^2+B^2}&1 \end{bmatrix} \quad\end{gathered} CH​=⎣⎢⎡​A2+B2B2−A2​A2+B22AB​−A2+B22AC​​A2+B22AB​A2+B2A2−B2​A2+B22BC​​001​⎦⎥⎤​​
2. α = 9 0 ∘ 时 2.\alpha=90^{\circ}时 2.α=90∘时

求出方程,代入仍满足总变换矩阵,即上式为通式。

小结

  • 比例、旋转、错切、对称等变换 ( a , b , c , d ) (a,b,c,d) (a,b,c,d)
  • 平移变换 ( l , m ) (l,m) (l,m)
  • 透视变换,产生投影( p , q p,q p,q,三维才有意义)
  • 整体的比例变换( s s s,全比例变换)它使整个图形沿 x 、 y x、y x、y轴作等比例均匀变换
    在这里插入图片描述

相关链接

超链接

如果你还想了解其他内容:
小白谈计算机图形学(一)如何画线
小白谈计算机图形学(二)如何画圆
小白谈计算机图形学(三)二维图形裁剪
小白谈计算机图形学(四)二维三维图形变换—1
参考文献:
齐次坐标变化