Khái niệm
Đối với một hình chữ nhật nhất định, nhiệm vụ của chúng ta là lấp đầy hình chữ nhật này bằng cách áp dụng thuật toán lấp đầy.
Đầu vào
rectangle(left = 50, top = 50, right= 100, bottom = 100) floodFill( a = 55, b = 55, NewColor = 12, OldColor = 0)
Đầu ra
Phương pháp
// Một hàm đệ quy để thay thế màu trước 'OldColor' tại '(a, b)' và tất cả các pixel xung quanh của (a, b) bằng màu mới 'NewColor' và lũ lụt (a, b, NewColor, OldColor)
-
Nếu a hoặc b nằm ngoài màn hình, hãy quay lại.
-
Nếu màu của getpixel (a, b) giống với OldColor thì
-
Lặp lại cho trên, dưới, phải và trái.
FloodFill (a + 1, b, NewColor, OldColor); <
FloodFill (a-1, b, NewColor, OldColor);
FloodFill (a, b + 1, NewColor, OldColor);
FloodFill (a, b-1, NewColor, OldColor);
Ví dụ
// Shows program to fill polygon using floodfill
// algorithm
#include <graphics.h>
#include <stdio.h>
// Describes flood fill algorithm
void flood(int x1, int y1, int new_col, int old_col){
// Checking current pixel is old_color or not
if (getpixel(x1, y1) == old_col) {
// Putting new pixel with new color
putpixel(x1, y1, new_col);
// Shows recursive call for bottom pixel fill
flood(x1 + 1, y1, new_col, old_col);
//Shows recursive call for top pixel fill
flood(x1 - 1, y1, new_col, old_col);
// Shows recursive call for right pixel fill
flood(x1, y1 + 1, new_col, old_col);
// Shows recursive call for left pixel fill
flood(x1, y1 - 1, new_col, old_col);
}
}
int main(){
int gd1, gm1 = DETECT;
// Initializing graph
initgraph(&gd1, &gm1, "");
//Shows rectangle coordinate
int top1, left1, bottom1, right1;
top1 = left1 = 50;
bottom1 = right1 = 300;
// Shows rectangle for print rectangle
rectangle(left1, top1, right1, bottom1);
// Fills start cordinate
int x1 = 51;
int y1 = 51;
// Shows new color to fill
int newcolor = 12;
// Shows old color which you want to replace
int oldcolor = 0;
// Calling for fill rectangle
flood(x1, y1, newcolor, oldcolor);
getch();
return 0;
} Đầu ra