2016-01-22 85 views
-1

我已經實現了洪水填充算法,可以正常工作,用於純色填充。現在我正在進行圖案填充並決定添加一個標記以查看如何填充該區域(使用顏色或圖案)。但是在使用帶圖案填充的算法在繪製區域時卡住。模式洪泛填充算法

這是我的原代碼,與純色的工作原理:

void floodFillStack(int x, int y, byte newColor, byte oldColor) { 
    int y1; 
    if (oldColor == newColor) return; 
    if (get_pixel(x, y) != oldColor) return; 
    //draw current scanline from start position to the top 
    y1 = y; 
    while (y1 < h && get_pixel(x, y1) == oldColor) { 
     plot_pixel(x, y1, newColor); 
     y1++; 
    }  

    //draw current scanline from start position to the bottom 
    y1 = y - 1; 
    while (y1 >= 0 && get_pixel(x, y1) == oldColor) { 
     plot_pixel(x, y1, newColor); 
     y1--; 
    } 

    //test for new scanlines to the left 
    y1 = y; 
    while (y1 < h && get_pixel(x, y1) == newColor) { 
     if (x > 0 && get_pixel(x - 1, y1) == oldColor) { 
      floodFillStack(x - 1, y1, newColor, oldColor); 
     } 
     y1++; 
    } 
    y1 = y - 1; 
    while (y1 >= 0 && get_pixel(x, y1) == newColor) { 
     if (x > 0 && get_pixel(x - 1, y1) == oldColor) { 
      floodFillStack(x - 1, y1, newColor, oldColor); 
     } 
     y1--; 
    } 

    //test for new scanlines to the right 
    y1 = y; 
    while (y1 < h && get_pixel(x, y1) == newColor) { 
     if (x < w - 1 && get_pixel(x + 1, y1) == oldColor) {   
      floodFillStack(x + 1, y1, newColor, oldColor); 
     } 
     y1++; 
    } 
    y1 = y - 1; 
    while (y1 >= 0 &&get_pixel(x, y1) == newColor) { 
     if (x < w - 1 && get_pixel(x + 1, y1) == oldColor) { 
      floodFillStack(x + 1, y1, newColor, oldColor); 
     } 
     y1--; 
    } 
} 

這裏是有圖案的修改(它仍然可與固色)。

int pattern1[6][6] = { {0,1,0,1,0,1}, 
         {1,0,1,0,1,0}, 
         {0,1,0,1,0,1}, 
         {1,0,1,0,1,0}, 
         {0,1,0,1,0,1}, 
         {1,0,1,0,1,0} };    
int pattern2[6][6] = { {0,0,1,1,0,0}, 
         {0,1,0,0,1,0}, 
         {1,0,0,0,0,1}, 
         {1,0,0,0,0,1}, 
         {0,1,0,0,1,0}, 
         {0,0,1,1,0,0} }; 

void floodFillStack(int x, int y, byte newColor, byte oldColor, int pattern_fill) { 
    int y1; 
    if (oldColor == newColor) return; 
    if (get_pixel(x, y) != oldColor) return; 
    //draw current scanline from start position to the top 
    y1 = y; 
    while (y1 < h && get_pixel(x, y1) == oldColor) { 
     if (pattern_fill == 0) { 
      if (fill_pattern == 1) { 
       if (pattern1[x%6][y1%6] == 1) { 
        plot_pixel(x, y1, newColor); 
       } 
      } else { 
       if (pattern2[x%6][y1%6] == 1) { 
        plot_pixel(x, y1, newColor);  
       }    
      } 
     } else {     
      plot_pixel(x, y1, newColor); 
     } 
     y1++; 
    }  
    //draw current scanline from start position to the bottom 
    y1 = y - 1; 
    while (y1 >= 0 && get_pixel(x, y1) == oldColor) { 
     if (pattern_fill == 0) { 
      if (fill_pattern == 1) { 
       if (pattern1[x%6][y1%6] == 1) { 
        plot_pixel(x, y1, newColor); 
       } 
      } else { 
       if (pattern2[x%6][y1%6] == 1) { 
        plot_pixel(x, y1, newColor); 
       }    
      } 
     } else { 
      plot_pixel(x, y1, newColor); 
     } 
     y1--; 
    } 
    //test for new scanlines to the left 
    y1 = y; 
    while (y1 < h && get_pixel(x, y1) == newColor) { 
     if (x > 0 && get_pixel(x - 1, y1) == oldColor) { 
      floodFillStack(x - 1, y1, newColor, oldColor, pattern_fill); 
     } 
     y1++; 
    } 
    y1 = y - 1; 
    while (y1 >= 0 && get_pixel(x, y1) == newColor) { 
     if (x > 0 && get_pixel(x - 1, y1) == oldColor) { 
      floodFillStack(x - 1, y1, newColor, oldColor, pattern_fill); 
     } 
     y1--; 
    } 
    //test for new scanlines to the right 
    y1 = y; 
    while (y1 < h && get_pixel(x, y1) == newColor) { 
     if (x < w - 1 &&get_pixel(x + 1, y1) == oldColor) {   
      floodFillStack(x + 1, y1, newColor, oldColor, pattern_fill); 
     } 
     y1++; 
    } 
    y1 = y - 1; 
    while (y1 >= 0 && get_pixel(x, y1) == newColor) { 
     if (x < w - 1 && get_pixel(x + 1, y1) == oldColor) { 
      floodFillStack(x + 1, y1, newColor, oldColor, pattern_fill); 
     } 
     y1--; 
    } 
} 

任何人都可以幫我看看問題嗎?

編輯:

感謝風向標的意見。該算法不再卡住,但它不覆蓋整個區域。 Here's圖片:

enter image description here

+1

刪除空行,正確縮進的代碼。 –

+0

@PabloEstrada如果你的洪水空間不是太大,你可以考慮使用遞歸。代碼將會簡單得多。 – user3437460

+0

謝謝你的建議,但我關心記憶。我曾嘗試使用遞歸,但是當使用該算法時,我跑出了堆棧,這就是爲什麼我將它改爲問題中的那個。 –

回答

1

如果忽略模式掩碼中的0值,則您的填充可能會失火。取而代之的是,總是用兩種顏色中的一種填充(與背景不同)。

您還必須更改一些條件測試。取而代之的

(... == newColor) 

你可以使用

(... != oldColor) 

(... == newColor1 || ... == newColor2) 
1

當填充用純色,每一個開始於oldColor像素被改爲newColor。這意味着稍後在該過程中對該像素的測試將不再與其匹配。

當您嘗試填充圖案時,其中一些像素將保持爲oldColor。你陷入無限循環,重複測試那些相同的像素。