如果它是一个点,那将是
dist(mouseX, mouseY, x, y)
Run Code Online (Sandbox Code Playgroud)
对于
point(x,y)
Run Code Online (Sandbox Code Playgroud)
但是如何从鼠标的当前位置计算dist()
rectMode(CORNERS);
rect(x1,y2,x2,y2);
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 6
这样的事情应该这样做:
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)));
}
Run Code Online (Sandbox Code Playgroud)
基本上,你需要弄清楚关闭点是在一侧还是在角落里.这张图可能有所帮助,它显示了点与不同位置的矩形点的距离:
