Tuesday, 31 March 2015

Paint Fill

//paint fill function : given a point , a 2d array , and a new color , fill the image (2d-array here) using new color

public enum Color
{  
    Black , White , Red , Yellow , Green
}

public static void boolean PaintFill(Color[][] screen , int x , int  y  ,  Color nColor)
{
    /*
    *   Here x is column index and y is the row index , as happens in screen orientations
    */
   
    if(screen[y][x] == nColor) return false ;
    return PaintFill(screen , int x , int y , screen[y][x] , nColor);
}


public static boolean PaintFill (Color[][] screen , int x  , int y , Color oColor , Color nColor)
{
    //check if the index is out of bounds of the screen
    if ( y < 0  || y > screen.length   || x < 0 || x > screen[0].length)
        return false ;
   
    //if the present point pixel is of old color , recurse coloring the neighbors
    if (screen[y][x] == oColor)
    {
        PaintFill(screen , x-1 , y, oColor , nColor ) ; //left
        PaintFill(screen , x+1 , y, oColor , nColor ) ; //right
        PaintFill(screen , x , y-1, oColor , nColor ) ; //top
        PaintFill(screen , x , y+1, oColor , nColor ) ; //bottom
    }
   
    return true ;
}

No comments:

Post a Comment