Converting Radians to Degrees and Degrees to Radians

April 12, 2011

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

3 comments

May I suggest to change #defines to
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI)) and
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI) — note the extra parentheses around radians and angle. Without them calling DEGREES_TO_RADIANS(20 + 45) will get you different results than calling DEGREES_TO_RADIANS(45).

by Rimantas on Apr 12, 2011. Reply #

Thank you Rimantas, I’ve updated the post.

by John Muchow on Apr 12, 2011. Reply #

nice utility :)

by Kaustubh Kushte on Sep 13, 2012. Reply #

Leave a Comment