Mid-Point Ellipse Drawing Algorithm

Mid-Point Ellipse Drawing Algorithm algorithm uses symmetry of ellipse and mid point algorithm is implemented for one quadrant only.We divide the quadrant into two regions and the boundary of two regions is the point at which the curve has a slope of -1. We process by taking unit steps in the x direction to the point P(where curve has a slope of -1), then taking unit steps in the y direction and applying midpoint algorithm at every step.

/*Header file for built-in graphics functions such as plot() and initgraph()*/ 

/* Function to draw ellipse in the 4 quadrants */

void drawEllipse(float xc, float yc, float x, float y)
/*This function plots a pixel at coordinates(x,y) specified by first 2 arguments and third argument specifies the color of the pixel*/
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);

/* Function to draw ellipse using the mid point algorithm */

void ellipseMidpoint(float xc, float yc, float rx, float ry)
    float rxSq = rx * rx;
    float rySq = ry * ry;
    float x = 0, y = ry, p;
    float px = 0, py = 2 * rxSq * y;

    drawEllipse(xc, yc, x, y);

    //Region 1
    p = rySq - (rxSq * ry) + (0.25 * rxSq);
    while (px < py)
        px = px + 2 * rySq;
        if (p < 0)
            p = p + rySq + px;
            py = py - 2 * rxSq;
            p = p + rySq + px - py;
        drawEllipse(xc, yc, x, y);

    //Region 2
    p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;
    while (y > 0)
        py = py - 2 * rxSq;
        if (p > 0)
            p = p + rxSq - py;
            px = px + 2 * rySq;
            p = p + rxSq - py + px;
        drawEllipse(xc, yc, x, y);

int main()
    int xc, yc, rx, ry;
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "c:\\tc\\bgi");/*loads the graphic driver and puts syetem into graphics mode*/

    syntax for initgraph() is:
    void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
    *graphdriver is an integer that specifies the graphics driver to be used.DETECT requests auto-detect for the driver.
    *graphmode is an integer that specifies the initial graphics mode.If *graphdriver equals DETECT,*graphmode is set by initgraph to the highest resolution available for the detected driver.

    printf("\t\t\tMID POINT ELLIPSE DRAWING ALGORITHM\n\n\tEnter the center coordinates of ellipse:");
    scanf("%d %d",&xc,&yc);
    printf("\n\tEnter x-radius coordinate:");
    printf("\n\tEnter y-radius coordinate:");
    ellipseMidpoint(xc, yc, rx, ry);
    /*closegraph function closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the original mode*/
    return 0;

Mid Point Algorithm

You can run this code in Turbo C++. Dev C++ and Microsoft visual studio do not support graphics libraries.
You can download Turbo C++ using this link Turbo C++

Other articles related to Computer Graphics available on TutsHeap are:
Cohen Sutherland Line Clipping Algorithm
Boundary-Fill Algorithm
Flood-Fill Algorithm
Mid-Point Circle Drawing Algorithm
DDA Line Drawing Algorithm
Bresenham’s Line Drawing Algorithm

This content has been helpful to you?

Thanks for contributing!

Yes No