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