Algorithm
Consider this layout:
+-------------+ | | | | | +--+ | | |##| | | |##| | | +--+------+ | |######| | |######| +------+------+
The black part is the occupied space. Now I need an algorithm that returns the largest remaining rectangular spaces. (Ordered from top to bottom, left to right.)
Like this:
1 2 3 4
+-------------+ +---- -------+
|#############| |### ######|
|#############| |### ######|
| +--+ | |###+ +######|
|###| |######|
|###| |######|
|###+ +------| | +--+
|### |######|
|### |######|
+---- +------+
Input
The width and height of the enclosing container. (A page in my code.)
A list of already occupied rectangles. They can be in any form that you like. E.g. (x,y,width,height) or (x1,y1,x2,y2)
I’m dealing with floats, therefore a mathematical solution would be preferred.
From your example it appears that you aren’t asking to exclude overlap (e.g. 1 and 2 have the top-left segment in common), so perhaps this will suit your needs:
Divide the space into rectangles based on the corners identified by the occupied spaces.
Form the “basic rectangles” by extending line segments from those corners to the edges of the entire space.
Using any systematic order (e.g. top-to-bottom, left-to-right):
3.1. Select a basic rectangle and extend it as far as possible with other basic rectangles that have a side in common.
3.2. Form the set of all (unique) such extended rectangles.
Note that this searches/builds based on the “basic rectangles” from step 2, and not point-by-point throughout the entire space, so the performance should be much better.