Get just the scaling transformation out of CGAffineTransform

前端 未结 7 1086
北荒
北荒 2021-02-01 19:26

I found a similar question about getting just the rotation, but as I understand scaling and rotating work different in the transform matrix.

Matrixes are not my strength

7条回答
  •  醉梦人生
    2021-02-01 19:39

    I'm not familiar with CGAffineTransform or Objective-C (you caught me with the math tag). In general, you need to back out the transforms individually. For instance if the affine transform A performs scaling, rotation and translation only (the order of scaling & rotation isn't important in the method below, but translation should be definitely be last):

    Translation: Applying A to the vector (0,0) will return the result (tx, ty) where tx and ty are the translations in the X and Y directions respectively.

    Scaling in X: Apply A to the vector (1, 0) and get (sx0 + tx, sx1 + ty). The scaling in X will be sqrt(sx0^2 + sx1^2)

    Scaling in Y: Apply A to the vector (0, 1) and get (sy0 + tx, sy1 + ty). The scaling in Y will be sqrt(sy0^2 + sy1^2)

    Since affine transformations are implemented by a simple trick with linear transformations and since linear transformations are not commutative, you need to understand how the transformations are ordered before actually working through how to pull the individual transformation out.

提交回复
热议问题