如何将鼠标移动转换为元素的旋转
我正在构建一个滚轮菜单控件。这个想法是你旋转轮子,直到你想要操作的项目出现在视图中,然后你点击它或其他什么。我试图弄清楚如何将用户的鼠标移动(x 和 y)转换为旋转滚轮的度数。我可以实现这一切,我只是缺少进行转换的数学。任何帮助或指示表示赞赏!
I'm building a wheel menu control. The idea is you spin the wheel until the item you want to act on is in view, then you click on it or whatever. I'm trying to figure out how to translate the user's mouse movements (x & y) into the number of degrees to spin the wheel. I can implement it all, I just am missing the math to do the conversion. Any help or pointers are appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果用户将鼠标从 [x1,y1] 移动到 [x2,y2],您基本上会发生什么想要的是找到
θ=θ1-θ2
其中:
θ1 = Math.atan2(y1, x1);
θ2 = Math.atan2(y2, x2);
现在所有这一切都取决于您定义原点的位置(车轮的中心)。如果您的原点是 [x0,y0],则只需从实际鼠标坐标中减去这些值即可。
同样在屏幕上,坐标系是颠倒的,因此 0,0 位于左上角而不是左下角,因此您需要翻转它,但数学本质上是相同的。
另请注意,角度以弧度而不是度数来测量。
If the user moves the mouse from [x1,y1] to [x2,y2], what you basically want is to find
θ=θ1-θ2
Where:
θ1 = Math.atan2(y1, x1);
θ2 = Math.atan2(y2, x2);
Now all of this depends on where you define your origin (center of your wheel). If your origin is [x0,y0], then just subtract those values from the actual mouse co-ordinates.
Also on screen, the co-ordinate system is upside down, so 0,0 is in the top-left instead of bottom-left, so you'd need to flip that, but the math is essentially the same.
Also note that the angle is measured in radians and not degrees.
获取距控件中心偏移的两个后续位置的
atan2()
并通过角度增量乘以常数来更改该值。Take the
atan2()
of two subsequent positions offset from the center of the control and change the value by the delta of the angles multiplied by a constant.