Posted By: HeelX
degree mod 180° - 07/05/14 10:20
Hi all,
I have written the following Lite-C function to shift a degree as a double value between -180° ... 0° ... +180°. The outcome is the same as the ang() function, but it works different: instead of repeatedly adding or subtracting a multiple of 360 degrees, I use the fmod function (which implements that maybe anyway, I don't know):
Can anyone think of a faster/more elegant solution? This feels somehow bloated...
I have written the following Lite-C function to shift a degree as a double value between -180° ... 0° ... +180°. The outcome is the same as the ang() function, but it works different: instead of repeatedly adding or subtracting a multiple of 360 degrees, I use the fmod function (which implements that maybe anyway, I don't know):
Code:
// clamps a floating point degree between -180° ... 0° ... +180° double avAngleDegreeMod180(double angleDegree) { if (angleDegree > 180) { double remainder = fmod(angleDegree, 360); if (remainder > 180) { return -180 + (remainder - 180); } else { return remainder; } } else { if (angleDegree >= -180) { return angleDegree; } else { return -avAngleDegreeMod180(-angleDegree); } } }
Can anyone think of a faster/more elegant solution? This feels somehow bloated...