Takes a grid and returns an optimal set of rectangles that fills that grid
For example, the following grid:
[[1,1,0,1,1],
[1,1,1,1,1],
[0,1,1,1,0]]
Should result in three rectangles (order doesn't matter)
[{x:0,y:0,w:2,h:2},
{x:3,y:0,w:2,h:2},
{x:1,y:1,w:3,h:2}]
Note that the rectangles need to overlap and not simply touch since they will be used to draw a shape with rounded and slightly inset borders.
[[1,1,0],
[1,1,1],
[0,0,1]]
Even though the following technically fills all the space:
[{x:0,y:0,w:2,h:2},
{x:2,y:1,w:1,h:2}]
It will appear as two distinct rectangles because they don't overlap.
The following is correct:
[{x:0,y:0,w:2,h:2},
{x:2,y:1,w:1,h:2},
{x:0,y:2,w:3,h:1}]
The following implementation works, but the result it not optimal, it often results in too many rectangles. Also there are some fairly expensive parts to the algorithm and it's a bit longer than I would like.
Clone this gist and make a better implementation and link to your submission as a comment here when done.