Javascript 比较两种罗盘方位
我有两个指南针方位(0-360度): 我需要比较这些方位,以确定车手是否会得到 a) 侧风 b) 尾风 c) 逆风 我尝试将方位转换为指南针方向,例如:Javascript 比较两种罗盘方位,javascript,jquery,Javascript,Jquery,我有两个指南针方位(0-360度): 我需要比较这些方位,以确定车手是否会得到 a) 侧风 b) 尾风 c) 逆风 我尝试将方位转换为指南针方向,例如: var compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']; var windDirection = compass[Math.round(bearing /
var compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'];
var windDirection = compass[Math.round(bearing / 22.5)];
然后进行基本的字符串比较:
if (routeDirection=='N') && (windDirection=='S') {
output = 'Headwind'
}
但显然这是冗长和低效的 假设你是笔直向上走,你会有:
\ Head wind /
\ /
\ /
\ | /
\ | /
Cross\|/Winds
/ \
/ \
/ \
/ \
/ \
/ Tail wind \
所以基本上。。。首先,旋转视点,以使您在方向角0处行驶:
var adjustedWindDirection = windDirection - routeDirection;
当然,轴承应在0-360范围内,因此再次调整:
adjustedWindDirection = (adjustedWindDirection+360)%360;
现在我们需要找出方向在哪个象限:
var quadrant = Math.round((adjustedWindDirection-45)/90);
最后:
var winds = ["head","cross (left)","tail","cross (right)"];
var resultingWind = winds[quadrant];
完成了 假设你是笔直向上走,你会有:
\ Head wind /
\ /
\ /
\ | /
\ | /
Cross\|/Winds
/ \
/ \
/ \
/ \
/ \
/ Tail wind \
所以基本上。。。首先,旋转视点,以使您在方向角0处行驶:
var adjustedWindDirection = windDirection - routeDirection;
当然,轴承应在0-360范围内,因此再次调整:
adjustedWindDirection = (adjustedWindDirection+360)%360;
现在我们需要找出方向在哪个象限:
var quadrant = Math.round((adjustedWindDirection-45)/90);
最后:
var winds = ["head","cross (left)","tail","cross (right)"];
var resultingWind = winds[quadrant];
完成了 我会直接比较路线方向和风向,并根据差异确定风的类型:
if(routeDirection > windDirection)
var difference = routeDirection - windDirection;
else
var difference = windDirection - routeDirection;
// left/right not important, so only need parallel (0) or antiparallel (180)
difference = difference - 180;
//keep value positive for comparison check
if(difference < 0)
difference = difference * -1;
if(difference <= 45) // wind going in roughly the same direction, up to you and your requirements
output = "headwind";
elseif(difference <= 135) // cross wind
output = "crosswind";
elseif (difference <= 180)
output = "tailwind";
else
output = "something has gone wrong with the calculation...";
if(路由方向>风向)
var差=路由方向-风向;
其他的
var差=风向-路由方向;
//左/右不重要,所以只需要平行(0)或反平行(180)
差值=差值-180;
//将值保持为正值以进行比较检查
如果(差异<0)
差异=差异*-1;
如果(差异我将直接比较路线方向和风向,并根据差异确定风的类型:
if(routeDirection > windDirection)
var difference = routeDirection - windDirection;
else
var difference = windDirection - routeDirection;
// left/right not important, so only need parallel (0) or antiparallel (180)
difference = difference - 180;
//keep value positive for comparison check
if(difference < 0)
difference = difference * -1;
if(difference <= 45) // wind going in roughly the same direction, up to you and your requirements
output = "headwind";
elseif(difference <= 135) // cross wind
output = "crosswind";
elseif (difference <= 180)
output = "tailwind";
else
output = "something has gone wrong with the calculation...";
if(路由方向>风向)
var差=路由方向-风向;
其他的
var差=风向-路由方向;
//左/右不重要,所以只需要平行(0)或反平行(180)
差值=差值-180;
//将值保持为正值以进行比较检查
如果(差异<0)
差异=差异*-1;
如果(不同之处)你能补充一些关于你想要的结果的更多信息吗?例如,为了显示逆风,风向必须与路线方向匹配多近?在22.5度范围内?是的,22.5度就可以了。我有一个风速变量,我将用它来表示强迎风、弱尾风等。你能补充一些信息吗请给出您想要的结果?例如,为了显示逆风,风向必须与路线方向匹配多近?在22.5度范围内?是的,22.5度就可以了。我有一个风速变量,我将用它来表示强迎风、弱尾风等。谢谢,解释得很好。但我得到的结果正好相反e、 g尾巴,当它应该是逆风时。请看这里:谢谢,解释得很好。但是我得到了相反的结果,例如尾巴,当它应该是逆风时。请看这里:谢谢,这对我有效。它还可以更容易地添加更多变量如果这是你接受的答案,勾选“接受答案”按钮会有所帮助(看起来像投票值旁边的一个勾号)。对不起,我得到了相反的结果,例如,如果我向西行驶,风向为东,它应该返回为逆风,但返回为顺风。请参见此处:答案已编辑;我刚刚交换了“逆风”和“顺风”大约。谢谢,这对我很有用。它还可以更轻松地添加更多变量。如果这是您接受的答案,请勾选“接受答案”按钮(看起来像投票值旁边的记号)。抱歉,我得到了相反的结果,例如,如果我向西行驶,风向为东,它应该作为逆风返回,但它作为顺风返回。请参见此处:答案已编辑;我刚刚交换了“逆风”和“顺风”。