Saturday, February 05, 2011

Seed Fill Sickle



/*
 * THIS IS A C PROGRAM THAT USES SEED FILL ALGORITHM TO FILL A DOUBLE SICKLE FIGURE
 */

#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){
             perror("Malloc Error");
             exit(0);
            }
            p->top=-1;
            return p;
}

int s_empty(stack s)     {
    return ((s->top)==-1)?1: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(1);
   }
}

void main()     {
            int x,y;
            int gdriver=DETECT,gmode;
            char msg[80];
            Pixel seed;
            seed.x=175;
            seed.y=150;
            int poly[10]={100,100,100,200,200,200,200,100,100,100};
            initgraph(&gdriver,&gmode,"c:\\tc\\bgi");  doit();
            setcolor(BOUNDARY_COLOR);
            setlinestyle(SOLID_LINE,2,3);
            arc(100,150,300,60,50);
            arc(150,150,60,120,50);
            arc(150,150,240,300,50);
            arc(200,150,60,120,50);
            arc(200,150,240,300,50);
            arc(250,150,120,240,50);
            seedfill(seed);
}

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.