获取具有相同起点的两个向量之间的角度2D

2024-10-01 02:32:19 发布

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

我在计算相同起点的2个向量之间的角度时遇到了一些问题-意味着有3个坐标

我曾尝试定义此函数,但当给出超过1个向量时,它不起作用:

def AngleBtw2Points(pointA, pointB):
    ang1 = np.arctan2(*pointA[::-1])
    ang2 = np.arctan2(*pointB[::-1])
    return np.rad2deg((ang1 - ang2) % (2 * np.pi))

picture with example of the issue

输入是(X0,Y0)(X1,Y1)(X2,Y2)


Tags: 函数return定义defnp向量角度起点
2条回答
def AngleBtw2Points(pointDES, pointSRC, pointA):
    ang = math.degrees(math.atan2(pointA[1]-pointSRC[1], pointA[0]-pointSRC[0]) - math.atan2(pointDES[1]-pointSRC[1], pointDES[0]-pointSRC[0]))
    return ang + 360 if ang < 0 else ang

这解决了我的问题,谢谢你的帮助:)

如注释中所述,可以很容易地使用点积来获得两个向量之间的角度

def AngleBtw2Vectors(vecA, vecB):
    unitVecA = vecA / np.linalg.norm(vecA)
    unitVecB = vecB / np.linalg.norm(vecB)
    return np.arccos(np.dot(unitVecA, unitVecB))

相关问题 更多 >