如何在处理中计算从 mouseX、mouseY 到矩形的 dist()
如果它是到某个点的距离
dist(mouseX, mouseY, x, y)
,
point(x,y)
那么我如何计算从鼠标当前位置到
rectMode(CORNERS);
rect(x1,y2,x2,y2);
谢谢的距离()
If it was the dist to a point it would be
dist(mouseX, mouseY, x, y)
for
point(x,y)
but how can I calculate dist() from the mouse's current position to
rectMode(CORNERS);
rect(x1,y2,x2,y2);
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的事情应该可以做到:
基本上,您需要弄清楚闭合点是在一侧还是在角落。这张图可能会有所帮助,它显示了点在不同位置处距矩形的距离:
Something like this should do it:
Basically, you need to figure out if the closes point is on one of the sides, or in the corner. This picture may help, it shows the distance of a point from a rectangle for different positions of the point:
这是一个有点交互式的程序,可以实现您正在寻找的内容。如果您愿意,可以将其放入“处理”中并运行它。
编辑:这是一个屏幕截图:
Here's a somewhat interactive program which accomplishes what you're looking for. You can drop it into Processing and run it if you would like.
EDIT: Here's a screenshot:
这就是我用的。如果您只对相对距离感兴趣,则可能不需要取平方根,这应该会稍微快一些。
This is what I use. If you are only interested in the relative distance there is probably no need to take the square root which should make it slightly quicker.