0

This is for an assignment. We were asked to make fully recursive and partially recursive flood-fill functions. I was able to complete the fully recursive function simply enough, but I'm struggling on the partially recursive one. I could use a second opinion on this because I've convinced myself that I have done it right and don't know where to look for a mistake anymore. I have included comments on my logic for each line of code.

 //setup function for the partially recursive function.
void DoFloodFill( int x, int y )
{
    x -= m_nTestShapeX;
    y -= m_nTestShapeY;
    m_nStartColor = GetPixel(x,y) | 0xff000000;
    Graphics canvas = getGraphics();
    canvas.setColor( m_objSelectedColor );

    int w = m_objShape.getWidth();
    int h = m_objShape.getHeight();


    if( m_nStartColor == m_nSelectedColor)
    {
        return;
    }

    FloodFill( x, y, w, h, canvas);
}

void FloodFill( int x, int y, int w, int h, Graphics canvas )
{
    int xx = 0, right = 0;

    // if the x or y values are out of bounds return
    if(x >= w || y >= h || x < 0 || y < 0)
        return; 

    //if the passed in pixel is not the start color return
    //base case for recursion
    if(GetPixel(x,y) != this.m_nStartColor)
        return;

    //used to walk right untill a wall or bound is hit.
    xx = x;

    //walk right from the current pixel setting it to the desired color
    while(xx < w && this.m_nStartColor == GetPixel(xx,y))
    {
        this.SetPixel(xx+100, y+100, canvas);
        this.SetPixel(xx+100, y+100, this.m_nSelectedColor);
        xx++;
    }
    //save the x value of the the pixel where the wall is
    right = xx;

    //used to left starting one pixel to the left of the current pixel 
    xx = x-1;

    //walk left of the current pixel setting it to the desired color
    while(xx >= 0 && this.m_nStartColor == GetPixel(xx,y) )
    {
        this.SetPixel(xx+100, y+100, canvas);
        this.SetPixel(xx+100, y+100, this.m_nSelectedColor);
        xx--;
    }

    //start from where the left wall is 
    for(; xx < right; xx++)
    {
        //now this should go up one/down one and repeat the right and left walks
        //the base cases should prevent this from running in most cases
        //because when it tries to walk down and the start color is not == to the current pixel it will return.
        FloodFill(xx,y+1,w,h,canvas);
        FloodFill(xx,y-1,w,h,canvas);
    }
}

this is what it looks like when i click on a pixel. Where i clicked is marked in red. eventual stack-overflows

1 Answers1

0

From the above code change:

this.SetPixel(xx+100, y+100, m_nSelectedColor);

to

this.SetPixel(xx, y, m_nSelectedColor);