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