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.
void MyDrawEllipse(HWND hWnd, HDC hDC)
{
double x, y, _x, _y, __x, __y, dx, dy, z, wx, hy;
RECT rc;
Graphics g(hDC);
Pen p(Color(0xff, 0, 0), 1.0); GetClientRect(hWnd, &rc);
z = 0.99; dx = double(rc.right) / 2.0 - 1.0; dy = double(rc.bottom) / 2.0 - 1.0; wx = double(rc.right) / 2.0; hy = double(rc.bottom) / 2.0; 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) {
g.DrawLine(&p, int(dx+_x+z), int(dy+-_y+z), int(dx+x+z), int(dy+-y+z));
} else {
__x = x;
__y = y;
}
_x = x;
_y = y;
}
g.DrawLine(&p, int(dx+x+z), int(dy+-y+z), int(dx+__x+z), int(dy+-__y+z));
}
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.