Code Snippets
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Home :: Programming :: Code Snippets :: OpenGL :: Shapes II: Drawing An Ellipse

[ Shapes II: Drawing An Ellipse ]

In the second part of the Shapes snippets, I will show you how to draw an ellipse. The good thing is that it is indeed very easy and not more difficult than drawing a circle which was shown in the previous snippet. First thing, what is an ellipse? It's a curve or simply a flattened circle whose sides have different length's. For a very detailed and mathematical definition, take a look here.

One way to draw the ellipse would be to take the function of an ellipse and draw the points of the ellipse using this equation:

(x^2)/(a^2) = (y^2)/(b^2)

a and b are the length's of the both sides. You would simply extract e.g. the y coord and and iterate from the left side of the ellipse on the x-axis to the right side and draw the corresponding y-coordinate You would have to repeat the same for the lower area of the ellipse making y negative.

But this method is terribly uneffective and rather complicated. The only good thing is that you don't have to use trigonometric functions. The best and fastest method is simply to draw the ellipse like the circle in the last snippet but using different scalings for both x and y coordinates. The x-coordinate is cos § times the x-radius and the y-coordinate is cos § times the y-radius. Take a look at the code:

#include <math.h> //for sin and cos
const float DEG2RAD = 3.14159/180;
void drawEllipse(float xradius, float yradius)
   for (int i=0; i < 360; i++)
      //convert degrees into radians
      float degInRad = i*DEG2RAD;

The code looks very similar to the code to draw a circle. In fact it's a circle but with two different radius'. As said above it's nothing more than a circle with two different scalings. The code is very easy and fast and the result will be a perfectly looking ellipse centered at (0,0,0). Example how to use it:

drawEllipse(3.5f,2.0f); //draw an ellipse, size: 7 (3.5 radius) x 4 (2 r)

If you have further questions, simply visit the message board. Happy Coding!

 Last edited 2003-07-26 14:36:56 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0