Minimum distance between two rectangles with known size and orientation

1.5k Views Asked by At

I have faced a problem, that I need to calculate a shortest distance between two rectangles, which are on a different angles.

Rectangulars

Known parameters: length, width, angle and coordinate of center point of each rectangular.

2

There are 2 best solutions below

2
On

You can easily deduce that the shortest distance will be attained for the vertices of one of the rectangles (but possibly the sides of the other). Then you really have to compare the distances of $8$ given points from $8$ given lines, which shouldn't be that bad...

0
On

The typical method is to rotate the coordinate system to one box and check the vertices of the other. This transforms the problem to this:

pic

NOTE: See https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection and links theirin. In separating axis theorem there is a way to find minimum distance which is equivalent to the above.