Thursday, 16 May 2013

Write a program to draw a circle and fill a color using Boundary fill algorithm.

There are two method to make the circle with fill the color one is Boundary fill algorithm and the another one is the Flood Fill Algorithm. Both algorithm are very easy and the very helpful for the draw the circle in the c language.Generally this algorithm is used in the Computer Graphic subject. 
The other method for the draw the circle it is midpoint algorithm.

 #include<stdio.h>
 #include<conio.h>
 #include<graphics.h>
      #include<stdlib.h>
 void bfill4(int x,int y,int fcolor,int bcolor)
 {
          int current;
          current = getpixel(x,y);
          if(current != bcolor && current!= fcolor)
          {
               putpixel(x,y,fcolor);
               delay(10);
               bfill4(x,y+1,fcolor,bcolor);
               bfill4(x-1,y+1,fcolor,bcolor);
               bfill4(x,y-1,fcolor,bcolor);
               bfill4(x-1,y-1,fcolor,bcolor);
          }
 }
 void bfill8(int x,int y,int fcolor,int bcolor)
 {
          int current;
          current= getpixel(x,y);
          if(current != bcolor && current != fcolor)
          {
               putpixel(x,y,fcolor);
               delay(10);
               bfill8(x,y+1,fcolor,bcolor);
               bfill8(x,y-1,fcolor,bcolor);
               bfill8(x+1,y-1,fcolor,bcolor);
               bfill8(x+1,y+1,fcolor,bcolor);
               bfill8(x-1,y-1,fcolor,bcolor);
               bfill8(x-1,y+1,fcolor,bcolor);
               bfill8(x+1,y,fcolor,bcolor);
               bfill8(x-1,y,fcolor,bcolor);
          }
 }  

 void main()
 {
          int gd=DETECT,gm,n,ch,xc,yc,r,bcolor,fcolor;
          initgraph(&gd,&gm,"");
          s:
          printf(“Enter Xc=”);
          scanf(“%d”,&xc);
          printf(“Enter Yc”);
          scanf(“%d”,&yc);
          printf(“\n Enter Radious=”);
          scanf(“%d”,&r);
          printf(“\nEnter boundary color code=”);
          scanf(“%d”,&bcolor);
          printf(“\nEnter fill color code=”);
          scanf(“%d”,&fcolr);
          printf("\n 1. 4connected");
          printf("\n 2. 8connected");
          printf("\n 3. Exit");
          printf("\nEnter your choice:à");
          scanf("%d",&ch);
          switch(ch)
          {
               case 1:
                   circle(xc,yc,r);
                   bfill4(xc,yc,fcolor,bcolor);
                   break;
               case 2:
                   circle(xc,yc,r);
                   bfill8(xc,yc,fcolor,bcolor);
                   break;
               case 3:
                   exit(0);
               default:
                   printf("\n Invalid choise:");
                   goto s;
          }
          getch();
          closegraph();
 }


I hope this code is true and its very helpful to you. if any kind of the mistake in the code please contact me on my email Id  i am surly replay as soon as possible.


0 comments:

Post a Comment