#include "cs148.h"
#include <math.h>

#ifndef M_PI
#define M_PI 3.14159
#endif

const int screenWidth = 640;
const int screenHeight = 480;

GLdouble f(GLdouble x) {
	return (exp(-x) * cos(2.0 * M_PI * x));
}


void graph_function(double start_x, double end_x) {
	
	GLdouble A, B, C, D, x;
	double x_span = end_x - start_x;
  double x_step = x_span / 1000.0;

	A = screenWidth / x_span;
	B = 0.0;
	C = screenHeight / 2.0;
	D = C;
	
	glBegin(GL_POINTS);
		for (x = start_x; x < end_x; x += x_step) 
      glVertex2d(A * x + B, C * f(x) + D);
	glEnd();
}	
	

void myDisplay(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	graph_function(0,10.0);	
	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 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();
}

