Last active
November 26, 2016 10:47
-
-
Save rattfieldnz/dc5397099abf3e856b322be130be7cd5 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* 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