/*
* THIS IS A C PROGRAM THAT USES SEED FILL ALGORITHM TO FILL EACH ENCLOSED PART OF A
5 VERTEX STAR WITH A SEPARATE COLOR
*/
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#define MAXST 5000
int BOUNDARY_COLOR=RED;
int FILL_COLOR;
typedef struct
{
int x,y;
}Pixel;
struct stack_t{
int top;
Pixel val[MAXST];
};
typedef struct stack_t *stack;
stack pixelstack;
/*-----The Stack Adt------------------------- */
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)--];
}
/* The seedfill routine */
void seedfill(Pixel seed)
{
Pixel pixel=seed,temppixel;
push(pixelstack,pixel);
setcolor(FILL_COLOR);
while(!s_empty(pixelstack))
{
pixel=pop(pixelstack);
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(pixelstack,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(pixelstack,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(pixelstack,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(pixelstack,temppixel);
}
delay(1);
}
}
void main(){
int x,y,r,originx,originy;
int gdriver=DETECT,gmode,i=0;
Pixel seed1,seed2,seed3,seed4,seed5,seed6;
/* creating stack */
pixelstack=s_create();
/* 6 seeds for 6 regions */
seed1.x=135;
seed1.y=115;
seed2.x=185;
seed2.y=80;
seed3.x=245;
seed3.y=115;
seed4.x=220;
seed4.y=185;
seed5.x=135;
seed5.y=184;
seed6.x=185;
seed6.y=130;
/* the vertices of the star */
int poly[12]={80,100,300,100,95,225,190,35,265,225,80,100};
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
/* DRAW STAR */
setcolor(BOUNDARY_COLOR);
setlinestyle(SOLID_LINE,2,3);
drawpoly(6,poly);
/* fill 6 regions with 6 colors */
FILL_COLOR=YELLOW;
seedfill(seed1);
FILL_COLOR=GREEN;
seedfill(seed2);
FILL_COLOR=CYAN;
seedfill(seed3);
FILL_COLOR=MAGENTA;
seedfill(seed4);
FILL_COLOR=WHITE;
seedfill(seed5);
FILL_COLOR=BLUE;
seedfill(seed6);
}
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.