I’m working on an efficient algorithm to “hide” all the pairs of intersecting rectangles laid out in 2D in a set of N rectangles (axis aligned).
All the rectangles have the same width and height.
Suppose my starting set of rectangles laid out in 2D is
R={r_1,r_2,...,r_n}
where r_i are all the rectangles, r_i has the boolean attribute visible.
I want to find the subset S of R such that for every r_i, r_j belonging to S r_i,_r_j don’t intersect.
A first trivial solution is the brute force-maximal independent set approach.
First I iterate over N(N-1)/2 rectangles (a given rectangle doesn’t intersect with itself) and check if they intersects or not. Contemporaneously I also put all the rectangles in the set R as the nodes of a graph G=(V,E). Edges in that graphs are represented by the pairs of intersecting triangles.
The set of non intersecting rectangles is then one maximal independent set of the graph.
I think this is not the fastest approach, although correct. I’ve read in some discussion for example how sorting in advance the list of rectangles by their x coordinate would speed up the computation of intersecting rectangle pairs to O(nlog(n) ) (rather than brute force O(n^2).
Now I’m asking, do someone know a better algorithms for such problem (both for the computation of intersecting pairs in a set of rectangles and for the filtering of those rectangles?)
Reformulating the problem as SAT problem can also be interesting, although this idea just came up to my mind. The problem would be here to find that permutation of visible attributes such that no visible rectangle intersects another one.
Yes you should sort the rectangles by x.
Since your rectangles are on the same x axis and are the same height, after you sort them, you should be able to find the intersection in linear time. The algorithm you’re looking for is called scan line.
You move a scan line over all your rectangles (jump from their x coords in the set you just sorted) and see which ones intersect at current x location. Load all the x coord overlapping rectangles into a datastructure and check if they intersect (based on y coord).