Converting Radians to Degrees and Degrees to RadiansTweet
In mathematics, a radian is often used as a standard unit of measure when expressing angles. For various methods in iOS classes that deal with angles and rotation, the parameters and return values are often in radians. My brain doesn’t think in radians, I visualize angles and rotations in degrees, hence the code in this post to convert between the two.
In the post Rotate an Image with Animation I wrote a method to rotate an image a specified number of degrees, however the method that does the work, CGAffineTransformMakeRotation, requires a radian value as input.
More recently, in the post Gestures Recognizers – Tap, Pinch/Zoom, Rotate, Swipe, Pan, Long Press to show angle of rotation of a two-finger gesture in degrees, I had to convert the value from the UIRotationGestureRecognizer rotation property, which was also expressed in radians.
Below are two macros that you can copy/paste into your code to simplify the conversion between radians and degrees.
Radians to Degrees
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI)) ... NSLog(@"Output radians as degrees: %f", RADIANS_TO_DEGREES(0.785398));
Degrees to Radians
To convert the other direction:
// Degrees to radians #define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI) ... NSLog(@"Output degrees as radians: %f", DEGREES_TO_RADIANS(45));
The value for π (M_PI) used in the above macros is defined in Math.h as 3.14159265358979323846264338327950288