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 178° apart, rather than 2° #192

Closed jedwards1211 closed 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 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))
float absAngleDifference(float a, float b) {
  float raw = fmod(fabs(a, b), 360f);
  return raw > 180f ? 360f - raw : raw;
}
jedwards1211 commented 3 years ago

Whoops I hadn't marked corrected backsight, but, there's a separate issue in that case