r309: plotfile changes
[ctsim.git] / libctgraphics / ezplot.cpp
index fb823b505f15feb1c7f0bbdee9ec052fab08190b..5a2ec69a34a2700a309722310ee61cc19bfb4e0c 100644 (file)
@@ -6,7 +6,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: ezplot.cpp,v 1.13 2000/09/02 05:10:39 kevin Exp $
+**  $Id: ezplot.cpp,v 1.20 2000/12/20 20:08:48 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
 #include "ezplot.h"
 #include <algorithm>
 
+#ifdef __GNUWIN32__
+int snprintf (char *, size_t, const char*, ...);
+#endif
 
 // Defaults
 static const double TICKRATIO = 0.4;   // ratio of minor to major tick lengths
 static const int MAXNUMFMT = 15;       // maximum length of a numeric format 
-static const double DEF_CHARHEIGHT = (1./43.); //size of characters in NDC 
-static const double DEF_CHARWIDTH = (1./80.); // size of characters in NDC 
 static const int DEF_CURVE_CLR = C_RED;
 
 
-
-EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n, int color, int linestyle, int symbol, int symfreq, const string& legend)
+EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n, int color, int linestyle, int symbol, int symfreq, const std::string& legend)
   : x(NULL), y(NULL), m_sLegend (legend)
 {
   x = new double [n];
@@ -62,47 +62,52 @@ EZPlotCurve::~EZPlotCurve ()
 void 
 EZPlot::addCurve (const double *y, int n)
 {
-  double x [n];
+  double* x = new double [n];
 
   for (int i = 0; i < n; i++)
     x[i] = i;
 
   addCurve (x, y, n);
+  delete x;
 }
 
 
 void 
 EZPlot::addCurve (const float *y, int n)
 {
-  double yDouble [n];
+  double* yDouble = new double [n];
 
   for (int i = 0; i < n; i++)
     yDouble[i] = y[i];
 
   addCurve (yDouble, n);
+  delete yDouble;
 }
 
 
 void
 EZPlot::addCurve (const float x[], const double y[], int num)
 {
-  double dx [num];
+  double* dx = new double [num];
 
   for (int i = 0; i < num; i++)
     dx[i] = x[i];
 
   addCurve (dx, y, num);
+  delete dx;
 }
 
 void
 EZPlot::addCurve (const double x[], const float y[], int num)
 {
-  double dy [num];
+  double* dy = new double [num];
 
   for (int i = 0; i < num; i++)
     dy[i] = y[i];
 
   addCurve (x, dy, num);
+
+  delete dy;
 }
 
 
@@ -142,9 +147,9 @@ EZPlot::EZPlot (SGP& sgp)
 void
 EZPlot::initPlotSettings ()
 {
-  charwidth = DEF_CHARWIDTH;   
-  charheight = DEF_CHARHEIGHT;
-  
+  charheight = rSGP.getCharHeight();
+  charwidth = rSGP.getCharWidth();
+
   c_xlabel = "";
   c_ylabel =  "";
   c_title = "";
@@ -189,7 +194,7 @@ EZPlot::initPlotSettings ()
   s_ylegend  = FALSE;
   s_textsize = FALSE;
   
-  clr_axis   = C_BLACK;                /* set fixed colors */
+  clr_axis   = C_BLACK;                // set fixed colors 
   clr_title  = C_CYAN;
   clr_label  = C_CYAN;
   clr_legend = C_RED;
@@ -214,19 +219,18 @@ EZPlot::initPlotSettings ()
 void
 EZPlot::plot ()
 {
-  double x_added_ticks;                /* number of tick spaces added to axis */
-  double y_added_ticks;
-  double symwidth, symheight;  /* size of symbol in NDC */
-  double leg_width, leg_height;        /* size of legend box */
-  int i, j, ip, n;
-  
   if (m_vecCurves.size() <= 0)
     return;
-  
-  if (s_textsize == TRUE)
+
+  rSGP.setWindow (0., 0., 1., 1.);
+
+  if (s_textsize == TRUE) {
     charheight = v_textsize;
-  else
-    charheight = DEF_CHARHEIGHT;
+    charwidth = rSGP.getCharWidth();
+  } else {
+    charheight = rSGP.getCharHeight();
+    charwidth = rSGP.getCharWidth();
+  }
   
   const EZPlotCurve& firstCurve = *m_vecCurves[0];
   double xmin = firstCurve.x[0];   // extent of curves in world coord
@@ -236,8 +240,8 @@ EZPlot::plot ()
   
   for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
     const EZPlotCurve& curve = **iterCurve;
-
-    for (ip = 0; ip < curve.m_iPointCount; ip++) {
+    
+    for (int ip = 0; ip < curve.m_iPointCount; ip++) {
       if (curve.x[ip] > xmax)
        xmax = curve.x[ip];
       else if (curve.x[ip] < xmin)
@@ -264,11 +268,11 @@ EZPlot::plot ()
       ymax = v_ycross;
   }
     
-  /* find nice endpoints for axes */
+  // find nice endpoints for axes 
   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))
     return;
     
-  /* check if user set x-axis extents */
+  // check if user set x-axis extents 
   if (s_xmin == TRUE) {
     xgw_min = v_xmin;
     x_nint = o_xmajortick - 1;
@@ -278,7 +282,7 @@ EZPlot::plot ()
     x_nint = o_xmajortick - 1;
   }
   
-  /* check if user set y-axis extents */
+  // check if user set y-axis extents 
   if (s_ymin == TRUE) {
     ygw_min = v_ymin;
     y_nint = o_ymajortick - 1;
@@ -288,11 +292,11 @@ EZPlot::plot ()
     y_nint = o_ymajortick - 1;
   }
     
-  /* calculate increment between major axis in world coordinates */
+  // calculate increment between major axis in world coordinates 
   xw_tickinc = (xgw_max - xgw_min) / x_nint;
   yw_tickinc = (ygw_max - ygw_min) / y_nint;
     
-  /* we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max */
+  // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max 
   
   // set the number of decimal point to users' setting or default 
   // Two formats for numbers: Fixed:    -nnn.f and  Exponent: -n.fffE+eee
@@ -309,12 +313,11 @@ EZPlot::plot ()
   make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
   make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
     
-  xtl_wid = x_fldwid * charwidth;              /* calc size of tick labels */
+  xtl_wid = x_fldwid * charwidth;              // calc size of tick labels 
   ytl_wid = y_fldwid * charwidth;
   tl_height = charheight;
 
-  //  rSGP.getViewport (xp_min, yp_min, xp_max, yp_max);
-  /* calculate the extent of the plot frame */
+  // calculate the extent of the plot frame 
   xp_min = o_xporigin;
   yp_min = o_yporigin;
   xp_max = xp_min + o_xlength;
@@ -325,30 +328,32 @@ EZPlot::plot ()
   yp_min = clamp (yp_min, 0., 1.);
   yp_max = clamp (yp_max, 0., 1.);
   
-  xa_min = xp_min;             /* extent of axes */
+  xa_min = xp_min;             // extent of axes 
   xa_max = xp_max;
   ya_min = yp_min;
   ya_max = yp_max;
-    
-  /* adjust frame for title */
-  if (c_title.length() > 0)
-    ya_max -= 2.5 * charheight;
-  title_row = ya_max + charheight;
 
-  /* calculate legend box boundaries */
-  int max_leg = 0;                     /* longest legend in characters */
-  int num_leg = 0;                     /* number of legend titles */
-  for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
-    const EZPlotCurve& curve = **iterCurve;
-    if ((n = curve.m_sLegend.length()) > 0) {
+  // adjust frame for title 
+  if (c_title.length() > 0)
+    ya_max -= 2 * charheight;
+  title_row = ya_max + 2 * charheight;
+
+  // calculate legend box boundaries 
+  int max_leg = 0;                     // longest legend in characters 
+  int num_leg = 0;                     // number of legend titles 
+  for (EZPlotCurveConstIterator iterCurve2 = m_vecCurves.begin(); iterCurve2 != m_vecCurves.end(); iterCurve2++) {
+    const EZPlotCurve& curve = **iterCurve2;
+    int nLegend = curve.m_sLegend.length();
+    if (nLegend > 0) {
       ++num_leg;
-      max_leg = max (max_leg, n);
+      if (nLegend > max_leg)\r
+               nLegend = max_leg;
     }
   }
 
   if (num_leg > 0 && o_legendbox != NOLEGEND) {
-    leg_width  = (max_leg + 2) * charwidth;
-    leg_height = num_leg * 3 * charheight;
+    double leg_width  = (max_leg + 2) * charwidth;     // size of legend box 
+    double leg_height = num_leg * 3 * charheight;
     
     if (s_xlegend == TRUE)
       xl_max = v_xlegend;
@@ -366,12 +371,10 @@ EZPlot::plot ()
     
     yl_min = yl_max - leg_height;
 
-    rSGP.setWindow  (xl_min, yl_min, xl_max, yl_max);
-    rSGP.setViewport (xl_min, yl_min, xl_max, yl_max);
     rSGP.setColor (clr_legend);
     rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
 
-    n = 0;                     /* current legend position */
+    int iLegend = 0;                   // current legend position 
     for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
        const EZPlotCurve& curve = **iterCurve;
        
@@ -380,7 +383,7 @@ EZPlot::plot ()
 
        double xmin = xl_min + 1.0 * charwidth;
        double xmax = xl_max - 1.0 * charwidth;
-       double y = yl_max - (2.0 + n * 3) * charheight;
+       double y = yl_max - (2.0 + iLegend * 3) * charheight;
        
        rSGP.moveAbs (xmin, y + 0.5 * charheight);
        rSGP.drawText (curve.m_sLegend);
@@ -393,40 +396,40 @@ EZPlot::plot ()
        if (curve.m_iSymbol > 0) {
            double xinc = (xmax - xmin) / (5 - 1);
            rSGP.setLineStyle (SGP::LS_SOLID);
-       for (j = 0; j < 5; j++) {
-         rSGP.moveAbs (xmin + j * xinc, y);
-         symbol(curve.m_iSymbol, 0.5 * charwidth, 0.5 * charheight);
+           for (int j = 0; j < 5; j++) {
+             rSGP.moveAbs (xmin + j * xinc, y);
+             symbol(curve.m_iSymbol, 0.5 * charwidth, 0.5 * charheight);
+           }
        }
-      }
-      ++n;             /* move to next legend position */
+       ++iLegend;      // move to next legend position 
     }
-  }   /* end legend printing */
+  }   // end legend printing 
 
-  /* calculate the extent of the axes */
+  // calculate the extent of the axes 
 
   /*-------------------------*/
   /* adjust frame for labels */
   /*-------------------------*/
   
-  /* x-label */
+  // X-Label 
   if (c_xlabel.length() > 0)
     ya_min += 3.0 * charheight;
-  xlbl_row = xp_min;           /* put x-label on bottom of plot frame */
+  xlbl_row = xp_min;           // put x-label on bottom of plot frame 
 
-  /* y-label */
+  // Y-Label 
   if (c_ylabel.length() > 0)
-    xa_min += 3.0 * charwidth; /* reverse rSGP.setTextSize because writing text sideways */
+    xa_min += 3.0 * charwidth; // reverse rSGP.setTextSize because writing text sideways 
   ylbl_col = xp_min + 2 * charwidth;
 
   /*------------------------------*/
   /* adjust frame for tick labels */
   /*------------------------------*/
 
-  /* calc offset of tick labels from axes */
+  // Calc offset of tick labels from axes 
   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
     xtl_ofs = 0.0;
   else if (o_xticks == BELOW)
-    xtl_ofs = -2.5 * charheight;
+    xtl_ofs = -1.5 * charheight;  // kr
   else if (o_xticks == ABOVE)
     xtl_ofs = 1.5 * charheight;
   
@@ -437,7 +440,12 @@ EZPlot::plot ()
   else if (o_yticks == RIGHT)
     ytl_ofs = 1.5 * charwidth;
 
-  /* see if need to shrink axis extents and/or tick extents */
+  xt_min = xa_min;
+  yt_min = ya_min;
+  xt_max = xa_max;
+  yt_max = ya_max;
+
+  // see if need to shrink axis extents and/or tick extents 
   if (xtl_ofs != 0.0 && s_ycross == FALSE) {
     if (o_xticks == BELOW) {
       ya_min += 2.5 * charheight;
@@ -446,7 +454,7 @@ EZPlot::plot ()
       ya_min += 0.0;
       yt_min = ya_min + 2.5 * charheight;
     }
-  } else                       /* noaxis, no t-labels, or user set cross */
+  } else   // noaxis, no t-labels, or user set cross 
     yt_min = ya_min;
   
   if (ytl_ofs != 0.0 && s_xcross == FALSE) {
@@ -460,15 +468,10 @@ EZPlot::plot ()
   } else
     xt_min = xa_min;
 
