Saturday, December 7, 2013

C graphics program to fill a rectangle using 4 -connected floodfill algorithm

#include <graphics.h>
#include <conio.h>
void flood_fill4(int x,int y,int newColor,int oldColor)
{
int c;
c=getpixel(x,y);
if(c==oldColor)
{
setcolor(newColor);
putpixel (x,y,newColor);
delay(10);
flood_fill4(x+1,y,newColor,oldColor);
flood_fill4(x,y+1,newColor,oldColor);
flood_fill4(x-1,y,newColor,oldColor);
flood_fill4(x,y-1,newColor,oldColor);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
rectangle(250,200,300,250);
flood_fill4(251,201,4,0); // 4(red) - newColor  0(black) - oldColor
          // x y point should be one greater than rectangle border
getch();
closegraph();
}

5 comments:

  1. why the old color is always in black ,it does not accept any other color

    ReplyDelete
    Replies
    1. just try using setbkcolor. it works for me. no need to be black as background color.u can set any color u like as background color

      Delete
  2. Nice program. I came across one more program for flood fill in C.
    http://jee-appy.blogspot.in/2011/10/flood-fill-program-in-c.html

    ReplyDelete
  3. would you please post scaling, shearing, rotation and transformation for RECTANGLE ...
    I need it like a hell

    ReplyDelete