### rectangular areas of free space

I've been working with an algorithm which places rectangles within a grid (itself a rectangle) so they do not overlap. Based on an algorithm used by the Fluxbox window manager, and unlike traditional bin-packing algorithms, the placement of the windows/rectangles within the workspace/bin/grid has the emphasis on position rather than best-fit.

The algorithm always tries to place the rectangle in the top-left of the grid. If it cannot place it there, it moves a little to the right, and tries again. If it reachs the far right, it then moves down a little and starts over at the left side again.

An important consideration here, is that this algorithm does not require the complete set of rectangles to be placed, it places the rectangles as and when requested to do so.

Here is an illustration showing three rectangles that have been placed in a grid by the algorithm:

+----+-----+------+-------+ |1 |2 |3 | | | | | | | | +-----+ | | | | | | | +----+ | | | | | | | | | | | | +------+ | | | | | | | | | | | | | | | +-------------------------+

I have been testing the algorithm for suitability in a real time scenario. Any algorithm which operates in a real time context must be coded to gracefully handle the worst case scenario.

I decided upon a worst case scenario of 255 rectangles (for good reasons I won't go into here). I soon discovered this scenario failed to meet the timing deadlines of real time operation.

Taking the algorithm out of the real time context, and displaying data about it as it ran, I discovered that when 254 rectangles have been placed by it, in order to add the 255th rectangle, the algorithm scans the entire list of all 254 rectangles over 1400 times.

In a nutshell, this is very bad.

After pondering about it for some days, I have come up with a different idea for an approach which stores not only the rectangles which have been placed, but the free unoccupied space also.

I began by envisioning that the free space rectangles would be connected to each other. This soon presented the problem of how many links each rectangle would have, and has been potentially solved by a single characteristic or rule:

**Each free space rectangle has a maximum of 4 free space rectangles directly adjacent to it.**

The following diagram illustrates how the free space would be divided when the same three rectangles as shown in the first diagram have been placed:

+----+-----+------+-------+ |1 |2 |3 |a | | | | | | | +-----+ | | | |b | | | +----+.....| | | |c | | | | | | | |..........+------+.......| |d .e .f | | . . | | . . | | . . | | . . | | . . | | . . | +-------------------------+

The numbered rectangles are the rectangles which have been placed by the algorithm.

The dotted rectangles are the subdivisions of the area of free space.

When placing a fourth rectangle *4* which is small enough to be contained entirely within free space
rectangle *a*, the result is free space a being split to form free space *g*:

+----+-----+------+---+---+ |1 |2 |3 |4 |a | | | | | | | | +-----+ | | | | |b | +---+...| +----+.....| |g | |c | | | | | | | |..........+------+.......| |d .e .f | | . . | | . . | | . . | | . . | | . . | | . . | +-------------------------+

However if only the width of new rectangle *4* is smaller than the width of frespace *a*, (ie only
the height is greater) then free space *f* is split:

+----+-----+------+----+--+ |1 |2 |3 |4 |a | | | | | | | | +-----+ | | | | |b | | | | +----+.....| | | | |c | | | | | | | | | |..........+------+ | | |d .e | | | | . | | | | . +----+ | | . .f . | | . . . | | . . . | | . . . | +-------------------------+

Lastly, the width of new rectangle *4* may be larger than that of *a*, in which case it's width is
measured against the first free space in a lower row. Which in this case is *b*, but the width of
*b* is too small, so the next free space down, *c*, is found, and the width of *4* is smaller
than the width of *c*.

But, the height of *4* is greater than the height of *c*, which means *4* extends
into *d* like so:

+----+-----+------+-------+ |1 |2 |3 |a | | | | | | | +-----+ | | | |b | | | +----+--+..| | | |4 |c | | | | | | | | | |..+------+.......| | | .e .f | | | . . | | | . . | | | . . | | | . . | +-------+ . . | |d . . | +-------------------------+

There are two ways to solve this:

The first is to subdivide *d*, creating *g*:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |4 |c | | | | |..+------+.......| | |g .e .f | | | . . | | | . . | | | . . | | | . . | +-------+ . . | |d . . . | +-------------------------+

The second crops *d* and extends *e*:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |4 |c | | | | |..+------+.......| | |e .f | | | . | | | . | | | . | | | . | +-------+ . | |d . . | +-------------------------+

The second solution is prefered.