What is the distance between the centres of two touching rectangles?

77 Views Asked by At

What is the distance between the centres of the two rectangles shown in the image?

image

This distance should be expressed in terms of the lengths of the sides of the rectangles and the angle $\alpha$ in degrees.

Preferably, the distance should be expressed in a way that can be computed efficiently on a computer, as this is the offset an entity will have once they will get teleported out of a portal (to prevent them from instantly colliding again).

So far I have tried to use vectors as they can be computed on a GPU, but with that I cannot find the intersection between the two rectangles. I have the idea that I may be able to draw a line parralel to the side that is intersected, but offset by half the width and height of the rectangle that is straight up. Then calculate where that line crosses the red (which a perpendicular bisector to the original line) one and calculate the distance that way. But I have no idea if that is the best approach computationally.

1

There are 1 best solutions below

1
On BEST ANSWER

TwoRectangles

$\text{If you fix,}$

$\quad\space\space\space\text{i. the slanting angle ($\alpha\gt 0$),}$

$\quad\space\space\text{ii. the length of the side $AD$ ($2h$) of the larger rectangle $ABCD$}$,

$\quad\space\text{iii. the length of the horizontal side $EF$ ($2a$) of the smaller rectangle $EFGH$}$,

$\quad\space\text{iv. the position of the $\color{red}{\text{red line}}$ as the perpendicular bisector of the longer side of the larger rectangle,}$

then you must have the other side $FG$ ($2b$) of the smaller rectangle $EFGH$ equal to $2a\tan\left(\alpha\right)$. Otherwise, you want be able to force the $\color{red}{\text{red line}}$ to lie on the perpendicular bisector of the longer side of the larger rectangle. In this particular case, which is depicted in the diagram, the distance between the centers of the two rectangles depends only on $AD$, $EF$, and $\alpha$ and is equal to $h+ a\sec\left(\alpha\right)$. The length of the other side $AB$ ($2w$) of the larger rectangle does not play any role here.

By posting this text, we are hoping to bring to your notice that the smaller rectangle cannot have arbitrary side-lengths, if you want the rectangles to contact each other while complying with the fourth constrain.