#include <GL/glut.h>
#include<math.h>
static float angle=20.0;
static float d=0.02;
static int isanimate=0;
void init(void)
{
	glClearColor(1.0, 1.0, 1.0, 0.0);
}
void draw()
{
	int N = 360;
	float Dtheta = 2 * 3.1415926 / N;
	float angle = 0.0;
	glPointSize(3);
	glBegin(GL_POINTS);
	glColor3f(0, 1, 0);
	for (int i = -90; i <= 90; i++)
	{
		float angle = i*Dtheta;
		glVertex2f(cos(angle) / 3, angle / 3);
		glVertex2f(-cos(angle) / 3, angle / 3);
	}
	for (int i = -90; i <= 90; i++)
	{
		float angle = i*Dtheta;
		glVertex2f(cos(angle) / 20, angle / 3);
	}

	for (int i = -200; i <= -170; i++)
	{
		float angle = i*Dtheta;
		glVertex2f((cos(angle)) + 0.98, (angle / 3) + 0.46);
	}
	glEnd();
	glLineWidth(3);
	glBegin(GL_LINES);
	glColor3f(0, 1, 0);
	angle = 60 * Dtheta;
	glVertex2f(cos(angle) / 3, angle / 3);
	angle = 35 * Dtheta;
	glVertex2f(cos(angle) / 25, angle / 3);
	angle = 60 * Dtheta;
	glVertex2f(-cos(angle) / 3, angle / 3);
	angle = 35 * Dtheta;
	glVertex2f(cos(angle) / 25, angle / 3);
	angle = 35 * Dtheta;
	glVertex2f(cos(angle) / 3, angle / 3);
	angle = 0 * Dtheta;
	glVertex2f(cos(angle) / 25, angle / 3);
	angle = 35 * Dtheta;
	glVertex2f(-cos(angle) / 3, angle / 3);
	angle = 0 * Dtheta;
	glVertex2f(cos(angle) / 25, angle / 3);
	angle = 5 * Dtheta;
	glVertex2f(cos(angle) / 3, angle / 3);
	angle = -35 * Dtheta;
	glVertex2f(cos(angle) / 25, angle / 3);
	angle = 5 * Dtheta;
	glVertex2f(-cos(angle) / 3, angle / 3);
	angle = -35 * Dtheta;
	glVertex2f(cos(angle) / 25, angle / 3);
	angle = -35 * Dtheta;
	glVertex2f(cos(angle) / 3, angle / 3);
	angle = -70 * Dtheta;
	glVertex2f(cos(angle) / 25, angle / 3);
	angle = -35 * Dtheta;
	glVertex2f(-cos(angle) / 3, angle / 3);
	angle = -70 * Dtheta;
	glVertex2f(cos(angle) / 25, angle / 3);
	glEnd();
}
void increaseAngle(void)
{
   angle += 0.005;
  // if (Angle > 360.0) Angle -= 360.0;
   glutPostRedisplay();
}
void animate(int value)
{
   if (isanimate) 
   {
      increaseAngle();

      glutPostRedisplay();     
      glutTimerFunc(100, animate, 1);
   }
}

void keyInput(unsigned char key, int x, int y)
{
   switch(key) 
   {      case 27:
         exit(0);
         break;
      case ' ':
         if(isanimate) 
		 {
            isanimate = 0;
	  glutIdleFunc(NULL);
		 }
		 else 
		 {
            isanimate = 1;
		    //glutIdleFunc(increaseAngle);
		    animate(1);

		 }
         break;
      default:
         break;
   }
}

void resize(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//gluOrtho2D(-10, -10, 10, 10);
	//glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void display() {
	glClear(GL_COLOR_BUFFER_BIT);


	 glRotatef(angle, 0.0, 1.0, 1.0);
//glTranslatef(0.0,0.0,d);
	draw();
	glFlush();
	glutSwapBuffers();
}
int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitWindowSize(500, 500);
	glutCreateWindow("Leaf.cpp");
	glutDisplayFunc(display);
	glutReshapeFunc(resize);
	init();
	glutKeyboardFunc( keyInput);
	glutMainLoop();
	return 0;
}
