Code:
#include <iostream>
//function: to simulate the paint bucket function to fill in an area of an "image"
// specifically using recursion.
//written by: justin mortensen
//date: 08/15/05
using namespace std;
const int rows = 10;
const int col = 15;
//example "image" for testing
char image[rows][col]= {
{'.','.','.','.','.','X','X','.','.','.','.','.','.','.','.'}, //0
{'.','.','.','.','X','.','.','X','X','.','.','.','.','.','.'}, //1
{'.','.','.','.','X','.','.','.','.','X','X','X','X','.','.'}, //2
{'.','.','.','X','.','.','.','.','.','.','.','.','X','X','X'}, //3
{'.','.','X','.','.','.','.','.','.','.','.','.','.','.','.'}, //4
{'.','.','.','X','.','.','.','.','.','.','.','.','X','X','.'}, //5
{'.','.','.','X','.','.','X','X','X','.','.','.','X','.','X'}, //6
{'.','.','.','X','.','.','X','.','.','X','.','X','.','.','.'}, //7
{'.','.','.','X','X','X','X','.','.','.','X','.','.','.','.'}, //8
{'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'} //9
};
// function prototypes
void printImage();
void AreaFill(int x, int y);
void AreaFill_helperLD(int x, int y); //Left and down
void AreaFill_helperRD(int x, int y); //Righ and down
void AreaFill_helperUL(int x, int y); //Up and left
void AreaFill_helperUR(int x, int y); //Up and right
int main ()
{
printImage();
cout << endl;
AreaFill(6,9); //putting these coordinates at the start doesn't fill
printImage(); // in the whole "imag"
cin.get();
return 0;
}
//function to fill the image
void AreaFill(int x, int y) {
AreaFill_helperLD(x,y);
AreaFill_helperRD(x,y);
AreaFill_helperUL(x,y);
AreaFill_helperUR(x,y);
}
void AreaFill_helperLD(int x, int y) {
if (image[x][y] == 'X') {
return;
}
else if (x > rows - 1) {
return;
}
else if (x < 0) {
return;
}
else if (y > col - 1) {
return;
}
else if (y < 0) {
return;
}
else {
image[x][y] = '0';
AreaFill_helperLD((x+1),(y));
AreaFill_helperLD((x),(y-1));
}
}
void AreaFill_helperRD(int x, int y) {
if (image[x][y] == 'X') {
return;
}
else if (x > rows - 1) {
return;
}
else if (x < 0) {
return;
}
else if (y > col - 1) {
return;
}
else if (y < 0) {
return;
}
else {
image[x][y] = '0';
AreaFill_helperRD((x),(y+1));
AreaFill_helperRD((x+1),(y));
}
}
void AreaFill_helperUL(int x, int y) {
if (image[x][y] == 'X') {
return;
}
else if (x > rows - 1) {
return;
}
else if (x < 0) {
return;
}
else if (y > col - 1) {
return;
}
else if (y < 0) {
return;
}
else {
image[x][y] = '0';
AreaFill_helperUL((x-1),(y));
AreaFill_helperUL((x),(y-1));
}
}
void AreaFill_helperUR(int x, int y) {
if (image[x][y] == 'X') {
return;
}
else if (x > rows - 1) {
return;
}
else if (x < 0) {
return;
}
else if (y > col - 1) {
return;
}
else if (y < 0) {
return;
}
else {
image[x][y] = '0';
AreaFill_helperUR((x),(y+1));
AreaFill_helperUR((x-1),(y));
}
}
void printImage() {
int i = 0;
while( i < 10) {
for (int j = 0; j < 15; j++) {
cout << image[i][j];
}
cout << endl;
i++;
}
}