社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

在Python中更正两点之间的角度

P.B • 5 年前 • 211 次点击  

我有一个Python代码应用程序,它记录鼠标活动(按规则间隔记录的点,时间等待),然后处理数据以输出鼠标移动的角度(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

再次感谢您的帮助,特别是关于我的角度被关闭。谢谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50725
 
211 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Elro444
Reply   •   1 楼
Elro444    6 年前

你用 math.atan2 计算角度。

如中所述 docs ,

从原点到点(x,y)的平面中的向量使得这个角度与正x轴成正比。

如果你想要的角度是相对于正Y轴,你可以减去90度的角度。(即。 angles.append(angle - 90) )