Flood fill algorithm

0

Bookmark and Share
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void bfill(int a,int b,int newcolor,int n);
int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "c:/turboc3/bgi");

   /* read result of initialization */
   errorcode = graphresult();

   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }

   /* draw a line */
int a,b,n,gx=0,gy=0,i,x[10],y[10],newcolor,xmax,ymax,xmin,ymin;

cout<<"\nEnter no. of points: ";
cin>>n;
for(i=0;i<n;i++)
{
    cout<<"\nEnter "<<"x"<<i<<" y"<<i<<" ";
    cin>>x[i]>>y[i];
    gx=gx+x[i];
    gy=gy+y[i];
}
a=gx/n;
b=gy/n;
cout<<"\nWhich colour do you want?(1 to 20)";
cin>>newcolor;
setcolor(newcolor);
for(i=0;i<n-1;i++)
{
    line(x[i],y[i],x[i+1],y[i+1]);
}
    line(x[i],y[i],x[0],y[0]);

bfill(a,b,newcolor,n);
   /* clean up */
   getch();
   closegraph();
   return 0;
}

void bfill(int a,int b,int newcolor,int n)
{

    int current=getpixel(a,b);
    if(current!=newcolor)
    {
        putpixel(a,b,newcolor);
        bfill(a+1,b,newcolor,n);
        bfill(a-1,b,newcolor,n);
        bfill(a,b+1,newcolor,n);
        bfill(a,b-1,newcolor,n);

    }
   
Read More >>
 
C -Programming Design by Trick and Tips Powered by Blogger