Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Scale Value from CGAffineTransform

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 value from cameraTransform?

like image 899
Mark Armstrong Avatar asked Oct 15 '11 05:10

Mark Armstrong


2 Answers

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.

like image 51
Martin Stämmler Avatar answered Sep 22 '22 06:09

Martin Stämmler


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.

like image 28
morningstar Avatar answered Sep 23 '22 06:09

morningstar