Find out whether two rectangles are intersecting in 3D space

2.1k Views Asked by At

I've got two rectangles in 3D space, each given by the coordinates of their 4 corners. They are not axis aligned, meaning their edges are not necessarily parallel/perpendicular to the world axes. Each rectangle can have any orientation.

Is there an easy way to know whether or not the two rectangles are intersecting?

2

There are 2 best solutions below

0
On

There are easy ways to decide (in some cases) the complement of this question, which is why you can devise a test to eliminate false positives. The problem is all the pathological fencepost cases, which you'll have to tackle systematically (one common vertex, one common edge etc) and floating point / precision issues, which are the bane of computational geometry.

For the first elimination test, I'd use the bounding boxes of the rectangles. If no more than two of the three coordinate ranges $\{min(x), max(x)\},\{min(y), max(y)\},\{min(z), max(z) \}$ of the two rectangles overlap, there is no intersection. You can convince yourself of that. You can check the x-, y- and z-ranges in succession eliminating false positives this way. ("Overlap" has to be understood as a non-empty set produced when you take the set intersection of the respective intervals).

After one round of elimination comes the tougher part. Even if there is overlap in the x,y, and z- ranges of two space rectangles, it does not mean that they intersect (think parallel rectangles). However, you can easily find the equations of the planes containing the two rectangles (taking three points each), so you can eliminate parallel planes.

Pairs of rectangles that pass both tests still need not intersect. At this stage you can try solving for the equation of the line in which both planes containing the rectangles intersect. Now you have to test the (endpoints of the) line against your intersection bounding box. If it lies outside, it is another false positive eliminated. If it lies inside, well, you still have more work to do.

0
On

If you can test for line-rectangle intersection, then two rectangles A and B intersect if and only if either a line from A intersects B or a line from B intersects A (inclusive of the endpoints). Each of A and B are only four lines, so this shouldn't be too bad to check.