校正 Python 中两点之间的角度

Correcting angles between two points in Python

我有一个python代码应用程序正在记录鼠标activity(定期记录点,timeWait)然后处理数据以输出鼠标移动的角度(与Y 轴为 0)及其移动的速度。

目前我的输出提供的东角为 0°,南角为 -90°,西角为 180°,北角为 90°,这不是我预期的输出。我不确定三角函数或其他计算中是否有问题,因此对此任何部分的任何帮助将不胜感激。

i = 0
angles = []
speeds = []

#positions is an array of coordinates taken 0.1 seconds apart from each other

for point in positions:
    if i is not 0:
        pointY = 2500 - point[1]  #invert position of X and assign variable
        deltaX = point[0]-pointXP # difference is current pointX minus PointXPast
        deltaY = pointY-pointYP
        dist = math.sqrt(deltaX**2 + deltaY**2) #distance is the hypotenuse
        if dist is 0: #if the mouse has not moved
            continue
        speed = dist/timeWait # speed is distance/time
        angle = math.degrees(math.atan2(deltaY,deltaX)) #angle using Tan

        angles.append(angle)
        speeds.append(speed)

        pointXP = point[0]
        pointYP = pointY

    else:  #this is only for the first iteration
        i = 1 
        pointXP = point[0]
        pointYP = 2500 - point[1]
        continue

再次感谢任何帮助,尤其是关于我的角度不对。谢谢

您使用 math.atan2 来计算角度。

docs

所述

The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis.

如果希望角度相对于正Y轴,可以将角度减去90度。 (即。angles.append(angle - 90)