Converting wind direction in angles to text words

后端 未结 15 763
执笔经年
执笔经年 2021-01-30 13:16

I have wind direction data coming from a weather vane, and the data is represented in 0 to 359 degrees.

I want to convert this into text format (compass rose) with 16 di

15条回答
  •  情歌与酒
    2021-01-30 13:50

    Here's a javascript implementation of steve-gregory's answer, which works for me.

    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)];
    }
    

    See his answer for an explanation of the logic.

提交回复
热议问题