Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get just the rotation from a CGAffineTransform

Is it possible to get just the angle of rotation from a CGAffineTransform that also has translation and scaling values? If yes, how?

Thanks in advance!

like image 229
Pieter Jongsma Avatar asked Oct 01 '09 14:10

Pieter Jongsma


2 Answers

atan2(rotationTransform.b, rotationTransform.d)

The acos(b) solution suggested by someone is only valid in the range +/-0.5pi. This solution seems robust under combinations of rotation, translation and scaling although almost certainly not shear. My earlier answer (atan2(rotationTransform.b, rotationTransform.a) was not robust to scaling.

This duplicate was the basis for my original answer.

like image 175
Joseph Lord Avatar answered Nov 11 '22 13:11

Joseph Lord


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

struct CGAffineTransform {
   CGFloat a;
   CGFloat b;
   CGFloat c;
   CGFloat d;
   CGFloat tx;
   CGFloat ty;
};
like image 21
zaph Avatar answered Nov 11 '22 13:11

zaph