# OpenGL Help with drawing an ellipse in opengl!

This topic is 5419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My professor gave us an assignment which involved using an ellipse generating algorithm to write opengl code that will draw an ellipse based on that algorithm. I've done part of the code but I'm lost as to how to do one of my functions. He doesn't want us to let opengl do the drawing for us, but rather have us draw it bit by bit. I don't know what to put in the ellipseMidpoint function. The code I have there now is from code I used to draw a circle. If anyone can help it would be great. Here's what I have so far: #include <windows.h> #include <GL/glut.h> #include <math.h> void setPixel (int x, int y, int color) { float R, G, B; int rColor; rColor = color % 255; R = (float) rColor/255; G = 0.25; B = 1.0 - R; glColor3f(R, G, B); glBegin(GL_POINTS); glEnd(); } void ellipsePlotPoints (int xc, int yc, int x, int y, int color) { setPixel(xc + x, yc + y, color); setPixel(xc + x, yc - y, color); setPixel(xc - x, yc + y, color); setPixel(xc - x, yc - y, color); } void ellipseMidpoint (int a, int b, int color) { int x = 0; int y = b; float p = 1 - b; ellipsePlotPoints(xc, yc, x, y, color); while (x < y) { x++; if (p < 0) p += 2 * x + 0.5; else { y--; p += 2 * (x - y) + 0.5; } ellipsePlotPoints(xc, yc, x, y, color); } } void init() { glClearColor(1.0, 1.0, 1.0, 0.0); glColor3f(0.0f, 0.0f, 0.0f); glPointSize(1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0); } //myDisplay void display() { glClear(GL_COLOR_BUFFER_BIT); //clear the screen ellipseMidpoint(300, 300, 50, 25); //draw ellipse glFlush(); } //Main void main (int argc, char** argv) { glutInit(&argc, argv); //initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set display mode glutInitWindowSize(640, 480); //set window size glutInitWindowPosition(0, 0); //set window position on screen glutCreateWindow("Demonstration of the Ellipse Algorithm"); //open the screen glutDisplayFunc(display); //register redraw function init(); glutMainLoop(); //go into a perpetual loop } I appreciate all of the help that I can get. Kinda lost.

##### Share on other sites
On a side note, a really simple way to draw an ellipse is just to generate points for a sphere, and use a nonlinear transform to deform it. More simply, to squish along y, multiply each y point by 0.8 or something. In opengl, its really simple, just use the glScalef (1.0f, 0.8f, 1.0f).

However, I'm not familiar enough with ellipse drawing to really help to fix the algorithm tho.

• 21
• 16
• 9
• 17
• 13