-  xt_max = xa_max;
-  yt_max = ya_max;
-  
-  /* decrease size of graph, if necessary, to accommadate space */
-  /* between axis boundary and boundary of ticks */
-  
-  x_added_ticks = -1;
-  y_added_ticks = -1;
-  
+  // decrease size of graph, if necessary, to accommadate space 
+  // between axis boundary and boundary of ticks 
+  double x_added_ticks = -1; // number of tick spaces added to axis 
+  double y_added_ticks = -1;
   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
     x_added_ticks = 0;
   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
@@ -508,45 +511,52 @@ EZPlot::plot ()
   ygn_min = yt_min;
   ygn_max = yt_max;
 
-  /*---------------------------------------------------------------------------*/
+  //------------------------------------------------------------------------
   
-  /* PLOT CURVES */
+  m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
+  m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
+
+  // PLOT CURVES 
   
   rSGP.setLineStyle (SGP::LS_SOLID);
   drawAxes();
 
-  /* Convert WC in graph boundary to axis boundary */
-  rSGP.setWindow  (xgw_min, ygw_min, xgw_max, ygw_max);        /* Graph boundary */
-  rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
-  double xminTemp = xa_min, xmaxTemp = xa_max;
-  double yminTemp = ya_min, ymaxTemp = ya_max;
-  rSGP.transformNDCtoMC (&xminTemp, &yminTemp); // calc WC to axis boundaries
-  rSGP.transformNDCtoMC (&xmaxTemp, &ymaxTemp);
-  rSGP.setWindow (xminTemp, yminTemp, xmaxTemp, ymaxTemp); // Set window to axis boundaries
-  rSGP.setViewport (xa_min, ya_min, xa_max, ya_max);   
+  // size of symbol in NDC 
+  double symwidth = charwidth;
+  double symheight = charheight;
   
