Calculating distance between a point and a rectangular box (nearest point)?

Either way you're probably best off getting the point-line (for 2D) or point-plane (3D) distance for each side, then selecting the minimum Edit: there's a much better way described here (last post). It involves transforming your point coordinates into box space, then "saturating" the coordinates with the box size to find the point on the box closest to the point. I haven't tried it, but it looks right to me.

Either way you're probably best off getting the point-line (for 2D) or point-plane (3D) distance for each side, then selecting the minimum. Edit: there's a much better way described here (last post). It involves transforming your point coordinates into box space, then "saturating" the coordinates with the box size to find the point on the box closest to the point.

I haven't tried it, but it looks right to me.

3D, i'll check that – Daniel Mar 10 at 2:50 Come to think of it, you'll need to check that the nearest point in the plane is actually on the box... – Rob Agar Mar 10 at 2:54.

I think you need to analyze cases; there's no single formula. It's easier to illustrate in two dimensions: 1 2 3 +-------------+ | | 4 | 0 | 5 | | +-------------+ 6 7 8 The edges of the box (extended) divide the outside into 9 regions. Region 0 (inside the box) is solved by computing the distance to each edge and taking the minimum.

Every point in region 1 is closest to the top left vertex, and similarly for regions 3, 6, and 8. For regions 2, 4, 5, and 7, you need to find the distance from the point to the closest edge, which is a fairly simple problem. You can determine which region a point is in by classifying it with respect to each edge.(It's easier to see how to do this by directing the edges say, counter-clockwise.) This will also tell you if the point is inside the box.

In 3D, the logic is exactly the same except that you classify with respect to the six faces and you have more cases. The problem is simpler if the edges of the box are parallel to the coordinate axes.

Let's say that the point is named P and ABCD is our rectangle. Then the problem can be decomposed into the following set of subproblems: (1) Develop a function dist(P, AB) that calculates the distance between a point P and an arbitrary segment AB. (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.

Now, we need to know how to calculate the distance between point P and an arbitrary segment AB, i.e. How to calculate dist(P, AB). This is done as follows (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.

That's it. There are some obvious ways to optimize the procedure, but even if implemented literally, it will work very well already. P.S.Of course, one can ask how to perform a projection of a point to a line.

I'll leave it as an exercise to the reader :).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions