Page 1 of 1

Fill a delimited region with a color in a draw

PostPosted: Thu Oct 04, 2012 9:37 am
by elfdav
Hey guys,

Do you know if there is a simple way to fill a delimited region with a selected color with just one click.
Just like mspaint and many other softwares does.

Just see the attached draw...

For those who know that, i'm looking for the FloodFill API equivalent in Windows world.

Thanks for your help, guys.

David

Re: Fill a delimited region with a color in a draw

PostPosted: Thu Oct 04, 2012 4:48 pm
by skydereign
If you set up a giant array to hold the values for your canvas, than it is pretty easy with a recursive function (something like this).
Code: Select all
int canv[100][100][3];

void
fill (int x, int y, int r1, int g1, int b1, int r2, int g2, int b2)
{
    canv[y][x][0] = r2; // might also use setpen here if you want this to be what updates your canvas
    canv[y][x][1] = g2;
    canv[y][x][2] = b2;

    if(x+1<100 && canv[y][x+1][0]==r1 && canv[y][x+1][1]==g1 && canv[y][x+1][g]==b1)
    { // right
        fill(x+1, y, r1, g1, b1, r2, g2, b2);
    }
    if(x-1>=0 && canv[y][x-1][0]==r1 && canv[y][x-1][1]==g1 && canv[y][x-1][g]==b1)
    { // left
        fill(x-1, y, r1, g1, b1, r2, g2, b2);
    }
    if(y+1<100 && canv[y+1][x][0]==r1 && canv[y+1][x][1]==g1 && canv[y+1][x][g]==b1)
    { // down
        fill(x, y+1, r1, g1, b1, r2, g2, b2);
    }
    if(y-1>=0 && canv[y-1][x][0]==r1 && canv[y-1][x][1]==g1 && canv[y-1][x][g]==b1)
    { // up
        fill(x, y-1, r1, g1, b1, r2, g2, b2);
    }
}

Re: Fill a delimited region with a color in a draw

PostPosted: Sat Oct 06, 2012 8:50 pm
by elfdav
Thanks Sky.