我正在尝试构建一个函数,以便在给定风向的情况下,我可以获得相对于行进方向的风向。我已经提供了一个长期这样做的例子。感觉臃肿和简陋。
我遇到了麻烦,因为风是基于矢量的,我需要找到一种方法来评估函数,同时考虑 360 度圆。
def relative_wind(wind_heading, rider_heading):
if 320 < wind_heading < 60:
wind_direction = 'north'
elif 140 > wind_heading > 60:
wind_direction = 'east'
elif 230 > wind_heading > 140:
wind_direction = 'south'
else:
wind_direction = 'west'
if 320 < rider_heading < 60:
rider_direction = 'north'
elif 140 > rider_heading > 60:
rider_direction = 'east'
elif 230 > rider_heading > 140:
rider_direction = 'south'
else:
rider_direction = 'west'
if rider_direction == 'north':
if wind_direction == 'north':
relative_direction = 'tail'
elif wind_direction == 'east':
relative_direction = 'right'
elif wind_direction == 'south':
relative_direction = 'head'
else:
relative_direction = 'left'
return relative_direction
elif rider_direction == 'east':
if wind_direction == 'north':
relative_direction = 'left'
elif wind_direction == 'east':
relative_direction = 'tail'
elif wind_direction == 'south':
relative_direction = 'right'
else:
relative_direction = 'head'
return relative_direction
elif rider_direction == 'west':
if wind_direction == 'north':
relative_direction = 'right'
elif wind_direction == 'east':
relative_direction = 'head'
elif wind_direction == 'south':
relative_direction = 'left'
else:
relative_direction = 'tail'
return relative_direction
else:
if wind_direction == 'north':
relative_direction = 'head'
elif wind_direction == 'east':
relative_direction = 'left'
elif wind_direction == 'south':
relative_direction = 'tail'
else:
relative_direction = 'right'
return relative_direction
print relative_wind(285, 285)
你选择的角度有点奇怪:
从勾勒出您的角度实际应该是什么样子开始可能是个好主意;以轴为中心的象限边界为 45、135、225 和 315 度。
与其使用硬编码的航向,我建议您创建一个函数来获取您的航向和风向并返回相对风向:
def relative_wind_direction(heading, wind_dir):
"""
Return wind direction relative to plane heading, in [-180..180) degrees
"""
return ((wind_dir - heading + 180) % 360) - 180
正如@sjy 所建议的,这使用 %
mod 运算符将结果固定在所需范围内。
您现在可以编写如下函数
def wind_aspect(heading, wind_dir):
rel_dir = relative_wind_direction(heading, wind_dir)
if rel_dir < -135:
return "head"
elif rel_dir < -45:
return "right"
elif rel_dir < 45:
return "tail"
elif rel_dir < 135:
return "left"
else:
return "head"
我是一名优秀的程序员,十分优秀!