简体   繁体   English

洪水填充算法分析

[英]Flood fill algorithm analysis

I've got some method with flood fill algorithm. 我有一些使用洪水填充算法的方法。 It is very simple 很简单

  1. Go to first obstacle on top. 转到顶部的第一个障碍。

  2. change pixels color to the bottom 将像素颜色更改为底部

  3. while changing check if left/right pixel is in different color 在更改时检查左/右像素是否具有不同的颜色

  4. if yes: color this column too (stack.push()) 如果是,则也为此列上色(stack.push())

  5. 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 (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++; } } 

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. 开头的if语句检查它们是否为假,并向堆栈中添加一个像素。 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. 如果不重置,则不会遇到spLeft区域2,因为在遇到第一个区域时,spLeft区域2会被设置为true(这避免了不必要地增加堆栈很多)。

在此处输入图片说明

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

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