-  symwidth = charwidth * (xgw_max - xgw_min);
-  symheight = charheight * (ygw_max - ygw_min);
-  
-  for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
-      const EZPlotCurve& curve = **iterCurve;
+  for (EZPlotCurveIterator iterCurve3 = m_vecCurves.begin(); iterCurve3 != m_vecCurves.end(); iterCurve3++) {
+      const EZPlotCurve& curve = **iterCurve3;
 
       rSGP.setColor (curve.m_iColor);
+
       if (curve.m_iLineStyle != SGP::LS_NOLINE) {
        rSGP.setLineStyle (curve.m_iLineStyle);
-       rSGP.polylineAbs (curve.x, curve.y, curve.m_iPointCount);
+       double x = convertWorldToNDC_X (curve.x[0]);
+       double y = convertWorldToNDC_Y (curve.y[0]);
+       rSGP.moveAbs (x, y);
+       for (int i = 1; i < curve.m_iPointCount; i++) {
+           x = convertWorldToNDC_X (curve.x[i]);
+           y = convertWorldToNDC_Y (curve.y[i]);
+           rSGP.lineAbs (x, y);
+       }
       }
       if (curve.m_iSymbol > 0) {
        rSGP.setLineStyle (SGP::LS_SOLID);
-       rSGP.moveAbs (curve.x[0], curve.y[0]);
+       double x = convertWorldToNDC_X (curve.x[0]);
+       double y = convertWorldToNDC_Y (curve.y[0]);
+       rSGP.moveAbs (x, y);
        symbol (curve.m_iSymbol, symwidth, symheight);
-       for (i = 1; i < curve.m_iPointCount; i++)
+       for (int i = 1; i < curve.m_iPointCount; i++)
          if (i % curve.m_iSymbolFreq == 0 || i == curve.m_iPointCount - 1) {
-           rSGP.moveAbs (curve.x[i], curve.y[i]);
+           x = convertWorldToNDC_X (curve.x[i]);
+           y = convertWorldToNDC_Y (curve.y[i]);
+           rSGP.moveAbs (x, y);
            symbol (curve.m_iSymbol, symwidth, symheight);
          }
       }
   }
