Wednesday, June 27, 2012

59. DDA CIRCLE DRAWING IN C


//DDA CIRCLE DRAWING
#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
float x1,y1,x2,y2,startx,starty,epsilon;
int gd,gm,i,val,cx,cy;
int r;
clrscr();
clrscr();
printf("Enter the center coordinates of a circle :"); 
scanf("%d%d",&cx,&cy); 
clrscr();


printf("Enter the radius of a circle :");
scanf("%d",&r);

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
x1=r*cos(0);
y1=r*sin(0);
startx = x1;
starty = y1;
i=0;
do
{
val = pow(2,i);
i++;
}while(val<r);
epsilon = 1/pow(2,i-1);
do
   {
    x2= x1 + y1*epsilon;
    y2 = y1 - epsilon*x2;
    putpixel(200+x2,200+y2,15);
    x1=x2;
    y1=y2;
    delay(100);   
    }
  while( (y1 - starty ) < epsilon || (startx - x1) > epsilon);
getch();
closegraph();
}


SAMPLE OUTPUT: