home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 May / cica_0595_4.zip / cica_0595_4 / UTIL / GPT34SRC / GRAPHICS.C < prev    next >
C/C++ Source or Header  |  1993-05-25  |  58KB  |  2,037 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: graphics.c%v 3.38.2.125 1993/05/05 00:02:28 woo Exp woo $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - graphics.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  *
  34.  * There is a mailing list for gnuplot users. Note, however, that the
  35.  * newsgroup 
  36.  *    comp.graphics.gnuplot 
  37.  * is identical to the mailing list (they
  38.  * both carry the same set of messages). We prefer that you read the
  39.  * messages through that newsgroup, to subscribing to the mailing list.
  40.  * (If you can read that newsgroup, and are already on the mailing list,
  41.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  42.  * removed from the mailing list.)
  43.  *
  44.  * The address for mailing to list members is
  45.  *       info-gnuplot@dartmouth.edu
  46.  * and for mailing administrative requests is 
  47.  *       info-gnuplot-request@dartmouth.edu
  48.  * The mailing list for bug reports is 
  49.  *       bug-gnuplot@dartmouth.edu
  50.  * The list of those interested in beta-test versions is
  51.  *       info-gnuplot-beta@dartmouth.edu
  52.  */
  53.  
  54. #include <stdio.h>
  55. #include <math.h>
  56. #include <assert.h>
  57. #if !defined(u3b2)
  58. #include <time.h>
  59. #endif
  60. #include "plot.h"
  61. #include "setshow.h"
  62.  
  63. #ifdef DJGPP
  64. #define time_t unsigned long
  65. #endif
  66.  
  67. #ifndef AMIGA_SC_6_1
  68. extern char *strcpy(),*strncpy(),*strcat(),*ctime();
  69. #endif /* !AMIGA_SC_6_1 */
  70. char *tdate;
  71. #ifdef AMIGA_AC_5
  72. time_t dated;
  73. #else
  74. #if defined(apollo) || defined(sequent) || defined(u3b2)
  75. #include <sys/types.h> /* typedef long time_t; */
  76. #endif
  77. time_t dated; /* ,time(); */
  78. #include <time.h>
  79. #endif
  80.  
  81. void plot_impulses();
  82. void plot_lines();
  83. void plot_points();
  84. void plot_dots();
  85. void plot_bars();
  86. void plot_boxes();
  87. void edge_intersect();
  88. TBOOLEAN two_edge_intersect();
  89.  
  90. void plot_steps();            /* JG */
  91. void edge_intersect_steps();         /* JG */
  92. TBOOLEAN two_edge_intersect_steps();    /* JG */
  93.  
  94. /* for plotting error bars */
  95. #define ERRORBARTIC (t->h_tic/2) /* half the width of error bar tic mark */
  96.  
  97. #ifndef max        /* Lattice C has max() in math.h, but shouldn't! */
  98. #define max(a,b) ((a > b) ? a : b)
  99. #endif
  100.  
  101. #ifndef min
  102. #define min(a,b) ((a < b) ? a : b)
  103. #endif
  104.  
  105. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  106.  
  107. /* True if a and b have the same sign or zero (positive or negative) */
  108. #define samesign(a,b) ((a) * (b) >= 0)
  109.  
  110. /* Define the boundary of the plot
  111.  * These are computed at each call to do_plot, and are constant over
  112.  * the period of one do_plot. They actually only change when the term
  113.  * type changes and when the 'set size' factors change. 
  114.  */
  115. static int xleft, xright, ybot, ytop;
  116.  
  117. /* Boundary and scale factors, in user coordinates */
  118. /* x_min, x_max, y_min, y_max are local to this file and
  119.  * are not the same as variables of the same names in other files
  120.  */
  121. static double x_min, x_max, y_min, y_max;
  122. static double xscale, yscale;
  123.  
  124. /* And the functions to map from user to terminal coordinates */
  125. #define map_x(x) (int)(xleft+(x-x_min)*xscale+0.5) /* maps floating point x to screen */ 
  126. #define map_y(y) (int)(ybot+(y-y_min)*yscale+0.5)    /* same for y */
  127.  
  128. /* (DFK) Watch for cancellation error near zero on axes labels */
  129. #define SIGNIF (0.01)        /* less than one hundredth of a tic mark */
  130. #define CheckZero(x,tic) (fabs(x) < ((tic) * SIGNIF) ? 0.0 : (x))
  131. #define NearlyEqual(x,y,tic) (fabs((x)-(y)) < ((tic) * SIGNIF))
  132.  
  133. /* (DFK) For some reason, the Sun386i compiler screws up with the CheckLog 
  134.  * macro, so I write it as a function on that machine.
  135.  */
  136. #ifndef sun386
  137. /* (DFK) Use 10^x if logscale is in effect, else x */
  138. #define CheckLog(is_log, base_log, x) ((is_log) ? pow(base_log, (x)) : (x))
  139. #else
  140. static double
  141. CheckLog(log, base_log, x)
  142.      TBOOLEAN is_log;
  143.      double base_log;
  144.      double x;
  145. {
  146.   if (is_log)
  147.     return(pow(base_log, x));
  148.   else
  149.     return(x);
  150. }
  151. #endif /* sun386 */
  152.  
  153. double
  154. LogScale(coord, is_log, log_base_log, what, axis)
  155.     double coord;            /* the value */
  156.     TBOOLEAN is_log;            /* is this axis in logscale? */
  157.         double log_base_log;        /* if so, the log of its base */
  158.     char *what;            /* what is the coord for? */
  159.     char *axis;            /* which axis is this for ("x" or "y")? */
  160. {
  161.     if (is_log) {
  162.        if (coord <= 0.0) {
  163.           char errbuf[100];        /* place to write error message */
  164.         (void) sprintf(errbuf,"%s has %s coord of %g; must be above 0 for log scale!",
  165.                 what, axis, coord);
  166.           (*term_tbl[term].text)();
  167.           (void) fflush(outfile);
  168.           int_error(errbuf, NO_CARET);
  169.        } else
  170.         return(log(coord)/log_base_log);
  171.     }
  172.     return(coord);
  173. }
  174.  
  175. /* borders of plotting area */
  176. /* computed once on every call to do_plot */
  177. boundary(scaling)
  178.     TBOOLEAN scaling;        /* TRUE if terminal is doing the scaling */
  179. {
  180.     register struct termentry *t = &term_tbl[term];
  181.     /* luecken@udel.edu modifications 
  182.        sizes the plot according to the presence of labels, title,... */
  183.     if (strlen(ylabel) == 0)
  184.         xleft = (t->h_char)*8;
  185.     else
  186.         xleft = (t->h_char)*10;
  187.     xright = (scaling ? 1 : xsize) * (t->xmax) - (t->h_char)*2 - (t->h_tic);
  188.     if (strlen(xlabel) == 0)
  189.         ybot = (t->v_char)*3/2 + 1;
  190.     else
  191.         if ((*t->text_angle)(1))
  192.             ybot = (t->v_char)*5/2 + 1;
  193.         else
  194.             ybot = (t->v_char)*7/2 + 1;    /* allow space for time at bottom */
  195.     if ( (strlen(title) != 0) || timedate ||
  196.       ((strlen(ylabel) != 0) && ((*t->text_angle)(1) == FALSE)) )
  197.         ytop = (scaling ? 1 : ysize) * (t->ymax) - (t->v_char)*3/2 - 1;
  198.     else
  199.         ytop = (scaling ? 1 : ysize) * (t->ymax) - (t->v_char)/2 - 1;
  200.     (void)(*t->text_angle)(0);
  201. }
  202.  
  203.  
  204. double dbl_raise(x,y)
  205. double x;
  206. int y;
  207. {
  208. register int i;
  209. double val;
  210.  
  211.     val = 1.0;
  212.     for (i=0; i < abs(y); i++)
  213.         val *= x;
  214.     if (y < 0 ) return (1.0/val);
  215.     return(val);
  216. }
  217.  
  218.  
  219. double make_tics(tmin,tmax,logscale,base_log)
  220. double tmin,tmax;
  221. TBOOLEAN logscale;
  222. double base_log;
  223. {
  224. register double xr,xnorm,tics,tic,l10;
  225.  
  226.     xr = fabs(tmin-tmax);
  227.     
  228.     l10 = log10(xr);
  229.     if (logscale) {
  230.         tic = dbl_raise(base_log,(l10 >= 0.0 ) ? (int)l10 : ((int)l10-1));
  231.         if (tic < 1.0)
  232.             tic = 1.0;
  233.     } else {
  234.         xnorm = pow(10.0,l10-(double)((l10 >= 0.0 ) ? (int)l10 : ((int)l10-1)));
  235.         if (xnorm <= 2)
  236.             tics = 0.2;
  237.         else if (xnorm <= 5)
  238.             tics = 0.5;
  239.         else tics = 1.0;    
  240.         tic = tics * dbl_raise(10.0,(l10 >= 0.0 ) ? (int)l10 : ((int)l10-1));
  241.     }
  242.     return(tic);
  243. }
  244.  
  245.  
  246. do_plot(plots, pcount, min_x, max_x, min_y, max_y)
  247. struct curve_points *plots;
  248. int pcount;            /* count of plots in linked list */
  249. double min_x, max_x;
  250. double min_y, max_y;
  251. {
  252. register struct termentry *t = &term_tbl[term];
  253. register int curve, xaxis_y, yaxis_x;
  254. register struct curve_points *this_plot;
  255. register double ytic, xtic;
  256. register int xl, yl;
  257.             /* only a Pyramid would have this many registers! */
  258. double xtemp, ytemp;
  259. struct text_label *this_label;
  260. struct arrow_def *this_arrow;
  261. TBOOLEAN scaling;
  262.  
  263.  
  264. /* store these in variables global to this file */
  265. /* otherwise, we have to pass them around a lot */
  266.      x_min = min_x;
  267.      x_max = max_x; 
  268.      y_min = min_y;
  269.      y_max = max_y;
  270.  
  271.     if (polar) {
  272.         /* will possibly change x_min, x_max, y_min, y_max */
  273.         polar_xform(plots,pcount);
  274.     }
  275.  
  276.     if (y_min == VERYLARGE || y_max == -VERYLARGE ||
  277.         x_min == VERYLARGE || x_max == -VERYLARGE)
  278.         int_error("all points undefined!", NO_CARET);
  279.  
  280. /*    Apply the desired viewport offsets. */
  281.      if (y_min < y_max) {
  282.         y_min -= boff;
  283.         y_max += toff;
  284.     } else {
  285.         y_max -= boff;
  286.         y_min += toff;
  287.     }
  288.      if (x_min < x_max) {
  289.         x_min -= loff;
  290.         x_max += roff;
  291.     } else {
  292.         x_max -= loff;
  293.         x_min += roff;
  294.     }
  295.  
  296. /* SETUP RANGES, SCALES AND TIC PLACES */
  297.     if (ytics && yticdef.type == TIC_COMPUTED) {
  298.        ytic = make_tics(y_min,y_max,is_log_y,base_log_y);
  299.     
  300.        if (autoscale_ly) {
  301.           if (y_min < y_max) {
  302.              y_min = ytic * floor(y_min/ytic);       
  303.              y_max = ytic * ceil(y_max/ytic);
  304.           }
  305.           else {            /* reverse axis */
  306.              y_min = ytic * ceil(y_min/ytic);       
  307.              y_max = ytic * floor(y_max/ytic);
  308.           }
  309.        }
  310.     }
  311.  
  312.     if (xtics && xticdef.type == TIC_COMPUTED) {
  313.        xtic = make_tics(x_min,x_max,is_log_x,base_log_x);
  314.        
  315.        if (autoscale_lx) {
  316.           if (x_min < x_max) {
  317.              x_min = xtic * floor(x_min/xtic);    
  318.              x_max = xtic * ceil(x_max/xtic);
  319.           } else {
  320.              x_min = xtic * ceil(x_min/xtic);
  321.              x_max = xtic * floor(x_max/xtic);    
  322.           }
  323.        }
  324.     }
  325.  
  326. /*    This used be x_max == x_min, but that caused an infinite loop once. */
  327.     if (fabs(x_max - x_min) < zero)
  328.         int_error("x_min should not equal x_max!",NO_CARET);
  329.     if (fabs(y_max - y_min) < zero)
  330.         int_error("y_min should not equal y_max!",NO_CARET);
  331.  
  332. /* INITIALIZE TERMINAL */
  333.     if (!term_init) {
  334.         (*t->init)();
  335.         term_init = TRUE;
  336.     }
  337.     screen_ok = FALSE;
  338.     scaling = (*t->scale)(xsize, ysize);
  339.     (*t->graphics)();
  340.  
  341.      /* now compute boundary for plot (xleft, xright, ytop, ybot) */
  342.      boundary(scaling);
  343.  
  344. /* SCALE FACTORS */
  345.     yscale = (ytop - ybot)/(y_max - y_min);
  346.     xscale = (xright - xleft)/(x_max - x_min);
  347.     
  348. /* DRAW AXES */
  349.     (*t->linetype)(-1);    /* axis line type */
  350.     xaxis_y = map_y(0.0);
  351.     yaxis_x = map_x(0.0); 
  352.  
  353.     if (xaxis_y < ybot)
  354.         xaxis_y = ybot;                /* save for impulse plotting */
  355.     else if (xaxis_y >= ytop)
  356.         xaxis_y = ytop ;
  357.     else if (xzeroaxis && !is_log_y) {
  358.         (*t->move)(xleft,xaxis_y);
  359.         (*t->vector)(xright,xaxis_y);
  360.     }
  361.  
  362.     if (yzeroaxis && !is_log_x && yaxis_x >= xleft && yaxis_x < xright ) {
  363.         (*t->move)(yaxis_x,ybot);
  364.         (*t->vector)(yaxis_x,ytop);
  365.     }
  366.  
  367. /* DRAW TICS */
  368.     (*t->linetype)(-2); /* border linetype */
  369.  
  370.     /* label y axis tics */
  371.      if (ytics) {
  372.         switch (yticdef.type) {
  373.            case TIC_COMPUTED: {
  374.                if (y_min < y_max)
  375.                 draw_ytics(ytic * floor(y_min/ytic),
  376.                         ytic,
  377.                         ytic * ceil(y_max/ytic));
  378.               else
  379.                 draw_ytics(ytic * floor(y_max/ytic),
  380.                         ytic,
  381.                         ytic * ceil(y_min/ytic));
  382.  
  383.               break;
  384.            }
  385.             case TIC_MONTH:{
  386.             draw_month_ytics();
  387.             break;
  388.             }
  389.             case TIC_DAY: {
  390.             draw_day_ytics();
  391.             break;
  392.             }
  393.            case TIC_SERIES: {
  394.               draw_series_ytics(yticdef.def.series.start, 
  395.                             yticdef.def.series.incr, 
  396.                             yticdef.def.series.end);
  397.               break;
  398.            }
  399.            case TIC_USER: {
  400.               draw_set_ytics(yticdef.def.user);
  401.               break;
  402.            }
  403.            default: {
  404.               (*t->text)();
  405.                 (void) fflush(outfile);
  406.               int_error("unknown tic type in yticdef in do_plot", NO_CARET);
  407.               break;        /* NOTREACHED */
  408.            }
  409.         }
  410.     }
  411.  
  412.     /* label x axis tics */
  413.      if (xtics) {
  414.         switch (xticdef.type) {
  415.            case TIC_COMPUTED: {
  416.                if (x_min < x_max)
  417.                 draw_xtics(xtic * floor(x_min/xtic),
  418.                         xtic,
  419.                         xtic * ceil(x_max/xtic));
  420.               else
  421.                 draw_xtics(xtic * floor(x_max/xtic),
  422.                         xtic,
  423.                         xtic * ceil(x_min/xtic));
  424.  
  425.               break;
  426.            }
  427.             case TIC_MONTH: {
  428.             draw_month_xtics();
  429.             break;
  430.             }
  431.             case TIC_DAY : {
  432.             draw_day_xtics();
  433.             break;
  434.             }
  435.            case TIC_SERIES: {
  436.               draw_series_xtics(xticdef.def.series.start, 
  437.                             xticdef.def.series.incr, 
  438.                             xticdef.def.series.end);
  439.               break;
  440.            }
  441.            case TIC_USER: {
  442.               draw_set_xtics(xticdef.def.user);
  443.               break;
  444.            }
  445.            default: {
  446.               (*t->text)();
  447.               (void) fflush(outfile);
  448.               int_error("unknown tic type in xticdef in do_plot", NO_CARET);
  449.               break;        /* NOTREACHED */
  450.            }
  451.         }
  452.     }
  453.  
  454. /* DRAW PLOT BORDER */
  455.     (*t->linetype)(-2); /* border linetype */
  456.     if (draw_border) {
  457.         (*t->move)(xleft,ybot);
  458.         (*t->vector)(xright,ybot);
  459.         (*t->vector)(xright,ytop);
  460.         (*t->vector)(xleft,ytop);
  461.         (*t->vector)(xleft,ybot);
  462.     }
  463.  
  464. /* PLACE YLABEL */
  465.     if (strlen(ylabel) > 0) {
  466.         int x, y;
  467.  
  468.         x = ylabel_xoffset * t->h_char;
  469.         y = ylabel_yoffset * t->v_char;
  470.         if ((*t->text_angle)(1)) {
  471.             if ((*t->justify_text)(CENTRE)) {
  472.                 (*t->put_text)(x+(t->v_char),
  473.                          y+(ytop+ybot)/2, ylabel);
  474.             }
  475.             else {
  476.                 (*t->put_text)(x+(t->v_char),
  477.                            y+(ytop+ybot)/2-(t->h_char)*strlen(ylabel)/2, 
  478.                          ylabel);
  479.             }
  480.         }
  481.         else {
  482.             (void)(*t->justify_text)(LEFT);
  483.             (*t->put_text)(x,y+ytop+(t->v_char), ylabel);
  484.         }
  485.         (void)(*t->text_angle)(0);
  486.     }
  487.  
  488. /* PLACE XLABEL */
  489.     if (strlen(xlabel) > 0) {
  490.         int x, y;
  491.  
  492.         x = xlabel_xoffset * t->h_char;
  493.         y = xlabel_yoffset * t->v_char;
  494.  
  495.             if ((*t->justify_text)(CENTRE)) 
  496.             (*t->put_text)(x+(xleft+xright)/2,
  497.                        y+ybot-2*(t->v_char), xlabel);
  498.             else
  499.             (*t->put_text)(x+(xleft+xright)/2 - strlen(xlabel)*(t->h_char)/2,
  500.                            y+ybot-2*(t->v_char), xlabel);
  501.     }
  502.  
  503. /* PLACE TITLE */
  504.     if (strlen(title) > 0) {
  505.         int x, y;
  506.  
  507.         x = title_xoffset * t->h_char;
  508.         y = title_yoffset * t->v_char;
  509.  
  510.             if ((*t->justify_text)(CENTRE))
  511.             (*t->put_text)(x+(xleft+xright)/2,
  512.                        y+ytop+(t->v_char), title);
  513.             else
  514.             (*t->put_text)(x+(xleft+xright)/2 - strlen(title)*(t->h_char)/2,
  515.                        y+ytop+(t->v_char), title);
  516.     }
  517.  
  518.  
  519. /* PLACE TIMEDATE */
  520.     if (timedate) {
  521.         int x, y;
  522.  
  523.         x = time_xoffset * t->h_char;
  524.         y = time_yoffset * t->v_char;
  525.         dated = time( (time_t *) 0);
  526.         tdate = ctime( &dated);
  527.         tdate[24]='\0';
  528.         (void)(*t->justify_text)(LEFT);
  529.         if ((*t->text_angle)(1)) {
  530.             (void)(*t->text_angle)(0);
  531.             (*t->put_text)(x, y+ytop+(t->v_char), tdate);
  532.         }
  533.         else {
  534.             (void)(*t->text_angle)(0);
  535.             (*t->put_text)(x,
  536.                          y+ybot-3*(t->v_char), tdate);
  537.         }
  538.     }
  539.  
  540. /* PLACE LABELS */
  541.     for (this_label = first_label; this_label!=NULL;
  542.             this_label=this_label->next ) {
  543.          xtemp = LogScale(this_label->x, is_log_x, log_base_log_x, "label", "x");
  544.          ytemp = LogScale(this_label->y, is_log_y, log_base_log_y, "label", "y");
  545.         if ((*t->justify_text)(this_label->pos)) {
  546.             (*t->put_text)(map_x(xtemp),map_y(ytemp),this_label->text);
  547.         }
  548.         else {
  549.             switch(this_label->pos) {
  550.                 case  LEFT:
  551.                     (*t->put_text)(map_x(xtemp),map_y(ytemp),
  552.                         this_label->text);
  553.                     break;
  554.                 case CENTRE:
  555.                     (*t->put_text)(map_x(xtemp)-
  556.                         (t->h_char)*strlen(this_label->text)/2,
  557.                         map_y(ytemp), this_label->text);
  558.                     break;
  559.                 case RIGHT:
  560.                     (*t->put_text)(map_x(xtemp)-
  561.                         (t->h_char)*strlen(this_label->text),
  562.                         map_y(ytemp), this_label->text);
  563.                     break;
  564.             }
  565.          }
  566.      }
  567.  
  568. /* PLACE ARROWS */
  569.     (*t->linetype)(0);    /* arrow line type */
  570.     for (this_arrow = first_arrow; this_arrow!=NULL;
  571.         this_arrow = this_arrow->next ) {
  572.        int sx = map_x(LogScale(this_arrow->sx, is_log_x, log_base_log_x, "arrow", "x"));
  573.        int sy = map_y(LogScale(this_arrow->sy, is_log_y, log_base_log_y, "arrow", "y"));
  574.        int ex = map_x(LogScale(this_arrow->ex, is_log_x, log_base_log_x, "arrow", "x"));
  575.        int ey = map_y(LogScale(this_arrow->ey, is_log_y, log_base_log_y, "arrow", "y"));
  576.        
  577.        (*t->arrow)(sx, sy, ex, ey, this_arrow->head);
  578.     }
  579.  
  580.  
  581. /* DRAW CURVES */
  582.     if (key == -1) {
  583.         xl = xright  - (t->h_tic) - (t->h_char)*5;
  584.         yl = ytop - (t->v_tic) - (t->v_char);
  585.     }
  586.     if (key == 1) {
  587.         xl = map_x( LogScale(key_x, is_log_x, log_base_log_x, "key", "x") );
  588.         yl = map_y( LogScale(key_y, is_log_y, log_base_log_y, "key", "y") );
  589.     }
  590.  
  591.     this_plot = plots;
  592.     for (curve = 0; curve < pcount; this_plot = this_plot->next_cp, curve++) {
  593.         int oldkey = key;
  594.  
  595.         if (this_plot->title && !*this_plot->title) {
  596.             key = 0;
  597.         } else {
  598.         (*t->linetype)(this_plot->line_type);
  599.         if (key != 0 && this_plot->title) {
  600.             if ((*t->justify_text)(RIGHT)) {
  601.                 (*t->put_text)(xl,
  602.                     yl,this_plot->title);
  603.             }
  604.             else {
  605.                 if (inrange(xl-(t->h_char)*strlen(this_plot->title), 
  606.                          xleft, xright))
  607.                  (*t->put_text)(xl-(t->h_char)*strlen(this_plot->title),
  608.                              yl,this_plot->title);
  609.             }
  610.         }
  611.         }
  612.  
  613.         switch(this_plot->plot_style) {
  614.             case IMPULSES: {
  615.                if (key != 0 && this_plot->title) {
  616.                   (*t->move)(xl+(t->h_char),yl);
  617.                   (*t->vector)(xl+4*(t->h_char),yl);
  618.                }
  619.                plot_impulses(this_plot, yaxis_x, xaxis_y);
  620.                break;
  621.             }
  622.             case LINES: {
  623.                if (key != 0 && this_plot->title) {
  624.                   (*t->move)(xl+(int)(t->h_char),yl);
  625.                   (*t->vector)(xl+(int)(4*(t->h_char)),yl);
  626.                }
  627.                plot_lines(this_plot);
  628.                break;
  629.             }
  630. /* JG */        case STEPS: {
  631.                if (key != 0 && this_plot->title) {
  632.                   (*t->move)(xl+(int)(t->h_char),yl);
  633.                   (*t->vector)(xl+(int)(4*(t->h_char)),yl);
  634.                }
  635.                plot_steps(this_plot);
  636.                break;
  637.             }
  638.             case POINTSTYLE: {
  639.                if (key != 0 && this_plot->title) {
  640.                   (*t->point)(xl+2*(t->h_char),yl,
  641.                             this_plot->point_type);
  642.                }
  643.                plot_points(this_plot);
  644.                break;
  645.             }
  646.             case LINESPOINTS: {
  647.                /* put lines */
  648.                if (key != 0 && this_plot->title) {
  649.                   (*t->move)(xl+(t->h_char),yl);
  650.                   (*t->vector)(xl+4*(t->h_char),yl);
  651.                }
  652.                plot_lines(this_plot);
  653.  
  654.                /* put points */
  655.                if (key != 0 && this_plot->title) {
  656.                   (*t->point)(xl+2*(t->h_char),yl,
  657.                             this_plot->point_type);
  658.                }
  659.                plot_points(this_plot);
  660.                break;
  661.             }
  662.             case DOTS: {
  663.                if (key != 0 && this_plot->title) {
  664.                   (*t->point)(xl+2*(t->h_char),yl, -1);
  665.                }
  666.                plot_dots(this_plot);
  667.                break;
  668.             }
  669.             case ERRORBARS: {
  670.                if (key != 0 && this_plot->title) {
  671.                   (*t->point)(xl+2*(t->h_char),yl,
  672.                             this_plot->point_type);
  673.                }
  674.                plot_points(this_plot);
  675.  
  676.                /* for functions, just like POINTSTYLE */
  677.                if (this_plot->plot_type == DATA) {
  678.                   if (key != 0 && this_plot->title) {
  679.                      (*t->move)(xl+(t->h_char),yl);
  680.                      (*t->vector)(xl+4*(t->h_char),yl);
  681.                      (*t->move)(xl+(t->h_char),yl+ERRORBARTIC);
  682.                      (*t->vector)(xl+(t->h_char),yl-ERRORBARTIC);
  683.                      (*t->move)(xl+4*(t->h_char),yl+ERRORBARTIC);
  684.                      (*t->vector)(xl+4*(t->h_char),yl-ERRORBARTIC);
  685.                   }
  686.                   plot_bars(this_plot);
  687.                }
  688.                break;
  689.             }
  690.             case BOXERROR: {
  691.                if (this_plot->plot_type == DATA) {
  692.                   if (key != 0 && this_plot->title) {
  693.                      (*t->move)(xl+(t->h_char),yl+ERRORBARTIC);
  694.                      (*t->vector)(xl+(t->h_char),yl-ERRORBARTIC);
  695.                      (*t->move)(xl+4*(t->h_char),yl+ERRORBARTIC);
  696.                      (*t->vector)(xl+4*(t->h_char),yl-ERRORBARTIC);
  697.                   }
  698.                   plot_bars(this_plot);
  699.                }
  700.             }
  701.             /* no break */
  702.             case BOXES: {
  703.                if (key != 0 && this_plot->title) {
  704.                   (*t->move)(xl+(t->h_char),yl);
  705.                   (*t->vector)(xl+4*(t->h_char),yl);
  706.                }
  707.                plot_boxes(this_plot,xaxis_y);
  708.                break;
  709.             }
  710.  
  711.         }
  712.         if (key && this_plot->title) {
  713.             yl = yl - (t->v_char);
  714.         }
  715.         key = oldkey;
  716.     }
  717.     (*t->text)();
  718.     (void) fflush(outfile);
  719. }
  720.  
  721. /* plot_impulses:
  722.  * Plot the curves in IMPULSES style
  723.  */
  724. void
  725. plot_impulses(plot, yaxis_x, xaxis_y)
  726.     struct curve_points *plot;
  727.     int yaxis_x, xaxis_y;
  728. {
  729.     int i;
  730.     int x,y;
  731.     struct termentry *t = &term_tbl[term];
  732.  
  733.     for (i = 0; i < plot->p_count; i++) {
  734.        switch (plot->points[i].type) {
  735.           case INRANGE: {
  736.              x = map_x(plot->points[i].x);
  737.              y = map_y(plot->points[i].y);
  738.              break;
  739.           }
  740.           case OUTRANGE: {
  741.              if (!inrange(plot->points[i].x, x_min,x_max))
  742.                continue;
  743.              x = map_x(plot->points[i].x);
  744.              if ((y_min < y_max 
  745.                  && plot->points[i].y < y_min)
  746.                 || (y_max < y_min 
  747.                     && plot->points[i].y > y_min))
  748.                y = map_y(y_min);
  749.              if ((y_min < y_max 
  750.                  && plot->points[i].y > y_max)
  751.                 || (y_max<y_min 
  752.                     && plot->points[i].y < y_max))
  753.                y = map_y(y_max);
  754.              break;
  755.           }
  756.           default:        /* just a safety */
  757.           case UNDEFINED: {
  758.              continue;
  759.           }
  760.        }
  761.                     
  762.        if (polar)
  763.           (*t->move)(yaxis_x,xaxis_y);
  764.        else
  765.           (*t->move)(x,xaxis_y);
  766.        (*t->vector)(x,y);
  767.     }
  768.  
  769. }
  770.  
  771. /* plot_lines:
  772.  * Plot the curves in LINES style
  773.  */
  774. void
  775. plot_lines(plot)
  776.     struct curve_points *plot;
  777. {
  778.     int i;                /* point index */
  779.     int x,y;                /* point in terminal coordinates */
  780.     struct termentry *t = &term_tbl[term];
  781.     enum coord_type prev = UNDEFINED; /* type of previous point */
  782.     double ex, ey;            /* an edge point */
  783.     double lx[2], ly[2];        /* two edge points */
  784.  
  785.     for (i = 0; i < plot->p_count; i++) {
  786.        switch (plot->points[i].type) {
  787.           case INRANGE: {
  788.              x = map_x(plot->points[i].x);
  789.              y = map_y(plot->points[i].y);
  790.  
  791.              if (prev == INRANGE) {
  792.                 (*t->vector)(x,y);
  793.              } else if (prev == OUTRANGE) {
  794.                 /* from outrange to inrange */
  795.                 if (!clip_lines1) {
  796.                     (*t->move)(x,y);
  797.                 } else {
  798.                     edge_intersect(plot->points, i, &ex, &ey);
  799.                     (*t->move)(map_x(ex), map_y(ey));
  800.                     (*t->vector)(x,y);
  801.                 }
  802.              } else {        /* prev == UNDEFINED */
  803.                 (*t->move)(x,y);
  804.                 (*t->vector)(x,y);
  805.              }
  806.                     
  807.              break;
  808.           }
  809.           case OUTRANGE: {
  810.              if (prev == INRANGE) {
  811.                 /* from inrange to outrange */
  812.                 if (clip_lines1) {
  813.                     edge_intersect(plot->points, i, &ex, &ey);
  814.                     (*t->vector)(map_x(ex), map_y(ey));
  815.                 }
  816.              } else if (prev == OUTRANGE) {
  817.                 /* from outrange to outrange */
  818.                 if (clip_lines2) {
  819.                     if (two_edge_intersect(plot->points, i, lx, ly)) {
  820.                        (*t->move)(map_x(lx[0]), map_y(ly[0]));
  821.                        (*t->vector)(map_x(lx[1]), map_y(ly[1]));
  822.                     }
  823.                 }
  824.              }
  825.              break;
  826.           }
  827.           default:        /* just a safety */
  828.           case UNDEFINED: {
  829.              break;
  830.           }
  831.        }
  832.        prev = plot->points[i].type;
  833.     }
  834. }
  835.  
  836. /* XXX - JG  */
  837. /* plot_steps:                
  838.  * Plot the curves in STEPS style
  839.  */
  840. void
  841. plot_steps(plot)
  842. struct curve_points *plot;
  843. {
  844.     int i;                /* point index */
  845.     int x,y;                /* point in terminal coordinates */
  846.     struct termentry *t = &term_tbl[term];
  847.     enum coord_type prev = UNDEFINED;    /* type of previous point */
  848.     double ex, ey;            /* an edge point */
  849.     double lx[2], ly[2];        /* two edge points */
  850.     int xprev, yprev;            /* previous point coordinates */
  851.  
  852.     for (i = 0; i < plot->p_count; i++) {
  853.        switch (plot->points[i].type) {
  854.           case INRANGE: {
  855.              x = map_x(plot->points[i].x);
  856.              y = map_y(plot->points[i].y);
  857.  
  858.              if (prev == INRANGE) {
  859.                 (*t->vector)(x,yprev);
  860.                 (*t->vector)(x,y);
  861.              } else if (prev == OUTRANGE) {
  862.                 /* from outrange to inrange */
  863.                 if (!clip_lines1) {
  864.                     (*t->move)(x,y);
  865.                 } else {        /* find edge intersection */
  866.                     edge_intersect_steps(plot->points, i, &ex, &ey);
  867.                     (*t->move)(map_x(ex), map_y(ey));
  868.                     (*t->vector)(x,map_y(ey));
  869.                     (*t->vector)(x,y);
  870.                 }
  871.              } else {        /* prev == UNDEFINED */
  872.                 (*t->move)(x,y);
  873.                 (*t->vector)(x,y);
  874.              }
  875.              break;
  876.           }
  877.           case OUTRANGE: {
  878.              if (prev == INRANGE) {
  879.                 /* from inrange to outrange */
  880.                 if (clip_lines1) {
  881.                     edge_intersect_steps(plot->points, i, &ex, &ey);
  882.                     (*t->vector)(map_x(ex), yprev);
  883.                     (*t->vector)(map_x(ex), map_y(ey));
  884.                 }
  885.              } else if (prev == OUTRANGE) {
  886.                 /* from outrange to outrange */
  887.                 if (clip_lines2) {
  888.                     if (two_edge_intersect_steps(plot->points, i, lx, ly)) {
  889.                        (*t->move)(map_x(lx[0]), map_y(ly[0]));
  890.                        (*t->vector)(map_x(lx[1]), map_y(ly[0]));
  891.                        (*t->vector)(map_x(lx[1]), map_y(ly[1]));
  892.                     }
  893.                 }
  894.              }
  895.              break;
  896.           }
  897.           default:        /* just a safety */
  898.           case UNDEFINED: {
  899.              break;
  900.           }
  901.        }
  902.        prev  = plot->points[i].type;
  903.        xprev = x;
  904.        yprev = y;
  905.     }
  906. }
  907.  
  908. /* plot_bars:
  909.  * Plot the curves in ERRORBARS style
  910.  *  we just plot the bars; the points are plotted in plot_points
  911.  */
  912. void
  913. plot_bars(plot)
  914.     struct curve_points *plot;
  915. {
  916.     int i;                /* point index */
  917.     struct termentry *t = &term_tbl[term];
  918.     double x;                /* position of the bar */
  919.     double ylow, yhigh;        /* the ends of the bars */
  920.     unsigned int xM, ylowM, yhighM; /* the mapped version of above */
  921.     TBOOLEAN low_inrange, high_inrange;
  922.     int tic = ERRORBARTIC;
  923.     
  924.     for (i = 0; i < plot->p_count; i++) {
  925.        /* undefined points don't count */
  926.        if (plot->points[i].type == UNDEFINED)
  927.         continue;
  928.  
  929.        /* check to see if in xrange */
  930.        x = plot->points[i].x;
  931.        if (! inrange(x, x_min, x_max))
  932.         continue;
  933.        xM = map_x(x);
  934.  
  935.        /* find low and high points of bar, and check yrange */
  936.        yhigh = plot->points[i].yhigh;
  937.        ylow = plot->points[i].ylow;
  938.  
  939.        high_inrange = inrange(yhigh, y_min,y_max);
  940.        low_inrange = inrange(ylow, y_min,y_max);
  941.  
  942.        /* compute the plot position of yhigh */
  943.        if (high_inrange)
  944.         yhighM = map_y(yhigh);
  945.        else if (samesign(yhigh-y_max, y_max-y_min))
  946.         yhighM = map_y(y_max);
  947.        else
  948.         yhighM = map_y(y_min);
  949.        
  950.        /* compute the plot position of ylow */
  951.        if (low_inrange)
  952.         ylowM = map_y(ylow);
  953.        else if (samesign(ylow-y_max, y_max-y_min))
  954.         ylowM = map_y(y_max);
  955.        else
  956.         ylowM = map_y(y_min);
  957.  
  958.        if (!high_inrange && !low_inrange && ylowM == yhighM)
  959.         /* both out of range on the same side */
  960.           continue;
  961.  
  962.        /* by here everything has been mapped */
  963.        (*t->move)(xM, ylowM);
  964.        (*t->vector)(xM, yhighM); /* draw the main bar */
  965.        (*t->move)(xM-tic, ylowM); /* draw the bottom tic */
  966.        (*t->vector)(xM+tic, ylowM);
  967.        (*t->move)(xM-tic, yhighM); /* draw the top tic */
  968.        (*t->vector)(xM+tic, yhighM);
  969.     }
  970. }
  971.  
  972. /* plot_boxes:
  973.  * Plot the curves in BOXES style
  974.  */
  975. void
  976. plot_boxes(plot,xaxis_y)
  977.     struct curve_points *plot;
  978.     int xaxis_y;
  979. {
  980.     int i;                /* point index */
  981.     int xl,xr,yt;            /* point in terminal coordinates */
  982.     double dxl,dxr,dyt;
  983.     struct termentry *t = &term_tbl[term];
  984.     enum coord_type prev = UNDEFINED; /* type of previous point */
  985.  
  986.     for (i = 0; i < plot->p_count; i++) {
  987.        switch (plot->points[i].type) {
  988.           case OUTRANGE:
  989.           case INRANGE: {
  990.             if (plot->points[i].z<0.0) {
  991.                if (boxwidth<0.0) {
  992.                     /* calculate width */
  993.                     if (prev!=UNDEFINED)
  994.                         dxl = (plot->points[i-1].x - plot->points[i].x)/2.0;
  995.                     else
  996.                         dxl = 0.0;
  997.                     if (i < plot->p_count-1) {
  998.                         if (plot->points[i+1].type!=UNDEFINED)
  999.                             dxr = (plot->points[i+1].x - plot->points[i].x)/2.0;
  1000.                         else
  1001.                             dxr = -dxl;
  1002.                     }
  1003.                     else {
  1004.                         dxr = -dxl;
  1005.                     }
  1006.                     if (prev==UNDEFINED)
  1007.                         dxl = -dxr;
  1008.                 }
  1009.                 else {
  1010.                     dxr = boxwidth/2.0;
  1011.                     dxl = -dxr;
  1012.                 }
  1013.             }
  1014.             else {
  1015.                 dxr = plot->points[i].z/2.0;
  1016.                 dxl = -dxr;
  1017.             }
  1018.  
  1019.             dxl= plot->points[i].x+dxl;
  1020.             dxr= plot->points[i].x+dxr;
  1021.             dyt= plot->points[i].y;
  1022.  
  1023.             /* clip to border */
  1024.             if ((y_min < y_max  && dyt < y_min)
  1025.                 || (y_max < y_min  && dyt > y_min))
  1026.                dyt = y_min;
  1027.             if ((y_min < y_max  && dyt > y_max)
  1028.                 || (y_max<y_min  && dyt < y_max))
  1029.                dyt = y_max;
  1030.             if ((x_min < x_max  && dxr < x_min)
  1031.                 || (x_max < x_min  && dxr > x_min))
  1032.                dxr = x_min;
  1033.             if ((x_min < x_max  && dxr > x_max)
  1034.                 || (x_max<x_min  && dxr < x_max))
  1035.                dxr = x_max;
  1036.             if ((x_min < x_max  && dxl < x_min)
  1037.                 || (x_max < x_min  && dxl > x_min))
  1038.                dxl = x_min;
  1039.             if ((x_min < x_max  && dxl > x_max)
  1040.                 || (x_max<x_min  && dxl < x_max))
  1041.                dxl = x_max;
  1042.  
  1043.             xl= map_x(dxl);
  1044.             xr= map_x(dxr);
  1045.             yt = map_y(dyt);
  1046.  
  1047.             (*t->move)(xl,xaxis_y);
  1048.             (*t->vector)(xl,yt);
  1049.             (*t->vector)(xr,yt);
  1050.             (*t->vector)(xr,xaxis_y);
  1051.             (*t->vector)(xl,xaxis_y);
  1052.             break;
  1053.           }
  1054.           default:        /* just a safety */
  1055.           case UNDEFINED: {
  1056.              break;
  1057.           }
  1058.        }
  1059.        prev = plot->points[i].type;
  1060.     }
  1061. }
  1062.  
  1063. /* plot_points:
  1064.  * Plot the curves in POINTSTYLE style
  1065.  */
  1066. void
  1067. plot_points(plot)
  1068.     struct curve_points *plot;
  1069. {
  1070.     int i;
  1071.     int x,y;
  1072.     struct termentry *t = &term_tbl[term];
  1073.  
  1074.     for (i = 0; i < plot->p_count; i++) {
  1075.        if (plot->points[i].type == INRANGE) {
  1076.           x = map_x(plot->points[i].x);
  1077.           y = map_y(plot->points[i].y);
  1078.           /* do clipping if necessary */
  1079.           if (!clip_points ||
  1080.              (   x >= xleft + t->h_tic  && y >= ybot + t->v_tic 
  1081.               && x <= xright - t->h_tic && y <= ytop - t->v_tic))
  1082.             (*t->point)(x,y, plot->point_type);
  1083.        }
  1084.     }
  1085. }
  1086.  
  1087. /* plot_dots:
  1088.  * Plot the curves in DOTS style
  1089.  */
  1090. void
  1091. plot_dots(plot)
  1092.     struct curve_points *plot;
  1093. {
  1094.     int i;
  1095.     int x,y;
  1096.     struct termentry *t = &term_tbl[term];
  1097.  
  1098.     for (i = 0; i < plot->p_count; i++) {
  1099.        if (plot->points[i].type == INRANGE) {
  1100.           x = map_x(plot->points[i].x);
  1101.           y = map_y(plot->points[i].y);
  1102.           /* point type -1 is a dot */
  1103.           (*t->point)(x,y, -1);
  1104.        }
  1105.     }
  1106. }
  1107.  
  1108. /* single edge intersection algorithm */
  1109. /* Given two points, one inside and one outside the plot, return
  1110.  * the point where an edge of the plot intersects the line segment defined 
  1111.  * by the two points.
  1112.  */
  1113. void
  1114. edge_intersect(points, i, ex, ey)
  1115.     struct coordinate GPHUGE *points; /* the points array */
  1116.     int i;                /* line segment from point i-1 to point i */
  1117.     double *ex, *ey;        /* the point where it crosses an edge */
  1118. {
  1119.     /* global x_min, x_max, y_min, x_max */
  1120.     double ax = points[i-1].x;
  1121.     double ay = points[i-1].y;
  1122.     double bx = points[i].x;
  1123.     double by = points[i].y;
  1124.     double x, y;            /* possible intersection point */
  1125.  
  1126.     if (by == ay) {
  1127.        /* horizontal line */
  1128.        /* assume inrange(by, y_min, y_max) */
  1129.        *ey = by;        /* == ay */
  1130.  
  1131.        if (inrange(x_max, ax, bx))
  1132.         *ex = x_max;
  1133.        else if (inrange(x_min, ax, bx))
  1134.         *ex = x_min;
  1135.        else {
  1136.         (*term_tbl[term].text)();
  1137.         (void) fflush(outfile);
  1138.         int_error("error in edge_intersect", NO_CARET);
  1139.        }
  1140.        return;
  1141.     } else if (bx == ax) {
  1142.        /* vertical line */
  1143.        /* assume inrange(bx, x_min, x_max) */
  1144.        *ex = bx;        /* == ax */
  1145.  
  1146.        if (inrange(y_max, ay, by))
  1147.         *ey = y_max;
  1148.        else if (inrange(y_min, ay, by))
  1149.         *ey = y_min;
  1150.        else {
  1151.         (*term_tbl[term].text)();
  1152.         (void) fflush(outfile);
  1153.         int_error("error in edge_intersect", NO_CARET);
  1154.        }
  1155.        return;
  1156.     }
  1157.  
  1158.     /* slanted line of some kind */
  1159.  
  1160.     /* does it intersect y_min edge */
  1161.     if (inrange(y_min, ay, by) && y_min != ay && y_min != by) {
  1162.        x = ax + (y_min-ay) * ((bx-ax) / (by-ay));
  1163.        if (inrange(x, x_min, x_max)) {
  1164.           *ex = x;
  1165.           *ey = y_min;
  1166.           return;            /* yes */
  1167.        }
  1168.     }
  1169.     
  1170.     /* does it intersect y_max edge */
  1171.     if (inrange(y_max, ay, by) && y_max != ay && y_max != by) {
  1172.        x = ax + (y_max-ay) * ((bx-ax) / (by-ay));
  1173.        if (inrange(x, x_min, x_max)) {
  1174.           *ex = x;
  1175.           *ey = y_max;
  1176.           return;            /* yes */
  1177.        }
  1178.     }
  1179.  
  1180.     /* does it intersect x_min edge */
  1181.     if (inrange(x_min, ax, bx) && x_min != ax && x_min != bx) {
  1182.        y = ay + (x_min-ax) * ((by-ay) / (bx-ax));
  1183.        if (inrange(y, y_min, y_max)) {
  1184.           *ex = x_min;
  1185.           *ey = y;
  1186.           return;
  1187.        }
  1188.     }
  1189.  
  1190.     /* does it intersect x_max edge */
  1191.     if (inrange(x_max, ax, bx) && x_max != ax && x_max != bx) {
  1192.        y = ay + (x_max-ax) * ((by-ay) / (bx-ax));
  1193.        if (inrange(y, y_min, y_max)) {
  1194.           *ex = x_max;
  1195.           *ey = y;
  1196.           return;
  1197.        }
  1198.     }
  1199.  
  1200.     /* It is possible for one or two of the [ab][xy] values to be -VERYLARGE.
  1201.     * If ax=bx=-VERYLARGE or ay=by=-VERYLARGE we have already returned 
  1202.     * FALSE above. Otherwise we fall through all the tests above. 
  1203.     * If two are -VERYLARGE, it is ax=ay=-VERYLARGE or bx=by=-VERYLARGE 
  1204.     * since either a or b must be INRANGE. 
  1205.     * Note that for ax=ay=-VERYLARGE or bx=by=-VERYLARGE we can do nothing.
  1206.     * Handle them carefully here. As yet we have no way for them to be 
  1207.     * +VERYLARGE.
  1208.     */
  1209.     if (ax == -VERYLARGE) {
  1210.        if (ay != -VERYLARGE) {
  1211.           *ex = min(x_min, x_max);
  1212.           *ey = by;
  1213.           return;
  1214.        }
  1215.     } else if (bx == -VERYLARGE) {
  1216.        if (by != -VERYLARGE) {
  1217.           *ex = min(x_min, x_max);
  1218.           *ey = ay;
  1219.           return;
  1220.        }
  1221.     } else if (ay == -VERYLARGE) {
  1222.        /* note we know ax != -VERYLARGE */
  1223.        *ex = bx;
  1224.        *ey = min(y_min, y_max);
  1225.        return;
  1226.     } else if (by == -VERYLARGE) {
  1227.        /* note we know bx != -VERYLARGE */
  1228.        *ex = ax;
  1229.        *ey = min(y_min, y_max);
  1230.        return;
  1231.     }
  1232.  
  1233.     /* If we reach here, then either one point is (-VERYLARGE,-VERYLARGE), 
  1234.     * or the inrange point is on the edge, and
  1235.      * the line segment from the outrange point does not cross any 
  1236.     * other edges to get there. In either case, we return the inrange 
  1237.     * point as the 'edge' intersection point. This will basically draw
  1238.     * line.
  1239.     */
  1240.     if (points[i].type == INRANGE) {
  1241.        *ex = bx; 
  1242.        *ey = by;
  1243.     } else {
  1244.        *ex = ax; 
  1245.        *ey = ay;
  1246.     }
  1247.     return;
  1248. }
  1249.  
  1250. /* XXX - JG  */
  1251. /* single edge intersection algorithm for "steps" curves */
  1252. /* 
  1253.  * Given two points, one inside and one outside the plot, return
  1254.  * the point where an edge of the plot intersects the line segments
  1255.  * forming the step between the two points. 
  1256.  *
  1257.  * Recall that if P1 = (x1,y1) and P2 = (x2,y2), the step from  
  1258.  * P1 to P2 is drawn as two line segments: (x1,y1)->(x2,y1) and 
  1259.  * (x2,y1)->(x2,y2). 
  1260.  */
  1261. void
  1262. edge_intersect_steps(points, i, ex, ey)
  1263.     struct coordinate *points; /* the points array */
  1264.     int i;                /* line segment from point i-1 to point i */
  1265.     double *ex, *ey;        /* the point where it crosses an edge */
  1266. {
  1267.     /* global x_min, x_max, y_min, x_max */
  1268.     double ax = points[i-1].x;
  1269.     double ay = points[i-1].y;
  1270.     double bx = points[i].x;
  1271.     double by = points[i].y;
  1272.  
  1273.     if (points[i].type == INRANGE) {    /* from OUTRANGE to INRANG */
  1274.         if (inrange(ay,y_min,y_max)) {
  1275.         *ey = ay;
  1276.         if (ax > x_max)
  1277.             *ex = x_max;
  1278.         else            /* x < x_min */
  1279.             *ex = x_min;
  1280.         } else {
  1281.             *ex = bx;
  1282.         if (ay > y_max)     
  1283.             *ey = y_max;
  1284.         else            /* y < y_min */
  1285.             *ey = y_min;
  1286.         }
  1287.     } else {                /* from INRANGE to OUTRANGE */
  1288.         if (inrange(bx,x_min,x_max)) {
  1289.         *ex = bx;
  1290.         if (by > y_max)
  1291.             *ey = y_max;
  1292.         else            /* y < y_min */
  1293.             *ey = y_min;
  1294.         } else {
  1295.             *ey = ay;
  1296.         if (bx > x_max)     
  1297.             *ex = x_max;
  1298.         else            /* x < x_min */
  1299.             *ex = x_min;
  1300.         }
  1301.     }
  1302.     return;
  1303. }
  1304.  
  1305. /* XXX - JG  */
  1306. /* double edge intersection algorithm for "steps" plot */
  1307. /* Given two points, both outside the plot, return the points where an 
  1308.  * edge of the plot intersects the line segments forming a step 
  1309.  * by the two points. There may be zero, one, two, or an infinite number
  1310.  * of intersection points. (One means an intersection at a corner, infinite
  1311.  * means overlaying the edge itself). We return FALSE when there is nothing
  1312.  * to draw (zero intersections), and TRUE when there is something to 
  1313.  * draw (the one-point case is a degenerate of the two-point case and we do 
  1314.  * not distinguish it - we draw it anyway).
  1315.  *
  1316.  * Recall that if P1 = (x1,y1) and P2 = (x2,y2), the step from  
  1317.  * P1 to P2 is drawn as two line segments: (x1,y1)->(x2,y1) and 
  1318.  * (x2,y1)->(x2,y2). 
  1319.  */
  1320. TBOOLEAN                /* any intersection? */
  1321. two_edge_intersect_steps(points, i, lx, ly)
  1322.     struct coordinate *points; /* the points array */
  1323.     int i;                /* line segment from point i-1 to point i */
  1324.     double *lx, *ly;        /* lx[2], ly[2]: points where it crosses edges */
  1325. {
  1326.     /* global x_min, x_max, y_min, x_max */
  1327.     double ax = points[i-1].x;
  1328.     double ay = points[i-1].y;
  1329.     double bx = points[i].x;
  1330.     double by = points[i].y;
  1331.  
  1332.     if ( max(ax,bx) < x_min || min(ax,bx) > x_max || 
  1333.          max(ay,by) < y_min || min(ay,by) > y_max ||
  1334.          ( (ay  > y_max || ay < y_min)            &&
  1335.            (bx  > x_max || bx < x_min)  ) ) {
  1336.     return(FALSE);                
  1337.     } else if (inrange(ay,y_min,y_max) && inrange(bx,x_min,x_max)) {    /* corner of step inside plotspace */
  1338.         *ly++ = ay;
  1339.     if (ax < x_min) 
  1340.         *lx++ = x_min;
  1341.     else 
  1342.         *lx++ = x_max;
  1343.  
  1344.     *lx++ = bx;
  1345.     if (by < x_min) 
  1346.         *ly++ = y_min;
  1347.     else 
  1348.         *ly++ = y_max;
  1349.  
  1350.     return(TRUE);
  1351.     } else if (inrange(ay,y_min,y_max)) {    /* cross plotspace in x-direction */
  1352.     *lx++ = x_min;
  1353.     *ly++ = ay;
  1354.     *lx++ = x_max;
  1355.     *ly++ = ay;
  1356.     return(TRUE);
  1357.     } else if (inrange(ax,x_min,x_max)) {    /* cross plotspace in y-direction */
  1358.     *lx++ = bx;
  1359.     *ly++ = y_min;
  1360.     *lx++ = bx;
  1361.     *ly++ = y_max;
  1362.     return(TRUE);
  1363.     } else
  1364.     return(FALSE);
  1365. }
  1366.  
  1367. /* double edge intersection algorithm */
  1368. /* Given two points, both outside the plot, return
  1369.  * the points where an edge of the plot intersects the line segment defined 
  1370.  * by the two points. There may be zero, one, two, or an infinite number
  1371.  * of intersection points. (One means an intersection at a corner, infinite
  1372.  * means overlaying the edge itself). We return FALSE when there is nothing
  1373.  * to draw (zero intersections), and TRUE when there is something to 
  1374.  * draw (the one-point case is a degenerate of the two-point case and we do 
  1375.  * not distinguish it - we draw it anyway).
  1376.  */
  1377. TBOOLEAN                /* any intersection? */
  1378. two_edge_intersect(points, i, lx, ly)
  1379.     struct coordinate GPHUGE *points; /* the points array */
  1380.     int i;                /* line segment from point i-1 to point i */
  1381.     double *lx, *ly;        /* lx[2], ly[2]: points where it crosses edges */
  1382. {
  1383.     /* global x_min, x_max, y_min, x_max */
  1384.     double ax = points[i-1].x;
  1385.     double ay = points[i-1].y;
  1386.     double bx = points[i].x;
  1387.     double by = points[i].y;
  1388.     double x, y;            /* possible intersection point */
  1389.     TBOOLEAN intersect = FALSE;
  1390.  
  1391.     if (by == ay) {
  1392.        /* horizontal line */
  1393.        /* y coord must be in range, and line must span both x_min and x_max */
  1394.        /* note that spanning x_min implies spanning x_max */
  1395.        if (inrange(by, y_min, y_max) && inrange(x_min, ax, bx)) {
  1396.           *lx++ = x_min;
  1397.           *ly++ = by;
  1398.           *lx++ = x_max;
  1399.           *ly++ = by;
  1400.           return(TRUE);
  1401.        } else
  1402.         return(FALSE);
  1403.     } else if (bx == ax) {
  1404.        /* vertical line */
  1405.        /* x coord must be in range, and line must span both y_min and y_max */
  1406.        /* note that spanning y_min implies spanning y_max */
  1407.        if (inrange(bx, x_min, x_max) && inrange(y_min, ay, by)) {
  1408.           *lx++ = bx;
  1409.           *ly++ = y_min;
  1410.           *lx++ = bx;
  1411.           *ly++ = y_max;
  1412.           return(TRUE);
  1413.        } else
  1414.         return(FALSE);
  1415.     }
  1416.  
  1417.     /* slanted line of some kind */
  1418.     /* there can be only zero or two intersections below */
  1419.  
  1420.     /* does it intersect y_min edge */
  1421.     if (inrange(y_min, ay, by)) {
  1422.        x = ax + (y_min-ay) * ((bx-ax) / (by-ay));
  1423.        if (inrange(x, x_min, x_max)) {
  1424.           *lx++ = x;
  1425.           *ly++ = y_min;
  1426.           intersect = TRUE;
  1427.        }
  1428.     }
  1429.     
  1430.     /* does it intersect y_max edge */
  1431.     if (inrange(y_max, ay, by)) {
  1432.        x = ax + (y_max-ay) * ((bx-ax) / (by-ay));
  1433.        if (inrange(x, x_min, x_max)) {
  1434.           *lx++ = x;
  1435.           *ly++ = y_max;
  1436.           intersect = TRUE;
  1437.        }
  1438.     }
  1439.  
  1440.     /* does it intersect x_min edge */
  1441.     if (inrange(x_min, ax, bx)) {
  1442.        y = ay + (x_min-ax) * ((by-ay) / (bx-ax));
  1443.        if (inrange(y, y_min, y_max)) {
  1444.           *lx++ = x_min;
  1445.           *ly++ = y;
  1446.           intersect = TRUE;
  1447.        }
  1448.     }
  1449.  
  1450.     /* does it intersect x_max edge */
  1451.     if (inrange(x_max, ax, bx)) {
  1452.        y = ay + (x_max-ax) * ((by-ay) / (bx-ax));
  1453.        if (inrange(y, y_min, y_max)) {
  1454.           *lx++ = x_max;
  1455.           *ly++ = y;
  1456.           intersect = TRUE;
  1457.        }
  1458.     }
  1459.  
  1460.     if (intersect)
  1461.      return(TRUE);
  1462.  
  1463.     /* It is possible for one or more of the [ab][xy] values to be -VERYLARGE.
  1464.     * If ax=bx=-VERYLARGE or ay=by=-VERYLARGE we have already returned
  1465.     * FALSE above.
  1466.     * Note that for ax=ay=-VERYLARGE or bx=by=-VERYLARGE we can do nothing.
  1467.     * Otherwise we fall through all the tests above. 
  1468.     * Handle them carefully here. As yet we have no way for them to be +VERYLARGE.
  1469.     */
  1470.     if (ax == -VERYLARGE) {
  1471.        if (ay != -VERYLARGE
  1472.           && inrange(by, y_min, y_max) && inrange(x_max, ax, bx)) {
  1473.           *lx++ = x_min;
  1474.           *ly = by;
  1475.           *lx++ = x_max;
  1476.           *ly = by;
  1477.           intersect = TRUE;
  1478.        }
  1479.     } else if (bx == -VERYLARGE) {
  1480.        if (by != -VERYLARGE
  1481.           && inrange(ay, y_min, y_max) && inrange(x_max, ax, bx)) {
  1482.           *lx++ = x_min;
  1483.           *ly = ay;
  1484.           *lx++ = x_max;
  1485.           *ly = ay;
  1486.           intersect = TRUE;
  1487.        }
  1488.     } else if (ay == -VERYLARGE) {
  1489.        /* note we know ax != -VERYLARGE */
  1490.        if (inrange(bx, x_min, x_max) && inrange(y_max, ay, by)) {
  1491.           *lx++ = bx;
  1492.           *ly = y_min;
  1493.           *lx++ = bx;
  1494.           *ly = y_max;
  1495.           intersect = TRUE;
  1496.        }
  1497.     } else if (by == -VERYLARGE) {
  1498.        /* note we know bx != -VERYLARGE */
  1499.        if (inrange(ax, x_min, x_max) && inrange(y_max, ay, by)) {
  1500.           *lx++ = ax;
  1501.           *ly = y_min;
  1502.           *lx++ = ax;
  1503.           *ly = y_max;
  1504.           intersect = TRUE;
  1505.        }
  1506.     }
  1507.  
  1508.     return(intersect);
  1509. }
  1510.  
  1511. /* Polar transform of all curves */
  1512. /* Original code by John Campbell (CAMPBELL@NAUVAX.bitnet) */
  1513. polar_xform (plots, pcount)
  1514.     struct curve_points *plots;
  1515.     int pcount;            /* count of curves in plots array */
  1516. {
  1517.      struct curve_points *this_plot;
  1518.      int curve;            /* loop var, for curves */
  1519.      register int i, p_cnt;    /* loop/limit var, for points */
  1520.      struct coordinate GPHUGE *pnts;    /* abbrev. for points array */
  1521.     double x, y;            /* new cartesian value */
  1522.     TBOOLEAN anydefined = FALSE;
  1523.     double d2r;
  1524.  
  1525.     if(angles_format == ANGLES_DEGREES){
  1526.         d2r = DEG2RAD;
  1527.     } else {
  1528.         d2r = 1.0;
  1529.     }
  1530.  
  1531. /*
  1532.     Cycle through all the plots converting polar to rectangular.
  1533.      If autoscaling, adjust max and mins. Ignore previous values.
  1534.     If not autoscaling, use the yrange for both x and y ranges.
  1535. */
  1536.     if (autoscale_ly) {
  1537.         x_min = VERYLARGE;
  1538.         y_min = VERYLARGE;
  1539.         x_max = -VERYLARGE;
  1540.         y_max = -VERYLARGE;
  1541.         autoscale_lx = TRUE;
  1542.     } else {
  1543.         x_min = y_min;
  1544.         x_max = y_max;
  1545.     }
  1546.     
  1547.     this_plot = plots;
  1548.     for (curve = 0; curve < pcount; this_plot = this_plot->next_cp, curve++) {
  1549.         p_cnt = this_plot->p_count;
  1550.         pnts = &(this_plot->points[0]);
  1551.  
  1552.     /*    Convert to cartesian all points in this curve. */
  1553.         for (i = 0; i < p_cnt; i++) {
  1554.             if (pnts[i].type != UNDEFINED) {
  1555.                  anydefined = TRUE;
  1556.                  /* modify points to reset origin and from degrees */
  1557.                  pnts[i].y -= rmin;
  1558.                  pnts[i].x *= d2r;
  1559.                  /* convert to cartesian coordinates */
  1560.                 x = pnts[i].y*cos(pnts[i].x);
  1561.                 y = pnts[i].y*sin(pnts[i].x);
  1562.                 pnts[i].x = x;
  1563.                 pnts[i].y = y;
  1564.                 if (autoscale_ly) {
  1565.                     if (x_min > x) x_min = x;
  1566.                     if (x_max < x) x_max = x;
  1567.                     if (y_min > y) y_min = y;
  1568.                     if (y_max < y) y_max = y;
  1569.                     pnts[i].type = INRANGE;
  1570.                 } else if(inrange(x, x_min, x_max) && inrange(y, y_min, y_max))
  1571.                   pnts[i].type = INRANGE;
  1572.                 else
  1573.                   pnts[i].type = OUTRANGE;
  1574.             }
  1575.         }    
  1576.     }
  1577.  
  1578.     if (autoscale_lx && anydefined && fabs(x_max - x_min) < zero) {
  1579.         /* This happens at least for the plot of 1/cos(x) (vertical line). */
  1580.         fprintf(stderr, "Warning: empty x range [%g:%g], ", x_min,x_max);
  1581.         if (x_min == 0.0) {
  1582.            x_min = -1; 
  1583.            x_max = 1;
  1584.         } else {
  1585.            x_min *= 0.9;
  1586.            x_max *= 1.1;
  1587.         }
  1588.         fprintf(stderr, "adjusting to [%g:%g]\n", x_min,x_max);
  1589.     }
  1590.     if (autoscale_ly && anydefined && fabs(y_max - y_min) < zero) {
  1591.         /* This happens at least for the plot of 1/sin(x) (horiz. line). */
  1592.         fprintf(stderr, "Warning: empty y range [%g:%g], ", y_min, y_max);
  1593.         if (y_min == 0.0) {
  1594.            y_min = -1;
  1595.            y_max = 1;
  1596.         } else {
  1597.            y_min *= 0.9;
  1598.            y_max *= 1.1;
  1599.         }
  1600.         fprintf(stderr, "adjusting to [%g:%g]\n", y_min, y_max);
  1601.     }
  1602. }
  1603.  
  1604. /* DRAW_YTICS: draw a regular tic series, y axis */
  1605. draw_ytics(start, incr, end)
  1606.         double start, incr, end; /* tic series definition */
  1607.         /* assume start < end, incr > 0 */
  1608. {
  1609.     double ticplace;
  1610.     int ltic;            /* for mini log tics */
  1611.     double lticplace;    /* for mini log tics */
  1612.     double ticmin, ticmax;    /* for checking if tic is almost inrange */
  1613.  
  1614.     if (end == VERYLARGE)            /* for user-def series */
  1615.         end = max(y_min,y_max);
  1616.  
  1617.     /* limit to right side of plot */
  1618.     end = min(end, max(y_min,y_max));
  1619.  
  1620.     /* to allow for rounding errors */
  1621.     ticmin = min(y_min,y_max) - SIGNIF*incr;
  1622.     ticmax = max(y_min,y_max) + SIGNIF*incr;
  1623.     end = end + SIGNIF*incr; 
  1624.  
  1625.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  1626.         if ( inrange(ticplace,ticmin,ticmax) )
  1627.             ytick(ticplace, yformat, incr, 1.0);
  1628.         if (is_log_y && incr == 1.0) {
  1629.             /* add mini-ticks to log scale ticmarks */
  1630.             int lstart, linc;
  1631.             if ((end - start) >= 10)
  1632.             {
  1633.             lstart = 10; /* No little ticks */
  1634.             linc = 5;
  1635.             }
  1636.             else if((end - start) >= 5)
  1637.             {
  1638.             lstart = 2; /* 4 per decade */
  1639.             linc = 3;
  1640.             }
  1641.             else
  1642.             {
  1643.             lstart = 2; /* 9 per decade */
  1644.             linc = 1;
  1645.             }
  1646.             for (ltic = lstart; ltic < (int)base_log_y; ltic += linc) {
  1647.                 lticplace = ticplace+log((double)ltic)/log_base_log_y;
  1648.                 if ( inrange(lticplace,ticmin,ticmax) )
  1649.                     ytick(lticplace, "\0", incr, 0.5);
  1650.             }
  1651.         }
  1652.     }
  1653. }
  1654.  
  1655. /* DRAW_XTICS: draw a regular tic series, x axis */
  1656. draw_xtics(start, incr, end)
  1657.         double start, incr, end; /* tic series definition */
  1658.         /* assume start < end, incr > 0 */
  1659. {
  1660.     double ticplace;
  1661.     int ltic;            /* for mini log tics */
  1662.     double lticplace;    /* for mini log tics */
  1663.     double ticmin, ticmax;    /* for checking if tic is almost inrange */
  1664.  
  1665.     if (end == VERYLARGE)            /* for user-def series */
  1666.         end = max(x_min,x_max);
  1667.  
  1668.     /* limit to right side of plot */
  1669.     end = min(end, max(x_min,x_max));
  1670.  
  1671.     /* to allow for rounding errors */
  1672.     ticmin = min(x_min,x_max) - SIGNIF*incr;
  1673.     ticmax = max(x_min,x_max) + SIGNIF*incr;
  1674.     end = end + SIGNIF*incr; 
  1675.  
  1676.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  1677.         if ( inrange(ticplace,ticmin,ticmax) )
  1678.             if(!polar || ticplace == start || ticplace == end) 
  1679.                 xtick(ticplace, xformat, incr, 1.0);
  1680.         if (is_log_x && incr == 1.0) {
  1681.             /* add mini-ticks to log scale ticmarks */
  1682.             int lstart, linc;
  1683.             if ((end - start) >= 10)
  1684.             {
  1685.             lstart = 10; /* No little ticks */
  1686.             linc = 5;
  1687.             }
  1688.             else if((end - start) >= 5)
  1689.             {
  1690.             lstart = 2; /* 4 per decade */
  1691.             linc = 3;
  1692.             }
  1693.             else
  1694.             {
  1695.             lstart = 2; /* 9 per decade */
  1696.             linc = 1;
  1697.             }
  1698.             for (ltic = lstart; ltic < (int)base_log_x; ltic += linc) {
  1699.                 lticplace = ticplace+log((double)ltic)/log_base_log_x;
  1700.                 if ( inrange(lticplace,ticmin,ticmax) )
  1701.                     xtick(lticplace, "\0", incr, 0.5);
  1702.             }
  1703.         }
  1704.     }
  1705. }
  1706.  
  1707. /* DRAW_SERIES_YTICS: draw a user tic series, y axis */
  1708. draw_series_ytics(start, incr, end)
  1709.         double start, incr, end; /* tic series definition */
  1710.         /* assume start < end, incr > 0 */
  1711. {
  1712.     double ticplace, place;
  1713.     double ticmin, ticmax;    /* for checking if tic is almost inrange */
  1714.     double spacing = is_log_y ? log(incr)/log_base_log_y : incr;
  1715.  
  1716.     if (end == VERYLARGE)
  1717.         end = max(CheckLog(is_log_y, base_log_y, y_min),
  1718.               CheckLog(is_log_y, base_log_y, y_max));
  1719.     else
  1720.       /* limit to right side of plot */
  1721.       end = min(end, max(CheckLog(is_log_y, base_log_y, y_min),
  1722.                  CheckLog(is_log_y, base_log_y, y_max)));
  1723.  
  1724.     /* to allow for rounding errors */
  1725.     ticmin = min(y_min,y_max) - SIGNIF*incr;
  1726.     ticmax = max(y_min,y_max) + SIGNIF*incr;
  1727.     end = end + SIGNIF*incr; 
  1728.  
  1729.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  1730.         place = (is_log_y ? log(ticplace)/log_base_log_y : ticplace);
  1731.         if ( inrange(place,ticmin,ticmax) )
  1732.          ytick(place, yformat, spacing, 1.0);
  1733.     }
  1734. }
  1735.  
  1736.  
  1737. /* DRAW_SERIES_XTICS: draw a user tic series, x axis */
  1738. draw_series_xtics(start, incr, end)
  1739.         double start, incr, end; /* tic series definition */
  1740.         /* assume start < end, incr > 0 */
  1741. {
  1742.     double ticplace, place;
  1743.     double ticmin, ticmax;    /* for checking if tic is almost inrange */
  1744.     double spacing = is_log_x ? log(incr)/log_base_log_x : incr;
  1745.  
  1746.     if (end == VERYLARGE)
  1747.         end = max(CheckLog(is_log_x, base_log_x, x_min),
  1748.               CheckLog(is_log_x, base_log_x, x_max));
  1749.     else
  1750.       /* limit to right side of plot */
  1751.       end = min(end, max(CheckLog(is_log_x, base_log_x, x_min),
  1752.                  CheckLog(is_log_x, base_log_x, x_max)));
  1753.  
  1754.     /* to allow for rounding errors */
  1755.     ticmin = min(x_min,x_max) - SIGNIF*incr;
  1756.     ticmax = max(x_min,x_max) + SIGNIF*incr;
  1757.     end = end + SIGNIF*incr; 
  1758.  
  1759.     for (ticplace = start; ticplace <= end; ticplace +=incr) {
  1760.         place = (is_log_x ? log(ticplace)/log_base_log_x : ticplace);
  1761.         if ( inrange(place,ticmin,ticmax) )
  1762.          xtick(place, xformat, spacing, 1.0);
  1763.     }
  1764. }
  1765. char GPFAR * GPFAR month[]={
  1766.     "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
  1767. };
  1768. draw_month_ytics()
  1769.     long l_tickplace,l_incr,l_end,m_calc;
  1770.  
  1771.     l_tickplace = (long)y_min;
  1772.     if((double)l_tickplace<y_min)l_tickplace++;
  1773.     l_end=(double)y_max;
  1774.     l_incr=(l_end-l_tickplace+1)/12;
  1775.     if(l_incr<1)l_incr=1;
  1776.     while(l_tickplace<=l_end)
  1777.     {
  1778.     m_calc = (l_tickplace-1)%12;
  1779.     if(m_calc<0)m_calc += 12;
  1780.     ytick((double)l_tickplace,month[m_calc],(double)l_incr,1.0);
  1781.     l_tickplace += l_incr;
  1782.     }
  1783. }
  1784. draw_month_xtics()
  1785. {
  1786.     long l_tickplace,l_incr,l_end,m_calc;
  1787.  
  1788.     l_tickplace = (long)x_min;
  1789.     if((double)l_tickplace<x_min)l_tickplace++;
  1790.     l_end=(double)x_max;
  1791.     l_incr=(l_end-l_tickplace+1)/12;
  1792.     if(l_incr<1)l_incr=1;
  1793.     while(l_tickplace<=l_end)
  1794.     {
  1795.     m_calc = (l_tickplace-1)%12;
  1796.     if(m_calc<0)m_calc += 12;
  1797.     xtick((double)l_tickplace,month[m_calc],(double)l_incr,1.0);
  1798.     l_tickplace += l_incr;
  1799.     }
  1800. }
  1801. char *day[]={
  1802.     "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
  1803. };
  1804. draw_day_ytics()
  1805.     long l_tickplace,l_incr,l_end,m_calc;
  1806.  
  1807.     l_tickplace = (long)y_min;
  1808.     if((double)l_tickplace<y_min)l_tickplace++;
  1809.     l_end=(double)y_max;
  1810.     l_incr=(l_end-l_tickplace+1)/14;
  1811.     if(l_incr<1)l_incr=1;
  1812.     while(l_tickplace<=l_end)
  1813.     {
  1814.     m_calc = l_tickplace%7;
  1815.     if(m_calc<0)m_calc += 7;
  1816.     ytick((double)l_tickplace,day[m_calc],(double)l_incr,1.0);
  1817.     l_tickplace += l_incr;
  1818.     }
  1819. }
  1820. draw_day_xtics()
  1821.     long l_tickplace,l_incr,l_end,m_calc;
  1822.  
  1823.     l_tickplace = (long)x_min;
  1824.     if((double)l_tickplace<x_min)l_tickplace++;
  1825.     l_end=(double)x_max;
  1826.     l_incr=(l_end-l_tickplace+1)/14;
  1827.     if(l_incr<1)l_incr=1;
  1828.     while(l_tickplace<=l_end)
  1829.     {
  1830.     m_calc = l_tickplace%7;
  1831.     if(m_calc<0)m_calc += 7;
  1832.     xtick((double)l_tickplace,day[m_calc],(double)l_incr,1.0);
  1833.     l_tickplace += l_incr;
  1834.     }
  1835. }
  1836. /* DRAW_SET_YTICS: draw a user tic set, y axis */
  1837. draw_set_ytics(list)
  1838.     struct ticmark *list;    /* list of tic marks */
  1839. {
  1840.     double ticplace;
  1841.     double incr = (y_max - y_min) / 10;
  1842.     /* global x_min, x_max, xscale, y_min, y_max, yscale */
  1843.  
  1844.     while (list != NULL) {
  1845.        ticplace = (is_log_y ? log(list->position)/log_base_log_y
  1846.                 : list->position);
  1847.        if ( inrange(ticplace, y_min, y_max)         /* in range */
  1848.           || NearlyEqual(ticplace, y_min, incr)    /* == y_min */
  1849.           || NearlyEqual(ticplace, y_max, incr))    /* == y_max */
  1850.         ytick(ticplace, list->label, incr, 1.0);
  1851.  
  1852.        list = list->next;
  1853.     }
  1854. }
  1855.  
  1856. /* DRAW_SET_XTICS: draw a user tic set, x axis */
  1857. draw_set_xtics(list)
  1858.     struct ticmark *list;    /* list of tic marks */
  1859. {
  1860.     double ticplace;
  1861.     double incr = (x_max - x_min) / 10;
  1862.     /* global x_min, x_max, xscale, y_min, y_max, yscale */
  1863.  
  1864.     while (list != NULL) {
  1865.        ticplace = (is_log_x ? log(list->position)/log_base_log_x
  1866.                 : list->position);
  1867.        if ( inrange(ticplace, x_min, x_max)         /* in range */
  1868.           || NearlyEqual(ticplace, x_min, incr)    /* == x_min */
  1869.           || NearlyEqual(ticplace, x_max, incr))    /* == x_max */
  1870.         xtick(ticplace, list->label, incr, 1.0);
  1871.  
  1872.        list = list->next;
  1873.     }
  1874. }
  1875.  
  1876. /* draw and label a y-axis ticmark */
  1877. ytick(place, text, spacing, ticscale)
  1878.         double place;                   /* where on axis to put it */
  1879.         char *text;                     /* optional text label */
  1880.         double spacing;         /* something to use with checkzero */
  1881.         double ticscale;         /* scale factor for tic mark (0..1] */
  1882. {
  1883.     register struct termentry *t = &term_tbl[term];
  1884.     char ticlabel[101];
  1885.     int ticsize = (int)((t->h_tic) * ticscale);
  1886.  
  1887.     place = CheckZero(place,spacing); /* to fix rounding error near zero */
  1888.     if (grid) {
  1889.            (*t->linetype)(-1);  /* axis line type */
  1890.            /* do not put a rectangular grid on a polar plot */
  1891.        if( !polar){
  1892.          (*t->move)(xleft, map_y(place));
  1893.          (*t->vector)(xright, map_y(place));
  1894.            } else {   /* put a circular grid for polar -- not clipped! */
  1895.              int i;
  1896.         (*t->move)(map_x(ZERO), map_y(place));
  1897.         for( i=0; i <= 360; i++)
  1898.            (*t->vector)( map_x(place*sin( (double) DEG2RAD*i)),
  1899.             map_y(place*cos( (double) DEG2RAD*i)) );
  1900.         }
  1901.        (*t->linetype)(-2); /* border linetype */
  1902.     }
  1903.     if (tic_in) {
  1904.       /* if polar plot, put the tics along the axes */
  1905.       if( polar){
  1906.            (*t->move)(map_x(ZERO),map_y(place));
  1907.            (*t->vector)(map_x(ZERO) + ticsize, map_y(place));
  1908.            (*t->move)(map_x(ZERO), map_y(place));
  1909.            (*t->vector)(map_x(ZERO) - ticsize, map_y(place));
  1910.      } else {
  1911.        (*t->move)(xleft, map_y(place));
  1912.            (*t->vector)(xleft + ticsize, map_y(place));
  1913.            (*t->move)(xright, map_y(place));
  1914.            (*t->vector)(xright - ticsize, map_y(place));
  1915.      }
  1916.     } else {
  1917.       if( polar){
  1918.            (*t->move)(map_x(ZERO), map_y(place));
  1919.            (*t->vector)(map_x(ZERO) - ticsize, map_y(place));
  1920.      }else{
  1921.            (*t->move)(xleft, map_y(place));
  1922.            (*t->vector)(xleft - ticsize, map_y(place));
  1923.      }
  1924.     }
  1925.  
  1926.     /* label the ticmark */
  1927.     if (text == NULL) 
  1928.      text = yformat;
  1929.     
  1930.     if( polar){
  1931.       (void) sprintf(ticlabel, text,
  1932.         CheckLog(is_log_y, base_log_y, fabs( place)+rmin));
  1933.       if ((*t->justify_text)(RIGHT)) {
  1934.        (*t->put_text)(map_x(ZERO)-(t->h_char),
  1935.                    map_y(place), ticlabel);
  1936.      } else {
  1937.        (*t->put_text)(map_x(ZERO)-(t->h_char)*(strlen(ticlabel)+1),
  1938.                    map_y(place), ticlabel);
  1939.      }
  1940.     } else {
  1941.     
  1942.       (void) sprintf(ticlabel, text, CheckLog(is_log_y, base_log_y, place));
  1943.       if ((*t->justify_text)(RIGHT)) {
  1944.        (*t->put_text)(xleft-(t->h_char),
  1945.                    map_y(place), ticlabel);
  1946.      } else {
  1947.        (*t->put_text)(xleft-(t->h_char)*(strlen(ticlabel)+1),
  1948.                    map_y(place), ticlabel);
  1949.      }
  1950.     }
  1951. }
  1952.  
  1953. /* draw and label an x-axis ticmark */
  1954. xtick(place, text, spacing, ticscale)
  1955.         double place;                   /* where on axis to put it */
  1956.         char *text;                     /* optional text label */
  1957.         double spacing;         /* something to use with checkzero */
  1958.         double ticscale;         /* scale factor for tic mark (0..1] */
  1959. {
  1960.     register struct termentry *t = &term_tbl[term];
  1961.     char ticlabel[101];
  1962.     int ticsize = (int)((t->v_tic) * ticscale);
  1963.  
  1964.     place = CheckZero(place,spacing); /* to fix rounding error near zero */
  1965.     if (grid) {
  1966.            (*t->linetype)(-1);  /* axis line type */
  1967.            if( !polar){  /* do not place a rectangular grid */
  1968.          (*t->move)(map_x(place), ybot);
  1969.          (*t->vector)(map_x(place), ytop);
  1970.            } else { /* angular lines only for start and stop */
  1971.          int i;
  1972.          for( i=0; i < 360; i+=10){
  1973.          (*t->move)(map_x(ZERO),map_y(ZERO) );
  1974.          (*t->vector)(map_x(-place*cos((double) DEG2RAD*i)),
  1975.              map_y(-place*sin( (double)DEG2RAD*i)));
  1976.          if( i%90 == 0){
  1977.              (void) sprintf(ticlabel, "%d", i);
  1978.          (*t->put_text)(map_x(-1.05*place*cos((double) DEG2RAD*i))
  1979.              +(t->h_char)*strlen(ticlabel)/2,
  1980.              map_y(-1.05*place*sin( (double)DEG2RAD*i))
  1981.                  , ticlabel);
  1982.          }
  1983.          }
  1984.            }
  1985.        (*t->linetype)(-2); /* border linetype */
  1986.     }
  1987.     if (tic_in) {
  1988.       if( polar){
  1989.            (*t->move)(map_x(place), map_y(ZERO));
  1990.            (*t->vector)(map_x(place), map_y(ZERO) + ticsize);
  1991.            (*t->move)(map_x(place), map_y(ZERO));
  1992.            (*t->vector)(map_x(place), map_y(ZERO) - ticsize);
  1993.      } else{
  1994.            (*t->move)(map_x(place), ybot);
  1995.            (*t->vector)(map_x(place), ybot + ticsize);
  1996.            (*t->move)(map_x(place), ytop);
  1997.            (*t->vector)(map_x(place), ytop - ticsize);
  1998.      }
  1999.     } else {
  2000.       if( polar){
  2001.            (*t->move)(map_x(place), map_y(ZERO));
  2002.            (*t->vector)(map_x(place), map_y(ZERO) - ticsize);
  2003.      }else{
  2004.            (*t->move)(map_x(place), ybot);
  2005.            (*t->vector)(map_x(place), ybot - ticsize);
  2006.      }
  2007.     }
  2008.     
  2009.     /* label the ticmark */
  2010.     if (text == NULL)
  2011.      text = xformat;
  2012.  
  2013.     if(polar){
  2014.       (void) sprintf(ticlabel, text, CheckLog(is_log_x, base_log_x, fabs(place)+rmin));
  2015.       if ((*t->justify_text)(CENTRE)) {
  2016.        (*t->put_text)(map_x(place),
  2017.                    map_y(ZERO)-(t->v_char), ticlabel);
  2018.      } else {
  2019.        (*t->put_text)(map_x(place)-(t->h_char)*strlen(ticlabel)/2,
  2020.                    map_y(ZERO)-(t->v_char), ticlabel);
  2021.      }
  2022.     }else{
  2023.  
  2024.       (void) sprintf(ticlabel, text, CheckLog(is_log_x, base_log_x, place));
  2025.       if ((*t->justify_text)(CENTRE)) {
  2026.        (*t->put_text)(map_x(place),
  2027.                    ybot-(t->v_char), ticlabel);
  2028.      } else {
  2029.        (*t->put_text)(map_x(place)-(t->h_char)*strlen(ticlabel)/2,
  2030.                    ybot-(t->v_char), ticlabel);
  2031.      }
  2032.     }
  2033. }
  2034.