Contents
- 1 How to calculate distance between a point and a rectangle?
- 2 What happens when the squared distance of a rectangle is zero?
- 3 How to calculate the distance between a and a?
- 4 How to calculate the distance between a point and a box?
- 5 How to calculate the distance between two points?
- 6 How to calculate distance between A and B?
How to calculate distance between a point and a rectangle?
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.
What happens when the squared distance of a rectangle is zero?
If that squared distance is zero, it means the point touches or is inside the rectangle. I assume your rectangle is Axis-Aligned. You just have to “clamp” the point into the rectangle and then compute the distance from the clamped point.
How to calculate the squared distance from a point?
If (x,y) is the centre of the rectangle, the squared distance from a point (px,py) to the rectangle’s border can be computed this way: dx = max(abs(px – x) – width / 2, 0); dy = max(abs(py – y) – height / 2, 0); return dx * dx + dy * dy;
How to calculate the distance between a and a?
(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.
How to calculate the distance between a point and a box?
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.
How to calculate the distance between two points in 3D?
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.
How to calculate the distance between two points?
It’s easier to illustrate in two dimensions: 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.
How to calculate distance between A and B?
(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.