/* a program that demos writing to multiple files in the same program 
   this program produces a gnuplot data file and gnuplot script file
   suitable for use with the command "gnuplot lab12b.gnuplot".  The
   result is a file lab12b.png that contains a graphic spelling "CIS"  */

#include <stdio.h>

#define DATFILE "lab12b.dat"
#define GPFILE  "lab12b.gnuplot"
#define PNGFILE  "lab12b.png"

/* function prototypes */

void writeCurvedTop(FILE *outfile, int x, int y);
void writeCurvedBottom(FILE *outfile, int x, int y);

void writeC(FILE *outfile, int x, int y);
void writeI(FILE *outfile, int x, int y);
void writeS(FILE *outfile, int x, int y);

int main ()
{
   FILE *datfile;     /* declare a file pointer for the data file*/
   FILE *gpfile;  /* declare a file pointer for the gnuplot file*/


   /* open the data file */
   
   datfile = fopen(DATFILE,"w"); /* try to open file for writing */
   if (datfile == NULL)               /* check return status */
     {
       perror("File " DATFILE " could not be opened: "); /* print error */
       exit(-1);
     }

   /* open the gnuplot file */
   
   gpfile = fopen(GPFILE,"w"); /* try to open file for writing */
   if (gpfile == NULL)               /* check return status */
     {
       perror("File " GPFILE " could not be opened: "); /* print error */
       exit(-1);
     }

   printf("Now generating the data file...\n");
   
   fprintf(datfile,"# lab12b.dat  CIS\n");
   writeC(datfile,0,0);
   writeI(datfile,5,0);
   writeS(datfile,10,0);
   fclose(datfile);

   printf("Now generating the gnuplot file...\n");
  
   fprintf(gpfile,"set xrange [-1:15]             \n");
   fprintf(gpfile,"set yrange [-1:15]             \n");
   fprintf(gpfile,"set output \"" PNGFILE "\"         \n");
   fprintf(gpfile,"set terminal png large color \n");
   fprintf(gpfile,"plot \"" DATFILE "\" with lines    \n");
   fprintf(gpfile,"\n");
   fclose(gpfile);

   printf("Done.\n");
   return 0;
}

void writeCurvedTop(FILE *outfile, int x, int y)
{
  /*   ___
      /   \  */

  fprintf(outfile, "#   output from writeCurvedTop(outfile,%d,%d)\n",x,y);
  fprintf(outfile, "%d %d\n", x+0, y+7);
  fprintf(outfile, "%d %d\n", x+1, y+8);
  fprintf(outfile, "%d %d\n", x+3, y+8);
  fprintf(outfile, "%d %d\n", x+4, y+7);
  fprintf(outfile, "\n");

}


void writeCurvedBottom(FILE *outfile, int x, int y)
{
  /*  \___/  */

  fprintf(outfile, "#   output from writeCurvedBottom(outfile,%d,%d)\n",x,y);
  fprintf(outfile, "%d %d\n", x+0, y+1);
  fprintf(outfile, "%d %d\n", x+1, y+0);
  fprintf(outfile, "%d %d\n", x+3, y+0);
  fprintf(outfile, "%d %d\n", x+4, y+1);
  fprintf(outfile, "\n");

}


void writeC(FILE *outfile, int x, int y)
{

  /*  ---
     /   \  
     | 
     \___/  */

  fprintf(outfile, "#   output from writeC(outfile,%d,%d)\n",x,y);
  writeCurvedTop(outfile, x, y);
  fprintf(outfile, "%d %d\n", x+0, y+7);
  fprintf(outfile, "%d %d\n", x+0, y+1);
  fprintf(outfile, "\n");
  writeCurvedBottom(outfile, x, y);
}

void writeI(FILE *outfile, int x, int y)
{

  /*  ---
       | 
       | 
      --- */

  fprintf(outfile, "#  output from writeI(outfile,%d,%d)\n",x,y);

  fprintf(outfile, "%d %d\n", x+1, y+0);
  fprintf(outfile, "%d %d\n", x+3, y+0);
  fprintf(outfile, "\n");

  fprintf(outfile, "%d %d\n", x+1, y+8);
  fprintf(outfile, "%d %d\n", x+3, y+8);
  fprintf(outfile, "\n");

  fprintf(outfile, "%d %d\n", x+2, y+0);
  fprintf(outfile, "%d %d\n", x+2, y+8);
  fprintf(outfile, "\n");

}

void writeS(FILE *outfile, int x, int y)
{

  /*  ---
     /   \ 
     | 
     |
     \---
         \
          |
          |
     \___/  */

  fprintf(outfile, "# output from writeS(outfile,%d,%d)\n",x,y);
  writeCurvedTop(outfile, x, y);
  fprintf(outfile, "%d %d\n", x+0, y+7);
  fprintf(outfile, "%d %d\n", x+0, y+5);
  fprintf(outfile, "%d %d\n", x+1, y+4);
  fprintf(outfile, "%d %d\n", x+3, y+4);
  fprintf(outfile, "%d %d\n", x+4, y+3);
  fprintf(outfile, "%d %d\n", x+4, y+2);
  fprintf(outfile, "\n");
  writeCurvedBottom(outfile, x, y);
}

