Easy way to keeping angles between -179 and 180 degrees

后端 未结 16 2696
长发绾君心
长发绾君心 2020-12-04 16:41

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:


/         


        
16条回答
  •  一个人的身影
    2020-12-04 17:21

    Here is an integer-only solution:

    int normalize(int angle)
    {
        angle %= 360;
        int fix = angle / 180; // Integer division!!
        return (fix) ? angle - (360 * (fix)) : angle;
    }
    

    Sometimes being clever is just more fun, Platinum Azure.

提交回复
热议问题