#include<math.h>
#include<GL/glut.h>
# define fun (sin(x)*(tan(x)+cos(x)))
const int screenWidth = 640;
// width of screen window in pixels
const int screenHeight = 480;
// height of screen window in pixels
GLdouble A,B,C,D; // values used for scaling and shifting
//----------Initialization--------------
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0); // Set white background color
glColor3f(0.0f,0.0f,0.0f);
// Drawing color is black
glPointSize(2.0);
// a ''dot'' is 2 by 2 pixels
glMatrixMode(GL_PROJECTION);
// Set "camera shape"
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);
// Set values used for scaling and shifting
A = screenWidth/16.0;
B = screenWidth/2.0;
C = D = screenHeight/2.0;
}
// --------Draw the "dot plots" of a function------
GLdouble h(GLdouble x)
{
return fun;
}
void dotPlots(void)
{
GLdouble x;
glClear(GL_COLOR_BUFFER_BIT);
//clear the screen
glBegin(GL_POINTS);
for(x=-16.0;x<16.0;x+=0.0001)
{
//GLdouble func = exp(-x) * cos(2*3.14159265*x);
GLdouble func = h(x);
glVertex2d(A*x+B,(C*func/20.0+D));
}
glEnd();
// Draw a horizontal line
glBegin(GL_LINES);
glVertex2i(0,screenHeight/2);
glVertex2i(screenWidth,screenHeight/2);
glEnd();
glBegin(GL_LINES);
glVertex2i(screenWidth/2,0);
glVertex2i(screenWidth/2,screenHeight);
glEnd();
glFlush(); //send all output to display
}
main(int argc,char** argv)
{
glutInit(&argc, argv);
// Initialize the toolkit
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
// Set display mode
glutInitWindowPosition(100, 150);
// Set window pozition on screen
glutInitWindowSize(screenWidth, screenHeight);
// Set window size
glutCreateWindow("Dot plot of a Function"); // Open the screen window
glutDisplayFunc(dotPlots); // Register redraw function
init();
glutMainLoop();
// Go into a perpetual loop
}