+
 }
 
 
@@ -559,15 +569,15 @@ EZPlot::plot ()
 
 
 void 
-EZPlot::drawAxes(void)
+EZPlot::drawAxes()
 {
-  double xticklen = 0, yticklen = 0; /* length of ticks in NDC */
-  double minorinc;             /* increment between minor axes */
-  double xaxispos, yaxispos;   /* crossing of axes */
+  double xticklen = 0, yticklen = 0; // length of ticks in NDC 
+  double minorinc;             // increment between minor axes 
+  double xaxispos, yaxispos;   // crossing of axes
   double x, y, x2, y2;
-  bool axis_near;                      /* TRUE if axis too close to print t-label */
+  bool axis_near;      // TRUE if axis too close to print t-label 
   int i, j;
-  char str[100];
+  char str[256];
   char *numstr;
   
   rSGP.setTextSize (charheight);
@@ -583,14 +593,11 @@ EZPlot::drawAxes(void)
   else if (o_yticks == LEFT)
     yticklen = -charwidth;
   
-  rSGP.setWindow  (xp_min, yp_min, xp_max, yp_max);
-  rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
-  
   if (c_title.length() > 0) {
     double wText, hText;
+    rSGP.setTextSize (charheight * 2.0);
     rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
     rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
-    rSGP.setTextSize (charheight * 2.0);
     rSGP.setTextColor (clr_title, -1);
     rSGP.drawText (c_title);
     rSGP.setTextSize (charheight);
@@ -605,25 +612,19 @@ EZPlot::drawAxes(void)
     rSGP.lineAbs (xa_min, ya_min);
   }
   
-  /* calculate position of axes */
+  // calculate position of axes 
   
-  /* x-axis */
-  if (s_ycross == TRUE) {      /* convert users' world-coord */
-    xaxispos = v_ycross;       /* axis to its position in NDC */
-    rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
-    rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
-    x = xgw_min;
-    rSGP.transformMCtoNDC (x, xaxispos, &x, &xaxispos);
+  // x-axis 
+  if (s_ycross == TRUE) {      // convert users' world-coord 
+    xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC 
+    x = convertWorldToNDC_X (xgw_min);
   } else
     xaxispos = ya_min;
   
-  /* y-axis */
-  if (s_xcross == TRUE) {      /* convert users' world-coord */
-    yaxispos = v_xcross;       /* axis to its NDC position */
-    rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
-    rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
-    y = ygw_min;
-    rSGP.transformMCtoNDC (yaxispos, y, &yaxispos, &y);
+  // y-axis 
+  if (s_xcross == TRUE) {      // convert users' world-coord 
+    yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position 
+    y = convertWorldToNDC_Y (ygw_min);
   } else
     yaxispos = xa_min;
   
@@ -632,10 +633,7 @@ EZPlot::drawAxes(void)
   /*-------------*/
   
   if (o_xaxis == LINEAR) {
-    rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
-    rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
-    
-    /* draw axis line */
+    // draw axis line 
     
     rSGP.setColor (clr_axis);
     if (o_tag && !o_grid && !o_box && s_xcross) {
@@ -676,15 +674,9 @@ EZPlot::drawAxes(void)
        }
       axis_near = FALSE;
       if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
-       double xw, x, y, d;
-       
-       xw = xgw_min + i * xw_tickinc;
-       rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
-       rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
-       rSGP.transformMCtoNDC (xw, y, &x, &y);
-       rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
-       rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
-       d = x - yaxispos;
+       double xw = xgw_min + i * xw_tickinc;
+       double x = convertWorldToNDC_X (xw);
+       double d = x - yaxispos;
        if (o_yticks == RIGHT && d >= 0  && d < 0.9 * xn_tickinc)
          axis_near = TRUE;
        if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
@@ -694,12 +686,12 @@ EZPlot::drawAxes(void)
       if (o_xtlabel == TRUE && axis_near == FALSE) {
        snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
        numstr = str_skip_head (str, " ");
-       rSGP.moveAbs (x-strlen(numstr)*charwidth/2,xaxispos + xtl_ofs);
+       rSGP.moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
        rSGP.setTextColor (clr_number, -1);
        rSGP.drawText (numstr);
       }
     }
-  }            /* x - axis */
+  }            // X - Axis 
   
   
   /*--------*/
@@ -707,8 +699,6 @@ EZPlot::drawAxes(void)
   /*--------*/
   
   if (o_yaxis == LINEAR) {
-    rSGP.setWindow  (xp_min, yp_min, xp_max, yp_max);
-    rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
     
     rSGP.setColor (clr_axis);
     if (o_tag && !o_grid && !o_box && s_ycross) {
@@ -730,7 +720,7 @@ EZPlot::drawAxes(void)
        rSGP.lineAbs (xa_min, y);
       }
     }
-    rSGP.moveAbs (ylbl_col,ya_min + (ya_max-ya_min)/2 - c_ylabel.length()*charheight);
+    rSGP.moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - c_ylabel.length()*charheight);
     rSGP.setTextAngle (HALFPI);
     rSGP.setTextSize (2 * charheight);
     rSGP.setTextColor (clr_label, -1);
