Skip to content

Instantly share code, notes, and snippets.

@doneill
Created September 16, 2024 20:27
Show Gist options
  • Save doneill/8a23a2895e8bc7c23650469329484e14 to your computer and use it in GitHub Desktop.
Save doneill/8a23a2895e8bc7c23650469329484e14 to your computer and use it in GitHub Desktop.
Calculate angle between 2 headings, used to determine if change of direction meets some angle threshold
const calculateAngleBetweenHeadings = (heading1: number, heading2: number): number => {
// Normalize headings to be between 0 and 360 degrees
// eslint-disable-next-line no-param-reassign
heading1 %= 360;
// eslint-disable-next-line no-param-reassign
heading2 %= 360;
// Calculate the absolute difference
let difference = Math.abs(heading1 - heading2);
// If the difference is greater than 180, we need to go the other way around the circle
if (difference > 180) {
difference = 360 - difference;
}
return difference;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment