Implementing Midpoint Ellipse Algorithm in C++

Ellipses are fundamental shapes in computer graphics, widely used in various applications such as image processing, animation, and CAD systems. In this blog post, we’ll explore how to draw an ellipse using the Midpoint Ellipse Algorithm in C++ with graphics.h.

Introduction to Midpoint Ellipse Algorithm

The Midpoint Ellipse Algorithm is an efficient way to generate an ellipse using only integer operations. It is based on the midpoint method, which determines the next pixel position by evaluating a decision parameter.

This algorithm divides the ellipse into two regions:

  1. Region 1: The slope of the curve is less than 1.
  2. Region 2: The slope of the curve is greater than 1.

Each region is processed separately to ensure a smooth curve.

C++ Code Implementation

Below is the C++ program that implements the Midpoint Ellipse Algorithm. It uses the graphics.h library, which is part of the Turbo C++ environment.

#include<iostream.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

// Function to plot points based on ellipse symmetry
void display(int xs1, int ys1, int x, int y);

// Function to implement the Midpoint Ellipse Algorithm
void ellips1(int xs1, int ys1, int rx, int ry)
{
    int x, y;
    float d1, d2, dx, dy;
    x = 0;
    y = ry;
    
    // Initial decision parameter for region 1
    d1 = pow(ry, 2) - (pow(rx, 2) * ry) + (0.25 * pow(rx, 2));
    dx = 2 * pow(ry, 2) * x;
    dy = 2 * pow(rx, 2) * y;

    // Region 1 (Slope < 1)
    do
    {
        display(xs1, ys1, x, y); // Plot the calculated points
        if (d1 < 0) // Choose the next pixel based on decision parameter
        {
            x++;
            dx += 2 * pow(ry, 2);
            d1 += dx + pow(ry, 2);
        }
        else
        {
            x++;
            y--;
            dx += 2 * pow(ry, 2);
            dy -= 2 * pow(rx, 2);
            d1 += dx - dy + pow(ry, 2);
        }
    } while (dx < dy); // Continue until slope becomes greater than 1

    // Initial decision parameter for region 2
    d2 = pow(ry, 2) * pow((x + 0.5), 2) + pow(rx, 2) * pow((y - 1), 2) - pow(rx, 2) * pow(ry, 2);
    
    // Region 2 (Slope > 1)
    do
    {
        display(xs1, ys1, x, y); // Plot the calculated points
        if (d2 > 0) // Choose the next pixel based on decision parameter
        {
            y--;
            dy -= 2 * pow(rx, 2);
            d2 -= dy + pow(rx, 2);
        }
        else
        {
            x++;
            y--;
            dx += 2 * pow(ry, 2);
            dy -= 2 * pow(rx, 2);
            d2 += dx - dy + pow(rx, 2);
        }
    } while (y > 0); // Continue until reaching the x-axis
}

// Function to plot ellipse points using 4-way symmetry
void display(int xs, int ys, int x, int y)
{
    putpixel(xs + x, ys + y, WHITE); // First quadrant
    putpixel(xs - x, ys - y, WHITE); // Third quadrant
    putpixel(xs + x, ys - y, WHITE); // Fourth quadrant
    putpixel(xs - x, ys + y, WHITE); // Second quadrant
}

// Main function
int main(void)
{
    int xs1, ys1;
    float rx1, ry1;
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "c:\\tc\\bgi"); // Initialize graphics mode

    // User input for center and radii
    cout << "\t\tMidpoint Ellipse Drawing Algorithm\n";
    cout << "Enter the Center Coordinates\n";
    cout << "xc = \t";
    cin >> xs1;
    cout << "yc = \t";
    cin >> ys1;
    cout << "Enter the X Radius\t";
    cin >> rx1;
    cout << "Enter the Y Radius\t";
    cin >> ry1;

    // Call function to draw the ellipse
    ellips1(xs1, ys1, rx1, ry1);
    getch();
    closegraph(); // Close the graphics window
    return 0;
}

Explanation of the Code

  1. Header Files
    • graphics.h for graphical functions.
    • math.h for mathematical operations.
    • conio.h for console input/output operations.
    • dos.h (used in Turbo C++ for delays).
  2. Functions
    • ellips1(): Implements the Midpoint Ellipse Algorithm.
    • display(): Plots the points using the four-way symmetry of the ellipse.
    • main(): Initializes the graphics mode and takes user input for center coordinates and radii.
  3. Algorithm Breakdown
    • The first region (where the slope < 1) is processed using decision parameter d1.
    • The second region (where the slope > 1) is processed using decision parameter d2.
    • The ellipse is drawn using putpixel() at four symmetric points.

Output

When executed, this program will prompt the user for the center coordinates (xc, yc) and the radii (rx, ry). It will then draw the corresponding ellipse in a graphical window.

Output
Output

This implementation of the Midpoint Ellipse Algorithm provides a fundamental understanding of ellipse drawing in computer graphics. While graphics.h is outdated, the same logic can be applied in modern frameworks such as OpenGL or SDL for advanced graphics programming.

Do you have any questions or suggestions? Let me know in the comments!

One thought on “Implementing Midpoint Ellipse Algorithm in C++”

Leave a Reply

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