Bresenham's Circle Algorithm

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Computer Graphics & Multimedia

Bresenhams Circle Drawing algorithm with horizontal, vertical and


diagonal move direction.
/*Program For Circle Generation Using Bresenham's Algorithm */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
int xi,yi,di;
void mh()
/*To Move in Horizontal direction */
{
xi=xi+1;
di=di+(2*xi)+1;
}
void md()
/*To Move in Diagonal direction */
{
xi=xi+1;
yi=yi-1;
di=di+(2*xi)-(2*yi)+2;
}
void mv()
/*To Move in Vertical direction */
{
yi=yi-1;
di=di-(2*yi)+1;
}
void main()
{
int gd,gm,dx,dy,rad,limit,d,d1,xc,yc;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("\nEnter the Center Co-ordinates of circle Xc & Yc->");
scanf("%d %d",&xc,&yc);
printf("\nEnter Radius Of Circle->");
scanf("%d",&rad);

xi=0;
yi=rad;
di=2*(1-rad);
limit=0;
while(yi>=limit)
{
/*Putpixel function to generate the circle in all quadrents */
putpixel(xc+xi,yc+yi,RED);
putpixel(xc+xi,yc-yi,RED);
putpixel(xc-xi,yc+yi,RED);
putpixel(xc-xi,yc-yi,RED);
delay(50);
if(di<0)
{
d=(2*di)+(2*yi)-1;
if(d<0)
mh();
else
md();
}
else if(di>0)
{
d1=(2*di)-(2*xi)-1;
if(d1<=0)
md();
else
mv();
}
else if(di==0)
md();

}
circle(xc,yc,rad);
getch();
closegraph();

Output:

You might also like