@@ -752,15 +742,9 @@ EZPlot::drawAxes(void)
        }
       axis_near = FALSE;
       if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
-       double yw, x, y, d;
-       
-       yw = ygw_min + i * yw_tickinc;
-       rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
-       rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
-       rSGP.transformMCtoNDC (x, yw, &x, &y);
-       rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
-       rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
-       d = y - xaxispos;
+       double yw = ygw_min + i * yw_tickinc;
+       double y = convertWorldToNDC_Y (yw);
+       double d = y - xaxispos;
        if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
          axis_near = TRUE;
        if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
@@ -768,12 +752,12 @@ EZPlot::drawAxes(void)
       }
       if (o_ytlabel == TRUE && axis_near == FALSE) {
        snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
-       rSGP.moveAbs (yaxispos + ytl_ofs, y - 0.5 * charheight);
-       rSGP.setTextColor (clr_number, -1);
+       rSGP.moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
+       rSGP.setTextColor (clr_number, -1);\r
        rSGP.drawText (str);
       }
     }
-  }            /* y - axis */
+  }            // Y - Axis
 }
 
 
@@ -867,12 +851,18 @@ EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp
     v = 5.0;
   double wdt = v * pow (10.0, e);
   double g = floor (mina / wdt);
-  if (fabs(g + 1 - mina / wdt) < j)
-    g = g + 1;
+    if (fabs(g + 1 - mina / wdt) < j)
+      g = g + 1;
+#if 1
+    g++;
+#endif
   *minp = wdt * g;
   double h = floor (maxa / wdt) + 1.0;
   if (fabs(maxa / wdt + 1 - h) < j)
