Ellipses Using OpenGL Ellipse • An ellipse is basically an oval. • The technical definition for an ellipse is a closed plane curve generated by a point moving in a way that the sums of its distances from two fixed points (called the foci) is a constant. Ellipse Graphics -Vertexes (V1 & V2 on the diagram) of the ellipse are defined as the intersections of the ellipse and a line passing through foci (F1 & F2 on the diagram). The distance between the vertexes are called major axis or focal axis. A line passing the center and perpendicular to the major axis is the minor axis. Standard equation of an ellipse with center at (0, 0) • (x^2 / a^2) + (y^2 / b^2) = 1 • Where a > b > 0 • This equation is known as the Cartesian Equation. The drawing looks as follows. • The ellipse is horizontal Equation of an ellipse cont. • • • • (x^2 / b^2) + (y^2 / a^2) = 1 Where a > b > 0 The drawing looks as follows The ellipse is vertical Equation of an ellipse with center not at the origin • Ellipses don’t have to always have it’s center at the origin. • The equation of an ellipse with center not at the origin is … where the center of the ellipse is at point (X0, Y0) Equations of an ellipse cont. • Now that we have considered the standard equation of an ellipse now we can look at the general equation. • With this equation however, comes a few properties to consider … Properties of the general equation • A is not equal to C If A=C, then the graph will turn into a circle • A is not equal to 0 If A=0, then the graph will turn into a parabola that opens horizontally (left of right). • C is not equal to 0 If C=0, then the graph will turn into a parabola that opens vertically (up or down). Area of an ellipse • The area of an ellipse with major axis radius, a, and minor axis radius, b, is given by … • The Area of an arbitrary ellipse given by the Quadratic Equation ( ) is Drawing an ellipse in OpenGL • To draw an ellipse in OpenGL you need to first draw a circle and then scale the circle to look like an ellipse. You need to do this because there isn’t a built in command to draw an ellipse. • The procedure you need to do this is gluDisk( ). The gluDisk function draws a circle (disk) void gluDisk( GLUquadricObj *qobj, GLdouble innerRadius, GLdouble outerRadius, GLint slices, GLint loops ); qobj – is the quadratic object innerRadius – the inner radius of the disk ( it may be 0 ) outerRadius – the outer radius of the disk slices – the number of subdivisions around the z-axis loops – the number of concentric rings about the origin into which the disk is subdivided. gluNewQuadric • Along with the gluDisk( ) function, you can’t draw an ellipse without using the function gluNewQuadric( ). • There are no parameters for this function and it is used to create and return a pointer to a new quadric object. Code Segments for an ellipse program void Display () { glClear(GL_COLOR_BUFFER_BIT); GLUquadricObj * quadricObj; //Creates a quadratic object quadricObj = gluNewQuadric(); //Sets a pointer to a new quadratic object gluQuadricDrawStyle(quadricObj, GLU_FILL); glColor3f (0.0, 1.0, 0.0); //Sets ellipse color to green glScalef(0.7, 1.5, 1.0); //Scales the ellipse //glColor3f (1.0, 0.0, 0.0); //Red ellipse //glScalef(1.4, 0.7, 1.0); //glColor3f (0.0, 0.0, 1.0); //Blue ellipse //glScalef(0.4, 1.6, 1.0); gluDisk(quadricObj, 0.0, 3.0, 100, 100); glFlush(); } //Sets the ellipse properties Display ( ) function • Inside the Display function is where the most important commands to draw the ellipse occur. The essential lines of code that you need to have are … • GLUquadricObj * quadricObj; //Creates a quadratic object quadricObj = gluNewQuadric(); //Sets a pointer to a new quadratic object gluQuadricDrawStyle(quadricObj, GLU_FILL); //Sets the draw style gluDisk(quadricObj, 0.0, 3.0, 100, 100); //Sets ellipse properties Code Segments for an ellipse program cont. void init() { glClearColor(0.0, 0.0, 0.0, 0.0); //Sets background color to black glMatrixMode( GL_PROJECTION); glLoadIdentity(); glOrtho(-8.0, 8.0, -8.0, 8.0, -8.0, 8.0); } Code Segments for an ellipse program cont. void main( int argc, char **argv) { glutInit ( &argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize( 300, 300); glutInitWindowPosition( 200, 200); glutCreateWindow("Ellipses"); init(); glutDisplayFunc( Display ); glutMainLoop(); } Ellipses • The three functions (display, init, and main) are all you need in order to draw a simple ellipse. As long as you have the essential few lines of code to implement the figure. • Demonstrate the simple ellipse.