Given the following c# array:
const bool X = true, _ = false;
bool[,] array = new bool[,] {
{ X, X, X, X, _, X, X, X, _, _ },
{ _, _, _, _, _, X, X, X, X, _ },
{ X, X, X, X, X, X, X, X, X, X },
{ X, X, X, X, X, X, X, X, X, X },
{ _, X, X, X, _, X, _, _, X, X },
{ _, X, X, X, _, X, X, _, X, X },
{ _, _, _, X, X, _, _, _, _, _ },
{ X, X, _, X, X, _, X, X, X, _ },
{ _, X, X, _, _, X, X, X, X, X },
{ _, _, X, X, _, _, X, X, X, X },
};
Visually something like this:
Are there any existing methods for converting this to as few rectangular areas as possible?
A possible solution might be something like:
Ideally I'm looking for something that can produce a list of rectangles:
public IEnumerable<Rectangle> ReduceMap(bool[,] map)
{
List<Rectangle> rects = new List<Rectangle>();
int width = map.GetLength(0), height = map.GetLength(1);
// Reduce
// ....?
return rects;
}
The result of which would be something like:
var output = new List<Rectangle>
{
new Rectangle(0, 0, 4, 1),
new Rectangle(5, 0, 3, 2),
new Rectangle(8, 1, 1, 1),
new Rectangle(0, 2, 10, 2),
// ... etc
};
Speed is important too, the fewer rectangles the better, but finding a decent solution should not be overly computationally expensive.
Apologies if this has been answered before, if anyone has any idea where to look that would be great!