Recall from analytic geometry the PARAMETRIC equations for an ellipse:
- x=W*cos(θ)
- y=H*sin(θ)
- θ (theta) is the angle, measured in radians, between the x-axis and any point (x,y) on the ellipse.
Recall there are 2*π radians in a circle (2*π=6.2832).
θ is the PARAMETER.
To obtain many points around the ellipse we plug-in to the equations many values of θ between 0 and 2*π and calculate the (x,y) coordinates of successive points on the ellipse.
If we set θ to various values between 0 and 2*π we can use the parametric equations to calculate the (x,y) coordinates of points on the ellipse.
Our "display" function looks like this:
- const float PI=3.14159;
- const float W=5.;
- const float H=3.;
- float theta.; // angle between x axis and a point on ellipse
- float dt=2*PI/36.; // how much to change theta for each line segment.
- float x, y; // a point on ellipse.
- glBegin(GL_LINE_LOOP);
- for (theta=0.; theta<2.*PI; theta=theta+dt) { // loop thru 36 vertices
- x=W*cos(theta); // calc x,y coordinates of this vertex
- y=H*sin(theta);
- glVertex3f(x,y,0.); // tell OpenGL this vertex
- }
- glEnd(); // cause OpenGL to draw the LINE_LOOP with 36 vertices
By convention we like the PARAMETER to vary between 0. and 1. (instead of 0. and 2*π).
We can easily "scale" the parameter so as to arrange this. In the above example we make the following changes:
Change parametric equations to:
- x=W*cos(θ*2π)
- y=H*sin(θ*2π)
Change the code loop to:
- float dt=1./36.;
- for (theta=0.; theta<1.; theta=theta+dt) {
- x=W*cos(2*PI*theta);
- y=H*sin(2*PI*theta);
- glVertex3f(x,y,0.); // tell OpenGL this vertex
- }
- glEnd();