Python 计算两点之间的逆时针角度
我有一个机器人,前面和后面分别安装了红色led和绿色led。我想计算机器人的头部方向,绿色-红色的矢量指向哪个方向 如何对其进行编码,使下图中标记为1和2的点具有相同的角度,即逆时针45度,而点3应为225度 我使用了以下脚本,但结果不正确:Python 计算两点之间的逆时针角度,python,numpy,atan2,Python,Numpy,Atan2,我有一个机器人,前面和后面分别安装了红色led和绿色led。我想计算机器人的头部方向,绿色-红色的矢量指向哪个方向 如何对其进行编码,使下图中标记为1和2的点具有相同的角度,即逆时针45度,而点3应为225度 我使用了以下脚本,但结果不正确: def headDirectionAngle(redLEDCoords, greenLEDCoords, referenceVector): greenRedLEDVector = np.array(greenLEDCoords) - np.ar
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])
我应该如何进行?谢谢您的帮助。回到基础,无需
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
这个向量的目标是什么?atan2给出了与水平轴相对的角度。有什么方法可以更新它来计算矢量的角度wrt吗?@Deathraser:您可以计算两个矢量的水平轴的角度wrt,并计算差值。