如何在 Python 中计算鼠标光标的速度大小?
http://dl.dropbox.com/u/779859/speedCalc_puradata.JPG
我用纯数据实现了它,看看我的想法的原理图:
- 从 ctlin 20 和 21
- Pipe 接收 Midi Control 输入会延迟它接收到的任何信号
- Pythagoras
- Viola,输入的速度。单位并不重要,只要它是绝对的即可。
我正在考虑做同样的事情,但是在Python中,对于鼠标光标。
基本上,当我移动鼠标时,我想查看鼠标移动的速度。 输入数据包的速率恒定为 200hz。
我可能已经想出了一种方法,尽管我还没有测试过。例如,如何在列表中收集 51 个值,保持 [0] 为最新值,[50] 为最旧值。然后简单地对这两个值进行数学计算?
http://dl.dropbox.com/u/779859/speedCalc_puradata.JPG
I achieved it in pure data, have a look at the schematic of what I'm thinking:
- Recieving Midi Control input from ctlin 20 and 21
- Pipe delays whatever signal it recieves
- Pythagoras
- Viola, the speed of the input. The units don't matter, as long as it is absolute.
I was thinking about doing the same but in python, for the mouse cursor.
Basically, when I move my mouse, I want to see at what speed the mouse is moving.
The rate of input packets is constant at 200hz.
I might have come up with a way, though I haven't tested it yet. How about collecting, say, 51 values in a list, keeping the [0] current, and [50] the oldest. Then simply doing the math on those two values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所描述的将为您提供速度的大小乘以测量时间间隔的长度。实际速度将是一个矢量。您可以将其第一个坐标设为
(posX - Delayed_posX)/t
,将其第二个坐标设为(posY-delayed_posY)/t
,其中t
为测量之间的时间间隔。请注意,这满足 Pfinal = Pstart + t V 其中 >P 是我们的位置向量。每当您想知道如何测量速度的近似值时,这始终是您的起点。时间间隔越小,您获得的速度图像就越准确。回答您关于 time.sleep 的问题,不,它不会减慢您的其他代码:它会完全停止它,除非它在另一个线程中运行。
你到底想做什么?很难说是否有更好的方法,除非我们知道您需要数据在哪里、何时需要数据以及需要数据的最新程度。
What you are describing will give you the magnitude of the velocity times the length of the time interval of measurement. The actual velocity will be a vector. You can get its first coordinate as
(posX - delayed_posX)/t
and its second coordinate as(posY-delayed_posY)/t
wheret
is the time interval between the measurements. Note that this satisfies Pfinal = Pstart + t V where P is our position vector. Whenever you want to know how to measure an approximation of the velocity, that's always your starting point. The smaller the time interval, the more accurate a picture of the velocity you will have.In response to your question about
time.sleep
, no it will not slow down your other code: it will stop it completely unless it runs in another thread.What exactly are you trying to do? It's hard to say if there's a better way unless we know where you need the data to be, when you need it to be there and how current you need it to be.
结果我需要的只是 X 的差值,然后我用它作为幅度。
其中“x”是光标的当前值,以 200hz 更新。
Turns out all I needed was the difference in X, and then I used that as the magnitude.
Where 'x' is the current value of the cursor, updating at 200hz.