Flood Fill Algorithm OpenGL

  • westwicked
    Likes 0

    Problem Description

    Hello,

    I’m starting on writing a simple paint application for OpenGL as part of my assignment. However, I am having problems with the Flood Fill algorithm needed to fill my polygons to a different color. I am using a seed of 4 recursive algorthm. 

    My non-functioning code is:

    void floodFill(Color oldColor, int x, int y){
    	Color currentPixelColor = getPixelColor(x, y);
    
    	if(equalColor(currentPixelColor, oldColor) == true){
    			setFillColor(x, y);
    			repaint();
    			floodFill(oldColor, x + 1, y);
    			floodFill(oldColor, x - 1, y);
    			floodFill(oldColor, x, y + 1);
    			floodFill(oldColor, x, y - 1);
    		}
    }

    For my filling function (setFillColor(x, y)) is implemented by pushing back a point of the newColor, which is a global variable, into a vector. So everytime I call my repaint() function, the paint canvas will be redrawn with the newly pushed point in the vector.

    The problem now is that whenever I run this function, I will be getting an infinite loop until the recursion is too deep. This happens to small polygons as well. I tried debugging by commenting out my lines so that the algorthm only fill pixels at the right of the seed, like this:

    ​
    void floodFill(Color oldColor, int x, int y){
    	Color currentPixelColor = getPixelColor(x, y);
    
    	if(equalColor(currentPixelColor, oldColor) == true){
    			setFillColor(x, y);
    			repaint();
    			floodFill(oldColor, x + 1, y);
    			//floodFill(oldColor, x - 1, y);
    			//floodFill(oldColor, x, y + 1);
    			//floodFill(oldColor, x, y - 1);
    		}
    }
    ​

    My output would be:

    which works! It works for a single line recursive.

    However, I encounter an infinite loop as soon as I uncomment a line and allow a 2 line-recursive algorithm. (left and right fill)

    Could you help me take a look and enlighten me on what could be the problem?

    Thank you so much, once again for your time smiley

  • Sonar Systems admin
    Likes 0

    What result are you trying to achieve.

  • westwicked
    Likes 0

    I am trying to change the color of my polygon with flood fill. I have made some changes here and there, discarding the use of vectors, to go easy on memory allocation. My algorithm now works, however it crashes halfway with an access violation exception.

    Why is that so, when I didn’t store anything? 

    My new code just draw points of the new color and glFlush() the command buffer to apply the change, point by point. 

  • Sonar Systems admin
    Likes 0

    Can you show us the error

Login to reply