Easy way to keeping angles between -179 and 180 degrees

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

    A short way which handles negative numbers is

    double mod = x - Math.floor((x + 179.0) / 360) * 360;
    

    Cast to taste.

    BTW: It appears that angles between (180.0, 181.0) are undefined. Shouldn't the range be (-180, 180] (exclusive, inclusive]

提交回复
热议问题