Computer Graphics
C Program for DDA circle drawing algorithm
#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();
}
#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();
}