/*  drawfunc.c
 *  This program demonstrates the dot plot of the following function
 *  f(x) = e**(-x) cos (2 pi x )
 *
 *  Click the right button of the mouse within the opened
 *  window to exit the program.
 */
#include << X11/Xlib.h>
#include << math.h>
#include << GLUT/glut.h>
// #include << OpenGL/gl.h>
#include << stdlib.h>
#include << stdio.h>

const int screenWidth = 640;
const int screenHeight = 480;        
GLdouble A, B, C, D;                

void myInit(void) 
{
   glClearColor (1.0, 1.0, 1.0, 0.0);   // set black background color
   glColor3f (0.0f, 0.0f, 0.0f);       // set the drawing color
   glPointSize (2.0);                  // set dot size 2 x 2
   glMatrixMode (GL_PROJECTION);       // set "camera shape"
   glLoadIdentity ();                    // clear the matrix
           // viewing transformation 
   gluOrtho2D (0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
           // set the Worlk Window
   A = screenWidth / 4.0 ;   //set values used for scaling and shifting
   B = 0.0 ;
   C = D = screenHeight / 2.0 ;
}

void myDisplay(void)
{
   GLdouble x, func;

   glClear (GL_COLOR_BUFFER_BIT);    // clear the screen
   glBegin(GL_POINTS);
   for (x = 0; x < 4.0; x += 0.005)
   {
       func = exp( -x ) * cos ( 2 * 3.14159 * x );
       glVertex2d (A * x + B, C * func + D );
   }
   glEnd();
   glFlush ();                       // send all out to display
}

void myMouse(int button, int state, int x, int y)
{
  switch (button) {
     case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
           exit (-1);
        break;
     default:
        break;
  }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);                            // initialize the toolkit
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);     // set display mode
   glutInitWindowSize (screenWidth, screenHeight);   // set screen window size
   glutInitWindowPosition (100, 150);       // set window position on screen
   glutCreateWindow (argv[0]);              // open the screen window
   myInit ();
   glutDisplayFunc(myDisplay);              // register redraw function
   glutMouseFunc(myMouse);              // register the mouse action function
   glutMainLoop();                          // go into a perpetual loop
   return 0;
}