我有两个指南针方位(0-360度):
var routeDirection = 28
var windDirection = 289我需要比较这些轴承,以确定骑手是否会得到一个
( 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 / 22.5)];然后进行基本的字符串比较:
if (routeDirection=='N') && (windDirection=='S') {
output = 'Headwind'
}但很明显这既冗长又低效..。
发布于 2014-02-28 11:21:54
我会直接比较路线方向和风向,并根据差异来确定风的类型:
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...";上面的计算意味着你没有对每个指南针点进行比较,只对船的航向和风的相对差异进行比较,从而减少了细节。它还允许多角度比较,使用较小的度步骤和添加更多的其他条件。这也可以用switch()来完成,但是也会有类似的代码行计数。
发布于 2014-02-28 11:06:21
如果你要直截了当的话,你就会有这个:
\ 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];完成了!
https://stackoverflow.com/questions/22093244
复制相似问题