c - 图案洪水填充算法

标签 c algorithm graphics

我已经实现了一种洪水填充算法,该算法对于纯色填充可以正常工作。现在我正在研究图案填充,并决定添加一个标志来查看如何填充该区域(使用颜色或图案)。然而,当使用具有图案填充的算法时,在绘制区域时会卡住。

这是我使用纯色的原始代码:

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

谁能帮我看看这个问题吗?

编辑:

感谢 WeatherVane 的建议。算法不再卡顿,但并没有覆盖整个区域。这是一张图片:

enter image description here

最佳答案

如果您忽略图案掩码中的 0 值,您的洪水填充可能会失败。相反,始终使用两种颜色之一填充(两者都与背景不同)。

您还必须更改一些条件测试。而不是

(... == newColor)

你可以使用

(... != oldColor)

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

关于c - 图案洪水填充算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34957240/

相关文章:

C:元素大小不同的数组

algorithm - 最大单笔销售利润 - 并行版本

c - 为什么SDL2在最后一个printf之前显示对话框消息?很有意思

python - Ubuntu 11.10 上的 VPython 错误

python - 导入禁用图形的 Pandas

c - 对变量使用 c 宏

c - 了解 uint32_t char 类型转换(字节)

c - OpenMP 中的 C 指针

c# - 给定 Vector3 列表和距离,如何计算分段线上的点?

performance - Floyd–Rivest 对比 Introselect 算法性能