-    h = h - 1;
+     h = h - 1;
+#if 1
+    h--;
+#endif
   *maxp = wdt * h;
   *nintp = static_cast<int>(h - g);
   if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
@@ -922,40 +912,42 @@ EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, doubl
 
   double delta = (maxval - minval) / nint;
   double absmin = fabs(minval);
-  double absmax = fabs(maxval);
-  double logt = log10( max(absmin, absmax) );
+  double absmax = fabs(maxval);\r
+  if (absmin > absmax)\r
+       absmax = absmin;
+  double logt = log10( absmax );
 
-  if (fabs(logt) >= 6) {               /* use exponential format */
+  if (fabs(logt) >= 6) {               // use exponential format 
     if (fabs(logt) > 99)
-      expon = 5;               /*  E+102 */
+      expon = 5;               //  E+102 
     else
-      expon = 4;               /*  E+00 */
+      expon = 4;               //  E+00 
     
-    if (*nfrac < 0) {          /* calculate frac */
-      delta /= pow (10., floor(logt)); /* scale delta */
+    if (*nfrac < 0) {          // calculate frac 
+      delta /= pow (10., floor(logt)); // scale delta 
       frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
       if (frac < 1)
-       frac = 1;               /* to be safe, add decimal pt */
-    } else                     /* use users' frac */
+       frac = 1;               // to be safe, add decimal pt 
+    } else                     // use users' frac 
       frac = *nfrac;
     
     wid = 2 + frac + expon;
     if (minval < 0. || maxval < 0.)
       ++wid;
     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
-  } else {                     /* use fixed format */
+  } else {                     // use fixed format 
     wid = static_cast<int>(trunc(logt)) + 1;
     if (wid < 1)
       wid = 1;
     if (minval < 0. || maxval < 0.)
       ++wid;
     
-    if (*nfrac < 0) {          /* calculate frac */
+    if (*nfrac < 0) {          // calculate frac 
       if (delta >= 0.999999)
-       frac = 1;               /* add a decimal pt to be safe */
+       frac = 1;               // add a decimal pt to be safe 
       else
        frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
-    } else                     /* use users' frac */
+    } else                     // use users' frac 
       frac = *nfrac;
     
     wid += 1 + frac;