[英]Calculating difference in angle between two angles
我有一个 dataframe 方向以度数(0-360)为单位,我想获得以度数(0-180)为单位的矢量化差异到一个主要方向。
main_direction: 340
物品 | 度数 |
---|---|
0 | 10 |
1 | 40 |
2 | 0 |
3 | 320 |
4 | 80 |
现在我想获得主方向(340)和方向之间的绝对差,对于 dataframe 中的每个值,所以我的结果如下所示:
物品 | 区别 |
---|---|
0 | 30 |
1 | 60 |
2 | 20 |
3 | 20 |
4 | 100 |
它应该能够通过以下方式解决它:
df['Difference'] = df['Degrees']-main_direction) %360
或者
df['Difference'] = np.where(main_direction-df['Degrees'] < 0, df['Degrees']-main_direction + 360, abs(df['Degrees']-main_direction))
但这些都没有给我正确的结果。
def diff(angle):
res = (angle-main_direction)%360
if res < 180:
return res
else:
return (360-res)
或者你可以使用 lambda function
diff = lambda x: (x-main_direction)%360 if (x-main_direction)%360<180 else 360-(x-main_direction)%360
# Example
main_direction = 320
angles = [10,40,0,320,80]
for angle in angles:
print(diff(angle))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.