Search code examples
javascripthuman-readableazimuth

convert precisely azimuth degrees to human


I'm write this bit of code to convert azimuth degrees to human redable, but the conversion is not more accurate, in cases of degrees is slightly smaller than 360.

function azimutHuman(ang) {     //can be 0 - 360

    var azimuthHumans = [
              'Nord','Nord-NE','Nord-Est','Est-NE',
              'Est','Est-SE','Sud-Est','Sud-SE',
              'Sud','Sud-SO','Sud-Ovest','Ovest-SO',
              'Ovest','Ovest-SO','Nord-Ovest','Nord-NO'];

    return azimuthHumans[ Math.round(ang/22.5) ];
}

for example:

azimutHuman(350);   //return undefined instead of "Nord"

Solution

  • 350 -> 360 = 0->10 : 360 is north as will as 0 .

    so you need to add an element "Nord" again .try this function now :

    function azimutHuman(ang) {     //can be 0 - 360
    
        var azimuthHumans = [
                  'Nord','Nord-NE','Nord-Est','Est-NE',
                  'Est','Est-SE','Sud-Est','Sud-SE',
                  'Sud','Sud-SO','Sud-Ovest','Ovest-SO',
                  'Ovest','Ovest-SO','Nord-Ovest','Nord-NO','Nord'];
    
        return azimuthHumans[ Math.round(ang/22.5) ];
    }