Mid-Point Algorithm for Circle Drawing in Computer Graphics

#include <graphics.h>
#include <stdio.h>
#include <conio.h>

void drawpoints(int,int,int,int);
void drawcircle(int,int,int);

void main()
{
  int gd = DETECT, gm;
  int xc,yc,r;
  initgraph(&gd, &gm, "C:\\TC\\BGI");
  printf("\n\nEnter the center co-ordinates:");
  scanf("%d%d",&xc,&yc);
  printf("Enter the radius");
  scanf("%d",&r);
  drawcircle(xc,yc,r);
  getch();
  closegraph();
}

void drawpoints(int x,int y, int xc,int yc)
{
  putpixel(xc+x,yc+y,1);
  putpixel(xc-x,yc+y,1);
  putpixel(xc+x,yc-y,1);
  putpixel(xc-x,yc-y,1);
  putpixel(xc+y,yc+x,1);
  putpixel(xc-y,yc+x,1);
  putpixel(xc+y,yc-x,1);
  putpixel(xc-y,yc-x,1);
}

void drawcircle(int xc,int yc,int r)
{
  int p,x,y;
  x=0;
  y=r;
  drawpoints(x,y,xc,yc);
  p=1-r;
  while(x<y)
  {
   if(p<0)
    {
      x=x+1;
      p=p+2*x+1;
    }
   else
    {
      x=x+1;
      y=y-1;
      p=p+2*(x-y)+1;
    }
   drawpoints(x,y,xc,yc);
  }
}


OUTPUT:


No comments:

Post a Comment