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