CGAffineTransformMake(a,b,c,d,tx,ty) 矩陣運算原理

CGAffineTransformMake(a,b,c,d,tx,ty)html

a d 縮放, b c 旋轉, tx ty 位移ios

(x , y) -> (x' , y') 的公式app

    x' = ax + cy + tx
    y' = bx + dy + ty函數

矩陣的基本知識:code

struct CGAffineTransform {
  CGFloat a, b, c, d;
  CGFloat tx, ty;
};

CGAffineTransform CGAffineTransformMake(CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty);orm

爲了把二維圖形的變化統一在一個座標系裏,引入了齊次座標的概念,即把一個圖形用一個三維矩陣表示,其中第三列老是(0,0,1),用來做爲座標系的標準。因此全部的變化都由前兩列完成。htm

以上參數在矩陣中的表示爲:blog

運算原理:原座標設爲(X,Y,  1)it

                          | a    b    0 |io

    (X,Y,  1)   ⅹ   | c    d    0 |     =     (aX + cY + tx , bX + dY + ty , 1) ;

                          | tx    ty   1 |

經過矩陣運算後的座標(aX + cY + tx, bX + dY + ty, 1) 咱們對比一下可知:

 

1、設a=d=1, b=c=0

(aX + cY + tx ,  bX + dY + ty , 1) = (X  + tx , Y + ty , 1)

可見,這個時候,座標是按照向量(tx,ty)進行平移,

也就是函數CGAffineTransform CGAffineMakeTranslation(CGFloat tx,CGFloat ty)的計算原理。

 

2、設b=c=tx=ty=0

(aX + cY + tx ,  bX + dY + ty , 1) = (aX , dY , 1)

可見,這個時候,座標X按照a進行縮放,Y按照d進行縮放,a,d就是X,Y的比例係數,

也就是函數CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)的計算原理。

a對應於sx,d對應於sy。

 

3、設tx=ty=0,a=cosβ,b=sinβ,c=-sinβ,d=cosβ

(aX + cY + tx , bX + dY + ty , 1) = (Xcosβ - Ysinβ , Xsinβ + Ycosβ , 1)

可見,這個時候,β就是旋轉的角度,逆時針爲正,順時針爲負。

也就是函數CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)的計算原理。

angle即β的弧度表示。

 

參考:

https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html

http://justsee.iteye.com/blog/1969933

相關文章
相關標籤/搜索