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