Write a program for Bresenham Circle Draw Algorithm in C

/*
    Bresenham Circle Draw Algorithm
*/
// C-program for circle drawing
// using Bresenham’s Algorithm
// in computer-graphics
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include<conio.h>
// Function to put pixels
// at subsequence points
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);
}

// Function for circle-generation
// using Bresenham's algorithm
void circleBres(int xc, int yc, int r)
{
    int x = 0, y = r;
    int d = 3 - 2 * r;

    while (y >= x)
    {
// for each pixel we will
// draw all eight pixels
drawCircle(xc, yc, x, y);
x++;

// check for decision parameter
// and correspondingly
// update d, x, y
if (d > 0)
{
    y--;
    d = d + 4 * (x - y) + 10;
}
else
    d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(50);
    }
}


// driver function
int main()
{
    int xc, yc, r;
    int gd = DETECT, gm;

    clrscr();
    printf("\n\n Enter x,y coordinates and radius of circle ");
    scanf("%d%d%d",&xc,&yc,&r);
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");  // initialize graph

    circleBres(xc, yc, r);    // function call

    getch();
    closegraph();
    return 0;
}

Previous
Next Post »