Cavewhere / cavewhere

3D Cave Mapping Software
https://cavewhere.com
GNU General Public License v3.0
34 stars 8 forks source link

Cavewhere thinks 1° and 359° are 358° apart, rather than 2° (when backsight is marked corrected) #195

Open jedwards1211 opened 3 years ago

jedwards1211 commented 3 years ago

image

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;
}