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
我不知道你在用哪个库,但我会用 pygame.mouse.get_rel()
来计算鼠标的移动速度,这样做很简单。
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
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