C# 洪水填充算法分析
我有一些洪水填充算法的方法。这很简单C# 洪水填充算法分析,c#,algorithm,fill,C#,Algorithm,Fill,我有一些洪水填充算法的方法。这很简单 前往顶部的第一个障碍物 将像素颜色更改为底部 更改时,检查左/右像素的颜色是否不同 如果是:也为该列着色(stack.push()) 循环 Stack<Point> st = new Stack<Point>(); bool spLeft, spRight; Bitmap b = canvas.buffer; st.Push(start); spLeft = spRight = false;
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++;
}
}
及
else if(spRight&p.X
如果没有这些,代码就可以正常工作,并且似乎有相同数量的迭代。你能帮我弄清楚这些话是真的没用还是我就是不懂?
(我真不敢相信我的朋友会毫无目的地使用它们)它们允许填充多个区域。opening if语句检查它们是否为false,并向堆栈中添加一个像素。当该区域完成时,这些将重置 如果不重置spLeft,则不会填充区域2,因为在遇到第一个区域时,该区域会被设置为true(这避免了不必要地向堆栈中添加批次)
它们允许填充多个区域。opening if语句检查它们是否为false,并向堆栈中添加一个像素。当该区域完成时,这些将重置 如果不重置spLeft,则不会填充区域2,因为在遇到第一个区域时,该区域会被设置为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;
else if (spRight && p.X < b.Width - 1 && b.GetPixel(p.X + 1, p.Y) != oldColor) {
spRight = false;
}