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