Finding the nearest perpendicular distance between a rectangle to another w.r.t a particular reference rectangle side

euclidean-geometrygeometry

I have a geometric question that is a slight variation from other similarly asked question regarding distance between two rectangles.

I wonder if there is a neat solution to compute the closest distance between two rectangles, assuming we are only interested in the perpendicular lateral distance from one particular side of one reference rectangle? This means that we cannot simply compute the euclidean distance because the euclidean distance can be non-perpendicular to the particular side of interest.

Of course, I'm open to using some computer program to solve this problem.

Example rectangles and the solution distance:

1

Best Answer

Cases Galery

We reproduce your gallery of examples $\mathrm{Fig.\space 1}$ after adding two more cases to it. The answer given below imposes no restrictions on the alignment of rectangles, but cannot be applied to cases of overlapping pairs of rectangles. A good knowledge of analytical geometry (a.k.a. coordinate geometry) is required to understand the method described in the answer, which can be easily implemented as a software program.

A simple program based on this method and written for Windows platform is available at DropBox for downloading. It included three built-in examples to give you an idea how it works. You can also test your own examples, for which you know the shortest lateral distance. After inputting the $x$- and $y$-coordinates of the eight vertices of the two rectangle to the program, click the Find-button and compare the program’s output with the known shortest lateral distances. Please note that this program accepts only integer numbers as input data.

It can be shown that the shortest lateral distance between two rectangles, which is defined as the gap between them measured along a perpendicular to a side of one of the rectangles, always occurs between one of the vertices of one of the rectangles and one of the sides of the other rectangle. This means that the problem of finding the shortest lateral distance between two rectangles can be reduced to an exercise of finding the shortest lateral distances between points (i.e. vertices) and line segments (i.e. sides) and, then, selecting the shortest of them. Please note that the shortest distance is not defined for all pairs of rectangles and the last scenario illustrated in $\mathrm{Fig.\space 1}$ depicts such a case.

You can follow the steps outlined below to either calculate the shortest distance manually or implement a program.

$\underline{\text{Step 1}}$:

Gather your input data, i.e. the $x$- and $y$-coordinates of the eight vertices of the two rectangles, say $ABCD$ and $EFGH$.

$\underline{\text{Step 2}}$:

We start our calculation process by determining the slopes and $y$-intercepts of the 8 sides of the 2 rectangles. The slope $m$ of a side is given by $$m = \frac{y_b – y_a}{x_b – x_a}, \tag{1}$$ where $(x_a,y_a) $ and $(x_b,y_b) $ are the coordinates of the two end points of the segment shown in $\mathrm{Fig.\space 2}$.

The $y$-intercepts of a line is given by $$c = y_a – m\times x_a. \tag{2}$$

The equation of the line (i.e. segment) can be expressed as $$y – mx – c = 0. \tag{3}$$

PointLinePair

$\underline{\text{Step 3}}$:

For each vertex of the rectangle $ABCD$, we need to calculate the perpendicular distances from that vertex to the four sides of the rectangle $EFGH$ and vice versa. In general, the shortest distance $d$ between a point and a line is given by $$d = \left| \frac{y_1 – m\times x_1 - c}{\sqrt{1+m^2}}\right|, \tag{4}$$ where $x_1$ and $y_1$ are the respective $x$- and $y$-coordinates of the point and $y – mx – c = 0$ is the equation of the line.

Before attempting to determine the distance $d$ relevant to a given point and a side, we must make sure that the lateral distance between these two entities actually exists. We explain how this is done with the help of $\mathrm{Fig.\space 3}$. Our aim here is to check the existence of lateral distances between the point $B\space(x_0,y_0)$, and the four sides of the rectangle $EFGH$.

It is obvious from the diagram that a point must be in one of the four colored areas if at least one lateral distance between it and the rectangle to exist. For example, if perpendiculars are drooped from the point $A$, which is not in any of the colored areas, their feet are located outside the side segments of the rectangle. Therefore, lateral distances cannot be defined for $A$ and the rectangle.

On the other hand, if perpendiculars are to be drawn from $B$, which lies inside one of the red colored areas, two of the perpendiculars will meet the sides $HE$ and $FG$ between their respective end points. However, the feet of perpendiculars from $B$ to the other two side segments (i.e. $EF$ and $GH$) are located outside the end points of the relevant side segments.

Assume that we know the equations of the four sides of the rectangle $EFGH$, i.e. $$EF :\space y-nx-c_1=0,\qquad FG :\space y-mx-c_2=0,$$ $$GH :\space y-nx-c_3=0,\qquad HE :\space y-mx-c_4=0.$$

For a given point $P$ having coordinates $(x_0, y_0)$, which lies outside the rectangle $EFGH$, and the side $HE$ (or $FG$), lateral distance between them is defined if and only if $$\left(y_0-nx_0-c_1\right) \times\left(y_0-nx_0-c_3\right) \le 0. \tag{5}$$

In similar vein, the lateral distance between the point $P$ and the side $EF$ (or $GH$) exists if and only if $$\left(y_0-mx_0-c_2\right) \times\left(y_0-mx_0-c_4\right) \le 0. \tag{6}$$

We need to calculate distances $d$ using (4) for only those point-side pairs, which pass the test given in (5) or (6).

$\underline{\text{Step 4}}$:

The shortest lateral distance is equal to the shortest of the distances calculated in Step-3.

Related Question