2016-01-22 35 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。你陷入无限循环,重复测试那些相同的像素。