将角度风向转换为文本单词

发布于 2024-12-06 01:09:00 字数 303 浏览 2 评论 0原文

我有来自风向标的风向数据,数据以 0 到 359 度表示。

我想将其转换为具有 16 个不同方向的文本格式(罗盘)。

基本上我想知道是否有一种快速灵活的方法将角度读数缩放为 16 个字符串数组,以打印出正确的风向,而无需使用一堆 if 语句并检查角度范围

可以找到风向 此处

谢谢!

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 different directions.

Basically I want to know if there is a fast slick way to scale the angle reading to a 16 string array to print out the correct wind direction without using a bunch of if statements and checking for ranges of angles

Wind direction can be found here.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

背叛残局 2024-12-13 01:09:00

编辑:

由于每 22.5 度就有一个角度变化,因此方向应在 11.25 度后交换方向。

因此:

349-360//0-11 = N
12-33 = NNE
34-56 = NE

使用 327-348 之间的值(整个 NNW 频谱)无法产生 eudoxos 答案的结果。
经过深思熟虑,我找不到他逻辑中的缺陷,所以我重写了自己的逻辑。

def degToCompass(num):
    val=int((num/22.5)+.5)
    arr=["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
    print arr[(val % 16)]

>>> degToCompass(0)
N
>>> degToCompass(180)
S
>>> degToCompass(720)
N
>>> degToCompass(11)
N
>>> 12
12
>>> degToCompass(12)
NNE
>>> degToCompass(33)
NNE
>>> degToCompass(34)
NE

步骤:

  1. 将角度除以 22.5,因为 360 度/16 个方向 = 22.5 度/方向变化。
  2. 添加 0.5,以便在截断值时可以打破更改阈值之间的“联系”。
  3. 使用整数除法截断值(因此不进行舍入)。
  4. 直接索引到数组并打印值 (mod 16)。

EDIT :

Since there is an angle change at every 22.5 degrees, the direction should swap hands after 11.25 degrees.

Therefore:

349-360//0-11 = N
12-33 = NNE
34-56 = NE

Using values from 327-348 (The entire NNW spectrum) failed to produce a result for eudoxos' answer.
After giving it some thought I could not find the flaw in his logic, so i rewrote my own..

def degToCompass(num):
    val=int((num/22.5)+.5)
    arr=["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
    print arr[(val % 16)]

>>> degToCompass(0)
N
>>> degToCompass(180)
S
>>> degToCompass(720)
N
>>> degToCompass(11)
N
>>> 12
12
>>> degToCompass(12)
NNE
>>> degToCompass(33)
NNE
>>> degToCompass(34)
NE

STEPS :

  1. Divide the angle by 22.5 because 360deg/16 directions = 22.5deg/direction change.
  2. Add .5 so that when you truncate the value you can break the 'tie' between the change threshold.
  3. Truncate the value using integer division (so there is no rounding).
  4. Directly index into the array and print the value (mod 16).
花海 2024-12-13 01:09:00

这是 steve-gregory 答案的 javascript 实现,它对我有用。

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

有关逻辑的解释,请参阅他的答案

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.

南薇 2024-12-13 01:09:00

此 JavaScript 适用于只需要 8 个基本方向并想要相应箭头的任何人。

function getCardinalDirection(angle) {
    const directions = ['↑ N', '↗ NE', '→ E', '↘ SE', '↓ S', '↙ SW', '← W', '↖ NW'];
    return directions[Math.round(angle / 45) % 8];
}

对于风,按照惯例,箭头是相反的,因为角度表示源的方向而不是运动的方向。

function getWindDirection(angle) {
    const directions = ['↓ N', '↙ NE', '← E', '↖ SE', '↑ S', '↗ SW', '→ W', '↘ NW'];
    return directions[Math.round(angle / 45) % 8];
}

This JavaScript will work for anyone who only needs 8 cardinal directions and would like corresponding arrows.

function getCardinalDirection(angle) {
    const directions = ['↑ N', '↗ NE', '→ E', '↘ SE', '↓ S', '↙ SW', '← W', '↖ NW'];
    return directions[Math.round(angle / 45) % 8];
}

For wind, the arrows are reversed by convention, as the angle indicates the direction of the source rather than movement.

function getWindDirection(angle) {
    const directions = ['↓ N', '↙ NE', '← E', '↖ SE', '↑ S', '↗ SW', '→ W', '↘ NW'];
    return directions[Math.round(angle / 45) % 8];
}
忘你却要生生世世 2024-12-13 01:09:00

注意舍入,349...11 之间的角度应为“N”,因此先添加半个扇区 (+(360/16)/2),然后按 %360 处理超过 360 的溢出,然后除以 360/16:

["N","NNW",...,"NNE"][((d+(360/16)/2)%360)/(360/16)]

Watch out for rounding, angles between 349...11 should be "N", therefore add half sector first (+(360/16)/2), then handle overflow over 360 by %360, then divide by 360/16:

["N","NNW",...,"NNE"][((d+(360/16)/2)%360)/(360/16)]
空城之時有危險 2024-12-13 01:09:00

我检查了这个,效果很好而且看起来很准确。
资料来源: http://www.themethodology.net /2013/12/how-to-convert- Degrees-to-cardinal.html 作者:阿德里安·史蒂文斯

    public static string DegreesToCardinal(double degrees)
    {
        string[] caridnals = { "N", "NE", "E", "SE", "S", "SW", "W", "NW", "N" };
        return caridnals[(int)Math.Round(((double)degrees % 360) / 45)];
    }

    public static string DegreesToCardinalDetailed(double degrees)
    {
        degrees *= 10;

        string[] caridnals = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N" };
        return caridnals[(int)Math.Round(((double)degrees % 3600) / 225)];
    }

I checked this and it works very good and seems accurate.
Source: http://www.themethodology.net/2013/12/how-to-convert-degrees-to-cardinal.html by Adrian Stevens

    public static string DegreesToCardinal(double degrees)
    {
        string[] caridnals = { "N", "NE", "E", "SE", "S", "SW", "W", "NW", "N" };
        return caridnals[(int)Math.Round(((double)degrees % 360) / 45)];
    }

    public static string DegreesToCardinalDetailed(double degrees)
    {
        degrees *= 10;

        string[] caridnals = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N" };
        return caridnals[(int)Math.Round(((double)degrees % 3600) / 225)];
    }
极致的悲 2024-12-13 01:09:00

我相信更容易:

  1. 将方向移动 11.25
  2. 在方向列表末尾添加“N”来处理“超过 360”,
DirTable = ["N","NNE","NE","ENE","E","ESE", "SE","SSE","S","SSW","SW","WSW", "W","WNW","NW","NNW",**"N"**]; 

wind_direction= DirTable[Math.floor((d+11.25)/22.5)];

I believe it is easier to:

  1. Shift the direction by 11.25
  2. Add an "N" at the end of the direction list to handle the 'over 360',
DirTable = ["N","NNE","NE","ENE","E","ESE", "SE","SSE","S","SSW","SW","WSW", "W","WNW","NW","NNW",**"N"**]; 

wind_direction= DirTable[Math.floor((d+11.25)/22.5)];
凉城已无爱 2024-12-13 01:09:00

这是一个单行 python 函数:

def deg_to_text(deg):
    return ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"][round(deg/22.5)%16]

显然它可以分成多行以提高可读性/pep8

Here's a one-line python function:

def deg_to_text(deg):
    return ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"][round(deg/22.5)%16]

Obviously it can be split into multiple lines for readability/pep8

会发光的星星闪亮亮i 2024-12-13 01:09:00

如果您到达这里并且只对将您的学位分为 8 个方向之一感兴趣。

function degToCompass(num){
    const val =  Math.floor((num / 45) + 0.5);
    const arr = ["N","NE","E", "SE","S","SW","W","NW"];
    return arr[(val % 8)]

If you arrived here and are only interested in breaking your degrees into one of 8 directions.

function degToCompass(num){
    const val =  Math.floor((num / 45) + 0.5);
    const arr = ["N","NE","E", "SE","S","SW","W","NW"];
    return arr[(val % 8)]
岁月染过的梦 2024-12-13 01:09:00

我可能只会做简单的度数除法来获取数组中的位置或枚举值或可以为您提供所需文本的内容。将您的所有部门向下舍入。 360/16 = 22.5,因此您需要除以 22.5 以获得位置。

字符串[] a = [N,NNW,NW,WNW,...,NNE]

I would probably just do simple division of degrees to get a position in an array or an enum value or something that would give you the text you need. Just round down on all your division. 360/16 = 22.5, so you would want to divide by 22.5 to get the position.

String[] a = [N,NNW,NW,WNW,...,NNE]

染柒℉ 2024-12-13 01:09:00

这很好用

#!/usr/bin/env python

def wind_deg_to_str1(deg):
        if   deg >=  11.25 and deg <  33.75: return 'NNE'
        elif deg >=  33.75 and deg <  56.25: return 'NE'
        elif deg >=  56.25 and deg <  78.75: return 'ENE'
        elif deg >=  78.75 and deg < 101.25: return 'E'
        elif deg >= 101.25 and deg < 123.75: return 'ESE'
        elif deg >= 123.75 and deg < 146.25: return 'SE'
        elif deg >= 146.25 and deg < 168.75: return 'SSE'
        elif deg >= 168.75 and deg < 191.25: return 'S'
        elif deg >= 191.25 and deg < 213.75: return 'SSW'
        elif deg >= 213.75 and deg < 236.25: return 'SW'
        elif deg >= 236.25 and deg < 258.75: return 'WSW'
        elif deg >= 258.75 and deg < 281.25: return 'W'
        elif deg >= 281.25 and deg < 303.75: return 'WNW'
        elif deg >= 303.75 and deg < 326.25: return 'NW'
        elif deg >= 326.25 and deg < 348.75: return 'NNW'
        else: return 'N'

def wind_deg_to_str2(deg):
        arr = ['NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']
        return arr[int(abs((deg - 11.25) % 360)/ 22.5)]

i = 0
while i < 360:
        s1 = wind_deg_to_str1(i)
        s2 = wind_deg_to_str2(i)
        print '%5.1f deg -> func1(%-3s), func2(%-3s), same:%s' % (i, s1, s2, ('ok' if s1 == s2 else 'different'))
        i += 0.5

this works fine

#!/usr/bin/env python

def wind_deg_to_str1(deg):
        if   deg >=  11.25 and deg <  33.75: return 'NNE'
        elif deg >=  33.75 and deg <  56.25: return 'NE'
        elif deg >=  56.25 and deg <  78.75: return 'ENE'
        elif deg >=  78.75 and deg < 101.25: return 'E'
        elif deg >= 101.25 and deg < 123.75: return 'ESE'
        elif deg >= 123.75 and deg < 146.25: return 'SE'
        elif deg >= 146.25 and deg < 168.75: return 'SSE'
        elif deg >= 168.75 and deg < 191.25: return 'S'
        elif deg >= 191.25 and deg < 213.75: return 'SSW'
        elif deg >= 213.75 and deg < 236.25: return 'SW'
        elif deg >= 236.25 and deg < 258.75: return 'WSW'
        elif deg >= 258.75 and deg < 281.25: return 'W'
        elif deg >= 281.25 and deg < 303.75: return 'WNW'
        elif deg >= 303.75 and deg < 326.25: return 'NW'
        elif deg >= 326.25 and deg < 348.75: return 'NNW'
        else: return 'N'

def wind_deg_to_str2(deg):
        arr = ['NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']
        return arr[int(abs((deg - 11.25) % 360)/ 22.5)]

i = 0
while i < 360:
        s1 = wind_deg_to_str1(i)
        s2 = wind_deg_to_str2(i)
        print '%5.1f deg -> func1(%-3s), func2(%-3s), same:%s' % (i, s1, s2, ('ok' if s1 == s2 else 'different'))
        i += 0.5
多孤肩上扛 2024-12-13 01:09:00

要进行相反的转换(罗盘字母缩写为度数):

function getDir($b)
{

   $dirs = array('N'=>0, 'NNE'=>22.5,"NE"=>45,"ENE"=>67.5, 'E'=>90,'ESE'=>112.5, 'SE'=>135,'SSE'=>157.5, 'S'=>180,'SSW'=>202.5, 'SW'=>225,'WSW'=>247.5, 'W'=>270,'WNW'=>292.5,'NW'=>315,'NNW'=>337.5, 'N'=>0,'North'=>0,'East'=>90,'West'=>270,'South'=>180);
   return $dirs[$b];
}

To do the reverse conversion (compass letter abbreviations to degrees):

function getDir($b)
{

   $dirs = array('N'=>0, 'NNE'=>22.5,"NE"=>45,"ENE"=>67.5, 'E'=>90,'ESE'=>112.5, 'SE'=>135,'SSE'=>157.5, 'S'=>180,'SSW'=>202.5, 'SW'=>225,'WSW'=>247.5, 'W'=>270,'WNW'=>292.5,'NW'=>315,'NNW'=>337.5, 'N'=>0,'North'=>0,'East'=>90,'West'=>270,'South'=>180);
   return $dirs[$b];
}
2024-12-13 01:09:00

Javascript 函数 100% 工作

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

步骤

  1. 给定 360 度角
  2. 由于北在 -11.25 到 11.25 之间,我们减去 11.25 以获得准确度
  3. 将角度除以 22.5,因为 360 度/16 个方向 = 22.5度/方向改变
  4. Math.abs 因为负值仍然是北
  5. 从答案中选择 arr 的线段

希望有帮助

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

雨夜星沙 2024-12-13 01:09:00

我大量使用 R,因此需要一个解决方案。这是我想出的方法,并且适用于我喂给它的所有可能的组合:

degToCardinal <- function(degrees) {
  val <- as.integer((degrees / 22.5) + 0.5)
  arr <- c("N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW")
  return(arr[((val+1) %% 16)])
}

I use R heavily and needed a solution for this. This is what I came up with and works well for all possible combinations I have fed it:

degToCardinal <- function(degrees) {
  val <- as.integer((degrees / 22.5) + 0.5)
  arr <- c("N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW")
  return(arr[((val+1) %% 16)])
}
夜雨飘雪 2024-12-13 01:09:00

想要使用@eudoxos,但需要将所有部分放在一起:

def deg_to_compass(d):
  return ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
        "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"] [math.floor(((d+(360/16)/2)%360)/(360/16))]

借用@Hristo markow来检查结果:

for i in range(0,360):
  print (i,deg_to_compass(i) == wind_deg_to_str2(i))

Wanted to use @eudoxos but needed to pull all the parts together:

def deg_to_compass(d):
  return ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
        "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"] [math.floor(((d+(360/16)/2)%360)/(360/16))]

Borrrowed @Hristo markow to check the results:

for i in range(0,360):
  print (i,deg_to_compass(i) == wind_deg_to_str2(i))
謌踐踏愛綪 2024-12-13 01:09:00
compass_direction =["NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N"]

for i in range (0,365):
    index = (int) ((((i / 11.25) - 1) /2) % 16) 
    print(f"Angle: {i:3}, Index: {index}, Compass: {compass_direction[index]}")
compass_direction =["NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N"]

for i in range (0,365):
    index = (int) ((((i / 11.25) - 1) /2) % 16) 
    print(f"Angle: {i:3}, Index: {index}, Compass: {compass_direction[index]}")
み格子的夏天 2024-12-13 01:09:00

这是 flutter/dart 版本:

const windDirections = [
  "↑N",
  "NNE",
  "↗NE",
  "ENE",
  "→E",
  "ESE",
  "↘SE",
  "SSE",
  "↓S",
  "SSW",
  "↙SW",
  "WSW",
  "←W",
  "WNW",
  "↖NW",
  "NNW"
];
String degToCompass(final int degrees) =>
    windDirections[((degrees / 22.5) + 0.5).toInt() % 16];

Here is a flutter/dart version:

const windDirections = [
  "↑N",
  "NNE",
  "↗NE",
  "ENE",
  "→E",
  "ESE",
  "↘SE",
  "SSE",
  "↓S",
  "SSW",
  "↙SW",
  "WSW",
  "←W",
  "WNW",
  "↖NW",
  "NNW"
];
String degToCompass(final int degrees) =>
    windDirections[((degrees / 22.5) + 0.5).toInt() % 16];
黒涩兲箜 2024-12-13 01:09:00

在 Excel 中使用了这个:
VLOOKUP(MROUND(N12,22.5),N14:O29,2,FALSE)

单元格 N12 是需要答案的方向(以度为单位)。
范围 N14:O29 正在查找扇区(A 到 R):

WIND SECTOR
0A
22.5乙
45℃
67.5D
90E
112.5 华氏度
135克
157.5小时
180焦耳
202.5K
225升
247.5M
270牛
292.5P
第315章
337.5R

Used this in Excel:
VLOOKUP(MROUND(N12,22.5),N14:O29,2,FALSE)

Cell N12 is direction toward in degrees for which an answer is needed.
The range N14:O29 is looking up the sector(A to R):

WIND SECTOR
0 A
22.5 B
45 C
67.5 D
90 E
112.5 F
135 G
157.5 H
180 J
202.5 K
225 L
247.5 M
270 N
292.5 P
315 Q
337.5 R

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文