Saturday, February 05, 2011

Seed Fill



/*
 * THIS IS A C PROGRAM THAT IMPLEMENTS SEED FILL ALGORITHM
 */

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#define MAXST 2048
#define BOUNDARY_COLOR BLACK
#define FILL_COLOR DARKGRAY

typedef struct
{
 int x,y;
}Pixel;

struct stack_t{
            int top;
            Pixel val[MAXST];
};
typedef struct stack_t *stack;

stack s_create(){
            stack p;
            p=(stack)malloc(sizeof(struct stack_t));
            if(p==NULL){
             exit(0);
            }
            p->top=-1;
            return p;
}
int s_empty(stack s){
            if(s->top==-1)
                        return 1;
            else return 0;
}

void push(stack s,Pixel e){
            s->val[++(s->top)]=e;
}

Pixel pop(stack s){
            if(!(s_empty(s)))
                        return s->val[(s->top)--];
}

void seedfill(Pixel seed)
{ 
   stack s=s_create();
   Pixel pixel=seed,temppixel;
   push(s,pixel);
   setcolor(FILL_COLOR);
   while(!s_empty(s))
   {
     pixel=pop(s);
     if(getpixel(pixel.x,pixel.y)!=BOUNDARY_COLOR && getpixel(pixel.x,pixel.y)!=FILL_COLOR)
      putpixel(pixel.x,pixel.y,FILL_COLOR);
     if(getpixel(pixel.x+1,pixel.y)!=BOUNDARY_COLOR && getpixel(pixel.x+1,pixel.y)!=FILL_COLOR)
     {
      temppixel=pixel;
      temppixel.x++;
      push(s,temppixel);
     }
     if(getpixel(pixel.x,pixel.y+1)!=BOUNDARY_COLOR && getpixel(pixel.x,pixel.y+1)!=FILL_COLOR)
     {
      temppixel=pixel;
      temppixel.y=temppixel.y+1;
      push(s,temppixel);
     }
     if(getpixel(pixel.x-1,pixel.y)!=BOUNDARY_COLOR && getpixel(pixel.x-1,pixel.y)!=FILL_COLOR)
     {
      temppixel=pixel;
      temppixel.x=temppixel.x-1;
      push(s,temppixel);
     }
     if(getpixel(pixel.x,pixel.y-1)!=BOUNDARY_COLOR && getpixel(pixel.x,pixel.y-1)!=FILL_COLOR)
     {
      temppixel=pixel;
      temppixel.y=temppixel.y-1;
      push(s,temppixel);
     }
     delay(5);
   }
}

void main(){

            int x,y,r,originx,originy;
            int gdriver=DETECT,gmode,i=0;
            char msg[80];
            Pixel seed;
            seed.x=150;
            seed.y=150;
            int poly[10]={100,100,100,200,200,200,200,100,100,100};
            //initialize the graphics support
            initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); doit();
            setcolor(BOUNDARY_COLOR);
            setlinestyle(SOLID_LINE,2,3);
            drawpoly(5,poly);
            seedfill(seed);
            getch();
}

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.