python计算鼠标速度

时间:2011-08-01 17:48:08

标签: python

我在python中使用以下方法来获取任何给定的

的X,Y坐标
data = display.Display().screen().root.query_pointer()._data 
x = data["root_x"] 
y = data["root_y"] 
z = time.time()

我想计算给定时间内的鼠标速度,有什么方法可以计算并以每小时英里数显示鼠标速度???

krisdigitx


我现在设法解决问题并使用此方法计算最后两个已知x和y位置之间的速度

        dx = float(x) - float(a1)
        dy = float(y) - float(b1)
        dist = math.sqrt( math.pow(dx,2) + math.pow(dy,2))
        dz = float(z) - float(c1)
        speed = float(dist/dz)

现在应遵循什么规则将速度转换为每小时英里数?感谢您的所有帮助,这是实时的输出..

speed = 1512.53949852 Time = 4:30:690187 CPUTime = 1312531470.7 X = 701 Y = 600 PX = 692 PY = 605 PT = 1312531470.69
speed = 0.0 Time = 4:30:697020 CPUTime = 1312531470.7 X = 701 Y = 600 PX = 701 PY = 600 PT = 1312531470.7
speed = 1563.45505256 Time = 4:30:703667 CPUTime = 1312531470.73 X = 734 Y = 586 PX = 701 PY = 600 PT = 1312531470.7
speed = 0.0 Time = 4:30:726614 CPUTime = 1312531470.73 X = 734 Y = 586 PX = 734 PY = 586 PT = 1312531470.73
speed = 882.257032576 Time = 4:30:735274 CPUTime = 1312531470.76 X = 753 Y = 580 PX = 734 PY = 586 PT = 1312531470.73
speed = 0.0 Time = 4:30:756930 CPUTime = 1312531470.76 X = 753 Y = 580 PX = 753 PY = 580 PT = 1312531470.76
speed = 363.108272412 Time = 4:30:764397 CPUTime = 1312531470.79 X = 762 Y = 580 PX = 753 PY = 580 PT = 1312531470.76
speed = 373.79057125 Time = 4:30:789201 CPUTime = 1312531470.8 X = 765 Y = 580 PX = 762 PY = 580 PT = 1312531470.79
speed = 92.0338354526 Time = 4:30:797211 CPUTime = 1312531470.82 X = 767 Y = 580 PX = 765 PY = 580 PT = 1312531470.8
speed = 0.0 Time = 4:30:818938 CPUTime = 1312531470.83 X = 767 Y = 580 PX = 767 PY = 580 PT = 1312531470.82
speed = 46.9571214259 Time = 4:30:826073 CPUTime = 1312531470.85 X = 767 Y = 579 PX = 767 PY = 580 PT = 1312531470.83
speed = 0.0 Time = 4:30:847362 CPUTime = 1312531470.85 X = 767 Y = 579 PX = 767 PY = 579 PT = 1312531470.85

3 个答案:

答案 0 :(得分:1)

存储起始位置和结束位置,以及开始时间和结束时间。获取距离,然后除以时间。这给你一个速度。据推测速度是每毫秒的像素,因此您只需将其转换为您想要的单位(英里/小时)。

# Start
data = display.Display().screen().root.query_pointer()._data 
x = data["root_x"] 
y = data["root_y"] 
z = time.time()

# Time passes...

# End
data = display.Display().screen().root.query_pointer()._data 
x2 = data["root_x"] 
y2 = data["root_y"] 
z2 = time.time()

# Determine distance traveled
dx = x2 - x1
dy = y2 - y1
dist = math.sqrt( math.pow(dx, 2) + math.pow(dy, 2) ) # Distance between 2 points

# Get the change in time
dz = z2 - z1

# Print out the speed
print "I've traveled {0}".format(dist/dz)
# Convert that to the units you want

答案 1 :(得分:1)

如果你在循环中运行它,只需在进入循环之前获取初始样本,然后在每次迭代时取一个位置,每次用新的位置替换初始位置。

import math
import collections
import time

PIXEL_MILE_RATIO = 6336000 # assumes 100 pixels/inch
                           # you'll need to come up with a value for this
pixels_to_miles = lambda p: p*PIXEL_MILE_RATIO

Sample = collections.namedtuple('Sample', 'x,y,z')

def calculate_speed(sample1, sample2):
    distance = math.sqrt((sample2.x - sample1.x)**2 + (sample2.y - sample1.y)**2)
    hours = (sample2.z - sample1.z) / 3600.
    return pixels_to_miles(distance)/hours


data0 = display.Display().screen().root.query_pointer()._data
sample0 = Sample(data0['root_x'], data0['root_y'], time.time()

while LOOP_CONDITIONAL:
    data1 = display.Display().screen().root.query_pointer()._data
    sample1 = Sample(data1['root_x'], data1['root_y'], time.time()

    print 'Your mouse is moving at {} miles per hour'.format(calculate_speed(sample0, sample1))

    sample0 = sample1

答案 2 :(得分:0)

我不知道您正在使用哪个库,但我会使用pygame.mouse.get_rel()来计算鼠标速度,这很容易。

相关问题