Skip to content

Instantly share code, notes, and snippets.

@thebeardphantom
Last active August 29, 2015 14:11
Show Gist options
  • Save thebeardphantom/527ffe8580893b232288 to your computer and use it in GitHub Desktop.
Save thebeardphantom/527ffe8580893b232288 to your computer and use it in GitHub Desktop.
Ever need to normalize an angle of 1748 or -191857 degrees to 0...360 or -180...180? Now you can!
namespace BSGTools.Math {
public static class Normalizer {
public static float NormalizeAngle(float value) {
return Normalize(value, 0f, 360f);
}
public static float Normalize(float value, float start, float end) {
float width = end - start;
float offsetValue = value - start; // value relative to 0
return (offsetValue - ((float)System.Math.Floor(offsetValue / width) * width)) + start;
}
public static int NormalizeAngle(int value) {
return Normalize(value, 0, 360);
}
public static int Normalize(int value, int start, int end) {
int width = end - start;
int offsetValue = value - start; // value relative to 0
return (offsetValue - (offsetValue / width * width)) + start;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment