#include "cs148.h"
#include <math.h>

#ifndef M_PI
#define M_PI 3.1415916
#endif

const int screenWidth = 640;
const int screenHeight = 480;

class GLintPoint {
public:
	GLint x, y;
};

void draw_something(GLintPoint pts[], int N) {
	
	float A = screenHeight/2.0;
  float B = screenWidth/2.0;

	glBegin(GL_LINE_LOOP);
  	for (int i=0; i<N; i++) glVertex2f(pts[i].x + B, pts[i].y + A);
	glEnd();
	
  glFlush();

}

void make_something(int r, int N) {
	
  GLintPoint pts[100];
	if (N < 3 || N >= 100) return;

	double the_angle = 2.0 * M_PI / (double)N;
	for (int i = 1; i <= N; i++) {
		double angle = ((i - 1) * the_angle);
		pts[i-1].x = ceil(r * cos(angle));
		pts[i-1].y = ceil(r * sin(angle));
	}

	pts[N].x = pts[0].x; pts[N].y = pts[0].y;	
	draw_something(pts, N);
}

void myDisplay(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	make_something(100,16);
	glFlush();
}


void myInit(void) {
	glClearColor(1.0,1.0,1.0,0.0);
	glColor3f(0.0f,0.0f,0.0f);
	glLineWidth(3.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}


void main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,150);
	glutCreateWindow("OpenGL");
	glutDisplayFunc(myDisplay);
	myInit();
	glutMainLoop();
}

