本文介绍了如何在Processing中计算从mouseX、mouseY到矩形的dist()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果它是某个点的距离,那就是
If it was the dist to a point it would be
dist(mouseX, mouseY, x, y)
为了
point(x,y)
但是我怎样才能从鼠标的当前位置计算 dist() 到
but how can I calculate dist() from the mouse's current position to
rectMode(CORNERS);
rect(x1,y2,x2,y2);
谢谢
推荐答案
应该这样做:
float distrect(float x, float y, float x1, float y1, float x2, float y2){
float dx1 = x - x1;
float dx2 = x - x2;
float dy1 = y - y1;
float dy2 = y - y2;
if (dx1*dx2 < 0) { // x is between x1 and x2
if (dy1*dy2 < 0) { // (x,y) is inside the rectangle
return min(min(abs(dx1), abs(dx2)),min(abs(dy1),abs(dy2)));
}
return min(abs(dy1),abs(dy2));
}
if (dy1*dy2 < 0) { // y is between y1 and y2
// we don't have to test for being inside the rectangle, it's already tested.
return min(abs(dx1),abs(dx2));
}
return min(min(dist(x,y,x1,y1),dist(x,y,x2,y2)),min(dist(x,y,x1,y2),dist(x,y,x2,y1)));
}
基本上,您需要确定闭合点是在一侧还是在角落.这张图片可能会有所帮助,它显示了一个点与该点不同位置的矩形的距离:
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:
这篇关于如何在Processing中计算从mouseX、mouseY到矩形的dist()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!