260d5badbc68b295c84a7f4b7bbc169526614bf4
[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.27 2001/01/02 09:58:11 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 xExtent, yExtent;
727     char s[1024];
728     snprintf (s, sizeof(s), y_numfmt, 0);
729     m_pSGP->getTextExtent (s, &xExtent, &yExtent);
730     ytl_ofs = -2.0 * charwidth - xExtent;
731   } else if (o_yticks == RIGHT)
732     ytl_ofs = 1.5 * charwidth;
733   
734   xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
735
736
737
738   xt_min = xa_min;
739   yt_min = ya_min;
740   xt_max = xa_max;
741   yt_max = ya_max;
742   
743   // see if need to shrink axis extents and/or tick extents 
744   if (xtl_ofs != 0.0 && s_ycross == FALSE) {
745     if (o_xticks == BELOW) {
746       ya_min += 1.5 * charheight;
747       yt_min = ya_min;
748     } else if (o_xticks == ABOVE) {
749       ya_min += 0.0;
750       yt_min = ya_min + 1.5 * charheight;
751     }
752   } else   // noaxis, no t-labels, or user set cross 
753     yt_min = ya_min;
754   
755   if (ytl_ofs != 0.0 && s_xcross == FALSE) {
756     if (o_yticks == LEFT) {
757       xa_min += (2 + y_fldwid) * charwidth;
758       xt_min = xa_min;
759     } else if (o_yticks == RIGHT) {
760       xa_min += 0.0;
761       xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
762     }
763   } else
764     xt_min = xa_min;
765
766   // decrease size of graph, if necessary, to accommadate space 
767   // between axis boundary and boundary of ticks 
768   double x_added_ticks = 0; // number of tick spaces added to axis 
769   double y_added_ticks = 0;
770   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
771     x_added_ticks = 0;
772   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
773     y_added_ticks = 0;
774   
775   if (o_grid == TRUE) {
776     if (x_added_ticks < 0)
777       x_added_ticks = 2;
778     if (y_added_ticks < 0)
779       y_added_ticks = 2;
780   }
781   
782   if (x_added_ticks < 0) {
783     if (o_yticks == LEFT || s_ycross)
784       x_added_ticks = 1;
785     else
786       x_added_ticks = 2;
787   }
788   
789   if (y_added_ticks < 0) {
790     if (o_xticks == BELOW || s_xcross)
791       y_added_ticks = 1;
792     else
793       y_added_ticks = 2;
794   }
795   
796   xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
797   yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
798   
799   xt_min += 0.5 * x_added_ticks * xn_tickinc;
800   xt_max -= 0.5 * x_added_ticks * xn_tickinc;
801   yt_min += 0.5 * y_added_ticks * yn_tickinc;
802   yt_max -= 0.5 * y_added_ticks * yn_tickinc;
803   
804   xgn_min = xt_min;
805   xgn_max = xt_max;
806   ygn_min = yt_min;
807   ygn_max = yt_max;
808   
809   //------------------------------------------------------------------------
810   
811   m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
812   m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
813   
814   // PLOT CURVES 
815   
816   m_pSGP->setLineStyle (SGP::LS_SOLID);
817   drawAxes();
818   
819   // size of symbol in NDC 
820   double symwidth = charwidth;
821   double symheight = charheight;
822   
823
824   double clipRect[4];
825
826   clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
827
828
829
830   for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
831     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
832     
833
834     m_pSGP->setColor (getColor (iCurve));
835
836     int iSym = getSymbol (iCurve);
837
838     int iLS = getLinestyle (iCurve);
839
840
841
842     if (iLS != SGP::LS_NOLINE) {
843       m_pSGP->setLineStyle (iLS);
844       double x1 = convertWorldToNDC_X (pCurve->x[0]);
845       double y1 = convertWorldToNDC_Y (pCurve->y[0]);
846
847       for (int i = 1; i < pCurve->m_iPointCount; i++) {
848         double x2 = convertWorldToNDC_X (pCurve->x[i]);
849         double y2 = convertWorldToNDC_Y (pCurve->y[i]);
850
851         double x2Clip = x2;
852
853         double y2Clip = y2;
854
855         if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
856
857           m_pSGP->moveAbs (x1, y1);
858
859           m_pSGP->lineAbs (x2Clip, y2Clip);
860
861         }
862
863         x1 = x2;
864
865         y1 = y2;
866
867       } 
868     }
869     if (iSym > 0) {
870
871       int iSymFreq = getSymbolFreq (iCurve);
872       m_pSGP->setLineStyle (SGP::LS_SOLID);
873       double x = convertWorldToNDC_X (pCurve->x[0]);
874       double y = convertWorldToNDC_Y (pCurve->y[0]);
875       m_pSGP->moveAbs (x, y);
876       symbol (iSym, symwidth, symheight);
877       for (int i = 1; i < pCurve->m_iPointCount; i++)
878         if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
879           x = convertWorldToNDC_X (pCurve->x[i]);
880           y = convertWorldToNDC_Y (pCurve->y[i]);
881
882           if (y >= ygn_min && y <= ygn_max) {
883             m_pSGP->moveAbs (x, y);
884             symbol (iSym, symwidth, symheight);
885
886           }
887         }
888     }
889   }
890   
891 }
892
893
894 /* NAME
895 *   drawAxes                    INTERNAL routine to draw axis & label them
896 *
897 * SYNOPSIS
898 *   drawAxes()
899 */
900
901
902 void 
903 EZPlot::drawAxes()
904 {
905   double xticklen = 0, yticklen = 0; // length of ticks in NDC 
906   double minorinc;              // increment between minor axes 
907   double xaxispos, yaxispos;    // crossing of axes
908   double x, y, x2, y2;
909   bool axis_near;       // TRUE if axis too close to print t-label 
910   int i, j;
911   char str[256];
912   char *numstr;
913   
914   m_pSGP->setTextSize (charheight);
915   m_pSGP->setTextColor (1, -1);
916   
917   if (o_xticks == ABOVE)
918     xticklen = 0.5 * charheight;
919   else if (o_xticks == BELOW)
920     xticklen = -0.5 * charheight;
921   
922   if (o_yticks == RIGHT)
923     yticklen = charwidth;
924   else if (o_yticks == LEFT)
925     yticklen = -charwidth;
926   
927   if (c_title.length() > 0) {
928     double wText, hText;
929     m_pSGP->setTextSize (charheight * 2.0);
930     m_pSGP->getTextExtent (c_title.c_str(), &wText, &hText);
931     m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
932     m_pSGP->setTextColor (clr_title, -1);
933     m_pSGP->drawText (c_title);
934     m_pSGP->setTextSize (charheight);
935   }
936   
937   if (o_grid == TRUE || o_box == TRUE) {
938     m_pSGP->setColor (clr_axis);
939     m_pSGP->moveAbs (xa_min, ya_min);
940     m_pSGP->lineAbs (xa_max, ya_min);
941     m_pSGP->lineAbs (xa_max, ya_max);
942     m_pSGP->lineAbs (xa_min, ya_max);
943     m_pSGP->lineAbs (xa_min, ya_min);
944   }
945   
946   // calculate position of axes 
947   
948   // x-axis 
949   if (s_ycross == TRUE) {       // convert users' world-coord 
950     xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC 
951     x = convertWorldToNDC_X (xgw_min);
952   } else
953     xaxispos = ya_min;
954   
955   // y-axis 
956   if (s_xcross == TRUE) {       // convert users' world-coord 
957     yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position 
958     y = convertWorldToNDC_Y (ygw_min);
959   } else
960     yaxispos = xa_min;
961   
962   /*-------------*/
963   /* draw x-axis */
964   /*-------------*/
965   
966   if (o_xaxis == LINEAR) {
967     // draw axis line 
968     
969     m_pSGP->setColor (clr_axis);
970     if (o_tag && !o_grid && !o_box && s_xcross) {
971       m_pSGP->moveAbs (xa_min, xaxispos - charheight);
972       m_pSGP->lineAbs (xa_min, xaxispos + charheight);
973     }
974     m_pSGP->moveAbs (xa_min, xaxispos);
975     m_pSGP->lineAbs (xa_max, xaxispos);
976     if (o_tag && !o_grid && !o_box) {
977       m_pSGP->moveAbs (xa_max, xaxispos - charheight);
978       m_pSGP->lineAbs (xa_max, xaxispos + charheight);
979     }
980     
981     if (o_grid == TRUE) {
982       m_pSGP->setColor (clr_grid);
983       for (i = 0; i <= x_nint; i++) {
984         m_pSGP->moveAbs (xt_min + xn_tickinc * i, ya_max);
985         m_pSGP->lineAbs (xt_min + xn_tickinc * i, ya_min);
986       }
987     }
988     m_pSGP->setTextSize (charheight * 1.5);
989     m_pSGP->setTextColor (clr_label, -1);
990
991     double wText, hText;
992
993     m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText);
994     m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row +  charheight * 1.5);
995
996     m_pSGP->drawText (c_xlabel);
997     m_pSGP->setTextSize (charheight);
998     minorinc = xn_tickinc / (o_xminortick + 1);
999     
1000     for (i = 0; i <= x_nint; i++) {
1001       x = xt_min + xn_tickinc * i;
1002       m_pSGP->setColor (clr_axis);
1003       m_pSGP->moveAbs (x, xaxispos);
1004       m_pSGP->lineAbs (x, xaxispos + xticklen);
1005       if (i != x_nint)
1006         for (j = 1; j <= o_xminortick; j++) {
1007           x2 = x + minorinc * j;
1008           m_pSGP->moveAbs (x2, xaxispos);
1009           m_pSGP->lineAbs (x2, xaxispos + TICKRATIO * xticklen);
1010         }
1011         axis_near = FALSE;
1012         if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
1013           double xw = xgw_min + i * xw_tickinc;
1014           double x = convertWorldToNDC_X (xw);
1015           double d = x - yaxispos;
1016           if (o_yticks == RIGHT && d >= 0  && d < 0.9 * xn_tickinc)
1017             axis_near = TRUE;
1018           if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
1019             axis_near = TRUE;
1020         }
1021         
1022         if (o_xtlabel == TRUE && axis_near == FALSE) {
1023           snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
1024           numstr = str_skip_head (str, " ");
1025           double xExtent, yExtent;
1026           m_pSGP->getTextExtent (numstr, &xExtent, &yExtent);
1027           m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs);
1028           m_pSGP->setTextColor (clr_number, -1);
1029           m_pSGP->drawText (numstr);
1030         }
1031     }
1032   }             // X - Axis 
1033   
1034   
1035   /*--------*/
1036   /* y-axis */
1037   /*--------*/
1038   
1039   if (o_yaxis == LINEAR) {
1040     
1041     m_pSGP->setColor (clr_axis);
1042     if (o_tag && !o_grid && !o_box && s_ycross) {
1043       m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
1044       m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
1045     }
1046     m_pSGP->moveAbs (yaxispos, ya_min);
1047     m_pSGP->lineAbs (yaxispos, ya_max);
1048     if (o_tag && !o_grid && !o_box) {
1049       m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
1050       m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
1051     }
1052     
1053     if (o_grid == TRUE) {
1054       m_pSGP->setColor (clr_grid);
1055       for (i = 0; i <= y_nint; i++) {
1056         y = yt_min + yn_tickinc * i;
1057         m_pSGP->moveAbs (xa_max, y);
1058         m_pSGP->lineAbs (xa_min, y);
1059       }
1060     }
1061
1062     m_pSGP->setTextAngle (HALFPI);
1063     m_pSGP->setTextSize (1.5 * charheight);
1064     m_pSGP->setTextColor (clr_label, -1);
1065
1066     double xExtent, yExtent;
1067
1068     m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
1069     m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
1070     m_pSGP->drawText (c_ylabel);
1071
1072     m_pSGP->setTextAngle (0.0);
1073     m_pSGP->setTextSize (charheight);
1074     minorinc = yn_tickinc / (o_yminortick + 1);
1075     
1076     for (i = 0; i <= y_nint; i++) {
1077       y = yt_min + yn_tickinc * i;
1078       m_pSGP->setColor (clr_axis);
1079       m_pSGP->moveAbs (yaxispos, y);
1080       m_pSGP->lineAbs (yaxispos + yticklen, y);
1081       if (i != y_nint)
1082         for (j = 1; j <= o_yminortick; j++) {
1083           y2 = y + minorinc * j;
1084           m_pSGP->moveAbs (yaxispos, y2);
1085           m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
1086         }
1087         axis_near = FALSE;
1088         if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
1089           double yw = ygw_min + i * yw_tickinc;
1090           double y = convertWorldToNDC_Y (yw);
1091           double d = y - xaxispos;
1092           if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
1093             axis_near = TRUE;
1094           if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
1095             axis_near = TRUE;
1096         }
1097         if (o_ytlabel == TRUE && axis_near == FALSE) {
1098           snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
1099           double xExtent, yExtent;
1100           m_pSGP->getTextExtent (str, &xExtent, &yExtent);
1101           if (o_yticks == LEFT)
1102             m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent);
1103           else
1104             m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent);
1105           m_pSGP->setTextColor (clr_number, -1);
1106           m_pSGP->drawText (str);
1107         }
1108     }
1109   }             // Y - Axis
1110 }
1111
1112
1113 void 
1114 EZPlot::symbol (int sym, double symwidth, double symheight)
1115 {
1116   if (sym <= 0)
1117     return;
1118   
1119   if (sym == SB_CROSS) {
1120     m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1121     m_pSGP->lineRel (symwidth, symheight);
1122     m_pSGP->moveRel (-symwidth, 0.0);
1123     m_pSGP->lineRel (symwidth, -symheight);
1124     m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1125   } else if (sym == SB_PLUS) {
1126     m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1127     m_pSGP->lineRel (symwidth, 0.0);
1128     m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1129     m_pSGP->lineRel (0.0, symheight);
1130     m_pSGP->moveRel (0.0, -0.5 * symheight);
1131   } else if (sym == SB_BOX) {
1132     m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1133     m_pSGP->lineRel (symwidth, 0.0);
1134     m_pSGP->lineRel (0.0, symheight);
1135     m_pSGP->lineRel (-symwidth, 0.0);
1136     m_pSGP->lineRel (0.0, -symheight);
1137     m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
1138   } else if (sym == SB_CIRCLE) {
1139     m_pSGP->drawCircle (symwidth);
1140   } else if (sym == SB_ERRORBAR) {
1141     m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1142     m_pSGP->lineRel (symwidth, 0.0);
1143     m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1144     m_pSGP->lineRel (0.0, -symheight);
1145     m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1146     m_pSGP->lineRel (symwidth, 0.0);
1147     m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1148   }
1149 }
1150
1151
1152
1153 /* NAME
1154 *    axis_scale                 calculates graph axis scaling
1155 *
1156 *  SYNOPSIS:
1157 *    retval = axis_scale (min, max, nint, minp, maxp, nintp, 
1158 *                          rec_total, rec_frac)
1159 *
1160 *    INPUT:
1161 *       double min         Smallest value to plot
1162 *       double max         Largest value to plot
1163 *       int    nint        Number of intervals desired
1164 *
1165 *    OUTPUT:
1166 *       int   retval       FALSE if illegal parameters, else TRUE
1167 *       double *minp       Minimum graph value
1168 *       double *maxp       Maximum graph value
1169 *       int    *nintp      Number of intervals for graph
1170 *      int    *rec_total  Recommended field width for printing out the number
1171 *       int    *rec_frac   Recommended number of digits for print fraction
1172 */
1173
1174 int 
1175 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
1176 {
1177   if (min >= max || nint < 1) {
1178     sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
1179     return (FALSE);
1180   }
1181   
1182   double eps = 0.025;
1183   double a = fabs(min);
1184   if (fabs(min) < fabs(max))
1185     a = fabs(max);
1186   double scale = pow (10.0, floor(log10(a)));
1187 loop:
1188   double mina = min / scale;
1189   double maxa = max / scale;
1190   double d = (maxa - mina) / nint;
1191   double j = d * eps;
1192   double e = floor (log10(d));
1193   double f = d / pow (10.0, e);
1194   double v = 10.0;
1195   if (f < sqrt(2.0))
1196     v = 1.0;
1197   else if (f < sqrt (10.0))
1198     v = 2.0;
1199   else if (f < sqrt (50.0))
1200     v = 5.0;
1201   double wdt = v * pow (10.0, e);
1202   double g = floor (mina / wdt);
1203   if (fabs(g + 1 - mina / wdt) < j)
1204     g = g + 1;
1205 #undef TEST1
1206
1207 #ifdef TEST1
1208   g++;
1209 #endif
1210   *minp = wdt * g;
1211   double h = floor (maxa / wdt) + 1.0;
1212   if (fabs(maxa / wdt + 1 - h) < j)
1213     h = h - 1;
1214 #ifdef TEST1
1215   h--;
1216 #endif
1217   *maxp = wdt * h;
1218   *nintp = static_cast<int>(h - g);
1219   if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1220     scale = scale * 10.0;
1221     goto loop;
1222   }
1223   
1224   *minp *= scale;
1225   *maxp *= scale;
1226   
1227   return (TRUE);
1228 }
1229
1230
1231 /* NAME
1232 *   make_numfmt         Make a numeric format string
1233 *
1234 * SYNOPSIS
1235 *   make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1236 *   char *fmtstr                Returned format string for printf()
1237 *   int  *fldwid                Returned field width
1238 *   int  *nfrac         If < 0, then calculate best number of
1239 *                               fraction places & return that value
1240 *                               If >= 0, then use that number of places
1241 *   double min                  Minimum value
1242 *   double max                  Maximum value
1243 *   int nint                    Number of intervals between min & max
1244 *
1245 * DESCRIPTION
1246 *   This  routine is written as an INTERNAL routine for EZPLOT
1247 */
1248
1249 static inline double 
1250 trunc (double x)
1251 {
1252   double integer;
1253   
1254   modf (x, &integer);
1255   
1256   return (integer);
1257 }
1258
1259 void 
1260 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1261 {
1262   int wid, frac, expon;
1263   
1264   double delta = (maxval - minval) / nint;
1265   double absmin = fabs(minval);
1266   double absmax = fabs(maxval);
1267
1268   if (absmin > absmax)
1269
1270     absmax = absmin;
1271   double logt = log10( absmax );
1272   
1273   if (fabs(logt) >= 6) {                // use exponential format 
1274     if (fabs(logt) > 99)
1275       expon = 5;                //  E+102 
1276     else
1277       expon = 4;                //  E+00 
1278     
1279     if (*nfrac < 0) {           // calculate frac 
1280       delta /= pow (10., floor(logt));  // scale delta 
1281       frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1282       if (frac < 1)
1283         frac = 1;               // to be safe, add decimal pt 
1284     } else                      // use users' frac 
1285       frac = *nfrac;
1286     
1287     wid = 2 + frac + expon;
1288     if (minval < 0. || maxval < 0.)
1289       ++wid;
1290     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1291   } else {                      // use fixed format 
1292     wid = static_cast<int>(trunc(logt)) + 1;
1293     if (wid < 1)
1294       wid = 1;
1295     if (minval < 0. || maxval < 0.)
1296       ++wid;
1297     
1298     if (*nfrac < 0) {           // calculate frac 
1299       if (delta >= 0.999999)
1300         frac = 1;               // add a decimal pt to be safe 
1301       else
1302         frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1303     } else                      // use users' frac 
1304       frac = *nfrac;
1305     
1306     wid += 1 + frac;
1307     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");
1308   }
1309   
1310   *fldwid = wid;
1311   *nfrac = frac;
1312 }
1313