/*
8. feladat
Rajzoljunk ellipszist az ismert Da Silva algoritmussal
Az algoritmust ismert Dr. Kuba Attila Számítogépes grafika jegyzetébõl
*/

#include "stdafx.h"
void init() {
 glClearColor(1.0,1.0,1.0,1.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluOrtho2D(-400,400,-400,400);
}

void pont(double e,double f){
    glPointSize(2.0);
    glBegin(GL_POINTS);
    glVertex2d(e,f);
    glVertex2d(-e,f);
    glVertex2d(-e,-f);
    glVertex2d(e,-f);
    glEnd();
}

void ellipse(double a,double b){

 double x,y;
 double d1,d2;

 x=0.0;
 y=b;
 d1=b*b-a*a*b+a*a/4;
 pont(x,y);

 while ( (a*a*(y-1/2)) > (b*b*(x+1)))
 { if (d1<0) {
   d1=d1+b*b*(2*x+3);
   x+=1;}
   else {
   d1=d1+b*b*(2*x+3)+a*a*(-2*y+2);
   x+=1;
   y-=1;}
 pont(x,y);
 }

 d2=b*b*(x*x+1/4+x)+a*a*(y*y-2*y+1)-a*a*b*b;
 while (y>0){
  if (d2<0) {
    d2=d2+b*b*(2*x+2)+a*a*(-2*y+3);
    x+=1;
    y-=1;
    }
  else {
    d2=d2+a*a*(-2*y+3);
    y-=1;
    }
  pont(x,y);
 }
}

void display() {
 glClear(GL_COLOR_BUFFER_BIT);
 glColor3d(0.0,0.0,0.0);
 ellipse(150,360);
 glFlush();
}

void keyboard( unsigned char key, int x,int y) {
 switch (key) {
 case 27:
  exit(0);
  break;
 }
}
 
 

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(200,200);
 glutInitWindowPosition(100,100);
 glutCreateWindow("ellipszis");
 init();
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 glutMainLoop();

 return 0;
}