Created
September 16, 2024 20:27
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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