Here are some angle functions I use. The fmod can be omitted if you don't expect the inputs to be outside the range [0, 360].
// returns rotation in the range [-180, 180] such that from + rotation = to (mod 360)
float rotationBetween(float from, float to) {
float raw = fmod((to - from), 360f);
return raw < -180f
? raw + 360f
: raw > 180f
? raw - 360f
: raw;
}
// more efficient version of fabs(rotationBetween(a, b))
// returns absolute difference in the range [0, 180]
float absAngleDifference(float a, float b) {
float raw = fmod(fabs(a - b), 360f);
return raw > 180f ? 360f - raw : raw;
}
Here are some angle functions I use. The
fmod
can be omitted if you don't expect the inputs to be outside the range [0, 360].