I have two functions that are intended to contain angles between (-180,180] and (-π,π]. The intent is that given any angle from -inf to +inf it will retain the equivalent an
For the angle in degrees, if x
is between -180 and 180, then 180 - x
is between 0 and 360. What you want is equivalent to asking that 180 - x
is between 0 (inclusive), and 360 (exclusive). So, as soon as 180 - x
reaches 360, we want to add 360 to the angle. This gives us:
return angle + 360d * Math.Floor((180d - angle) / 360d);
Same thing for the angle in radians:
return angle + twopi * Math.Floor((Math.PI - angle) / twopi);