0
votes

I am trying to find a way to get the line (two points in 3D space) of the intersection between two rectangles.

I ran into this question: Intersection between two rectangles in 3D

But this is not my issue. In that question, the rectangle is treated as only the bounds (the perimeter), while I am looking for the rectangle as a whole (think about a picture frame vs the picture itself).

I've figured out that, in every case, there will either be an intersection line (two points), or no intersection at all. If the intersection was just on the borders, therefore just a point, it can be treated as no intersection in my case.

My scenario is that one of these rectangle represents a "static" surface, which cannot move or change. The other one represents a "dynamic" surface, which I have to adapt to avoid crossing

Example:

enter image description here

Once I obtain p1 and p2, which are points in the 3D space, my goal is to modify the Dynamic rectangle into a 3d polygon, which will no longer cross the static rectangle, like this:

enter image description here

So you can see why "edge intersections" are irrelevant to my situation. I am turning "real" intersections into edge intersections, so any edge intersection doesn't require me to do anything with it.

I am only looking for a formula, starting with two sets of 4 points (the rectangles), that would give me the two points of the line of their intersection, or would tell me that there is no (relevant) intersection.

Every formula I've found on this site or others doesn't fit my needs, or doesn't let me input arbitrary rectangles (for example, I can't fix my problem with a formula that uses planes or that treats a rectangle as simply 4 lines)

I am, of course, trying to code it (in C#), therefore any code answer is a great help, but I am confident that even a math-only answer would suffice for me to produce the code from it, therefore I will accept an answer that is only composed of pseudo-code or straight up mathematical formulas, provided they are either simple enough or explained well enough for me to understand what is happening.

1
Tough problem with a lot of edge cases. Not simple. CAD tools do this well. Why would you not be able to use a tool?duffymo
I did not give out the whole picture here, but the rectangles are actually a big simplification of what I have, so having our own solution instead of using a tool is more flexibleKaito Kid

1 Answers

1
votes

If you are OK with just algorithm rather than full code here is a sketch:

  1. Build 2 planes from the rectangles (any 3 points will do as in this answer)
  2. Find the intersection line I of those 2 planes as in this answer or find out that the planes are parallel so there is no intersection
  3. Find the intersections of the I line with the lines containing all sides of the rectangles as in this answer
  4. Check whether some points found in the previous step lie inside the corresponding sides of the rectangles (line segments). This step potentially can be merged with the previous one, but I put it separately for simplicity. Now you potentially have 0, 1 or 2 segments that represent the intersections of the I line with your two rectangles (note that here point is treated as an edge case of a segment where both ends are the same). If you don't have 2 segments, there is no intersection of the rectangles.
  5. Assuming at the previous step you found 2 segments (one in each rectangle) on the line I, you just need to find their intersection and it will be your answer (again, empty means no intersection).