Friday, August 14, 2015

Program to Draw circle in Computer Graphics

#include<stdio.h>

#include<graphics.h>

void draw_circle(int,int,int);
void symmetry(int,int,int,int);

main()
{
 int xc,yc,R;
 int driver,mode;


 printf("Enter the center of the circle:\n");
 printf("Xc =");
 scanf("%d",&xc);
 printf("Yc =");
 scanf("%d",&yc);
 printf("Enter the radius of the circle :");
 scanf("%d",&R);


 driver = DETECT;
 initgraph(&driver,&mode,"\\tc\\bgi"); //the path may be different in your case.
 draw_circle(xc,yc,R);

 closegraph();
}

void draw_circle(int xc,int yc,int rad)
{
 int x = 0;
 int y = rad;
 int p = 1-rad;
 symmetry(x,y,xc,yc);


 for(x= 0;y>x;x++)
    {
    if(p<0)
       p += 2*x + 3;
    else
      {
       p += 2*(x-y) + 5;
       y--;
      }
    symmetry(x,y,xc,yc);
delay(5000);
     }
}

void symmetry(int x,int y,int xc,int yc)
{
 putpixel(xc+x,yc-y,WHITE);//For pixel (x,y)
 putpixel(xc+y,yc-x,WHITE);//For pixel (y,x)
 putpixel(xc+y,yc+x,WHITE);//For pixel (y,-x)
 putpixel(xc+x,yc+y,WHITE);//For pixel (x,-y)
 putpixel(xc-x,yc+y,WHITE);//For pixel (-x,-y)
 putpixel(xc-y,yc+x,WHITE);//For pixel (-y,-x)
 putpixel(xc-y,yc-x,WHITE);//For pixel (-y,x)
 putpixel(xc-x,yc-y,WHITE);//For pixel (-x,y)
}

No comments: