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.

🎯 Key Points