Easy way to keeping angles between -179 and 180 degrees

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

    // reduce the angle  
    angle =  angle % 360; 
    
    // force it to be the positive remainder, so that 0 <= angle < 360  
    angle = (angle + 360) % 360;  
    
    // force into the minimum absolute value residue class, so that -180 < angle <= 180  
    if (angle > 180)  
        angle -= 360;  
    

提交回复
热议问题