2012-08-16 128 views
0

我有一些洪水填充算法的方法。這是非常簡單的洪水填充算法分析

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

的一點是,我只是不明白,這些部件

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

如果沒有這些,代碼工作正常,並且現在看來似乎有等量的遊覽。你能幫我弄清楚這些線條是否真的無用或者我不理解它們? (我無法相信我的朋友把他們沒有目的地)

+0

爲什麼不問你的朋友他爲什麼加了他們? – Kevin 2012-08-16 15:49:23

+0

沒有聯繫atm。假日:/ – user1321706 2012-08-16 15:54:45

回答

3

他們允許多個地區填補。打開if語句檢查它們是否爲false,並向堆棧中添加一個像素。那些地區完成後重置。

不重置spLeft區域2將不會被填充,因爲它會被設置爲true(這樣可以避免在遇到第一個區域時將堆棧非法添加到堆棧)。

enter image description here

+0

作品!謝謝 ! – user1321706 2012-08-16 18:21:18