小白談計算機圖形學(四)二維三維圖形變換—1

窗口與視圖

  • 窗口::在計算機圖形學中,將在用戶座標系中需要進行觀 察和處理的一個座標區域稱爲窗口(Window)。即用戶在 用戶域中指定的任意區域W。 比如下圖可以是屏幕上的一個窗口,窗口裏的位置都是相對不變的。
    在這裏插入圖片描述
  • 視圖:將窗口映射到顯示設備上的座標區域稱爲視圖區 (Viewport),視圖區可以在屏幕上隨意改變位置,如下圖中的京香照片。

二維圖形的幾何變換

平移變換

Tx,TyT_x,T_y爲平移矢量
{x=x+Txy=y+Ty \left\{ \begin{aligned} & x'=x+T_x\\ &y'=y+T_y \end{aligned} \right.

在這裏插入圖片描述

比例變換

Sx,SyS_x,S_y爲比例係數
{x=xSxy=ySy \left\{ \begin{aligned} & x'=xS_x\\ &y'=yS_y \end{aligned} \right.

旋轉變換

α\alphapp點的原始角度位置與水平線的夾角,θ\theta是旋轉角
在這裏插入圖片描述
{x=xcosθysinθy=ysinθ+ycosθ \left\{ \begin{aligned} & x'=xcos\theta-ysin\theta\\ &y'=ysin\theta+ycos\theta \end{aligned} \right.

二維圖形變換的矩陣表示

  • 圖形變換就是要變換圖形的頂點座標,同時保持圖形的原拓撲關係不變。
  • 二維圖形可看成是一個點集,點的座標可用行向量(x,y)(x,y)或列向量表示 ,圖形點集可表示成m2m*22m2*m ,圖形的變換\Rightarrow點的變換。

三種變換

  • 平移(Translation)變換
    [x,y]=[x,y]+[Tx,Ty][x',y']=[x,y]+[T_x,T_y]

T=[Tx,Ty]T=[T_x,T_y],則p=p+Tp'=p+T

  • 比例(Scale)變換
    [x,y]=[x,y][Sx00Sy] \begin{gathered} [x',y']=[x,y]\begin{bmatrix} S_x & 0 \\ 0 & S_y \end{bmatrix} \quad \end{gathered}
    設S=[x,y]=[x,y][Sx00Sy]\begin{gathered} [x',y']=[x,y]\begin{bmatrix} S_x & 0 \\ 0 & S_y \end{bmatrix} \end{gathered},則p=pSp'=p*S
  • 旋轉(Rotate)變換
    [x,y]=[x,y][cosθsinθsinθcosθ] \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維向量(p1,p2,...,pn)(p_1,p_2,...,p_n)表示爲(hp1,hp2,...,hpn,h)(hp_1,hp_2,...,hp_n,h),其中h稱爲啞座標,當h=1h=1時稱爲 “規格化座標” ,前nn個座標就是普通座標系下的nn維座標。
  • 使用原因;對於圖形來說,沒有實質性的差別,但是卻給後面的矩陣運算提供了可行性和方便性。

原二維線性變換

x=a1x+b1y+c1y=a2x+b2y+c2x'=a_1x+b_1y+c_1與y'=a_2x+b_2y+c_2改寫成:
[x,y]=[x,y,1][a1a2b1b2c1c2] \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][a1a20b1b20c1c21] \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][100010TxTy1] \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)變換
    Sx=SyS_x=S_y均勻比例變換,SxSyS_x\not=S_y非均勻比例變換
    [x,y]=[x,y,1][Sx000Sy0001] \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>1s>1,整體縮小;若0<s<10<s<1, 整體放大;若s<0s<0,發生關於原點的對稱變換。
    [x,y]=[x,y,1][10001000s] \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][cosαsinα0sinαcosα0001] \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α0sinαcosα0001] \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軸 [100010001]\begin{gathered}\begin{bmatrix}-1&0&0\\0& 1&0\\0&0&1\end{bmatrix}\quad\end{gathered} x=xy=yx'=-x\\y'=y
對稱於x軸 [100010001]\begin{gathered}\begin{bmatrix}1 & 0 &0\\ 0 & -1&0\\0&0&1\end{bmatrix}\quad\end{gathered} x=xy=yx'=x\\y'=-y
對稱於原點 [100010001]\begin{gathered}\begin{bmatrix}-1 & 0 &0\\ 0 & -1&0\\0&0&1\end{bmatrix}\quad\end{gathered} x=xy=yx'=-x\\y'=-y
對稱於直線y=x [010100001]\begin{gathered}\begin{bmatrix}0 & 1 &0\\ 1 & 0&0\\0&0&1\end{bmatrix}\quad\end{gathered} x=yy=xx'=y\\y'=x
對稱於直線y=-x [010100001]\begin{gathered}\begin{bmatrix}0 & -1 &0\\ -1 & 0&0\\0&0&1\end{bmatrix}\quad\end{gathered} x=yy=xx'=-y\\y'=-x
  • 錯切變換:哪個錯切變哪個
    [1b0c10001]\begin{gathered} \begin{bmatrix} 1 & b &0\\ c&1&0\\ 0&0&1 \end{bmatrix} \quad\end{gathered}

沿x軸方向關於y錯切:x=x+cyx=x+cy
在這裏插入圖片描述
沿y軸方向關於x錯切:y=y+bxy=y+bx
在這裏插入圖片描述

複合變換

作一次以上的幾何變換時,可以將複雜變換轉化成變換矩陣相乘。 不可以交換律可以結合律。平移可交換律

例題:任意直線的對稱變換

以沿x軸平移使之過原點再旋轉使直線與x軸重合爲例

1.α901.\alpha\not=90^{\circ}時

在這裏插入圖片描述

  • 平移: 沿xx軸平移LL使之過原點,變換矩陣爲:T1=[100010CA01]T_1=\begin{gathered} \begin{bmatrix} 1 & 0 &0\\ 0&1&0\\ \frac{C}{A}&0&1 \end{bmatrix} \quad\end{gathered}
  • 旋轉:順時針旋轉α\alpha角使LLxx軸重合,變換矩陣:R1=[cosαsinα0sinαcosα0001]R_1=\begin{gathered} \begin{bmatrix} cos\alpha & -sin\alpha &0\\ sin\alpha & cos\alpha &0\\0&0&1 \end{bmatrix} \quad\end{gathered}
  • 對稱:沿xx軸對稱,變換矩陣爲:S1=[100010001]S_1=\begin{gathered}\begin{bmatrix}1 & 0 &0\\ 0 & -1&0\\0&0&1\end{bmatrix}\quad\end{gathered}
  • 反旋轉:逆時針轉α\alpha,使LL返回原位置,變換矩陣爲:R1=[cosαsinα0sinαcosα0001]R_1=\begin{gathered} \begin{bmatrix} cos\alpha & sin\alpha &0\\ -sin\alpha & cos\alpha &0\\0&0&1 \end{bmatrix} \quad\end{gathered}
  • 反平移:沿xx軸平移到原LL的初始位置,變換矩陣爲:T2=[100010CA01]T_2=\begin{gathered} \begin{bmatrix} 1 & 0 &0\\ 0&1&0\\ -\frac{C}{A}&0&1 \end{bmatrix} \quad\end{gathered}
  • 總變換矩陣爲:CH=[cos2αsin2α0sin2αcos2α0CA(cos2α1)CAsin2α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,CA,B,C三個參數替換總變換矩陣中的α\alpha,有如下關係:
    {sin2α=2ABA2+B2cos2α=B2A2A2+B2 \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.
    新的總變換矩陣爲:CH=[B2A2A2+B22ABA2+B202ABA2+B2A2B2A2+B202ACA2+B22BCA2+B21]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.α=902.\alpha=90^{\circ}時

求出方程,代入仍滿足總變換矩陣,即上式爲通式。

小結

  • 比例、旋轉、錯切、對稱等變換(a,b,c,d)(a,b,c,d)
  • 平移變換(l,m)(l,m)
  • 透視變換,產生投影(p,qp,q,三維纔有意義)
  • 整體的比例變換(ss,全比例變換)它使整個圖形沿xyx、y軸作等比例均勻變換
    在這裏插入圖片描述

相關鏈接

超鏈接

如果你還想了解其他內容:
小白談計算機圖形學(一)如何畫線
小白談計算機圖形學(二)如何畫圓
小白談計算機圖形學(三)二維圖形裁剪
小白談計算機圖形學(四)二維三維圖形變換—1
參考文獻:
齊次座標變化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章