Ellipse using OpenGl

#include<gl/glut.h>
#include<gl/glu.h>
#include<gl/gl.h>
#include<iostream>

using namespace std;
int xc=0,yc=0,rx=40,ry=60;

void display()
{
float rxsq=rx*rx;
float rysq=ry*ry;

float x=0,y=ry,p;
float px=0,py=2*rxsq*y;

glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1);
glBegin(GL_POINTS);
glVertex2i(xc+x,yc+y);
glVertex2i(-(xc+x),-(yc+y));
cout<<xc+x<<" "<<yc+y<<endl;
p=rysq-(rxsq-ry)+(0.25*rxsq);
while(px<py)
{
x++;
px=px+2*rysq;
if(p<0)
p=p+rysq+px;
else
{
y--;
py=py-2*rxsq;
p=p+rysq+px-py;
}
glVertex2i(xc+x,yc+y);
glVertex2i(xc+x,-(yc+y));
glVertex2i(-(xc+x),yc+y);
glVertex2i(-(xc+x),-(yc+y));

cout<<xc+x<<" "<<yc+y<<endl;
}

p=rysq*(x+0.5)*(x+0.5) + rxsq*(y-1)*(y-1) -rxsq*rysq;
while(y>0)
{
y--;
py=py-2*rxsq;
if(p>0)
{
p=p+rxsq-py;
}
else
{
x++;
px=px+2*rysq;
p=p+rxsq-py+px;
}
glVertex2i(xc+x,yc+y);

glVertex2i(xc+x,-(yc+y));
glVertex2i(-(xc+x),yc+y);
glVertex2i(-(xc+x),-(yc+y));

cout<<xc+x<<" "<<yc+y<<endl;
}
glEnd();
glFlush();
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Mid Point Ellipse Algorithm ");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

This program is to draw ellipse using points. You have to enter horizontal and vertical radius to draw ellipse. 

Popular posts from this blog

Shutdown Pc

Program to plot Quadrant strip