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

2024-10-04 11:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个机器人,前面和后面分别安装了红色和绿色的发光二极管。我想计算机器人的头部方向,绿色-红色的矢量指向哪个方向。在

我如何编码,使下面图像中标记为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])

我该怎么做?谢谢你的帮助。在


Tags: 矢量np机器人方向array指向二极管绿色
1条回答
网友
1楼 · 发布于 2024-10-04 11:29:28

回到基础,没有numpy。在

^{}已经给出了一个逆时针的角度,但在-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也是如此。在

相关问题 更多 >