2012-08-16 2 views
0

홍수 채우기 알고리즘을 사용하는 몇 가지 방법이 있습니다. 매우 간단합니다홍수 채우기 알고리즘 분석

  1. 맨 위에있는 첫 번째 장애물로 이동하십시오. 바닥

  2. 변화 픽셀 컬러 검사를 변경해두면하면서/우측 픽셀들은 다른 색에

  3. 예인 경우 : 색이 항목도 (stack.push())

  4. 루프.

    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

+0

atm과 접촉하지 않습니다. 휴일 :/ – user1321706

답변

3

여러 영역을 채울 수 있습니다. opening if 문은 false인지 확인하고 스택에 픽셀을 추가합니다. 해당 지역이 끝나면 재설정됩니다.

재설정하지 않고 spLeft 영역 2는 true로 설정되어 있으므로 (첫 번째 영역을 만났을 때 스택에 더미를 추가하지 않아도 됨) 채워지지 않습니다.

enter image description here

+0

작동! 감사 ! – user1321706