First, create a set of all “atomic” rectangles in the composition, i.e. areas formed by the intersections of the rectangle and not separated by themselves. Each actual rectangle spans 1 or more atomic rectangles. Then follow a combinatorial optimization algorithm, for example. SETCOVER to calculate the minimum number of rectangles you need to cover.
Here is an illustration of the approach. You have three rectangles (A, B, C). They create 5 atomic regions (1-5):
+---------+A | 1 | | +----+-----+B | | 2 | 3 | | | +-+---+C| | | |4| | | +----+--+-+ 5 | | | +-----+ | +--+-------+
Rectangles cover atomic regions according to this table:
A {1,2,4} B {2,3,4,5} C {4,5}
Now the problem with SETCOVER is to select the smallest subset of rectangles {A, B, C} to cover all atomic regions when you assemble the atomic regions covered by rectangles. Minimal solution (obviously)
A {1,2,4} + B {2,3,4,5} = {1,2,3,4,5}
Note that here the areas are non-rectangular, for example. area 3 has a complex shape. You can get rid of this problem with the following approach: take all the clear X-coordinates of the corner points of the source rectangles and consider them as the X-coordinates of the grid and do the same for the Y-coordinates; then each rectangle covers a set of grid squares that are never subdivided, that is:
+---------+A - | 1 | | +----+-----+B - | | 2 | 3 | | | +-+---+C| - | | |4| | | +----+--+-+ 5 | | - | +-----+ | - +--+-------+ - | | | | | |
Which gives you the following 5x5 grid:
AAA A = rectangle A only A**BB B = rectangle B only A*
From this you can easily extract 5 regions; in fact, they are already marked :) (A, B, C, *, #)