#include "cs148.h"
#include <math.h>

#ifndef M_PI
#define M_PI 3.14159
#endif

#define PI 3.1415926

const int screenWidth = 640;
const int screenHeight = 480;

class GLintPoint {
public:
	GLint x, y;
};


void drawpoly(GLintPoint pts[], int N) {
	
	glBegin(GL_POLYGON);
	  for (int i=0; i<N; i++)
		  glVertex2f(pts[i].x, pts[i].y);
	glEnd();
	glFlush();

}


void make_ngon(int x, int y, int radius, int N) {
	
  int i;
	double angle, the_angle;
	GLintPoint pts[100];

	if (N < 3 || N >= 100) return;

	the_angle = 2.0 * M_PI / (double)N;
	for (i = 1; i <= N; i++) {
		angle = ((i - 1) * the_angle);
		pts[i-1].x = ceil(radius * cos(angle)) + x;
		pts[i-1].y = ceil(radius * sin(angle)) + y;
	}

	// close it up
	pts[N].x = pts[0].x + x;
	pts[N].y = pts[0].y + y;	
	drawpoly(pts, N);
}


void myMouseMove(int mousex, int mousey) {
	GLint x = mousex;
	GLint y = screenHeight - mousey;
	glClear(GL_COLOR_BUFFER_BIT);
	make_ngon(x, y, 300, 60);
	glFlush();	
}


void myInit(void) {
	glClearColor(1.0,1.0,1.0,0.0);
	glColor3f(0.0f,0.0f,0.0f);
	glPointSize(2.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}


void myDisplay(void) {
  glClear(GL_COLOR_BUFFER_BIT);
  glFlush();
}


void main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,150);
	glutCreateWindow("Single-buffered animation");
	glutDisplayFunc(myDisplay);
	glutMotionFunc(myMouseMove);
	myInit();
	glutMainLoop();
}

