Closest Points Between Rectangles – Algorithm Solutions

algorithmsgeometry

On a 2D plane, I have 2 rectangles.
I want to find the closest pair of points (one on each rectangle), which are closest to each other. By points I mean the corners of the rectangles. And no, they do not overlap.

Is there any way to do that other than checking all the distances between all the combinations of points and finding the smallest one?

Best Answer

How you do it depends on what your "business" requirements are.

If simplicity of source code (maintainability) is your primary concern, then brute force calculation of all of the point to point distances will be a simple and effective method. If you have to optimize for processing speed, then you may want to find an optimized algorithm. I suspect that brute force will be nearly optimal in any case since there are only 16 point-to-point distances to calculate.