Boundary and Flood Fill Algorithms
Write a program for Boundary and Flood Fill Algorithms in C++ Language . Boundary and Flood Fill Algorithms in C++ should accept the coordinates for polygon.
- INPUT
- FLOOD FILL
- BOUNDARY FILL
- EXIT
After selecting Boundary and Flood fill algorithm option , It must fill polygon by both algorithms.
Boundary and Flood Fill Algorithms Code
#include #include #include #include #define max 100000 class fill { int edge,a[50],sx,sy,midx,midy; public: void input() { cout<<"\nENTER NO OF EDGES:"; cin>>edge; int j=0; for(int i=0;i<edge;i++) <br=""> { cout<<"\nENTER THE X"<<i+1<<"coordinate:"; <br=""> cin>>a[j++]; cout<<"\nENTER THE Y"<<i+1<<"coordinate:"; <br=""> cin>>a[j++]; } } void b_fill(int x,int y,int clr) { int j=0; midx=getmaxx()/2; midy=getmaxy()/2; setcolor(WHITE); line(0,midy,2*midx,midy); line(midx,0,midx,2*midy); setcolor(RED); for(int i=0;i<edge;i++) <br=""> { if(edge-i!=1) line(a[j]+midx,midy-a[j+1],a[j+2]+midx,midy-a[j+3]); else line(a[j]+midx,midy-a[j+1],a[0]+midx,midy-a[1]); j=j+2; } getch(); int st[max][2],top=-1; x=midx+x; y=midy-y; st[++top][0]=x; st[top][1]=y; while(top!=-1) { x=st[top][0]; y=st[top][1]; top--; if((getpixel(x+1,y)!=clr)&&(getpixel(x+1,y)!=RED)) { putpixel(x+1,y,clr); st[++top][0]=x+1; st[top][1]=y; } if((getpixel(x-1,y)!=clr)&&(getpixel(x-1,y)!=RED)) { putpixel(x-1,y,clr); st[++top][0]=x-1; st[top][1]=y; } if((getpixel(x,y+1)!=clr)&&(getpixel(x,y+1)!=RED)) { putpixel(x,y+1,clr); st[++top][0]=x; st[top][1]=y+1; } if((getpixel(x,y-1)!=clr)&&(getpixel(x,y-1)!=RED)) { putpixel(x,y-1,clr); st[++top][0]=x; st[top][1]=y-1; } } } void f_fill(int x,int y,int clr) { int j=0; midx=getmaxx()/2; midy=getmaxy()/2; setcolor(WHITE); line(0,midy,2*midx,midy); line(midx,0,midx,2*midy); setcolor(RED); for(int i=0;i<edge;i++) <br=""> { if(edge-i!=1) line(a[j]+midx,midy-a[j+1],a[j+2]+midx,midy-a[j+3]); else line(a[j]+midx,midy-a[j+1],a[0]+midx,midy-a[1]); j=j+2; } getch(); int st[max][2],top=-1; x=midx+x; y=midy-y; st[++top][0]=x; st[top][1]=y; putpixel(x,y,col); while(top!=-1) { x=st[top][0]; y=st[top][1]; top--; if((getpixel(x+1,y)!=clr)&&(getpixel(x+1,y)==7)) { putpixel(x+1,y,clr); st[++top][0]=x+1; st[top][1]=y; } if((getpixel(x-1,y)!=clr)&&(getpixel(x-1,y)==7)) { putpixel(x-1,y,clr); st[++top][0]=x-1; st[top][1]=y; } if((getpixel(x,y+1)!=clr)&&(getpixel(x,y+1)==7)) { putpixel(x,y+1,clr); st[++top][0]=x; st[top][1]=y+1; } if((getpixel(x,y-1)!=clr)&&(getpixel(x,y-1)==7)) { putpixel(x,y-1,clr); st[++top][0]=x; st[top][1]=y-1; } } } }; void menu() { clrscr(); cout<<"\n\t##### MAIN MENU #####"; cout<<"\n\t1.INPUT"; cout<<"\n\t2.FLOOD FILL"; cout<<"\n\t3.BOUNDARY FILL"; cout<<"\n\t4.EXIT"; cout<<"\n\tENTER UR CHOICE:"; } void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); int ch,x,y,clr; fill f; do { menu(); cin>>ch; clrscr(); switch(ch) { case 1: f.input(); break; case 2: cout<<"\n#### FLOOD FILL ####"; cout<<"\nENTER THE COLOR CODE TO FILL:"; cin>>clr; cout<<"\nENTER THE X-COORDINATE OF SEED POINT:"; cin>>x; cout<<"\nENTER THE Y-COORDINATE OF SEED POINT:"; cin>>y; f.f_fill(x,y,clr); getch(); break; case 3: cout<<"\n#### BOUNDARY FILL ####"; cout<<"\nENTER THE COLOR CODE TO FILL:"; cin>>clr; cout<<"\nENTER THE X-COORDINATE OF SEED POINT:"; cin>>x; cout<<"\nENTER THE Y-COORDINATE OF SEED POINT:"; cin>>y; f.b_fill(x,y,clr); getch(); break; case 4: break; } }while(ch!=4); }
ZH says
Hi. your code looks interesting. but i can’t compile it, too many errors.
can you provide any information about the compiling tools and required headers?
thank you very much.
ProjectsGeek says
@zh….I used turbo c compiler for developing this code. Try to compile this code using turbo c IDE.