How to fill a rectangle with smaller specific rectangles that have cardinal information about their adjacent neighbours

144 Views Asked by At

Lets say its 6x6 grid that is represented by top left(0,0) and bottom right(1,1) in coordinate system.

Next, I have set of objects with their cardinal directional information about each of their adjacent neighbors.

List should be read from top the bottom and rectangles should fill the smallest area they can possibly can.

Last rectangle get to drawn fills the remaining area.

Example 1:

A = (N: outside, E: rectangle, S: outside, W: outside)

B = (N: outside, E: outside, S: outside, W: rectangle)

Meaning A needs to be rectangle that;

its north touches the grid top border

its east touches B left border

its south touches the grid bottom border

its west touches the grid left border

and B needs to be rectangle that;

its north touches the grid top border

its east touches the grid right border

its south touches the grid bottom border

its west touches the A right border.

Which should correspond to this shape (6x6 grid):

**************
* *          *
* *          *
*A*     B    *
* *          *
* *          *
* *          *
**************

Example 2:

A = (N: outside, E: rectangle, S: outside, W: outside)

B = (N: outside, E: outside, S: rectangle, W: rectangle)

C = (N: rectangle, E: outside, S: outside, W: rectangle)

Which should correspond to this shape (6x6 grid):

**************
* *     B    *
* ************
*A*          *
* *     C    *
* *          *
* *          *
**************

Example 3:

A= (N: outside, E: rectangle, S: outside, W: outside)

B= (N: outside, E: rectangle, S: rectangle, W: rectangle)

C= (N: rectangle, E: rectangle, S: rectangle, W: rectangle)

D= (N: rectangle, E: outside, S: outside, W: rectangle)

E= (N: outside, E: outside, S: rectangle, W: rectangle)

Which should correspond to this shape (6x6 grid):

**************
* *  B  *    *
* *******  E *
*A*  C  *    *
* ************
* *     D    *
* *          *
**************

It is easy to draw the shape by just thinking for each of the example, but how to build it into an algorithm?

What would be the correct approach to this problem?