Back to index

radiance  4R0+20100331
cgraph.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: cgraph.c,v 1.2 2003/11/15 02:13:36 schorsch Exp $";
00003 #endif
00004 /*
00005  *  cgraph.c - routines for sending graphs to tty's.
00006  *
00007  *     Greg Ward
00008  *     7/7/86
00009  */
00010 
00011 
00012 #include  <stdio.h>
00013 
00014 #include  "rterror.h"
00015 #include  "meta.h"
00016 #include  "mgvars.h"
00017 
00018 
00019 #define  FHUGE  1e10
00020 
00021 #define  MAXSIZE  10000            /* Maximum size in characters of output */
00022 
00023 extern char  *progname;                   /* argv[0] */
00024 
00025 static char  outcarr[MAXSIZE];            /* character output array */
00026 
00027 static double  xmin = XMAX, xmax = XMIN,  /* extrema */
00028               ymin = YMAX, ymax = YMIN;
00029 
00030 static int  dwidth, dlength;              /* device width and length */
00031 
00032 static int  nplottable;            /* number of plottable points */
00033 
00034 static void climits(void);
00035 static void cstretch(int  c, double  x, double  y);
00036 
00037 
00038 extern void
00039 cgraph(              /* do a character graph to stdout */
00040        int width,
00041        int length
00042 )
00043 {
00044        if (width * length > MAXSIZE) {
00045               fprintf(stderr, "%s: page too big\n", progname);
00046               quit(1);
00047        }
00048        dwidth = width;
00049        dlength = length;
00050        climits();                  /* get min & max values */
00051        cplot();                    /* do character plot */
00052 }
00053 
00054 
00055 static void
00056 climits(void)               /* get min & max values */
00057 {
00058        int  i;
00059 
00060        xmin = gparam[XMIN].flags & DEFINED ?
00061                      varvalue(gparam[XMIN].name) :
00062                      FHUGE ;
00063        xmax = gparam[XMAX].flags & DEFINED ?
00064                      varvalue(gparam[XMAX].name) :
00065                      -FHUGE ;
00066        ymin = gparam[YMIN].flags & DEFINED ?
00067                      varvalue(gparam[YMIN].name) :
00068                      FHUGE ;
00069        ymax = gparam[YMAX].flags & DEFINED ?
00070                      varvalue(gparam[YMAX].name) :
00071                      -FHUGE ;
00072 
00073        nplottable = 0;
00074        for (i = 0; i < MAXCUR; i++)
00075               mgcurve(i, cstretch);
00076 
00077        if (nplottable == 0) {
00078               fprintf(stderr, "%s: no plottable data\n", progname);
00079               quit(1);
00080        }
00081        printf("XMIN= %f  XMAX= %f  YMIN= %f  YMAX= %f\n",
00082                      xmin, xmax, ymin, ymax);
00083 }
00084 
00085 
00086 void
00087 cstretch(                   /* stretch our boundaries */
00088        int  c,
00089        double  x,
00090        double  y
00091 )
00092 {
00093        if (gparam[XMIN].flags & DEFINED &&
00094                      x < xmin)
00095               return;
00096        if (gparam[XMAX].flags & DEFINED &&
00097                      x > xmax)
00098               return;
00099        if (gparam[YMIN].flags & DEFINED &&
00100                      y < ymin)
00101               return;
00102        if (gparam[YMAX].flags & DEFINED &&
00103                      y > ymax)
00104               return;
00105 
00106        if (x < xmin)
00107               xmin = x;
00108        if (x > xmax)
00109               xmax = x;
00110        if (y < ymin)
00111               ymin = y;
00112        if (y > ymax)
00113               ymax = y;
00114               
00115        nplottable++;
00116 }
00117 
00118 
00119 extern void
00120 cplot(void)                        /* do character  plot */
00121 {
00122        int  i, j;
00123        register char  *op;
00124 
00125        for (op = outcarr+dlength*dwidth; op > outcarr; )
00126               *--op = ' ';
00127 
00128        for (i = 0; i < MAXCUR; i++)
00129               mgcurve(i, cpoint);
00130 
00131        for (i = 0; i < dlength; i++) {
00132               for (j = 0; j < dwidth; j++)
00133                      putchar(*op++);
00134               putchar('\n');
00135        }
00136 }
00137 
00138 
00139 extern void
00140 cpoint(                     /* store a point */
00141        int  c,
00142        double  x,
00143        double  y
00144 )
00145 {
00146        register int  ndx;
00147 
00148        if (x < xmin || x > xmax || y < ymin || y > ymax)
00149               return;
00150 
00151        ndx = (dlength-1)*(1.0 - (y - ymin)/(ymax - ymin)) + 0.5;
00152        ndx = dwidth*ndx + (dwidth-1)*(x-xmin)/(xmax-xmin) + 0.5;
00153 
00154        if (outcarr[ndx] == ' ')
00155               outcarr[ndx] = c+'A';
00156        else if (outcarr[ndx] > '1' && outcarr[ndx] < '9')
00157               outcarr[ndx]++;
00158        else if (outcarr[ndx] == '9')
00159               outcarr[ndx] = '*';
00160        else if (outcarr[ndx] != '*')
00161               outcarr[ndx] = '2';
00162 }