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

窗口与视图

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

二维图形的几何变换

平移变换

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

在这里插入图片描述

比例变换

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

旋转变换

α \alpha 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 , y ) (x,y) 或列向量表示 ,图形点集可表示成 m 2 m*2 2 m 2*m ,图形的变换 \Rightarrow 点的变换。

三种变换

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

T = [ T x , T y ] T=[T_x,T_y] ,则 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}
    设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} ,则 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}

齐次坐标变换

  • 定义:用n+1维的向量表示一个 n 维向量的方法
  • 如:n维向量 ( p 1 , p 2 , . . . , p n ) (p_1,p_2,...,p_n) 表示为 ( h p 1 , h p 2 , . . . , h p n , h ) (hp_1,hp_2,...,hp_n,h) ,其中h称为哑坐标,当 h = 1 h=1 时称为 “规格化坐标” ,前 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 , 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 ] [ 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}

  • 平移(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}
  • 比例(Scale)变换
    S x = S y S_x=S_y 均匀比例变换, S x S y S_x\not=S_y 非均匀比例变换
    [ 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}
    整体比例变换时,若 s > 1 s>1 ,整体缩小;若 0 < s < 1 0<s<1 , 整体放大;若 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}
  • 旋转(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 ] [ 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}
  • 镜像对称
    在这里插入图片描述
对称方式 变换矩阵 新坐标点
对称于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} x = x y = y x'=-x\\y'=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} x = x y = y x'=x\\y'=-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} x = x y = y x'=-x\\y'=-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} x = y y = x x'=y\\y'=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} x = y y = x x'=-y\\y'=-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}

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

复合变换

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

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

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

1. α 9 0 1.\alpha\not=90^{\circ}时

在这里插入图片描述

  • 平移: 沿 x x 轴平移 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}
  • 旋转:顺时针旋转 α \alpha 角使 L L 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}
  • 对称:沿 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}
  • 反旋转:逆时针转 α \alpha ,使 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}
  • 反平移:沿 x x 轴平移到原 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}
  • 总变换矩阵为: 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}
  • 用直线的 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.
    新的总变换矩阵为: 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}

2. α = 9 0 2.\alpha=90^{\circ}时

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

小结

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

相关链接

超链接

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

发布了13 篇原创文章 · 获赞 14 · 访问量 3213

猜你喜欢

转载自blog.csdn.net/liuyiming2019/article/details/105540168