Get only turn from CGAffineTransform

Is it possible to get only the rotation angle from CGAffineTransform, which also has translation and scaling values? If so, how?

Thanks in advance!

+6
objective-c iphone transform
source share
5 answers

Take asin () or acos () of the affine element b or c.

struct CGAffineTransform { CGFloat a; CGFloat b; CGFloat c; CGFloat d; CGFloat tx; CGFloat ty; }; 
+5
source share
 atan2(rotationTransform.b, rotationTransform.d) 

The acos (b) solution proposed by someone is only valid in the +/- 0.5pi range. This solution seems reliable with a combination of rotation, translation, and scaling, although it almost certainly does not move. My previous answer ( atan2(rotationTransform.b, rotationTransform.a ) was not scalable.

This duplicate served as the basis for my initial answer .

+5
source share

You can try using the acos () and asin () functions to undo what CGAffineTransformMakeRotation does . However, since you have a scale transformation matrix multiplied by it, it can be a little difficult to do.

0
source share

In the past, I used the XAffineTransform class from the GeoTools toolkit to extract rotation from an affine matrix. It works even when using zoom and shift.

This is a Java library, but you should easily convert it to (Objective-) C. You can see the source for XAffineTransform here . (The method is called getRotation.) And you can read the API here .

This is the basis of the method:

 final double scaleX = getScaleX0(tr); final double scaleY = getScaleY0(tr) * flip; return Math.atan2(tr.getShearY()/scaleY - tr.getShearX()/scaleX, tr.getScaleY()/scaleY + tr.getScaleX()/scaleX); 

You also need to implement the getScale / getShear methods. Not difficult. (For the most part, you can simply copy Java code as is.)

0
source share

asin and acos are incorrect when there is a scale.

correct formula:

 CGFloat radians = atan2f(self.view.transform.b, self.view.transform.a); CGFloat degrees = radians * (180 / M_PI); 

or

 CGFloat angle = [(NSNumber *)[view valueForKeyPath:@"layer.transform.rotation.z"] floatValue]; 
0
source share

All Articles