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";
}
@tomasgreen
Copy link

Saved me some time. Thanks!

@zacck-zz
Copy link

A few Hours Saved! Thank you so much !

@jamespjarvis
Copy link

function getCardinalDirection(angle) {
  if (typeof angle === 'string') angle = parseInt(angle);
  if (angle <= 0 || angle > 360 || typeof angle === 'undefined') return '☈';
  const arrows = { north: '↑ N', north_east: '↗ NE', east: '→ E', south_east: '↘ SE', south: '↓ S', south_west: '↙ SW', west: '← W', north_west: '↖ NW' };
  const directions = Object.keys(arrows);
  const degree = 360 / directions.length;
  angle = angle + degree / 2;
  for (let i = 0; i < directions.length; i++) {
    if (angle >= (i * degree) && angle < (i + 1) * degree) return arrows[directions[i]];
  }
  return arrows['north'];
}

@JanineAmelie
Copy link

Thank you very much!

@S-Keilani
Copy link

Here is a short version:

function degToCompass(num) {
    var val = Math.floor((num / 22.5) + 0.5);
    var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
    return arr[(val % 16)];
}

https://stackoverflow.com/a/25867068/9282204

@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