Skip to main content

Implementing Midpoint Circle Algorithm in C++

The Midpoint Circle Algorithm (MPC) is an efficient way to draw a circle using raster graphics. It is an improved version of Bresenham’s circle drawing algorithm and avoids using floating-point calculations, making it faster for computer graphics. This blog post explains a C++ program that implements the Midpoint Circle Algorithm using the graphics.h library.

// Midpoint Circle Algorithm Implementation
#include<graphics.h>  // Graphics library for drawing functions
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<iostream.h>

void main()
{
    int gdriver = DETECT, gmode, errorcode;
    initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); // Initialize graphics mode
    
    int xm = getmaxx() / 2; // Get center of the screen (X-coordinate)
    int ym = getmaxy() / 2; // Get center of the screen (Y-coordinate)
    
    cout << "\n Enter radius:";
    int r;
    cin >> r; // User input for circle radius
    
    int x = 0;
    int y = r;
    float d = 1.25 - r; // Decision parameter initialization
    
    while (x <= y)
    {
        // Plot the 8 symmetric points of the circle
        putpixel(xm + x, ym - y, 15); // Top-right
        putpixel(xm + y, ym - x, 15); // Top-right diagonal
        putpixel(xm + y, ym + x, 15); // Bottom-right diagonal
        putpixel(xm + x, ym + y, 15); // Bottom-right
        putpixel(xm - x, ym + y, 15); // Bottom-left
        putpixel(xm - y, ym + x, 15); // Bottom-left diagonal
        putpixel(xm - y, ym - x, 15); // Top-left diagonal
        putpixel(xm - x, ym - y, 15); // Top-left
        
        // Update decision parameter
        if (d < 0)
        {
            d += (2 * x) + 3; // Choose next pixel in the same row
        }
        else
        {
            d = d + (2 * (x - y)) + 5; // Move diagonally
            y = y - 1;
        }
        x = x + 1;
    }
    
    getch(); // Wait for user input before closing the graphics window
    closegraph(); // Close graphics mode
}

Overall Process

  1. Initialize the graphics mode: The program sets up the graphics driver and mode to enable graphical rendering.
  2. Determine the center of the screen: The midpoint of the screen is calculated to place the circle at the center.
  3. Take user input for radius: The user enters the desired radius for the circle.
  4. Apply the Midpoint Circle Algorithm: Using incremental calculations, the program plots pixels for one-eighth of the circle and mirrors them to complete the full circle.
  5. Update the decision parameter: The algorithm decides whether the next pixel should move horizontally or diagonally to ensure a smooth circular shape.
  6. Display the final circle: Once all pixels are plotted, the circle appears on the screen.
  7. Wait for user input and close the graphics mode: The program waits for user interaction before closing the graphical window.

Step-by-step Explanation

  1. Initializing Graphics Mode:
    • The initgraph() function initializes the graphics mode.
    • The DETECT macro automatically detects the graphics driver.
  2. Setting Up the Circle’s Center:
    • getmaxx() and getmaxy() are used to get the maximum screen width and height.
    • The center of the screen is determined by dividing these values by 2.
  3. Taking User Input:
    • The program asks the user to input the radius of the circle.
  4. Midpoint Circle Algorithm Logic:
    • The initial values for x and y are set (x = 0, y = r).
    • The decision parameter d is initialized.
    • The loop continues until x is greater than y.
    • putpixel() is used to plot the eight symmetrical points of the circle.
    • The decision parameter is updated:
      • If d < 0, the next point is chosen within the same row.
      • Otherwise, the next point moves diagonally.
    • The values of x and y are updated accordingly.
  5. Displaying the Circle:
    • The getch() function waits for a key press before closing the graphics mode.
    • closegraph() closes the graphics mode.

Output

Output
Output

The above screenshot shows sample output for radius = 150.


The Midpoint Circle Algorithm is an efficient way to draw a circle with minimal calculations. This C++ program successfully implements the algorithm using the graphics.h library. Understanding this algorithm is fundamental in computer graphics, especially for raster-based drawing operations. If you wish to experiment, try modifying the radius value or changing the color of the circle by modifying the color code in putpixel().

No Comments yet!

Leave a Reply

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