Polygon Filling

1.Non recursive Seed Fill/Flood Fill
Flood Filling Algorithm 8 connected


/* FloodFill of Polygon with eight connected region */
#include<stdio.h>
#include<graphics.h>
void main()
{int gd,gm;

/* Initialise graphics mode
---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\bgi");

rectangle(50,50,100,100);
flood(55,55,4,15);
getch();
closegraph();
}

flood(seed_x,seed_y,foreground_col,background_col)
{
 if (getpixel(seed_x,seed_y)!= background_col && getpixel(seed_x,seed_y)!= foreground_col)
  {
   putpixel(seed_x,seed_y,foreground_col);
   flood(seed_x+1,seed_y,foreground_col,background_col);
   flood(seed_x-1,seed_y,foreground_col,background_col);
   flood(seed_x,seed_y+1,foreground_col,background_col);
   flood(seed_x,seed_y-1,foreground_col,background_col);
   flood(seed_x+1,seed_y+1,foreground_col,background_col);
   flood(seed_x-1,seed_y-1,foreground_col,background_col);
   flood(seed_x+1,seed_y-1,foreground_col,background_col);
   flood(seed_x-1,seed_y+1,foreground_col,background_col);
  }
return 0;
}

FLOOD FILL USING 4 Connected Region

#include<stdio.h>
#include<graphics.h>
void main()
{
int gd,gm;

/* Initialise graphics mode
---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\bgi");

rectangle(50,50,100,100);
flood(55,55,4,15);
getch();
closegraph();
}

flood(seed_x,seed_y,foreground_col,background_col)
{
 if (getpixel(seed_x,seed_y)!= background_col && getpixel(seed_x,seed_y)!= foreground_col)
  {
   putpixel(seed_x,seed_y,foreground_col);                 delay(1);
   flood(seed_x+1,seed_y,foreground_col,background_col);   delay(1);
   flood(seed_x-1,seed_y,foreground_col,background_col);   delay(1);
   flood(seed_x,seed_y+1,foreground_col,background_col);   delay(1);
   flood(seed_x,seed_y-1,foreground_col,background_col);   delay(1);
  }
 return 0;
}
2.Scan Line Algorithm
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

main()
{
 int n,i,j,k,gd,gm,dy,dx;  //variable declaration
 int x,y,temp;
 int a[20][2],xi[20];   //2D arrary declaration for a.. 20 is the max size.
 float slope[20];   //1D array declaration for slope

 clrscr();
 printf("\n\n\tEnter the no. of edges of polygon : ");
 scanf("%d",&n);
 printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

 for(i=0;i<n;i++)
 {
  printf("\t X=%d Y=%d : ",i,i);   //accepting cordinates
  scanf("%d %d",&a[i][0],&a[i][1]);
 }

 a[n][0]=a[0][0];    //assign a[0][0] value to a[n][0]
 a[n][1]=a[0][1];    //assign a[0][1] value to a[n][1]
 detectgraph(&gd,&gm);
 initgraph(&gd,&gm,"c:\\tc\\bgi");


 /*- draw polygon -*/

 for(i=0;i<n;i++)
 {
  line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); //draw lines
 }
 getch();

 for(i=0;i<n;i++)
 {
  dy=a[i+1][1]-a[i][1];   // suppose dy=y2-y1
  dx=a[i+1][0]-a[i][0];   //suppose dx=x2-x1

  if(dy==0)
   slope[i]=1.0;
  if(dx==0)
   slope[i]=0.0;
  if((dy!=0)&&(dx!=0))    /*- calculate inverse slope -*/
  {
   slope[i]=(float) dx/dy;  // typecast to float
  }
 }

 for(y=0;y< 480;y++)    // maximum range of y length resolution is 480 
 {
  k=0;
  for(i=0;i<n;i++)
  {
   if( ((a[i][1]<=y)&&(a[i+1][1]>y))||  //check value is greater or less than y
    ((a[i][1]>y)&&(a[i+1][1]<=y)))
   {
    xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1])); // find x value array using this formula
    k++;
   }
  }

  for(j=0;j<k-1;j++)    /*- Arrange x-intersections in order -*/
   for(i=0;i<k-1;i++)
   {
    if(xi[i]>xi[i+1]) //sort x intersection in xi[k] in order using swapping
    {
     temp=xi[i];
     xi[i]=xi[i+1];
     xi[i+1]=temp;
    }
   }
  setcolor(35);
  for(i=0;i<k;i+=2)   // draw lines to fill polygon
  {
   line(xi[i],y,xi[i+1],y);
   getch();
  }
 }
 return 0;
}