Easy way to keeping angles between -179 and 180 degrees

后端 未结 16 2654
长发绾君心
长发绾君心 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:09

    It is better to use library functions. They handle special cases like NaN and infinities.

    public static double normalizeAngleDegrees(double angle) {
        return Math.toDegrees(Math.atan2(Math.sin(Math.toRadians(angle)), Math.cos(Math.toRadians(angle))));
    }   
    

提交回复
热议问题