Circle Drawing Algorithm in Computer Graphics

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define SQUARE(x) ((x)*(x))

void drawcircle(int, int, int);

void main()
{
  int gd=DETECT,gm;
  int xc,yc,r;
  initgraph(&gd,&gm,"C:\\TC\\BGI");
  xc=getmaxx()/2;
  yc=getmaxy()/2;
  r=100;
  drawcircle(xc,yc,r);
  getch();
  closegraph();
}

void drawcircle(int xc,int yc,int r)
{
  int i,x,y,y1;
  for(i=xc-r;i<=xc+r;i++)
  {
    x=i;
    y=yc+sqrt(SQUARE(r)-SQUARE(x-xc));
    y1=yc-sqrt(SQUARE(r)-SQUARE(x-xc));
    putpixel(x,y,1);
    putpixel(x,y1,1);
    delay(100);
  }
}

//Drawing circle using polar equations
void drawcircle(int xc,int yc,int r)
{
  int x,y;
  float theta;
  const float PI=3.14;
  for(theta=0.0;theta<=360;theta+=1)
  {
    x=xc+r*cos(theta*PI/180.0);
    y=yc+r*sin(theta*PI/180.0);
    putpixel(x,y,1);
  }
}

OUTPUT:


No comments:

Post a Comment