{steinsoft.net}

What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
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 &lt;math.h&gt; //for sin and cos

{
glBegin(GL_LINE_LOOP);

for (int i=0; i &lt; 360; i++)
{
}

glEnd();
}
```

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:

```glColor3f(0.0f,1.0f,0.0f);
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