For the past few days, I've been trying to implement a simple filling tool for graphic regions. The scenario that I am currently working on:
It has the same region but split into two subregions.
Here is where the problem begins. I get the region scans and loop over them in order to differentiate between the rectangles that belong to one subregion and the ones that belong to the other. This works quite well but, of course, it is an approximation, and when I finish the filling process I get something like this:
As you can see, there is still color information of the previous region, so I need a more robust approximation to get rid of those unexpected pixels. Does anyone know how can I improve this method to split accurately a region into subregions?
Thank you in advance!
Code:
List<Region> regionSplits = new List<Region>();
RectangleF[] rectanglesReg = regSelected.GetRegionScans(new Matrix());
foreach (var rect in rectanglesReg.OrderBy(item => (item.Y)))
{
if (regionSplits.Count == 0)
regionSplits.Add(new Region(rect));
else
{
bool added = false;
foreach (var reg in regionSplits)
{
RectangleF rectExpanded = new RectangleF(rect.X - 2, rect.Y - 2, rect.Width + 4, rect.Height + 4);
if (reg.IsVisible(rectExpanded))
{
reg.Union(rect);
added = true;
}
}
if (!added)
regionSplits.Add(new Region(rect));
}
}
To sum up, when I talk about filling a region, I am talking about a feature that I need to add to my app that behaves kinda like a floodfill tool. I mean, if the user clicks over a graphics region, that region has to change its category from for example yellow to blue. So in the sample pictures I have posted, when I click over the left yellow region, I need to change its category to another color. Internally I have to consider the two yellow regions as part of a unique region, so I need to find an algorithm that splits it into two (the code I provide is the one I am currently using).