开发者

python calculate mouse speed

i am using the following method in python to get the X,Y corordinates at any given this

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

I want to calculate the mouse speed over a given time, is there any way i can calculate and show mouse speed in miles per hour???

krisdigitx


i now managed to fix the problem and calculated the speed between the last two known x and y positions using this method

        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)

now what rule should i follow to convert the speed to miles per hour?? thanks for all your help, this is the output in realtime..

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 Tim开发者_JAVA技巧e = 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


Store the start position and end position, as well as the start time and end time. Get the distance, then divide by the time. That gives you a speed. Presumably that speed is pixels per millisecond, so you just need to convert that to the units you want (miles per hour).

# 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


If you're running this in a loop, just get an initial sample before entering the loop and then take a position on each iteration, replacing the initial position with the newer one each time.

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


I don't know which library you're using but I would use pygame.mouse.get_rel() to calculate mouse speed, it would be something easy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