The task is built on top of two one-dimensional search for overlapping ranges.
The initial array is not very useful, it is necessary to lead to arrays of coordinates only, two X each and two Y per rectangle. Let them always be ascending, X beginnings are always less than X of the end of the rectangle.
Using the example of X. Two rectangles gave the following X's: [3,7], [5,9] . These values must somehow be merged into one array and sorted by ascending coordinates, while retaining their purpose — who is the beginning, and who is the end of a rectangle. You can bring them to objects like:
[ { x:3, io: 1, rect_id: 0 }, { x:7, io:-1, rect_id: 0 }, { x:5, io: 1, rect_id: 1 }, { x:9, io:-1, rect_id: 1 }, ]
and sort by property x .
Then it remains to move from the smallest to the largest and count the current amount io . While there are no intersections, there will be 0 or 1 . As the intersection begins, the value >1 will appear. This means that the projection of the rectangle to which the current point belongs on the X axis is superimposed with the projection of the rectangle to which the previous point belongs.
So there will be pairs (or more companies) of candidates for intersections along the X and Y axes. And it remains to find common ones among them.