我已經實現了洪水填充算法,可以正常工作,用於純色填充。現在我正在進行圖案填充並決定添加一個標記以查看如何填充該區域(使用顏色或圖案)。但是在使用帶圖案填充的算法在繪製區域時卡住。模式洪泛填充算法
這是我的原代碼,與純色的工作原理:
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圖片:
刪除空行,正確縮進的代碼。 –
@PabloEstrada如果你的洪水空間不是太大,你可以考慮使用遞歸。代碼將會簡單得多。 – user3437460
謝謝你的建議,但我關心記憶。我曾嘗試使用遞歸,但是當使用該算法時,我跑出了堆棧,這就是爲什麼我將它改爲問題中的那個。 –