r313: *** empty log message ***
[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.22 2000/12/25 21:54:26 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;
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 += 2.0 * charheight;
525   xlbl_row = xp_min;            // put x-label on bottom of plot frame 
526   
527   // Y-Label 
528   if (c_ylabel.length() > 0)
529     xa_min += 2.0 * charheight; 
530   ylbl_col = xp_min;
531   
532   /*------------------------------*/
533   /* adjust frame for tick labels */
534   /*------------------------------*/
535   
536   // Calc offset of tick labels from axes 
537   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
538     xtl_ofs = 0.0;
539   else if (o_xticks == BELOW)
540     xtl_ofs = -1.5 * charheight;
541   else if (o_xticks == ABOVE)
542     xtl_ofs = 1.5 * charheight;
543   
544   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
545     ytl_ofs = 0.0;
546   else if (o_yticks == LEFT)
547     ytl_ofs = -(1 + y_fldwid) * charwidth;
548   else if (o_yticks == RIGHT)
549     ytl_ofs = 1.5 * charwidth;
550   
551   xt_min = xa_min;
552   yt_min = ya_min;
553   xt_max = xa_max;
554   yt_max = ya_max;
555   
556   // see if need to shrink axis extents and/or tick extents 
557   if (xtl_ofs != 0.0 && s_ycross == FALSE) {
558     if (o_xticks == BELOW) {
559       ya_min += 2.5 * charheight;
560       yt_min = ya_min;
561     } else if (o_xticks == ABOVE) {
562       ya_min += 0.0;
563       yt_min = ya_min + 2.5 * charheight;
564     }
565   } else   // noaxis, no t-labels, or user set cross 
566     yt_min = ya_min;
567   
568   if (ytl_ofs != 0.0 && s_xcross == FALSE) {
569     if (o_yticks == LEFT) {
570       xa_min += (1 + y_fldwid) * charwidth;
571       xt_min = xa_min;
572     } else if (o_yticks == RIGHT) {
573       xa_min += 0.0;
574       xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
575     }
576   } else
577     xt_min = xa_min;
578   
579   // decrease size of graph, if necessary, to accommadate space 
580   // between axis boundary and boundary of ticks 
581   double x_added_ticks = 0; // number of tick spaces added to axis 
582   double y_added_ticks = 0;
583   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
584     x_added_ticks = 0;
585   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
586     y_added_ticks = 0;
587   
588   if (o_grid == TRUE) {
589     if (x_added_ticks < 0)
590       x_added_ticks = 2;
591     if (y_added_ticks < 0)
592       y_added_ticks = 2;
593   }
594   
595   if (x_added_ticks < 0) {
596     if (o_yticks == LEFT || s_ycross)
597       x_added_ticks = 1;
598     else
599       x_added_ticks = 2;
600   }
601   
602   if (y_added_ticks < 0) {
603     if (o_xticks == BELOW || s_xcross)
604       y_added_ticks = 1;
605     else
606       y_added_ticks = 2;
607   }
608   
609   xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
610   yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
611   
612   xt_min += 0.5 * x_added_ticks * xn_tickinc;
613   xt_max -= 0.5 * x_added_ticks * xn_tickinc;
614   yt_min += 0.5 * y_added_ticks * yn_tickinc;
615   yt_max -= 0.5 * y_added_ticks * yn_tickinc;
616   
617   xgn_min = xt_min;
618   xgn_max = xt_max;
619   ygn_min = yt_min;
620   ygn_max = yt_max;
621   
622   //------------------------------------------------------------------------
623   
624   m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
625   m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
626   
627   // PLOT CURVES 
628   
629   rSGP.setLineStyle (SGP::LS_SOLID);
630   drawAxes();
631   
632   // size of symbol in NDC 
633   double symwidth = charwidth;
634   double symheight = charheight;
635   \r
636   double clipRect[4];\r
637   clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;\r
638 \r
639   for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
640     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
641     \r
642     rSGP.setColor (getColor (iCurve));\r
643     int iSym = getSymbol (iCurve);\r
644     int iLS = getLinestyle (iCurve);\r
645 \r
646     if (iLS != SGP::LS_NOLINE) {
647       rSGP.setLineStyle (iLS);
648       double x1 = convertWorldToNDC_X (pCurve->x[0]);
649       double y1 = convertWorldToNDC_Y (pCurve->y[0]);\r
650       for (int i = 1; i < pCurve->m_iPointCount; i++) {
651         double x2 = convertWorldToNDC_X (pCurve->x[i]);
652         double y2 = convertWorldToNDC_Y (pCurve->y[i]);\r
653         double x2Clip = x2;\r
654         double y2Clip = y2;\r
655         if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {\r
656           rSGP.moveAbs (x1, y1);\r
657           rSGP.lineAbs (x2Clip, y2Clip);\r
658         }\r
659         x1 = x2;\r
660         y1 = y2;\r
661       } 
662     }
663     if (iSym > 0) {\r
664       int iSymFreq = getSymbolFreq (iCurve);
665       rSGP.setLineStyle (SGP::LS_SOLID);
666       double x = convertWorldToNDC_X (pCurve->x[0]);
667       double y = convertWorldToNDC_Y (pCurve->y[0]);
668       rSGP.moveAbs (x, y);
669       symbol (iSym, symwidth, symheight);
670       for (int i = 1; i < pCurve->m_iPointCount; i++)
671         if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
672           x = convertWorldToNDC_X (pCurve->x[i]);
673           y = convertWorldToNDC_Y (pCurve->y[i]);\r
674           if (y >= ygn_min && y <= ygn_max) {
675             rSGP.moveAbs (x, y);
676             symbol (iSym, symwidth, symheight);\r
677           }
678         }
679     }
680   }
681   
682 }
683
684
685 /* NAME
686 *   drawAxes                    INTERNAL routine to draw axis & label them
687 *
688 * SYNOPSIS
689 *   drawAxes()
690 */
691
692
693 void 
694 EZPlot::drawAxes()
695 {
696   double xticklen = 0, yticklen = 0; // length of ticks in NDC 
697   double minorinc;              // increment between minor axes 
698   double xaxispos, yaxispos;    // crossing of axes
699   double x, y, x2, y2;
700   bool axis_near;       // TRUE if axis too close to print t-label 
701   int i, j;
702   char str[256];
703   char *numstr;
704   
705   rSGP.setTextSize (charheight);
706   rSGP.setTextColor (1, -1);
707   
708   if (o_xticks == ABOVE)
709     xticklen = charheight;
710   else if (o_xticks == BELOW)
711     xticklen = -charheight;
712   
713   if (o_yticks == RIGHT)
714     yticklen = charwidth;
715   else if (o_yticks == LEFT)
716     yticklen = -charwidth;
717   
718   if (c_title.length() > 0) {
719     double wText, hText;
720     rSGP.setTextSize (charheight * 2.0);
721     rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
722     rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
723     rSGP.setTextColor (clr_title, -1);
724     rSGP.drawText (c_title);
725     rSGP.setTextSize (charheight);
726   }
727   
728   if (o_grid == TRUE || o_box == TRUE) {
729     rSGP.setColor (clr_axis);
730     rSGP.moveAbs (xa_min, ya_min);
731     rSGP.lineAbs (xa_max, ya_min);
732     rSGP.lineAbs (xa_max, ya_max);
733     rSGP.lineAbs (xa_min, ya_max);
734     rSGP.lineAbs (xa_min, ya_min);
735   }
736   
737   // calculate position of axes 
738   
739   // x-axis 
740   if (s_ycross == TRUE) {       // convert users' world-coord 
741     xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC 
742     x = convertWorldToNDC_X (xgw_min);
743   } else
744     xaxispos = ya_min;
745   
746   // y-axis 
747   if (s_xcross == TRUE) {       // convert users' world-coord 
748     yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position 
749     y = convertWorldToNDC_Y (ygw_min);
750   } else
751     yaxispos = xa_min;
752   
753   /*-------------*/
754   /* draw x-axis */
755   /*-------------*/
756   
757   if (o_xaxis == LINEAR) {
758     // draw axis line 
759     
760     rSGP.setColor (clr_axis);
761     if (o_tag && !o_grid && !o_box && s_xcross) {
762       rSGP.moveAbs (xa_min, xaxispos - charheight);
763       rSGP.lineAbs (xa_min, xaxispos + charheight);
764     }
765     rSGP.moveAbs (xa_min, xaxispos);
766     rSGP.lineAbs (xa_max, xaxispos);
767     if (o_tag && !o_grid && !o_box) {
768       rSGP.moveAbs (xa_max, xaxispos - charheight);
769       rSGP.lineAbs (xa_max, xaxispos + charheight);
770     }
771     
772     if (o_grid == TRUE) {
773       rSGP.setColor (clr_grid);
774       for (i = 0; i <= x_nint; i++) {
775         rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
776         rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
777       }
778     }
779     rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_xlabel.length()*charwidth * 1.5, xlbl_row +  charheight * 1.5);
780     rSGP.setTextSize (charheight * 1.5);
781     rSGP.setTextColor (clr_label, -1);
782     rSGP.drawText (c_xlabel);
783     rSGP.setTextSize (charheight);
784     minorinc = xn_tickinc / (o_xminortick + 1);
785     
786     for (i = 0; i <= x_nint; i++) {
787       x = xt_min + xn_tickinc * i;
788       rSGP.setColor (clr_axis);
789       rSGP.moveAbs (x, xaxispos);
790       rSGP.lineAbs (x, xaxispos + xticklen);
791       if (i != x_nint)
792         for (j = 1; j <= o_xminortick; j++) {
793           x2 = x + minorinc * j;
794           rSGP.moveAbs (x2, xaxispos);
795           rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
796         }
797         axis_near = FALSE;
798         if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
799           double xw = xgw_min + i * xw_tickinc;
800           double x = convertWorldToNDC_X (xw);
801           double d = x - yaxispos;
802           if (o_yticks == RIGHT && d >= 0  && d < 0.9 * xn_tickinc)
803             axis_near = TRUE;
804           if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
805             axis_near = TRUE;
806         }
807         
808         if (o_xtlabel == TRUE && axis_near == FALSE) {
809           snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
810           numstr = str_skip_head (str, " ");
811           rSGP.moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
812           rSGP.setTextColor (clr_number, -1);
813           rSGP.drawText (numstr);
814         }
815     }
816   }             // X - Axis 
817   
818   
819   /*--------*/
820   /* y-axis */
821   /*--------*/
822   
823   if (o_yaxis == LINEAR) {
824     
825     rSGP.setColor (clr_axis);
826     if (o_tag && !o_grid && !o_box && s_ycross) {
827       rSGP.moveAbs (yaxispos - charwidth, ya_min);
828       rSGP.lineAbs (yaxispos + charwidth, ya_min);
829     }
830     rSGP.moveAbs (yaxispos, ya_min);
831     rSGP.lineAbs (yaxispos, ya_max);
832     if (o_tag && !o_grid && !o_box) {
833       rSGP.moveAbs (yaxispos - charwidth, ya_max);
834       rSGP.lineAbs (yaxispos + charwidth, ya_max);
835     }
836     
837     if (o_grid == TRUE) {
838       rSGP.setColor (clr_grid);
839       for (i = 0; i <= y_nint; i++) {
840         y = yt_min + yn_tickinc * i;
841         rSGP.moveAbs (xa_max, y);
842         rSGP.lineAbs (xa_min, y);
843       }
844     }\r
845 #if 0\r
846     rSGP.moveAbs(.5,.5);\r
847     char s[128];\r
848     snprintf(s,sizeof(s),"%f, %f",charwidth, charheight);\r
849     rSGP.drawText(s);\r
850 #endif\r
851     rSGP.setTextAngle (HALFPI);
852     rSGP.setTextSize (1.5 * charheight);
853     rSGP.setTextColor (clr_label, -1);\r
854     double xExtent, yExtent;\r
855     rSGP.getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);\r
856     rSGP.moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent);\r
857     rSGP.drawText (c_ylabel);
858     rSGP.setTextAngle (0.0);
859     rSGP.setTextSize (charheight);
860     minorinc = yn_tickinc / (o_yminortick + 1);
861     
862     for (i = 0; i <= y_nint; i++) {
863       y = yt_min + yn_tickinc * i;
864       rSGP.setColor (clr_axis);
865       rSGP.moveAbs (yaxispos, y);
866       rSGP.lineAbs (yaxispos + yticklen, y);
867       if (i != y_nint)
868         for (j = 1; j <= o_yminortick; j++) {
869           y2 = y + minorinc * j;
870           rSGP.moveAbs (yaxispos, y2);
871           rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
872         }
873         axis_near = FALSE;
874         if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
875           double yw = ygw_min + i * yw_tickinc;
876           double y = convertWorldToNDC_Y (yw);
877           double d = y - xaxispos;
878           if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
879             axis_near = TRUE;
880           if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
881             axis_near = TRUE;
882         }
883         if (o_ytlabel == TRUE && axis_near == FALSE) {
884           snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
885           rSGP.moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
886           rSGP.setTextColor (clr_number, -1);\r
887           rSGP.drawText (str);
888         }
889     }
890   }             // Y - Axis
891 }
892
893
894 void 
895 EZPlot::symbol (int sym, double symwidth, double symheight)
896 {
897   if (sym <= 0)
898     return;
899   
900   if (sym == SB_CROSS) {
901     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
902     rSGP.lineRel (symwidth, symheight);
903     rSGP.moveRel (-symwidth, 0.0);
904     rSGP.lineRel (symwidth, -symheight);
905     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
906   } else if (sym == SB_PLUS) {
907     rSGP.moveRel (-0.5 * symwidth, 0.0);
908     rSGP.lineRel (symwidth, 0.0);
909     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
910     rSGP.lineRel (0.0, symheight);
911     rSGP.moveRel (0.0, -0.5 * symheight);
912   } else if (sym == SB_BOX) {
913     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
914     rSGP.lineRel (symwidth, 0.0);
915     rSGP.lineRel (0.0, symheight);
916     rSGP.lineRel (-symwidth, 0.0);
917     rSGP.lineRel (0.0, -symheight);
918     rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
919   } else if (sym == SB_CIRCLE) {
920     rSGP.drawCircle (symwidth);
921   } else if (sym == SB_ERRORBAR) {
922     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
923     rSGP.lineRel (symwidth, 0.0);
924     rSGP.moveRel (-0.5 * symwidth, 0.0);
925     rSGP.lineRel (0.0, -symheight);
926     rSGP.moveRel (-0.5 * symwidth, 0.0);
927     rSGP.lineRel (symwidth, 0.0);
928     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
929   }
930 }
931
932
933
934 /* NAME
935 *    axis_scale                 calculates graph axis scaling
936 *
937 *  SYNOPSIS:
938 *    retval = axis_scale (min, max, nint, minp, maxp, nintp, 
939 *                          rec_total, rec_frac)
940 *
941 *    INPUT:
942 *       double min         Smallest value to plot
943 *       double max         Largest value to plot
944 *       int    nint        Number of intervals desired
945 *
946 *    OUTPUT:
947 *       int   retval       FALSE if illegal parameters, else TRUE
948 *       double *minp       Minimum graph value
949 *       double *maxp       Maximum graph value
950 *       int    *nintp      Number of intervals for graph
951 *      int    *rec_total  Recommended field width for printing out the number
952 *       int    *rec_frac   Recommended number of digits for print fraction
953 */
954
955 int 
956 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
957 {
958   if (min >= max || nint < 1) {
959     sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
960     return (FALSE);
961   }
962   
963   double eps = 0.025;
964   double a = fabs(min);
965   if (fabs(min) < fabs(max))
966     a = fabs(max);
967   double scale = pow (10.0, floor(log10(a)));
968 loop:
969   double mina = min / scale;
970   double maxa = max / scale;
971   double d = (maxa - mina) / nint;
972   double j = d * eps;
973   double e = floor (log10(d));
974   double f = d / pow (10.0, e);
975   double v = 10.0;
976   if (f < sqrt(2.0))
977     v = 1.0;
978   else if (f < sqrt (10.0))
979     v = 2.0;
980   else if (f < sqrt (50.0))
981     v = 5.0;
982   double wdt = v * pow (10.0, e);
983   double g = floor (mina / wdt);
984   if (fabs(g + 1 - mina / wdt) < j)
985     g = g + 1;
986 #undef TEST1\r
987 #ifdef TEST1
988   g++;
989 #endif
990   *minp = wdt * g;
991   double h = floor (maxa / wdt) + 1.0;
992   if (fabs(maxa / wdt + 1 - h) < j)
993     h = h - 1;
994 #ifdef TEST1
995   h--;
996 #endif
997   *maxp = wdt * h;
998   *nintp = static_cast<int>(h - g);
999   if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1000     scale = scale * 10.0;
1001     goto loop;
1002   }
1003   
1004   *minp *= scale;
1005   *maxp *= scale;
1006   
1007   return (TRUE);
1008 }
1009
1010
1011 /* NAME
1012 *   make_numfmt         Make a numeric format string
1013 *
1014 * SYNOPSIS
1015 *   make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1016 *   char *fmtstr                Returned format string for printf()
1017 *   int  *fldwid                Returned field width
1018 *   int  *nfrac         If < 0, then calculate best number of
1019 *                               fraction places & return that value
1020 *                               If >= 0, then use that number of places
1021 *   double min                  Minimum value
1022 *   double max                  Maximum value
1023 *   int nint                    Number of intervals between min & max
1024 *
1025 * DESCRIPTION
1026 *   This  routine is written as an INTERNAL routine for EZPLOT
1027 */
1028
1029 static inline double 
1030 trunc (double x)
1031 {
1032   double integer;
1033   
1034   modf (x, &integer);
1035   
1036   return (integer);
1037 }
1038
1039 void 
1040 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1041 {
1042   int wid, frac, expon;
1043   
1044   double delta = (maxval - minval) / nint;
1045   double absmin = fabs(minval);
1046   double absmax = fabs(maxval);\r
1047   if (absmin > absmax)\r
1048     absmax = absmin;
1049   double logt = log10( absmax );
1050   
1051   if (fabs(logt) >= 6) {                // use exponential format 
1052     if (fabs(logt) > 99)
1053       expon = 5;                //  E+102 
1054     else
1055       expon = 4;                //  E+00 
1056     
1057     if (*nfrac < 0) {           // calculate frac 
1058       delta /= pow (10., floor(logt));  // scale delta 
1059       frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1060       if (frac < 1)
1061         frac = 1;               // to be safe, add decimal pt 
1062     } else                      // use users' frac 
1063       frac = *nfrac;
1064     
1065     wid = 2 + frac + expon;
1066     if (minval < 0. || maxval < 0.)
1067       ++wid;
1068     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1069   } else {                      // use fixed format 
1070     wid = static_cast<int>(trunc(logt)) + 1;
1071     if (wid < 1)
1072       wid = 1;
1073     if (minval < 0. || maxval < 0.)
1074       ++wid;
1075     
1076     if (*nfrac < 0) {           // calculate frac 
1077       if (delta >= 0.999999)
1078         frac = 1;               // add a decimal pt to be safe 
1079       else
1080         frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1081     } else                      // use users' frac 
1082       frac = *nfrac;
1083     
1084     wid += 1 + frac;
1085     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");
1086   }
1087   
1088   *fldwid = wid;
1089   *nfrac = frac;
1090 }
1091