繁体   English   中英

洪水填充算法分析

[英]Flood fill algorithm analysis

我有一些使用洪水填充算法的方法。 很简单

  1. 转到顶部的第一个障碍。

  2. 将像素颜色更改为底部

  3. 在更改时检查左/右像素是否具有不同的颜色

  4. 如果是,则也为此列上色(stack.push())

  5. 环。

      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 (pY >= 0 && b.GetPixel(pX, pY) == oldColor) pY--; p.Y++; spLeft = spRight = false; //looping on every oldColored pixel in column while (pY < b.Height && b.GetPixel(pX, pY) == oldColor) { b.SetPixel(pX, pY, state.currentColor); //setting new color //checking if left pixel is oldColored and if it doesn't belong to span if (!spLeft && pX > 0 && b.GetPixel(pX - 1, pY) == oldColor) { st.Push(new Point(pX - 1, pY)); spLeft = true; } //checking if left pixel isn't oldColored and if it belongs to span else if (spLeft && pX > 0 && b.GetPixel(pX - 1, pY) != oldColor) { spLeft = false; } if (!spRight && pX < b.Width - 1 && b.GetPixel(pX + 1, pY) == oldColor) { st.Push(new Point(pX + 1, pY)); spRight = true; } else if (spRight && pX < b.Width - 1 && b.GetPixel(pX + 1, pY) != oldColor) { spRight = false; } p.Y++; } } 

关键是我只是不了解这些部分

    //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;
            }

没有这些,代码就可以正常工作,并且似乎具有相同的迭代次数。 您能帮我弄清楚这些行是否真的没用,或者我只是不理解它们? (我不能相信我的朋友没有目的就把它们放了)

它们允许填充多个区域。 开头的if语句检查它们是否为假,并向堆栈中添加一个像素。 当该区域结束时,那些重置。

如果不重置,则不会遇到spLeft区域2,因为在遇到第一个区域时,spLeft区域2会被设置为true(这避免了不必要地增加堆栈很多)。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM