Hello,

i wrote a program for filling polygon.First i did it with recursive calls. But recursive calls are too bad they fill the stack memory and my compiler crashed. So then i used stack now in stack also my work is done but the memory requirement is way too high. This is a flood fill algorithm. Can anybody optimize my solution i will be very grateful to that person. The code is .

Code:

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
struct points
{
int x;
int y;
};
struct stack
{
int dataX[10000];
int dataY[10000];
int top;
};
void push(struct stack *s,int m,int n)
{
s->top++;
s->dataX[s->top]=m;
s->dataY[s->top]=n;
}
struct points pop(struct stack *s)
{
struct points t;
t.x=s->dataX[s->top];
t.y=s->dataY[s->top];
s->top--;
return t;
}
int main(void)
{
int gd=DETECT,gm,errorcode;
int n=4,i;
struct points w[10],t;
int x=105,y=105;
struct stack s;
s.top=-1;
initgraph(&gd,&gm,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("%s",grapherrormsg(errorcode));
exit(1);
}
w[0].x=100; // assigning to draw square
w[0].y=100;
w[1].x=200;
w[1].y=100;
w[2].x=200;
w[2].y=200;
w[3].x=100;
w[3].y=200;
for(i=0;i<=n-2;i++) //drawing square
line(w[i].x,w[i].y,w[i+1].x,w[i+1].y);
line(w[0].x,w[0].y,w[n-1].x,w[n-1].y);
push(&s,x,y);
while(s.top!=-1) //code for flood fill
{
putpixel(x,y,14);
if(getpixel(x,y+1)==0)
push(&s,x,y+1);
if(getpixel(x,y-1)==0)
push(&s,x,y-1);
if(getpixel(x+1,y)==0)
push(&s,x+1,y);
if(getpixel(x-1,y)==0)
push(&s,x-1,y);
delay(3);
t=pop(&s);
x=t.x;
y=t.y;
}
return 0;
}