计算2点之间的逆时针角度

时间:2017-05-03 14:47:50

标签: python numpy atan2

我有一个机器人,前面和后面分别安装了红色LED和绿色LED。我想计算机器人的头部方向,因为它指向的是greenLEd - redLed向量。

如何对其进行编码,使得下图中标记为1和2的点具有相同的角度,即逆时针45度,而点3应为225度。

enter image description here

我使用了以下脚本,但它给了我错误的结果:

def headDirectionAngle(redLEDCoords, greenLEDCoords, referenceVector):
    greenRedLEDVector = np.array(greenLEDCoords) - np.array(redLEDCoords)
    angle = np.math.atan2(np.linalg.det([referenceVector,greenRedLEDVector]),np.dot(referenceVector,greenRedLEDVector))
    return np.degrees(angle)
referenceVector = np.array([0,240])

我该怎么办?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

返回基础,没有numpy

atan2已经给出了逆时针角度,但介于-180和180之间。您可以添加360并计算模360以获得0到360之间的角度:

from math import atan2, degrees

def anti_clockwise(x,y):
    alpha = degrees(atan2(y,x))
    return (alpha + 360) % 360

print(anti_clockwise(480, 480))
# 45.0
print(anti_clockwise(-480, -480))
# 225.0

x应该是绿色和红色LED之间X坐标的差异。同样适用于y

相关问题