Monday, 29 December 2014

Program to Draw a Circle using Bresenham's Algorithm

Program to Draw a Circle using Bresenham's Algorithm

#include <stdio.h>
#include <conio.h>
#include <graphics.h>

void circleBres(int xc, int yc, int r);
void drawCircle(int xc, int yc, int x, int y);
void main()
{

int xc, yc, r;
        int gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm,"c:\\TurboC3\\BGI");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleBres(xc, yc, r);
getch();

}

void circleBres(int xc, int yc, int r)
{

int x = 0, y = r;
int d = 3 - 2 * r;
while (x < y)
{
drawCircle(xc, yc, x, y);
if (d >= 0)
      {
x++;
y--;
d=d+4*(x-y)+10;
}
else
{
x++;
d = d + 4 * x + 6;
}

drawCircle(xc, yc, x, y);
delay(50);
}
}

void drawCircle(int xc, int yc, int x, int y)
{

putpixel(xc+x, yc+y, RED);

putpixel(xc-x, yc+y, RED);

putpixel(xc+x, yc-y, RED);

putpixel(xc-x, yc-y, RED);

putpixel(xc+y, yc+x, RED);

putpixel(xc-y, yc+x, RED);

putpixel(xc+y, yc-x, RED);

putpixel(xc-y, yc-x, RED);
}

OUTPUT:



No comments:

Post a Comment