Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. /**
  2. * Given "0-360" returns the nearest cardinal direction "N/NE/E/SE/S/SW/W/NW"
  3. */
  4. export function getCardinal(angle) {
  5. /**
  6. * Customize by changing the number of directions you have
  7. * We have 8
  8. */
  9. const degreePerDirection = 360 / 8;
  10.  
  11. /**
  12. * Offset the angle by half of the degrees per direction
  13. * Example: in 4 direction system North (320-45) becomes (0-90)
  14. */
  15. const offsetAngle = angle + degreePerDirection / 2;
  16.  
  17. return (offsetAngle >= 0 * degreePerDirection && offsetAngle < 1 * degreePerDirection) ? "N"
  18. : (offsetAngle >= 1 * degreePerDirection && offsetAngle < 2 * degreePerDirection) ? "NE"
  19. : (offsetAngle >= 2 * degreePerDirection && offsetAngle < 3 * degreePerDirection) ? "E"
  20. : (offsetAngle >= 3 * degreePerDirection && offsetAngle < 4 * degreePerDirection) ? "SE"
  21. : (offsetAngle >= 4 * degreePerDirection && offsetAngle < 5 * degreePerDirection) ? "S"
  22. : (offsetAngle >= 5 * degreePerDirection && offsetAngle < 6 * degreePerDirection) ? "SW"
  23. : (offsetAngle >= 6 * degreePerDirection && offsetAngle < 7 * degreePerDirection) ? "W"
  24. : "NW";
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement