Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Ellipse drawing algorithm

2.18/5 (11 votes)
25 Oct 20072 min read 1   724  
A basic algorithm for drawing ellipses on a window
Screenshot - drwelpsalg.jpg

Introduction

First of all, I must mention that I'm an old English language beginner, and this article may contains many mistakes!

I'm now working on a graphic library that is totally independent of the system. I wrote many routines that draw lines, points, rectangles, etc. All the routines now are basic so I can later optimize them for extra performance and speed.

I was looking today morning for an algorithm that draws ellipses. I visited www.codeproject.com and I searched for an article explaining how to draw ellipses without using the Windows GDI. I didn't find anything! Either there's really nothing about this subject or I missed up something during the search process. I said to myself, why not, write an article about the subject and maybe some one read it and suggest me an other way to make things better!


Using the code

The routine I wrote last week draws an ellipse of N segments on the screen. I'll show here the basic one. MyEllipseDraw calculate the coordinate of N points and link them with lines that is in the end an ellipse.

MyDrawEllipse has two parameters which are the handle to the window where the ellipse will be drown and the device context used to draw on the window using GDI+.

It starts by preparing objects and data needed by the draw process. nSeg is a global variable that specifies the number of segments of the ellipse to be drawn.

C++
//A basic algorithm to draw an N segments ellipse on a window
// hWnd +> Handle to the window where the ellipse will be drawn
// hDC +> Device context to the window where the ellipse will be drawn
// nSeg +> A global variable contains the number of segments or points linked each other to form the ellipse
void MyDrawEllipse(HWND hWnd, HDC hDC)
{
    double x, y, _x, _y, __x, __y, dx, dy, z, wx, hy;
    RECT rc;

    //Prepare objects and data
    Graphics g(hDC);
    Pen p(Color(0xff, 0, 0), 1.0); //Red
    GetClientRect(hWnd, &rc);
    z = 0.99; //Point coordinate affinity
    dx = double(rc.right) / 2.0 - 1.0; //Half window height
    dy = double(rc.bottom) / 2.0 - 1.0; //Half window width
    wx = double(rc.right) / 2.0; //Ellipse center
    hy = double(rc.bottom) / 2.0; //Ellipse center
    for(int i = 0; i < int(nSeg); i++) {
        x = wx * sin((double(i) / double(nSeg)) * (pi*2.0));
        y = hy * cos((double(i) / double(nSeg)) * (pi*2.0));
        if(i > 0) {
            //Draw a line connecting the last point to the one being calculated now
            g.DrawLine(&p, int(dx+_x+z), int(dy+-_y+z), int(dx+x+z), int(dy+-y+z));
        } else {
            //Save the first point coordinate to link it with the last one
            __x = x;
            __y = y;
        }
        //Save the actual point coordinate to link it with the next one
        _x = x;
        _y = y;
    }
    //Draw a line between the last point and the first one
    g.DrawLine(&p, int(dx+x+z), int(dy+-y+z), int(dx+__x+z), int(dy+-__y+z));

    /*
    //I use the GDI+ DrawEllipse function to compare my algorithm with
    //the one used by GDI+. Only the visual result is important for me now!
    p.SetColor(Color(0, 0xff, 0));
    g.DrawEllipse(&p, 0, 0, rc.right-1, rc.bottom-1);
    */
}

After downloading the source files extract the files to a directory, look for drwelpsalg.cpp file where the routine MyDrawEllipse is written. The file contains the standard code generated by the Win32 project AppWizard. I deleted all the comments so only the code I added will be, that way you can read the source file easily.

When the demo program is executed, it draws an ellipse adjusted to fill all the client area of the main window. The title bar show informations about the client area and the number of segments of the drawn ellipse. You can click with left button to increase the number of segments or click with the right button to decrease it. Try values smaller than 16 segments.

In the end, I must say sorry for all mistakes you'll find in this 'article' since it's my first using English language!

In the end, I hope that you'll find something useful and interesting while reading those lines!

In the end, thank you for reading till this word :)

History

None for the moment.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here