Easy way to keeping angles between -179 and 180 degrees

Posted by User1 on Stack Overflow See other posts from Stack Overflow or by User1
Published on 2010-02-23T19:16:16Z Indexed on 2010/05/14 4:44 UTC
Read the original article Hit count: 122

Filed under:
|

Is there an easy way to convert an angle (in degrees) to be between -179 and 180? I'm sure I could use mod (%) and some if statements, but it gets ugly:


//Make angle between 0 and 360
angle%=360;

//Make angle between -179 and 180
if (angle>180) angle-=360;

It just seems like there should be a simple math operation that will do both statements at the same time. I may just have to create a static method for the conversion for now.

© Stack Overflow or respective owner

Related posts about java

Related posts about math