Processing 如何在处理过程中计算从mouseX、mouseY到矩形的dist()

Processing 如何在处理过程中计算从mouseX、mouseY到矩形的dist(),processing,Processing,如果是距离到了某一点,那就是 dist(mouseX,mouseY,x,y) 为了 点(x,y) 但是如何计算从鼠标当前位置到 rectMode(转角); 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

如果是距离到了某一点,那就是

dist(mouseX,mouseY,x,y)
为了

点(x,y)
但是如何计算从鼠标当前位置到

rectMode(转角);
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)));
}
浮点区(浮点x、浮点y、浮点x1、浮点y1、浮点x2、浮点y2){
浮点数dx1=x-x1;
浮点数dx2=x-x2;
浮动dy1=y-y1;
浮动dy2=y-y2;
如果(dx1*dx2<0){//x在x1和x2之间
如果(dy1*dy2<0){//(x,y)在矩形内
返回最小值(最小值(abs(dx1)、abs(dx2))、最小值(abs(dy1)、abs(dy2));
}
返回最小值(abs(dy1),abs(dy2));
}
如果(dy1*dy2<0){//y在y1和y2之间
//我们不必测试矩形的内部,它已经被测试过了。
返回最小值(abs(dx1),abs(dx2));
}
返回min(min(dist(x,y,x1,y1),dist(x,y,x2,y2)),min(dist(x,y,x1,y2),dist(x,y,x2,y1));
}
基本上,您需要确定闭合点是在一条边上,还是在一个角上。这张图片可能会有所帮助,它显示了一个点与一个矩形的不同位置的距离:

这里有一个互动程序,它可以完成您所寻找的内容。如果愿意,您可以将其放入处理并运行它

编辑:这里是一个屏幕截图:

// Declare vars.
int x_click = -20;      // Initializes circle and point off-screen (drawn when draw executes)
int y_click = -20;
float temp = 0.0;
float min_dist = 0.0;
int x1, x2, x3, x4, y1, y2, y3, y4;

// Setup loop.
void setup() {
  size(400, 400);

 // Calculate the points of a 40x40 centered rectangle
  x1 = width/2 - 20; 
  y1 = height/2 - 20;
  x2 = width/2 + 20;
  y2 = y1;
  x3 = x1;
  y3 = height/2 + 20;
  x4 = x2;
  y4 = y3;
}


// Draw loop.
void draw(){
  background(255); 

  // Draws a purple rectangle in the center of the screen.
  rectMode(CENTER);
  fill(154, 102, 200);
  rect(width/2, height/2, 40, 40);

  // Draws an orange circle where the user last clicked.
  ellipseMode(CENTER);
  fill(204, 102, 0);
  ellipse(x_click, y_click, 10, 10);

  // Draws black point where the user last clicked.
  fill(0);
  point(x_click, y_click);

  // Draws min dist onscreen.
  textAlign(CENTER);
  fill(0);
  text("min dist = " + min_dist, width/2, height/2 + 150);  
}


void mousePressed(){
  x_click = mouseX;
  y_click = mouseY;

  // If the click isn't perpendicular to any side of the rectangle, the min dist is a corner.
  if ( ((x_click <= x1) || (x_click >= x2)) && ((y_click <= y1) || (y_click >= y3))  ) {
    min_dist = min(min(dist(x1,y1,x_click,y_click),dist(x2,y2,x_click,y_click)), min(dist(x3,y3,x_click,y_click),dist(x4,y4,x_click,y_click)));

  } else if( (x_click > x1)  && (x_click < x2) && ((y_click < y1) || (y_click > y3)) ) {
    // outside of box, closer to top or bottom
    min_dist = min(abs(y_click - y1), abs(y_click - y3));

  } else if( (y_click > y1) && (y_click < y3) && ((x_click < x1) || (x_click > x2)) ) {
   // outside of box, closer to right left
   min_dist = min(abs(x_click - x1), abs(x_click - x2));
  } else {
    // inside of box, check against all boundaries
    min_dist = min(min(abs(y_click - y1), abs(y_click - y3)),min(abs(x_click - x1), abs(x_click - x2)));
  }
  // Print to console for debugging.
  //println("minimum distance = " + min_dist);

}

//声明变量。
int x_click=-20;//初始化屏幕外的圆和点(在执行绘制时绘制)
int y_click=-20;
浮子温度=0.0;
浮动最小距离=0.0;
int x1、x2、x3、x4、y1、y2、y3、y4;
//设置循环。
无效设置(){
尺寸(400400);
//计算以40x40为中心的矩形的点
x1=宽度/2-20;
y1=高度/2-20;
x2=宽度/2+20;
y2=y1;
x3=x1;
y3=高度/2+20;
x4=x2;
y4=y3;
}
//画圈。
作废提款(){
背景(255);
//在屏幕中央绘制一个紫色矩形。
矩形模式(中心);
填充(154102200);
矩形(宽度/2,高度/2,40,40);
//在用户上次单击的位置绘制橙色圆圈。
ellipseMode(中心);
填充(204、102、0);
椭圆(x_点击,y_点击,10,10);
//在用户上次单击的位置绘制黑点。
填充(0);
点(x_单击,y_单击);
//在屏幕上绘制最小距离。
文本对齐(中心);
填充(0);
文本(“最小距离=”+最小距离,宽度/2,高度/2+150);
}
void mousePressed(){
x_click=mouseX;
y_click=mouseY;
//如果单击不垂直于矩形的任何一侧,则最小距离为一个角。
如果((x_点击=x2))&((y_点击=y3))){
最小距离=最小距离(最小距离(x1,y1,x_点击,y_点击),距离(x2,y2,x_点击,y_点击)),最小距离(距离(x3,y3,x_点击,y_点击),距离(x4,y4,x_点击,y_点击));
}否则如果((x_单击>x1)和((x_单击y3))){
//盒子外部,靠近顶部或底部
min_dist=min(abs(y_click-y1),abs(y_click-y3));
}否则如果((y_单击>y1)和((y_单击x2))){
//在箱子外面,靠近左右
min_dist=min(abs(x_click-x1)、abs(x_click-x2));
}否则{
//在框内,选中所有边界
min_dist=min(min(abs(y_click-y1)、abs(y_click-y3))、min(abs(x_click-x1)、abs(x_click-x2));
}
//打印到控制台进行调试。
//println(“最小距离=”+最小距离);
}

这就是我使用的。如果你只对相对距离感兴趣,可能不需要求平方根,这样会稍微快一点

- (NSInteger) distanceFromRect: (CGPoint) aPoint rect: (CGRect) aRect
{
    NSInteger posX = aPoint.x;
    NSInteger posY = aPoint.y;

    NSInteger leftEdge   = aRect.origin.x;
    NSInteger rightEdge  = aRect.origin.x + aRect.size.width;

    NSInteger topEdge    = aRect.origin.y;
    NSInteger bottomEdge = aRect.origin.y + aRect.size.height;

    NSInteger deltaX = 0;
    NSInteger deltaY = 0;

    if (posX < leftEdge)       deltaX = leftEdge - posX;
    else if (posX > rightEdge) deltaX = posX - rightEdge;

    if (posY < topEdge)         deltaY = topEdge - posY;
    else if (posY > bottomEdge) deltaY = posY - bottomEdge;

    NSInteger distance = sqrt(deltaX * deltaX + deltaY * deltaY);

    return distance;
}
-(NSInteger)distance from rect:(CGPoint)aPoint rect:(CGRect)aRect
{
NSInteger posX=aPoint.x;
NSInteger posY=aPoint.y;
NSInteger leftEdge=aRect.origin.x;
NSInteger rightEdge=aRect.origin.x+aRect.size.width;
NSInteger topEdge=aRect.origin.y;
NSInteger BOTTEDGE=aRect.origin.y+aRect.size.height;
NSInteger-deltaX=0;
NSInteger三角洲=0;
如果(posXrightEdge)deltaX=posX-rightEdge,则为else;
如果(posYbottomEdge)deltaY=posY-bottomEdge,则为else;
NSInteger距离=sqrt(deltaX*deltaX+deltaY*deltaY);
返回距离;
}

如果
(mouseX,mouseY)
位于矩形内,是否希望距离为0?或者你是说到矩形边界的距离?@Jan它将是到矩形边界的距离+1我使用了你嵌套的最小值(min())的想法,而不是设置最小值,对照临时最小值检查它,设置新的最小值等等。