#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <bios.h>
#include <mem.h>
#include <string.h>
#include <alloc.h>
#include <time.h>

#include "graph.h"

makepic (char *picture [], unsigned codes [], int rows, int columns)
{
   int currentrow;
   int currentcolumn;
   unsigned mask;

   /* generate the picture */

   for (currentrow = 0; currentrow <= rows-1; currentrow++)
      for (mask = 1, currentcolumn = 0; currentcolumn <= columns-1; mask <<= 1, currentcolumn++)
         if (picture [currentrow] [currentcolumn] == PCHAR)
            codes [currentrow] |= mask;  /* turn pixel on */
               else
                  codes [currentrow] &= (~mask); /* turn pixel off */

}

putpic (int x, int y, unsigned picture [], int rows, int columns)
{
   int currentrow;
   int currentcolumn;
   unsigned mask;

   for (currentrow = 0; currentrow <= rows-1; currentrow++)
      for (mask = 1, currentcolumn = 1; currentcolumn <= columns; mask <<= 1, currentcolumn++)
         if ((picture [currentrow]) & mask)
            putpixel (x+currentcolumn, y+currentrow, DARKGRAY);
}

putpicture (int x, int y, char *picture [], int rows, int columns)
{
   int currentrow;
   int currentcolumn;

   for (currentrow = 0; currentrow <= rows-1; currentrow++)
      for (currentcolumn = 0; currentcolumn <= columns-1; currentcolumn++)
         if (picture [currentrow] [currentcolumn] == PCHAR)
            putpixel (x+currentcolumn, y+currentrow, DARKGRAY);
}


graphinit ()
{
   int graphdriver;
   int graphmode;
   char *graphicspath;
   int errorcode;

   graphdriver = DETECT;

   initgraph (&graphdriver, &graphmode, graphicspath);

   errorcode = graphresult ();
   if (errorcode != grOk) {
         printf ("\nGraphics System Error: %s\n",grapherrormsg (errorcode));
         exit (1);
      }
}


putpoint (int x, int y, int color, int writemode)
{
   int currentcolor;  /* current color of the point being written */

   currentcolor = getpixel (x,y);

   switch (writemode)   {
         case COPY_PUT:
            break;  /* do nothing -- color stays the same */

         case XOR_PUT:
            color = xor_bits (color, currentcolor); break;

         case OR_PUT:
            color = color | currentcolor; break;

         case AND_PUT:
            color = color & currentcolor; break;

         case NOT_PUT:
            color = ~color; break;
      }

   putpixel (x, y, color);
}

void triangle (int x, int y, unsigned radius)
{
   unsigned x1, y1;
   unsigned x2, y2;
   unsigned x3, y3;

   x1 = x;
   y1 = y - radius;

   x2 = x + cos (330)*radius;
   y2 = y + sin (330)*radius;

   x3 = x - cos (210)*radius;
   y3 = y + sin (210)*radius;

   line (x1, y1, x2, y2);
   line (x1, y1, x3, y3);
   line (x2, y2, x3, y3);

}

 void graph_function (double (*f) (double arg), double lower_bound, double upper_bound, double step)  
{

   int max_x, max_y;
   int center_x, center_y;
   int increment;
   int point_count;
   double *data;
   double current_value;
   int i;
   double minimum_y, maximum_y;
   double y_range;
   double y_factor;

   int x_skip;
   int skip_values;

   max_x = getmaxx ();
   max_y = getmaxy ();

   center_x = max_x / 2;
   center_y = max_y / 2;

   cleardevice ();

   /* draw the coordinate axes */

   line (center_x, 0, center_x, max_y);
   line (0, center_y, max_x, center_y);

   point_count = (upper_bound-lower_bound)/step;



   data = (double *) calloc (point_count, sizeof (double));

   minimum_y = (*f) (lower_bound);
   maximum_y = (*f) (lower_bound);

   for (i = 0, current_value = lower_bound; i < point_count; i++, current_value += step)   {
         data [i] = (*f) (current_value);

         if (data [i] < minimum_y)
            minimum_y = data [i];

         if (data [i] > maximum_y)
            maximum_y = data [i];
      }

   y_range = maximum_y - minimum_y;
   y_factor = y_range;


}








