DDA circle drawing algorithm
ALGORITHM
1 . read the radius r ,of the circle and
calculate value of E
2n-1<=r<2n
E=2-n
2
. start_x=0
start_y=r
3 x1=start_x
y1=start_y
4 do
{
x2=x1+Ey1
y2=y1-Ex2
[x2 represents xn+1 and x1
presents xn]
plot (int(x2),int(y2))
x1=x2;
y1=y2;
[reinitialize the
current point]
}while (y1-start_y)<E or (start_x-x1)>E
[check if the
current point is the starting point or not .if current point is not
starting point repeat step 4; otherwise stop]
5. Stop.
C Program
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void circl(int xc,int yc,int rad)
{
int x=0,y=rad,p=1-rad;
void plot(int,int,int,int);
plot(xc,yc,x,y);
while(x<y)
{
x++;
if(p< 0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
plot(xc,yc,x,y);
}
}
void plot(int xc,int yc,int x,int y)
{
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 main()
{
int xc,yc,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"T:\\BGI");
printf("Enter the center coordinates and radius\n");
scanf("%d%d%d",&xc,&yc,&r);
setbkcolor(BLACK);
setcolor(WHITE);
circl(xc,yc,r);
getch();
closegraph();
}
Comments
Post a Comment