我已经实现了一种洪水填充算法,该算法对于纯色填充可以正常工作。现在我正在研究图案填充,并决定添加一个标志来查看如何填充该区域(使用颜色或图案)。然而,当使用具有图案填充的算法时,在绘制区域时会卡住。
这是我使用纯色的原始代码:
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 的建议。算法不再卡顿,但并没有覆盖整个区域。这是一张图片:
最佳答案
如果您忽略图案掩码中的 0
值,您的洪水填充可能会失败。相反,始终使用两种颜色之一填充(两者都与背景不同)。
您还必须更改一些条件测试。而不是
(... == newColor)
你可以使用
(... != oldColor)
或
(... == newColor1 || ... == newColor2)
关于c - 图案洪水填充算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34957240/