r314: ezplot fixes
[ctsim.git] / libctgraphics / ezplot.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **    EZPLOT                                    
5 **
6 **  This is part of the CTSim program
7 **  Copyright (C) 1983-2000 Kevin Rosenberg
8 **
9 **  $Id: ezplot.cpp,v 1.23 2000/12/26 21:13:15 kevin Exp $
10 **
11 **  This program is free software; you can redistribute it and/or modify
12 **  it under the terms of the GNU General Public License (version 2) as
13 **  published by the Free Software Foundation.
14 **
15 **  This program is distributed in the hope that it will be useful,
16 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 **  GNU General Public License for more details.
19 **
20 **  You should have received a copy of the GNU General Public License
21 **  along with this program; if not, write to the Free Software
22 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 ******************************************************************************/
24
25 #include "ezplot.h"
26 #include <algorithm>
27
28 #ifdef __GNUWIN32__
29 int snprintf (char *, size_t, const char*, ...);
30 #endif
31
32 // Defaults
33 static const double TICKRATIO = 0.4;    // ratio of minor to major tick lengths
34 static const int MAXNUMFMT = 15;        // maximum length of a numeric format 
35 static const int DEF_CURVE_CLR = C_RED;
36
37
38 EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n)
39 : x(NULL), y(NULL)
40 {
41   x = new double [n];
42   y = new double [n];
43   
44   int copyCount = n * sizeof(double);
45   memcpy (x, xData, copyCount);
46   memcpy (y, yData, copyCount);
47   
48   m_iPointCount = n;
49 }
50
51 EZPlotCurve::~EZPlotCurve ()
52 {
53   delete x;
54   delete y;
55 }
56
57
58 void 
59 EZPlot::addCurve (const double *y, int n)
60 {
61   double* x = new double [n];
62   
63   for (int i = 0; i < n; i++)
64     x[i] = i;
65   
66   addCurve (x, y, n);
67   delete x;
68 }
69
70
71 void 
72 EZPlot::addCurve (const float *y, int n)
73 {
74   double* yDouble = new double [n];
75   
76   for (int i = 0; i < n; i++)
77     yDouble[i] = y[i];
78   
79   addCurve (yDouble, n);
80   delete yDouble;
81 }
82
83
84 void
85 EZPlot::addCurve (const float x[], const double y[], int num)
86 {
87   double* dx = new double [num];
88   
89   for (int i = 0; i < num; i++)
90     dx[i] = x[i];
91   
92   addCurve (dx, y, num);
93   delete dx;
94 }
95
96 void
97 EZPlot::addCurve (const double x[], const float y[], int num)
98 {
99   double* dy = new double [num];
100   
101   for (int i = 0; i < num; i++)
102     dy[i] = y[i];
103   
104   addCurve (x, dy, num);
105   
106   delete dy;
107 }
108
109
110 void
111 EZPlot::addCurve (const double x[], const double y[], int num)
112 {
113   if (num < 1)
114     return;
115   
116   EZPlotCurve* pCurve = new EZPlotCurve (x, y, num);
117   m_vecCurves.push_back (pCurve);
118 }
119
120
121 EZPlot::~EZPlot ()
122 {
123   for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
124     delete *i;
125 }
126
127 void
128 EZPlot::clearCurves ()
129 {
130   for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)    
131     delete *i;
132   m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
133   initPlotSettings();
134 }
135
136
137 EZPlot::EZPlot (SGP& sgp)
138 : rSGP (sgp)
139 {
140   initPlotSettings();
141 }
142
143 void
144 EZPlot::initPlotSettings ()
145 {
146   charheight = rSGP.getCharHeight();
147   charwidth = rSGP.getCharWidth();
148 \r
149   m_iCurrentCurve = -1;\r
150
151   c_xlabel = "";
152   c_ylabel =  "";
153   c_title = "";
154   
155   o_xporigin = 0.0;
156   o_yporigin = 0.0;
157   o_xlength  = 1.0;
158   o_ylength  = 1.0;
159   
160   o_xaxis = LINEAR;
161   o_yaxis = LINEAR;
162   
163   o_grid = FALSE;
164   o_box = FALSE;
165   
166   o_xmajortick = 10;
167   o_ymajortick =  8;
168   o_xminortick =  4;
169   o_yminortick =  4;
170   
171   o_color = DEF_CURVE_CLR;
172   o_symfreq = 1;
173   o_symbol = -1;
174   o_linestyle = SGP::LS_SOLID;
175   
176   o_xtlabel = TRUE;
177   o_ytlabel = TRUE;
178   o_xticks = BELOW;
179   o_yticks = LEFT;
180   
181   o_legendbox = INSIDE;
182   o_tag = FALSE;
183   
184   s_xtitle   = FALSE;
185   s_ytitle   = FALSE;
186   s_xcross   = FALSE;
187   s_ycross   = FALSE;
188   s_lxfrac   = FALSE;
189   s_lyfrac   = FALSE;
190   s_xlegend  = FALSE;
191   s_ylegend  = FALSE;
192   s_textsize = FALSE;
193   
194   clr_axis   = C_BLACK;         // set fixed colors 
195   clr_title  = C_RED;
196   clr_label  = C_BLUE;
197   clr_legend = C_CYAN;
198   clr_number = C_GREEN;
199   clr_grid   = C_LTGRAY;
200 }
201
202 void\r
203 EZPlot::setColor (unsigned int iCurve, int iColor)\r
204 {\r
205   if (m_veciColor.size() <= iCurve) {\r
206     m_veciColor.resize ((m_iCurrentCurve + 1) * 2);\r
207     m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2);\r
208   }\r
209   m_veciColor [iCurve] = iColor;\r
210   m_vecbColorSet [iCurve] = true;\r
211 }\r
212 \r
213 void\r
214 EZPlot::setSymbol (unsigned int iCurve, int iSymbol)\r
215 {\r
216   if (m_veciSymbol.size() <= iCurve) {\r
217     m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2);\r
218     m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2);\r
219   }\r
220   m_veciSymbol [iCurve] = iSymbol;\r
221   m_vecbSymbolSet [iCurve] = true;\r
222 }\r
223 \r
224 void\r
225 EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq)\r
226 {\r
227   if (m_veciSymbolFreq.size() <= iCurve) {\r
228     m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2);\r
229     m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2);\r
230   }\r
231   m_veciSymbolFreq [iCurve] = iSymbolFreq;\r
232   m_vecbSymbolFreqSet [iCurve] = true;\r
233 }\r
234 \r
235 void\r
236 EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle)\r
237 {\r
238   if (m_veciLinestyle.size() <= iCurve) {\r
239     m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2);\r
240     m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2);\r
241   }\r
242   m_veciLinestyle [iCurve] = iLinestyle;\r
243   m_vecbLinestyleSet [iCurve] = true;\r
244 }\r
245 \r
246 void\r
247 EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend)\r
248 {\r
249   if (m_vecsLegend.size() <= iCurve) {\r
250     m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);\r
251     m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);\r
252   }\r
253   m_vecsLegend [iCurve] = strLegend;\r
254   m_vecbLegendSet [iCurve] = true;\r
255 }\r
256 \r
257 void\r
258 EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend)\r
259 {\r
260   if (m_vecsLegend.size() <= iCurve) {\r
261     m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);\r
262     m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);\r
263   }\r
264   m_vecsLegend [iCurve] = pszLegend;\r
265   m_vecbLegendSet [iCurve] = true;\r
266 }\r
267 \r
268 int\r
269 EZPlot::getColor (unsigned int iCurve) const\r
270 {\r
271   if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve])\r
272     return m_veciColor[iCurve];\r
273   else\r
274     return o_color;\r
275 }\r
276     \r
277 int\r
278 EZPlot::getSymbol (unsigned int iCurve) const\r
279 {\r
280   if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve])\r
281     return m_veciSymbol[iCurve];\r
282   else\r
283     return o_symbol;\r
284 }\r
285     \r
286 int\r
287 EZPlot::getSymbolFreq (unsigned int iCurve) const\r
288 {\r
289   if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve])\r
290     return m_veciSymbolFreq[iCurve];\r
291   else\r
292     return o_symfreq;\r
293 }\r
294     \r
295 int\r
296 EZPlot::getLinestyle (unsigned int iCurve) const\r
297 {\r
298   if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve])\r
299     return m_veciLinestyle[iCurve];\r
300   else\r
301     return o_linestyle;\r
302 }\r
303     \r
304 const std::string*\r
305 EZPlot::getLegend (unsigned int iCurve) const\r
306 {\r
307   if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve])\r
308     return &m_vecsLegend[iCurve];\r
309   else\r
310     return NULL;\r
311 }\r
312     \r
313 \r
314 /* NAME
315 *   plot                Plots all curves collected by addCurves ()
316 *
317 * SYNOPSIS
318 *   plot()
319 *
320 * DESCRIPTION
321 *   This routine plots the curves that have stored by addCurves().
322 *
323 * CALLS
324 *   drawAxes() & make_numfmt()
325 */
326
327 void
328 EZPlot::plot ()
329 {\r
330   if (m_vecCurves.size() <= 0)
331     return;
332   
333   rSGP.setWindow (0., 0., 1., 1.);
334   
335   if (s_textsize == TRUE)
336     rSGP.setTextPointSize (v_textsize);\r
337   charheight = rSGP.getCharHeight();
338   charwidth = rSGP.getCharWidth();
339  
340   const EZPlotCurve& firstCurve = *m_vecCurves[0];\r
341   double xmin = firstCurve.x[0];   // extent of curves in world coord
342   double xmax = xmin;       
343   double ymin = firstCurve.y[0];
344   double ymax = ymin; \r
345   unsigned int iCurve;
346   for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
347     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
348     
349     for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
350       if (pCurve->x[ip] > xmax)
351         xmax = pCurve->x[ip];
352       else if (pCurve->x[ip] < xmin)
353         xmin = pCurve->x[ip];
354       if (pCurve->y[ip] > ymax)
355         ymax = pCurve->y[ip];
356       else if (pCurve->y[ip] < ymin)
357         ymin = pCurve->y[ip];
358     }
359   }
360   
361   // extend graph limits for user defined axis cross positions 
362   if (s_xcross == TRUE) {
363     if (v_xcross < xmin)
364       xmin = v_xcross;
365     else if (v_xcross > xmax)
366       xmax = v_xcross;
367   }
368   
369   if (s_ycross == TRUE) {
370     if (v_ycross < ymin)
371       ymin = v_ycross;
372     else if (v_ycross > ymax)
373       ymax = v_ycross;
374   }
375   
376   // find nice endpoints for axes 
377   if (! axis_scale (xmin, xmax, o_xmajortick - 1, &xgw_min, &xgw_max, &x_nint) || ! axis_scale (ymin, ymax, o_ymajortick - 1, &ygw_min, &ygw_max, &y_nint))
378     return;
379   
380   // check if user set x-axis extents 
381   if (s_xmin == TRUE) {
382     xgw_min = v_xmin;
383     x_nint = o_xmajortick - 1;
384   }
385   if (s_xmax == TRUE) {
386     xgw_max = v_xmax;
387     x_nint = o_xmajortick - 1;
388   }
389   
390   // check if user set y-axis extents 
391   if (s_ymin == TRUE) {
392     ygw_min = v_ymin;
393     y_nint = o_ymajortick - 1;
394   }
395   if (s_ymax == TRUE) {
396     ygw_max = v_ymax;
397     y_nint = o_ymajortick - 1;
398   }
399   
400   // calculate increment between major axis in world coordinates 
401   xw_tickinc = (xgw_max - xgw_min) / x_nint;
402   yw_tickinc = (ygw_max - ygw_min) / y_nint;
403   
404   // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max 
405   
406   // set the number of decimal point to users' setting or default 
407   // Two formats for numbers: Fixed:    -nnn.f and  Exponent: -n.fffE+eee
408   if (s_lxfrac == TRUE)
409     x_frac = v_lxfrac;
410   else
411     x_frac = -1;
412   
413   if (s_lyfrac == TRUE)
414     y_frac = v_lyfrac;
415   else
416     y_frac = -1;
417   
418   make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
419   make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
420   
421   xtl_wid = x_fldwid * charwidth;               // calc size of tick labels 
422   ytl_wid = y_fldwid * charwidth;
423   tl_height = charheight;
424   
425   // calculate the extent of the plot frame 
426   xp_min = o_xporigin;
427   yp_min = o_yporigin;
428   xp_max = xp_min + o_xlength;
429   yp_max = yp_min + o_ylength;
430   
431   xp_min = clamp (xp_min, 0., 1.);
432   xp_max = clamp (xp_max, 0., 1.);
433   yp_min = clamp (yp_min, 0., 1.);
434   yp_max = clamp (yp_max, 0., 1.);
435   
436   xa_min = xp_min;              // extent of axes 
437   xa_max = xp_max;
438   ya_min = yp_min;
439   ya_max = yp_max;
440   
441   // adjust frame for title 
442   title_row = ya_max;;\r
443   if (c_title.length() > 0)
444     ya_max -= 2 * charheight;\r
445
446   // calculate legend box boundaries 
447   int max_leg = 0;                      // longest legend in characters 
448   int num_leg = 0;                      // number of legend titles \r
449   for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
450     const std::string* pstrLegend = getLegend (iCurve);\r
451     if (pstrLegend && pstrLegend->length() > 0) {\r
452       int nLegend = pstrLegend->length();
453       if (nLegend > 0) {
454         ++num_leg;
455         if (nLegend > max_leg)\r
456           nLegend = max_leg;\r
457       }
458     }
459   }
460   
461   if (num_leg > 0 && o_legendbox != NOLEGEND) {
462     double leg_width  = (max_leg + 2) * charwidth;      // size of legend box 
463     double leg_height = num_leg * 3 * charheight;
464     
465     if (s_xlegend == TRUE)
466       xl_max = v_xlegend;
467     else {
468       xl_max = xa_max;
469       if (o_legendbox == OUTSIDE)
470         xa_max -= (leg_width + 0.5 * charwidth);
471     }
472     xl_min = xl_max - leg_width;
473     
474     if (s_ylegend == TRUE)
475       yl_max = v_ylegend;
476     else
477       yl_max = ya_max;
478     
479     yl_min = yl_max - leg_height;
480     
481     rSGP.setColor (clr_legend);
482     rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
483     
484     int iLegend = 0;                    // current legend position \r
485     for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
486       const std::string* pstrLegend = getLegend (iCurve);
487       if (! pstrLegend || pstrLegend->length() == 0)
488         continue;
489       
490       double xmin = xl_min + 1.0 * charwidth;
491       double xmax = xl_max - 1.0 * charwidth;
492       double y = yl_max - (2.0 + iLegend * 3) * charheight;
493       
494       rSGP.moveAbs (xmin, y + 0.5 * charheight);\r
495       rSGP.drawText (pstrLegend->c_str());
496       rSGP.setColor (getColor (iCurve));\r
497       int iLS = getLinestyle (iCurve);
498       if (iLS != SGP::LS_NOLINE) {
499         rSGP.setLineStyle (iLS);
500         rSGP.moveAbs (xmin, y);
501         rSGP.lineAbs (xmax, y);
502       }\r
503       int iSymbol = getSymbol (iCurve);
504       if (iSymbol > 0) {
505         double xinc = (xmax - xmin) / (5 - 1);
506         rSGP.setLineStyle (SGP::LS_SOLID);
507         for (int j = 0; j < 5; j++) {
508           rSGP.moveAbs (xmin + j * xinc, y);
509           symbol(iSymbol, 0.5 * charwidth, 0.5 * charheight);
510         }
511       }
512       ++iLegend;        // move to next legend position 
513     }
514   }   // end legend printing 
515   
516   // calculate the extent of the axes 
517   
518   /*-------------------------*/
519   /* adjust frame for labels */
520   /*-------------------------*/
521   
522   // X-Label 
523   if (c_xlabel.length() > 0)
524     ya_min += 1.5 * charheight;
525   xlbl_row = xp_min;            // put x-label on bottom of plot frame 
526   
527   // Y-Label 
528   if (c_ylabel.length() > 0) {\r
529     rSGP.setTextAngle (HALFPI);\r
530     rSGP.setTextSize (1.5 * charheight);\r
531     double xExtent, yExtent;\r
532     rSGP.getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);\r
533     rSGP.setTextSize (charheight);\r
534     xa_min += xExtent;\r
535     rSGP.setTextAngle (0.0);\r
536   }
537   ylbl_col = xp_min;
538   
539   /*------------------------------*/
540   /* adjust frame for tick labels */
541   /*------------------------------*/
542   
543   // Calc offset of tick labels from axes 
544   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
545     xtl_ofs = 0.0;
546   else if (o_xticks == BELOW)
547     xtl_ofs = -1.5 * charheight;
548   else if (o_xticks == ABOVE)
549     xtl_ofs = 1.5 * charheight;
550   
551   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
552     ytl_ofs = 0.0;
553   else if (o_yticks == LEFT)
554     ytl_ofs = -(1 + y_fldwid) * charwidth;
555   else if (o_yticks == RIGHT)
556     ytl_ofs = 1.5 * charwidth;
557   
558   xt_min = xa_min;
559   yt_min = ya_min;
560   xt_max = xa_max;
561   yt_max = ya_max;
562   
563   // see if need to shrink axis extents and/or tick extents 
564   if (xtl_ofs != 0.0 && s_ycross == FALSE) {
565     if (o_xticks == BELOW) {
566       ya_min += 2.5 * charheight;
567       yt_min = ya_min;
568     } else if (o_xticks == ABOVE) {
569       ya_min += 0.0;
570       yt_min = ya_min + 2.5 * charheight;
571     }
572   } else   // noaxis, no t-labels, or user set cross 
573     yt_min = ya_min;
574   
575   if (ytl_ofs != 0.0 && s_xcross == FALSE) {
576     if (o_yticks == LEFT) {
577       xa_min += (1 + y_fldwid) * charwidth;
578       xt_min = xa_min;
579     } else if (o_yticks == RIGHT) {
580       xa_min += 0.0;
581       xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
582     }
583   } else
584     xt_min = xa_min;
585   
586   // decrease size of graph, if necessary, to accommadate space 
587   // between axis boundary and boundary of ticks 
588   double x_added_ticks = 0; // number of tick spaces added to axis 
589   double y_added_ticks = 0;
590   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
591     x_added_ticks = 0;
592   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
593     y_added_ticks = 0;
594   
595   if (o_grid == TRUE) {
596     if (x_added_ticks < 0)
597       x_added_ticks = 2;
598     if (y_added_ticks < 0)
599       y_added_ticks = 2;
600   }
601   
602   if (x_added_ticks < 0) {
603     if (o_yticks == LEFT || s_ycross)
604       x_added_ticks = 1;
605     else
606       x_added_ticks = 2;
607   }
608   
609   if (y_added_ticks < 0) {
610     if (o_xticks == BELOW || s_xcross)
611       y_added_ticks = 1;
612     else
613       y_added_ticks = 2;
614   }
615   
616   xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
617   yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
618   
619   xt_min += 0.5 * x_added_ticks * xn_tickinc;
620   xt_max -= 0.5 * x_added_ticks * xn_tickinc;
621   yt_min += 0.5 * y_added_ticks * yn_tickinc;
622   yt_max -= 0.5 * y_added_ticks * yn_tickinc;
623   
624   xgn_min = xt_min;
625   xgn_max = xt_max;
626   ygn_min = yt_min;
627   ygn_max = yt_max;
628   
629   //------------------------------------------------------------------------
630   
631   m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
632   m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
633   
634   // PLOT CURVES 
635   
636   rSGP.setLineStyle (SGP::LS_SOLID);
637   drawAxes();
638   
639   // size of symbol in NDC 
640   double symwidth = charwidth;
641   double symheight = charheight;
642   \r
643   double clipRect[4];\r
644   clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;\r
645 \r
646   for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
647     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
648     \r
649     rSGP.setColor (getColor (iCurve));\r
650     int iSym = getSymbol (iCurve);\r
651     int iLS = getLinestyle (iCurve);\r
652 \r
653     if (iLS != SGP::LS_NOLINE) {
654       rSGP.setLineStyle (iLS);
655       double x1 = convertWorldToNDC_X (pCurve->x[0]);
656       double y1 = convertWorldToNDC_Y (pCurve->y[0]);\r
657       for (int i = 1; i < pCurve->m_iPointCount; i++) {
658         double x2 = convertWorldToNDC_X (pCurve->x[i]);
659         double y2 = convertWorldToNDC_Y (pCurve->y[i]);\r
660         double x2Clip = x2;\r
661         double y2Clip = y2;\r
662         if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {\r
663           rSGP.moveAbs (x1, y1);\r
664           rSGP.lineAbs (x2Clip, y2Clip);\r
665         }\r
666         x1 = x2;\r
667         y1 = y2;\r
668       } 
669     }
670     if (iSym > 0) {\r
671       int iSymFreq = getSymbolFreq (iCurve);
672       rSGP.setLineStyle (SGP::LS_SOLID);
673       double x = convertWorldToNDC_X (pCurve->x[0]);
674       double y = convertWorldToNDC_Y (pCurve->y[0]);
675       rSGP.moveAbs (x, y);
676       symbol (iSym, symwidth, symheight);
677       for (int i = 1; i < pCurve->m_iPointCount; i++)
678         if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
679           x = convertWorldToNDC_X (pCurve->x[i]);
680           y = convertWorldToNDC_Y (pCurve->y[i]);\r
681           if (y >= ygn_min && y <= ygn_max) {
682             rSGP.moveAbs (x, y);
683             symbol (iSym, symwidth, symheight);\r
684           }
685         }
686     }
687   }
688   
689 }
690
691
692 /* NAME
693 *   drawAxes                    INTERNAL routine to draw axis & label them
694 *
695 * SYNOPSIS
696 *   drawAxes()
697 */
698
699
700 void 
701 EZPlot::drawAxes()
702 {
703   double xticklen = 0, yticklen = 0; // length of ticks in NDC 
704   double minorinc;              // increment between minor axes 
705   double xaxispos, yaxispos;    // crossing of axes
706   double x, y, x2, y2;
707   bool axis_near;       // TRUE if axis too close to print t-label 
708   int i, j;
709   char str[256];
710   char *numstr;
711   
712   rSGP.setTextSize (charheight);
713   rSGP.setTextColor (1, -1);
714   
715   if (o_xticks == ABOVE)
716     xticklen = charheight;
717   else if (o_xticks == BELOW)
718     xticklen = -charheight;
719   
720   if (o_yticks == RIGHT)
721     yticklen = charwidth;
722   else if (o_yticks == LEFT)
723     yticklen = -charwidth;
724   
725   if (c_title.length() > 0) {
726     double wText, hText;
727     rSGP.setTextSize (charheight * 2.0);
728     rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
729     rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
730     rSGP.setTextColor (clr_title, -1);
731     rSGP.drawText (c_title);
732     rSGP.setTextSize (charheight);
733   }
734   
735   if (o_grid == TRUE || o_box == TRUE) {
736     rSGP.setColor (clr_axis);
737     rSGP.moveAbs (xa_min, ya_min);
738     rSGP.lineAbs (xa_max, ya_min);
739     rSGP.lineAbs (xa_max, ya_max);
740     rSGP.lineAbs (xa_min, ya_max);
741     rSGP.lineAbs (xa_min, ya_min);
742   }
743   
744   // calculate position of axes 
745   
746   // x-axis 
747   if (s_ycross == TRUE) {       // convert users' world-coord 
748     xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC 
749     x = convertWorldToNDC_X (xgw_min);
750   } else
751     xaxispos = ya_min;
752   
753   // y-axis 
754   if (s_xcross == TRUE) {       // convert users' world-coord 
755     yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position 
756     y = convertWorldToNDC_Y (ygw_min);
757   } else
758     yaxispos = xa_min;
759   
760   /*-------------*/
761   /* draw x-axis */
762   /*-------------*/
763   
764   if (o_xaxis == LINEAR) {
765     // draw axis line 
766     
767     rSGP.setColor (clr_axis);
768     if (o_tag && !o_grid && !o_box && s_xcross) {
769       rSGP.moveAbs (xa_min, xaxispos - charheight);
770       rSGP.lineAbs (xa_min, xaxispos + charheight);
771     }
772     rSGP.moveAbs (xa_min, xaxispos);
773     rSGP.lineAbs (xa_max, xaxispos);
774     if (o_tag && !o_grid && !o_box) {
775       rSGP.moveAbs (xa_max, xaxispos - charheight);
776       rSGP.lineAbs (xa_max, xaxispos + charheight);
777     }
778     
779     if (o_grid == TRUE) {
780       rSGP.setColor (clr_grid);
781       for (i = 0; i <= x_nint; i++) {
782         rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
783         rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
784       }
785     }
786     rSGP.setTextSize (charheight * 1.5);
787     rSGP.setTextColor (clr_label, -1);\r
788     double wText, hText;\r
789     rSGP.getTextExtent (c_xlabel.c_str(), &wText, &hText);
790     rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row +  charheight * 1.5);\r
791     rSGP.drawText (c_xlabel);
792     rSGP.setTextSize (charheight);
793     minorinc = xn_tickinc / (o_xminortick + 1);
794     
795     for (i = 0; i <= x_nint; i++) {
796       x = xt_min + xn_tickinc * i;
797       rSGP.setColor (clr_axis);
798       rSGP.moveAbs (x, xaxispos);
799       rSGP.lineAbs (x, xaxispos + xticklen);
800       if (i != x_nint)
801         for (j = 1; j <= o_xminortick; j++) {
802           x2 = x + minorinc * j;
803           rSGP.moveAbs (x2, xaxispos);
804           rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
805         }
806         axis_near = FALSE;
807         if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
808           double xw = xgw_min + i * xw_tickinc;
809           double x = convertWorldToNDC_X (xw);
810           double d = x - yaxispos;
811           if (o_yticks == RIGHT && d >= 0  && d < 0.9 * xn_tickinc)
812             axis_near = TRUE;
813           if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
814             axis_near = TRUE;
815         }
816         
817         if (o_xtlabel == TRUE && axis_near == FALSE) {
818           snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
819           numstr = str_skip_head (str, " ");
820           rSGP.moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
821           rSGP.setTextColor (clr_number, -1);
822           rSGP.drawText (numstr);
823         }
824     }
825   }             // X - Axis 
826   
827   
828   /*--------*/
829   /* y-axis */
830   /*--------*/
831   
832   if (o_yaxis == LINEAR) {
833     
834     rSGP.setColor (clr_axis);
835     if (o_tag && !o_grid && !o_box && s_ycross) {
836       rSGP.moveAbs (yaxispos - charwidth, ya_min);
837       rSGP.lineAbs (yaxispos + charwidth, ya_min);
838     }
839     rSGP.moveAbs (yaxispos, ya_min);
840     rSGP.lineAbs (yaxispos, ya_max);
841     if (o_tag && !o_grid && !o_box) {
842       rSGP.moveAbs (yaxispos - charwidth, ya_max);
843       rSGP.lineAbs (yaxispos + charwidth, ya_max);
844     }
845     
846     if (o_grid == TRUE) {
847       rSGP.setColor (clr_grid);
848       for (i = 0; i <= y_nint; i++) {
849         y = yt_min + yn_tickinc * i;
850         rSGP.moveAbs (xa_max, y);
851         rSGP.lineAbs (xa_min, y);
852       }
853     }\r
854     rSGP.setTextAngle (HALFPI);
855     rSGP.setTextSize (1.5 * charheight);
856     rSGP.setTextColor (clr_label, -1);\r
857     double xExtent, yExtent;\r
858     rSGP.getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);\r
859     rSGP.moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);\r
860     rSGP.drawText (c_ylabel);\r
861     rSGP.setTextAngle (0.0);
862     rSGP.setTextSize (charheight);
863     minorinc = yn_tickinc / (o_yminortick + 1);
864     
865     for (i = 0; i <= y_nint; i++) {
866       y = yt_min + yn_tickinc * i;
867       rSGP.setColor (clr_axis);
868       rSGP.moveAbs (yaxispos, y);
869       rSGP.lineAbs (yaxispos + yticklen, y);
870       if (i != y_nint)
871         for (j = 1; j <= o_yminortick; j++) {
872           y2 = y + minorinc * j;
873           rSGP.moveAbs (yaxispos, y2);
874           rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
875         }
876         axis_near = FALSE;
877         if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
878           double yw = ygw_min + i * yw_tickinc;
879           double y = convertWorldToNDC_Y (yw);
880           double d = y - xaxispos;
881           if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
882             axis_near = TRUE;
883           if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
884             axis_near = TRUE;
885         }
886         if (o_ytlabel == TRUE && axis_near == FALSE) {
887           snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
888           rSGP.moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
889           rSGP.setTextColor (clr_number, -1);\r
890           rSGP.drawText (str);
891         }
892     }
893   }             // Y - Axis
894 }
895
896
897 void 
898 EZPlot::symbol (int sym, double symwidth, double symheight)
899 {
900   if (sym <= 0)
901     return;
902   
903   if (sym == SB_CROSS) {
904     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
905     rSGP.lineRel (symwidth, symheight);
906     rSGP.moveRel (-symwidth, 0.0);
907     rSGP.lineRel (symwidth, -symheight);
908     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
909   } else if (sym == SB_PLUS) {
910     rSGP.moveRel (-0.5 * symwidth, 0.0);
911     rSGP.lineRel (symwidth, 0.0);
912     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
913     rSGP.lineRel (0.0, symheight);
914     rSGP.moveRel (0.0, -0.5 * symheight);
915   } else if (sym == SB_BOX) {
916     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
917     rSGP.lineRel (symwidth, 0.0);
918     rSGP.lineRel (0.0, symheight);
919     rSGP.lineRel (-symwidth, 0.0);
920     rSGP.lineRel (0.0, -symheight);
921     rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
922   } else if (sym == SB_CIRCLE) {
923     rSGP.drawCircle (symwidth);
924   } else if (sym == SB_ERRORBAR) {
925     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
926     rSGP.lineRel (symwidth, 0.0);
927     rSGP.moveRel (-0.5 * symwidth, 0.0);
928     rSGP.lineRel (0.0, -symheight);
929     rSGP.moveRel (-0.5 * symwidth, 0.0);
930     rSGP.lineRel (symwidth, 0.0);
931     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
932   }
933 }
934
935
936
937 /* NAME
938 *    axis_scale                 calculates graph axis scaling
939 *
940 *  SYNOPSIS:
941 *    retval = axis_scale (min, max, nint, minp, maxp, nintp, 
942 *                          rec_total, rec_frac)
943 *
944 *    INPUT:
945 *       double min         Smallest value to plot
946 *       double max         Largest value to plot
947 *       int    nint        Number of intervals desired
948 *
949 *    OUTPUT:
950 *       int   retval       FALSE if illegal parameters, else TRUE
951 *       double *minp       Minimum graph value
952 *       double *maxp       Maximum graph value
953 *       int    *nintp      Number of intervals for graph
954 *      int    *rec_total  Recommended field width for printing out the number
955 *       int    *rec_frac   Recommended number of digits for print fraction
956 */
957
958 int 
959 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
960 {
961   if (min >= max || nint < 1) {
962     sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
963     return (FALSE);
964   }
965   
966   double eps = 0.025;
967   double a = fabs(min);
968   if (fabs(min) < fabs(max))
969     a = fabs(max);
970   double scale = pow (10.0, floor(log10(a)));
971 loop:
972   double mina = min / scale;
973   double maxa = max / scale;
974   double d = (maxa - mina) / nint;
975   double j = d * eps;
976   double e = floor (log10(d));
977   double f = d / pow (10.0, e);
978   double v = 10.0;
979   if (f < sqrt(2.0))
980     v = 1.0;
981   else if (f < sqrt (10.0))
982     v = 2.0;
983   else if (f < sqrt (50.0))
984     v = 5.0;
985   double wdt = v * pow (10.0, e);
986   double g = floor (mina / wdt);
987   if (fabs(g + 1 - mina / wdt) < j)
988     g = g + 1;
989 #undef TEST1\r
990 #ifdef TEST1
991   g++;
992 #endif
993   *minp = wdt * g;
994   double h = floor (maxa / wdt) + 1.0;
995   if (fabs(maxa / wdt + 1 - h) < j)
996     h = h - 1;
997 #ifdef TEST1
998   h--;
999 #endif
1000   *maxp = wdt * h;
1001   *nintp = static_cast<int>(h - g);
1002   if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1003     scale = scale * 10.0;
1004     goto loop;
1005   }
1006   
1007   *minp *= scale;
1008   *maxp *= scale;
1009   
1010   return (TRUE);
1011 }
1012
1013
1014 /* NAME
1015 *   make_numfmt         Make a numeric format string
1016 *
1017 * SYNOPSIS
1018 *   make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1019 *   char *fmtstr                Returned format string for printf()
1020 *   int  *fldwid                Returned field width
1021 *   int  *nfrac         If < 0, then calculate best number of
1022 *                               fraction places & return that value
1023 *                               If >= 0, then use that number of places
1024 *   double min                  Minimum value
1025 *   double max                  Maximum value
1026 *   int nint                    Number of intervals between min & max
1027 *
1028 * DESCRIPTION
1029 *   This  routine is written as an INTERNAL routine for EZPLOT
1030 */
1031
1032 static inline double 
1033 trunc (double x)
1034 {
1035   double integer;
1036   
1037   modf (x, &integer);
1038   
1039   return (integer);
1040 }
1041
1042 void 
1043 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1044 {
1045   int wid, frac, expon;
1046   
1047   double delta = (maxval - minval) / nint;
1048   double absmin = fabs(minval);
1049   double absmax = fabs(maxval);\r
1050   if (absmin > absmax)\r
1051     absmax = absmin;
1052   double logt = log10( absmax );
1053   
1054   if (fabs(logt) >= 6) {                // use exponential format 
1055     if (fabs(logt) > 99)
1056       expon = 5;                //  E+102 
1057     else
1058       expon = 4;                //  E+00 
1059     
1060     if (*nfrac < 0) {           // calculate frac 
1061       delta /= pow (10., floor(logt));  // scale delta 
1062       frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1063       if (frac < 1)
1064         frac = 1;               // to be safe, add decimal pt 
1065     } else                      // use users' frac 
1066       frac = *nfrac;
1067     
1068     wid = 2 + frac + expon;
1069     if (minval < 0. || maxval < 0.)
1070       ++wid;
1071     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1072   } else {                      // use fixed format 
1073     wid = static_cast<int>(trunc(logt)) + 1;
1074     if (wid < 1)
1075       wid = 1;
1076     if (minval < 0. || maxval < 0.)
1077       ++wid;
1078     
1079     if (*nfrac < 0) {           // calculate frac 
1080       if (delta >= 0.999999)
1081         frac = 1;               // add a decimal pt to be safe 
1082       else
1083         frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1084     } else                      // use users' frac 
1085       frac = *nfrac;
1086     
1087     wid += 1 + frac;
1088     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");
1089   }
1090   
1091   *fldwid = wid;
1092   *nfrac = frac;
1093 }
1094