Skip to content

Instantly share code, notes, and snippets.

@rattfieldnz
Last active November 26, 2016 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rattfieldnz/dc5397099abf3e856b322be130be7cd5 to your computer and use it in GitHub Desktop.
Save rattfieldnz/dc5397099abf3e856b322be130be7cd5 to your computer and use it in GitHub Desktop.
/**
* A function to convert direction in degrees to general compass direction.
* @param {Number} deg The value to be converted, in degrees.
* @return {String} The converted compass direction.
*/
function convertDegreesToCompass(deg){
// The 16 general compass directions
var compassDirections = ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"];
// There are 16 general compass directions.
// Dividing 360 by 16 equals 22.5.
// 0.5 is added to prevent ties.
var directionIndex = null;
if($.isNumeric(deg))
{
if(deg >=0){
directionIndex=+Math.round(((deg/22.5)+0.5)%16);
return compassDirections[directionIndex - 1];
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment