Mid point ellipse


/*
* main.c
*
* Created on: Apr 29, 2014
* Author: akshaythakare
*/

#include<openGL/gl.h>
#include<openGL/glu.h>
#include<GLUT/glut.h>
#include<math.h>

float xc,yc,x_radius,y_radius;

void plot(float x, float y)
{
glBegin(GL_POINTS);
glVertex2f(xc+x,yc+y);
glVertex2f(xc+x,yc-y);
glVertex2f(xc-x,yc+y);
glVertex2f(xc-x,yc-y);
glEnd();
}

void ellipse()
{
xc=100;yc=100;x_radius=60;y_radius=40;

float x,y;
float dx,dy,d1,d2;
x = 0;
y = y_radius;

// d1 = pow(y_radius,2) - pow(x_radius,2)*y_radius + (0.25)*pow(x_radius,2);
//d2 = pow(y_radius,2)*pow(x+0.5,2) + pow(x_radius,2)*pow(y-1,2) - pow(x_radius,2)*pow(y_radius,2);
d2=0;d1=0;
dy = 2*pow(x_radius,2)*y;
dx = 2*pow(y_radius,2)*x;

do
{
plot(x,y);
if(d1<0)
{
x++;
dx = dx + 2*pow(y_radius,2);
d1 = d1 + dx + pow(y_radius,2);
}
else
{
x++;
y--;
dy = dy - 2*pow(x_radius,2);
dx = dx + 2*pow(y_radius,2);
d1 = d1 + dx - dy + pow(y_radius,2);
}

}while(dx<dy);

do
{
plot(x,y);
if(d2>=0)
{
y--;
dy = dy - 2*pow(x_radius,2);
d2 = d2 - dy + pow(x_radius,2);
}
else
{
x++;
y--;
dy = dy - 2*pow(x_radius,2);
dx = dx + 2*pow(y_radius,2);
d2 = d2 + dx - dy + pow(x_radius,2);
}
}while(y>=0);

}

void disp()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);

ellipse();

glFlush();
}

int main(int argc, char ** argv)
{
glutInit(&argc,argv);
glutInitWindowSize(300,300);
glutCreateWindow("Ellipse");
gluOrtho2D(0,300,0,300);
glutDisplayFunc(disp);
glutMainLoop();
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s