I've got some method with flood fill algorithm. It is very simple
Go to first obstacle on top.
change pixels color to the bottom
while changing check if left/right pixel is in different color
if yes: color this column too (stack.push())
loop.
Stack<Point> st = new Stack<Point>();
bool spLeft, spRight;
Bitmap b = canvas.buffer;
st.Push(start);
spLeft = spRight = false;
Point p = new Point();
while (st.Count > 0)
{
//going as far top as possible (finding first obstacle)
p = st.Pop();
while (p.Y >= 0 && b.GetPixel(p.X, p.Y) == oldColor) p.Y--;
p.Y++;
spLeft = spRight = false;
//looping on every oldColored pixel in column
while (p.Y < b.Height && b.GetPixel(p.X, p.Y) == oldColor) {
b.SetPixel(p.X, p.Y, state.currentColor); //setting new color
//checking if left pixel is oldColored and if it doesn't belong to span
if (!spLeft && p.X > 0 && b.GetPixel(p.X - 1, p.Y) == oldColor) {
st.Push(new Point(p.X - 1, p.Y));
spLeft = true;
}
//checking if left pixel isn't oldColored and if it belongs to span
else if (spLeft && p.X > 0 && b.GetPixel(p.X - 1, p.Y) != oldColor) {
spLeft = false;
}
if (!spRight && p.X < b.Width - 1 && b.GetPixel(p.X + 1, p.Y) == oldColor) {
st.Push(new Point(p.X + 1, p.Y));
spRight = true;
}
else if (spRight && p.X < b.Width - 1 && b.GetPixel(p.X + 1, p.Y) != oldColor) {
spRight = false;
}
p.Y++;
}
}
The point is that i just do not understand these parts
//checking if left pixel isn't oldColored and if it belongs to span
else if (spLeft && p.X > 0 && b.GetPixel(p.X - 1, p.Y) != oldColor) {
spLeft = false;
and
else if (spRight && p.X < b.Width - 1 && b.GetPixel(p.X + 1, p.Y) != oldColor) {
spRight = false;
}
Without these, code works fine, and it seems like it have the same amount of iterrations. Can you help me to figure if these line are realy useless or I just dont understand them ? (I cant belive my friend put them without purpose)
They allow multiple regions to be filled. The opening if statements checks they are false and adds a pixel to the stack. Those reset when that region has finished.
Without resetting spLeft region 2 would not be filled since it would have been set to true (which avoids add lots to the stack unecesserily) when the first region was encountered.