Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating distance between a point and a rectangular box (nearest point)

is there a easy formula to calculate this? i've been working on some math but i can only find a way to calculate the distance directed to the center of the box, not directed to the nearest point.. are there some resources on this problem?

like image 601
Daniel Avatar asked Mar 10 '11 02:03

Daniel


People also ask

How to find the distance between a point and a line?

(1) Perform a perpendicular projection of the point P to the line AB. You get the new point P' on AB. (2) If P' lies between A and B, then dist (P, AB) is the distance between P and P'. (3) Otherwise, dist (P, AB) is the distance between P and either A or B, whichever is shorter.

How do you find the distance between two segments of a rectangle?

(2) Calculate four distances between your point P and each side of the rectangle (each side is a segment) and take the shortest of the four distance = min (dist (P, AB), dist (P,BC), dist (P, CD), dist (P, DA)) That's your answer.

What is the distance from the point inside the box?

If you want to obtain the distance from the area of the box, I would say that it is zero when the point is inside the box.

How do you find the absolute distance of a rectangle?

A rectangle is made of 4 lines, so you can find the absolute distance from the point to each line and take the min value of the four distances. This will work regardless of whether the point is inside or outside the rectangle. @MatthewPope a rectangle is made of four line segments, not of four lines. Your link does not discuss line segments at all.


1 Answers

Here is a single formula that avoids all the case logic. (I happen to be working in JS right now, so here's a JS implementation). Let rect = {max:{x:_, y:_}, min:{x:_, y:_}} and p={x:_, y:_}

function distance(rect, p) {   var dx = Math.max(rect.min.x - p.x, 0, p.x - rect.max.x);   var dy = Math.max(rect.min.y - p.y, 0, p.y - rect.max.y);   return Math.sqrt(dx*dx + dy*dy); } 

Explanation: This breaks down the problem into calculating the x distance dx and the y distance dy. It then uses distance formula.

For calculating dx, here is how that works. (dy is analogous)

Look at the tuple being provided to the max function: (min-p, 0, p-max). Let's designate this tuple (a,b,c).

If p is left of min, then we have p < min < max, which means the tuple will evaluate to (+,0,-), and so the max function will correctly return a = min - p.

If p is between min and max, then we have min < p < max, which means the tuple will evaluate to (-,0,-). So again, the max function will correctly return b = 0.

Lastly, if p is to the right of max, then we have, min < max < p, and the tuple evaluates to (-,0,+). Once again, Math.max correctly returns c = p - max.

So it turns out all the case logic is taken care of by Math.max, which leads to a nice 3-line, control-flow-free function.

like image 105
MultiRRomero Avatar answered Sep 25 '22 08:09

MultiRRomero