Search code examples
c++recursionmatrixdivide-and-conquerboundary

Boundary Fill Simulation (C++)


I'm trying to simulate the Boundary Fill method with a matrix of integer, which in each position has a number from 0-255 that identifies the "pixel color" and I ask a position, a color to be changed an color to replace it. The code I implemented works well for square matrices, but if it's not square I have two bugs:

1- if the number of lines is greater than the number of columns, the algorithm ignores the last line and does everything without change on this last line.

2 - if the number of columns is greater than the number of lines, I get a segmentation error around the firsts iterations.

I'd like to know from someone who could help, what I'm doing wrong. I tried to debug (with prints, if some one could show me a better way wolb be thankful). The logic seems right and this segmentation error is bizarre.

Here is the code of the function:

void BoundaryFill(int*** img, int x, int y, int newColor, int oldColor, int WIDTH, int HEIGTH){
 if(x >= 0 && x < WIDTH && y >= 0 && y < HEIGTH && (*img)[x][y] == oldColor && (*img)[x][y] != newColor){

     (*img)[x][y] = newColor; //set color before starting recursion
     BoundaryFill(img, x + 1, y, newColor, oldColor, WIDTH, HEIGHT);
     BoundaryFill(img, x - 1, y, newColor, oldColor, WIDTH, HEIGHT);
     BoundaryFill(img, x, y + 1, newColor, oldColor, WIDTH, HEIGHT);
     BoundaryFill(img, x, y - 1, newColor, oldColor, WIDTH, HEIGHT);
     BoundaryFill(img,x + 1, y + 1, newColor, oldColor,WIDTH,HEIGHT);
     BoundaryFill(img,x - 1, y - 1, newColor, oldColor,WIDTH,HEIGHT);
     BoundaryFill(img,x - 1, y + 1, newColor, oldColor,WIDTH,HEIGHT);
     BoundaryFill(img,x + 1, y - 1, newColor, oldColor,WIDTH,HEIGHT);
 } }

Here is main code:

int main(){
int x, y, new_color,old_color;

//Reads the size of the matrix
int HEIGHT; cin >> HEIGHT;
int WIDTH; cin >> WIDTH;

int** img = new int* [HEIGHT];
for (int i=0;i<HEIGHT;i++)
    img[i] = new int [WIDTH];

//Reads the matrix
for (int i=0;i<HEIGHT;i++){
    for (int j=0;j<WIDTH;j++){
        cin >> img[i][j];
    }
}

cin >> x >> y;
old_color = 1; //assuming the old color always gonna be 1
cin >> new_color;

BoundaryFill(&img,x,y,new_color,old_color ,WIDTH,HEIGHT);

//Shows the matrix
for (int i=0;i<HEIGHT;i++){
    for (int j=0;j<WIDTH;j++){
        cout << img[i][j] << " ";
    }
    cout << endl;
}

//Free the HEAP
for(int i = 0;i < WIDTH; i++)
    delete []img[i];
delete []img;}

Here is some of input files, with the matrices, that I used to test (couldn't place the numbers of the input on the matrix format, but what those numbers mean is: first 2 define the size of the matrix, number of lines and number of columns. Last three define the start position an the color that will replace. The rest is the input of values for the matrix):

More lines:

8 7 2 1 1 1 1 1 2 1 2 1 1 1 2 1 1 1 2 1 2 1 1 1 1 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 1 1 2 1 2 1 1 1 1 1 2 2 2 1 1 1 2 2 2 1 5

More Columns:

7 8 2 1 1 1 1 1 2 2 1 2 1 1 1 2 1 1 1 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 2 1 1 1 1 2 1 1 1 2 1 1 2 1 1 1 1 1 2 2 2 1 5


Solution

  • When you set up your arrays in main, the first subscript to img is the vertical (HEIGHT) one, and the second is the horizontal (WIDTH) one. In BoundaryFill, you have this reversed where you use the horizontal index first and the vertical index second.

    You should use (*img)[y][x] within BoundaryFill.