#include <iostream>
#include <cmath>
#include <GL/glut.h>
static int isAnimate = 0; 

using namespace std;
void animate(int);
void disk();


int delay=10, N=64;
float Dtheta = 2*3.1415926/N;
float centerx = 0.0, centery = 0.0, radius = 0.1;
float Dx = 0.02, Dy=0.01;
int win;
static int animationPeriod = 50; 
void keyInput(unsigned char key, int x, int y)
{
   switch(key) 
   {
      case 27:
         exit(0);
         break;
	  case ' ': 
         if (isAnimate) isAnimate = 0;
		 else 
		 {
	        isAnimate = 1; 
			animate(1);
		 }
		 break;
      default:
         break;
   }
}
void animate(int value)
{
   if (isAnimate) 
   {
  
      glutPostRedisplay();     
      glutTimerFunc(animationPeriod, animate, 1);
   }
}
void specialKeyInput(int key, int x, int y)
{
   if (key == GLUT_KEY_DOWN) animationPeriod +=5;
   if( key == GLUT_KEY_UP) 
      if (animationPeriod > 5) animationPeriod -=5;
   glutPostRedisplay();
}

void disk()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.8,0.5,0.2);
centerx = centerx+Dx;
centery = centery+Dy;
if(centerx+radius > 1.0) Dx = - Dx;
if(centerx-radius < -1.0) Dx = - Dx;
if(centery+radius > 1.0) Dy = - Dy;
if(centery-radius <-1.0) Dy = - Dy;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(centerx, centery);
for (int i=0; i <= N; i++)
{
float angle = i*Dtheta;
glVertex2f(centerx+cos(angle)*radius,
centery+sin(angle)*radius);
}
glEnd();
 glutSwapBuffers();}
//---------- main
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(500,100);
glutInitWindowSize(320,320);
win = glutCreateWindow("Bouncing disk");
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glClearColor(0.5,0.0,0.8,1.0);
glutDisplayFunc(disk); 
glutSpecialFunc(specialKeyInput);
glutKeyboardFunc( keyInput);


glutMainLoop();
return 0;
}
