# Bresenham's Circle Drawing Algorithm Using C Programming

## ðŸŽ¯ Introduction

Bresenham's Circle Drawing Algorithm is a simple and efficient method used to draw circles on a digital screen or a graphics window. It was developed by Jack E. Bresenham in 1962 and has since become one of the most popular algorithms for circle generation due to its speed and accuracy. The algorithm is especially useful when hardware-based circle-drawing operations are not available, making it an essential technique in computer graphics and related applications.

In this blog post, we will explore the concept of Bresenham's Circle Drawing Algorithm, understand its working principles, and analyze the provided C programming implementation. Additionally, we will enhance the code to make it compatible with modern systems while providing detailed explanations and insights throughout the process.

## ï»¿ðŸŽ¯ Concept

The primary idea behind Bresenham's Circle Drawing Algorithm is to determine the best positions to plot pixels along the circumference of a circle to minimize error. Instead of using the traditional method of calculating the coordinates based on the circle equation, Bresenham's algorithm employs incremental calculations to decide the next pixel position efficiently.

## ï»¿ðŸŽ¯ Bresenham's Circle Drawing Example Program

#include<stdio.h>Â

#include<conio.h>Â

#include<graphics.h>Â

void main() {

Â Â Â int gd = DETECT, gm;

Â Â Â int x, y, r;

Â Â Â void cir(int, int, int);

Â Â Â printf("Enter the Mid points and Radious:");

Â Â Â scanf("%d%d%d", &x, &y, &r);

Â Â Â initgraph(&gd, &gm, "");

Â Â Â cir(x, y, r);

Â Â Â getch();

Â Â Â closegraph();

}

void cir(int x1, int y1, int r) {

Â Â Â int x = 0, y = r, p = 1 - r;

Â Â Â void cliplot(int, int, int, int);

Â Â Â cliplot(x1, y1, x, y);

Â Â Â while (x < y) {

Â Â Â Â Â Â x++;

Â Â Â Â Â Â if (p < 0)

Â Â Â Â Â Â Â Â Â p += 2 * x + 1;

Â Â Â Â Â Â else {

Â Â Â Â Â Â Â Â Â y--;

Â Â Â Â Â Â Â Â Â p += 2 * (x - y) + 1;

Â Â Â Â Â Â }

Â Â Â Â Â Â cliplot(x1, y1, x, y);

Â Â Â }

}

void cliplot(int xctr, int yctr, int x, int y) {

Â Â Â putpixel(xctr + x, yctr + y, 1);

Â Â Â putpixel(xctr - x, yctr + y, 1);

Â Â Â putpixel(xctr + x, yctr - y, 1);

Â Â Â putpixel(xctr - x, yctr - y, 1);

Â Â Â putpixel(xctr + y, yctr + x, 1);

Â Â Â putpixel(xctr - y, yctr + x, 1);

Â Â Â putpixel(xctr + y, yctr - x, 1);

Â Â Â putpixel(xctr - y, yctr - x, 1);

Â Â Â getch();

}Â

## ï»¿ðŸŽ¯ Bresenham's Circle Example Detailed Explanation

void main():

In the original code, the main() function initializes the graphics mode and takes user input for the circle's center (x, y) coordinates and its radius (r).

The graphics mode is set using the initgraph() function, and the circle-drawing function cir() is called to draw the circle.

After drawing, the program waits for a keystroke with getch() and closes the graphics mode with closegraph().

void cir(int x1, int y1, int r):

This function is responsible for drawing the circle using Bresenham's algorithm.

It takes three arguments: x1 and y1 (the center coordinates) and r (the radius).

Inside the function, two variables x and y are initialized to 0 and r, respectively, representing the initial pixel position.

The variable p is used to calculate the decision parameter for determining the next pixel position to plot.

The function cliplplot() is called to plot the initial pixel and then iteratively calculates the next pixel positions until x becomes greater than or equal to y.

void cliplot(int xctr, int yctr, int x, int y):

This function is responsible for plotting pixels symmetrically along the circle's circumference based on the current position (x, y).

It takes four arguments: xctr and yctr (the center coordinates of the circle) and x and y (the current pixel positions to be plotted).

Using the putpixel() function, it plots eight symmetric points around the circle's center, resulting in a complete circle when all the pixels are drawn.

## ï»¿ðŸŽ¯ Summary

In this blog post, we have explored Bresenham's Circle Drawing Algorithm, a widely used method for efficiently drawing circles on a digital screen. We've seen how the algorithm differs from conventional circle-drawing techniques, utilizing incremental calculations to determine the best pixel positions along the circumference. The provided C programming implementation was enhanced by introducing modern graphics libraries and adding detailed explanations to help readers grasp the concept and application of the algorithm.