Friday, December 4, 2009

Boundary and Flood Filling (recursive and non recursive)

#include
#include
#include
#include
#define round(a)((int)(a+0.5))
struct point{ int xc;int yc;}curpix, stk [9999];
int top=-1;
void push(int x,int y)
{ top++;
stk[top].xc=x;
stk[top].yc=y;
}
void pop()
{ curpix.xc=stk[top].xc;
curpix.yc=stk[top].yc;
top--;
}

void ddaline(int x1,int y1,int x2,int y2,int col)
{
int dx,dy,steps,k;
float x,y,xinc,yinc;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(round(x),round(y),col);
for(k=0;k { x+=xinc;
y+=yinc;
putpixel(round(x),round(y),col);
}
}
void bfill(int x,int y,int bclr,int fclr)
{ int curclr;
curclr=getpixel(x,y);
if(curclr!=fclr && curclr!=bclr)
{
putpixel(x,y,fclr);
bfill(x+1,y,bclr,fclr);
bfill(x,y+1,bclr,fclr);
bfill(x-1,y,bclr,fclr);
bfill(x,y-1,bclr,fclr);
}
}

void ffill(int x,int y,int oclr,int fclr)
{ int curclr;
curclr=getpixel(x,y);
if(curclr==oclr)
{
putpixel(x,y,fclr);
ffill(x+1,y,oclr,fclr);
ffill(x,y+1,oclr,fclr);
ffill(x-1,y,oclr,fclr);
ffill(x,y-1,oclr,fclr);
}
}

void rff(int x,int y,int oclr,int fclr)
{ int curclr;
while(top!=-1)
{ curclr=getpixel(x,y);
if(curclr==oclr)
{
putpixel(x,y,fclr);
push(x+1,y);
push(x,y+1);
push(x-1,y);
push(x,y-1);
}
pop();
x=curpix.xc;
y=curpix.yc;

}
}
void rbf(int x,int y,int bclr,int fclr)
{ int curclr;
while(top!=-1)
{ curclr=getpixel(x,y);
if(curclr!=bclr && curclr!=fclr)
{
putpixel(x,y,fclr);
push(x+1,y);
push(x,y+1);
push(x-1,y);
push(x,y-1);
}
pop();
x=curpix.xc;
y=curpix.yc;

}
}
//*********************************************
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x[10],y[10],n,i,sumx=0,sumy=0,seedx,seedy,old;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the no of sides of polygon\n");
scanf("%d",&n);
printf("\n enter the end points of polygon\n");
for(i=0;i {
scanf("%d",&x[i]);
scanf("%d",&y[i]);
sumx+=x[i];sumy+=y[i];
}
x[i]=x[0];y[i]=y[0];
for(i=0;i {
ddaline(x[i],y[i],x[i+1],y[i+1],2);
}
seedx=sumx/n;
seedy=sumy/n;
old=getpixel(seedx,seedy);
push(seedx,seedy);
rbf(seedx,seedy,2,RED);
getch();
}



No comments:

Post a Comment