开发者

Find Scale Value from CGAffineTransform

开发者 https://www.devze.com 2023-04-13 09:08 出处:网络
Ok, so I realize I can find the scale value from a layer\'s CATransform3D like this: float scale = [[layer valueForKeyPath: @\"transform.scale\"] floatValue];

Ok, so I realize I can find the scale value from a layer's CATransform3D like this:

 float scale = [[layer valueForKeyPath: @"transform.scale"] floatValue];

But I can't for the life of me figure out how I would find the scale value from a CGAffineTransform. Say for instance I have this CGAffineTransform called "cameraTransform":

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
CGAffineTransform *cameraTransform =  imagePicker.cameraViewTransform;

Now how do I get the scale v开发者_如何学Pythonalue from cameraTransform?


I try to give a general answer for all kinds of CGAffineTransforms, even rotated ones.

Assuming your CGAffineTransform contains (optionally)

  • rotation
  • translation
  • scaling

and

  • NO skew

then the there’s a general formula that gives you the scale factor:

CGAffineTransform transform = ...;
CGFloat scaleFactor = sqrt(fabs(transform.a * transform.d - transform.b * transform.c));

"Mirroring" or flipping coordinate directions will be ignored, that means (x --> -x; y --> y) will result in scaleFactor == 1 instead of -1.


http://en.wikipedia.org/wiki/Determinant

"A geometric interpretation can be given to the value of the determinant of a square matrix with real entries: the absolute value of the determinant gives the scale factor by which area or volume is multiplied under the associated linear transformation, while its sign indicates whether the transformation preserves orientation. Thus a 2 × 2 matrix with determinant −2, when applied to a region of the plane with finite area, will transform that region into one with twice the area, while reversing its orientation."

The article goes on to give formulas for the determinant of a 3x3 matrix and a 2x2 matrix. CGAffineTransforms are 3x3 matrices, but their right column is always 0 0 1. The result is the determinant will be equal to the determinant of the 2x2 upper left square of the matrix. So you can use the values from the struct and compute the scale yourself.


The Quartz 2D Programming Guide tells you where to find the scale values. Compare that with the definition of the CGAffineTransform structure.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号