寻找方向角θ值
Finding directional angle theta value
我有两个角(以度为单位,限于0-359
),我想求出这两个角在需要数最小的方向上的方向差
例如:
0, 359
将是 -1
(左 1
),而不是 +359
180, 2
将是 -178
(左 178
),而不是 +182
我找到了一些代码,可以让我在没有方向的情况下找到不同之处。我应该如何修改它才能定向工作?
180 - abs(abs(old - new) - 180)
我首先开始了人行道并比较了两种可能的旋转方式:
def nearest_signed(old, new):
angles = ((new - old)%360, (new - old)%360 - 360)
return min(angles, key=abs)
我们检查模 360 角及其在另一个方向的补角。
似乎有效:
>>> nearest_signed(0, 359)
-1
>>> nearest_signed(359, 0)
1
>>> nearest_signed(180, 2)
-178
>>> nearest_signed(2, 180)
178
现在,我想看看它的表现如何,所以我绘制了每个角度组合的图:
import numpy as np
matplotlib.pyplot as plt
news,olds = np.ogrid[:360, :360]
rights = (news - olds) % 360
lefts = rights - 360
take_rights = abs(rights) < abs(lefts)
nearest_signed = np.where(take_rights, rights, lefts)
fig,ax = plt.subplots()
cf = ax.contourf(news.ravel(), olds.ravel(), nearest_signed, cmap='viridis', levels=np.linspace(-180, 180, 100), vmin=-180, vmax=180)
ax.set_xlabel('new angle')
ax.set_ylabel('old angle')
cb = plt.colorbar(cf, boundaries=(-180, 180))
plt.show()
现在很明显,角度差的简单模数应该起作用。果然:
>>> np.array_equal((news - olds + 180) % 360 - 180, nearest_signed)
True
意思是你要找的公式是
(new - old + 180) % 360 - 180
在你的约定中给出或采取不同的符号。如果你反过来算旋转标志,只需交换两个角度:
(old - new + 180) % 360 - 180
我有两个角(以度为单位,限于0-359
),我想求出这两个角在需要数最小的方向上的方向差
例如:
0, 359
将是-1
(左1
),而不是+359
180, 2
将是-178
(左178
),而不是+182
我找到了一些代码,可以让我在没有方向的情况下找到不同之处。我应该如何修改它才能定向工作?
180 - abs(abs(old - new) - 180)
我首先开始了人行道并比较了两种可能的旋转方式:
def nearest_signed(old, new):
angles = ((new - old)%360, (new - old)%360 - 360)
return min(angles, key=abs)
我们检查模 360 角及其在另一个方向的补角。
似乎有效:
>>> nearest_signed(0, 359)
-1
>>> nearest_signed(359, 0)
1
>>> nearest_signed(180, 2)
-178
>>> nearest_signed(2, 180)
178
现在,我想看看它的表现如何,所以我绘制了每个角度组合的图:
import numpy as np
matplotlib.pyplot as plt
news,olds = np.ogrid[:360, :360]
rights = (news - olds) % 360
lefts = rights - 360
take_rights = abs(rights) < abs(lefts)
nearest_signed = np.where(take_rights, rights, lefts)
fig,ax = plt.subplots()
cf = ax.contourf(news.ravel(), olds.ravel(), nearest_signed, cmap='viridis', levels=np.linspace(-180, 180, 100), vmin=-180, vmax=180)
ax.set_xlabel('new angle')
ax.set_ylabel('old angle')
cb = plt.colorbar(cf, boundaries=(-180, 180))
plt.show()
现在很明显,角度差的简单模数应该起作用。果然:
>>> np.array_equal((news - olds + 180) % 360 - 180, nearest_signed)
True
意思是你要找的公式是
(new - old + 180) % 360 - 180
在你的约定中给出或采取不同的符号。如果你反过来算旋转标志,只需交换两个角度:
(old - new + 180) % 360 - 180