Implementing Bresenham’s Circle Drawing Algorithm in C++

Bresenham’s circle algorithm is an efficient way to draw a circle using only integer calculations. It eliminates the need for floating-point arithmetic and is widely used in computer graphics.

Below is the implementation of Bresenham’s circle drawing algorithm using C++ and the graphics.h library. This program takes user input for the circle’s center and radius, then uses the algorithm to plot the circle.

#include<iostream.h>  // Header file for input-output operations
#include<conio.h>      // Header file for console input/output functions
#include<stdlib.h>     // Standard library for utility functions
#include<graphics.h>   // Header file for graphics functions
#include<dos.h>        // Header file for delay functions

// Function prototype for Bresenham's circle algorithm
void circ_bre(int x,int y,int rad);

// Function prototype for displaying pixels of the circle
void display(int,int,int,int);

void main()
{
    int gd = DETECT, gm, x, y, r;
    initgraph(&gd, &gm, "c:\\tc\\bgi");  // Initialize graphics mode
    cleardevice();  // Clear the graphics screen

    cout << "Bresenham's Circle Generation Algorithm ";
    cout << "\nEnter the center coordinates for the circle: ";
    cin >> x >> y;
    cout << "\nEnter the radius of the circle: ";
    cin >> r;
    
    circ_bre(x, y, r);  // Call function to draw the circle
    getch();  // Wait for user input before closing the graphics window
    closegraph();  // Close graphics mode
}

// Function implementing Bresenham's circle algorithm
void circ_bre(int x, int y, int rad)
{
    float dp;  // Decision parameter
    int x1, y1;
    x1 = 0;  // Initializing x1 and y1 for circle drawing
    y1 = rad;
    dp = 3 - 2 * rad;  // Initial decision parameter

    while (x1 <= y1)
    {
        if (dp <= 0)
            dp += (4 * x1) + 6;  // Update decision parameter if inside the circle
        else
        {
            dp += 4 * (x1 - y1) + 10;  // Update decision parameter if outside the circle
            y1--;  // Move towards the center
        }
        x1++;
        display(x1, y1, x, y);  // Plot the symmetric points
    }
}

// Function to plot symmetric pixels of the circle
void display(int x1, int y1, int x, int y)
{
    putpixel(x1 + x, y1 + y, WHITE);  // Plot points in all 8 octants
    putpixel(x1 + x, y - y1, WHITE);
    putpixel(x - x1, y1 + y, WHITE);
    putpixel(x - x1, y - y1, WHITE);
    putpixel(x + y1, y + x1, WHITE);
    putpixel(x + y1, y - x1, WHITE);
    putpixel(x - y1, y + x1, WHITE);
    putpixel(x - y1, y - x1, WHITE);
}

Explanation

  1. Initializing Graphics Mode:
    • initgraph(&gd, &gm, "c:\\tc\\bgi"); initializes the graphics mode using the BGI (Borland Graphics Interface) path.
    • cleardevice(); clears the screen before drawing.
  2. User Input:
    • The program takes user input for the center coordinates (x, y) and the radius r of the circle.
  3. Bresenham’s Algorithm Logic:
    • Uses a decision parameter dp to determine the next pixel position.
    • Adjusts dp based on whether the pixel is inside or outside the ideal circle boundary.
    • Exploits the symmetry of a circle by plotting 8 symmetric points for each computed (x1, y1) pair.
  4. Displaying Pixels:
    • The display() function uses putpixel() to plot the circle’s points in all 8 octants, ensuring a smooth and complete circle.

Output

Output of Bresenham's Circle Drawing algortihm
Output of Bresenham’s Circle Drawing algortihm

Bresenham’s circle algorithm is a fundamental technique in computer graphics, especially for low-level graphics rendering. Understanding this algorithm helps in grasping rasterization techniques used in modern rendering engines.

If you’re working with Turbo C++ or any other old graphics library, ensure that the graphics drivers are set up correctly before running the program.

Happy Coding! 🚀

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.