Skip to content

Instantly share code, notes, and snippets.

@basarat
Last active November 14, 2022 17:44
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save basarat/4670200 to your computer and use it in GitHub Desktop.
Save basarat/4670200 to your computer and use it in GitHub Desktop.
Snapping an angle to the nearest cardinal direction in javascript
/**
* Given "0-360" returns the nearest cardinal direction "N/NE/E/SE/S/SW/W/NW"
*/
export function getCardinal(angle) {
/**
* Customize by changing the number of directions you have
* We have 8
*/
const degreePerDirection = 360 / 8;
/**
* Offset the angle by half of the degrees per direction
* Example: in 4 direction system North (320-45) becomes (0-90)
*/
const offsetAngle = angle + degreePerDirection / 2;
return (offsetAngle >= 0 * degreePerDirection && offsetAngle < 1 * degreePerDirection) ? "N"
: (offsetAngle >= 1 * degreePerDirection && offsetAngle < 2 * degreePerDirection) ? "NE"
: (offsetAngle >= 2 * degreePerDirection && offsetAngle < 3 * degreePerDirection) ? "E"
: (offsetAngle >= 3 * degreePerDirection && offsetAngle < 4 * degreePerDirection) ? "SE"
: (offsetAngle >= 4 * degreePerDirection && offsetAngle < 5 * degreePerDirection) ? "S"
: (offsetAngle >= 5 * degreePerDirection && offsetAngle < 6 * degreePerDirection) ? "SW"
: (offsetAngle >= 6 * degreePerDirection && offsetAngle < 7 * degreePerDirection) ? "W"
: "NW";
}
@breyed
Copy link

breyed commented Feb 13, 2019

I added an answer that combined the array index method with direction glyphs.

@Akash0333
Copy link

Why we divide degree/2 ? . Anyone can help me

@Akash0333
Copy link

@basarat can you please help me. I am taking only directions 4 so I will divide degree/4 is it's correct?

@basarat
Copy link
Author

basarat commented Aug 28, 2019

@Akash0333 Yes, divide by 4. I've also updated to what I would right today. Should be easier now 🌹

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment