home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Gnuplot / Source / graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  53.1 KB  |  2,008 lines

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