Converting wind direction in angles to text words

后端 未结 15 788
执笔经年
执笔经年 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:51

    Javascript function 100% working

    function degToCompass(num) { 
        while( num < 0 ) num += 360 ;
        while( num >= 360 ) num -= 360 ; 
        val= Math.round( (num -11.25 ) / 22.5 ) ;
        arr=["N","NNE","NE","ENE","E","ESE", "SE", 
              "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"] ;
        return arr[ Math.abs(val) ] ;
    }
    

    steps

    1. Given a 360 degree angle
    2. Since north is between -11.25 to 11.25 we subtract 11.25 for accuracy
    3. Divide the angle by 22.5 because 360deg/16 directions = 22.5deg/direction change
    4. Math.abs for as negative is still north
    5. Select the segment from arr from answer

    Hope it helps

提交回复
热议问题