Skip to main content

Bresenham's Circle Drawing Algorithm Using C Programming

2 min read
Share:
On this page (5sections)

Bresenham’s Circle Drawing Algorithm Using C Programming

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);
}

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 initgraph(), 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 draws the circle using Bresenham’s algorithm. It takes the center coordinates and radius, initializes x = 0, y = r, and decision parameter p = 1 - r, then iteratively plots pixels via cliplot() until x >= y.

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

This function plots eight symmetric points around the circle center using putpixel(), producing a complete circle outline.

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

  • Bresenham’s Circle Drawing Algorithm is an efficient method to draw circles on digital screens.

  • It utilizes incremental calculations to determine pixel positions for circle generation.

  • The algorithm is widely used in computer graphics when hardware-based circle-drawing operations are not available.

  • We enhanced the original C program by using standard libraries and adding detailed explanations for better comprehension.

Related Tutorials

Search tutorials