X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctgraphics%2Fezplot.cpp;h=099eff5ded8d050d0d067b225adfce18823b4429;hp=267ad1509d2f3e8b4b0108088a730280b5f07ec9;hb=1a050c98763fbbc0662731b0b76953acede6f5d7;hpb=c8b19dfaffba9f06d8b6c40cb1bb83a8964867f7 diff --git a/libctgraphics/ezplot.cpp b/libctgraphics/ezplot.cpp index 267ad15..099eff5 100644 --- a/libctgraphics/ezplot.cpp +++ b/libctgraphics/ezplot.cpp @@ -1,7 +1,7 @@ /***************************************************************************** ** FILE IDENTIFICATION ** -** EZPLOT +** EZPLOT ** ** This is part of the CTSim program ** Copyright (c) 1983-2001 Kevin Rosenberg @@ -30,8 +30,8 @@ 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 TICKRATIO = 0.4; // ratio of minor to major tick lengths +static const int MAXNUMFMT = 15; // maximum length of a numeric format static const int DEF_CURVE_CLR = C_RED; @@ -42,7 +42,7 @@ EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n) x[i] = xData[i]; y[i] = yData[i]; } - + m_iPointCount = n; } @@ -53,27 +53,27 @@ EZPlotCurve::~EZPlotCurve () } -void +void EZPlot::addCurve (const double *y, int n) { double* x = new double [n]; - + for (int i = 0; i < n; i++) x[i] = i; - + addCurve (x, y, n); delete x; } -void +void EZPlot::addCurve (const float *y, int n) { double* yDouble = new double [n]; - + for (int i = 0; i < n; i++) yDouble[i] = y[i]; - + addCurve (yDouble, n); delete yDouble; } @@ -83,10 +83,10 @@ void EZPlot::addCurve (const float x[], const double y[], int num) { double* dx = new double [num]; - + for (int i = 0; i < num; i++) dx[i] = x[i]; - + addCurve (dx, y, num); delete dx; } @@ -95,12 +95,12 @@ void EZPlot::addCurve (const double* const x, const float* const y, int num) { double* dy = new double [num]; - + for (int i = 0; i < num; i++) dy[i] = y[i]; - + addCurve (x, dy, num); - + delete dy; } @@ -110,7 +110,7 @@ EZPlot::addCurve (const double* const x, const double* const y, int num) { if (num < 1) return; - + EZPlotCurve* pCurve = new EZPlotCurve (x, y, num); m_vecCurves.push_back (pCurve); } @@ -125,7 +125,7 @@ EZPlot::~EZPlot () void EZPlot::clearCurves () { - for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++) + for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++) delete *i; m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end()); initPlotSettings(); @@ -152,7 +152,7 @@ EZPlot::EZPlot () m_pol.addSkipChar ('='); - + m_pol.usefile (POL::P_USE_STR,""); @@ -178,36 +178,36 @@ EZPlot::initPlotSettings () c_xlabel = ""; c_ylabel = ""; c_title = ""; - + o_xporigin = 0.0; o_yporigin = 0.0; o_xlength = 1.0; o_ylength = 1.0; - + o_xaxis = LINEAR; o_yaxis = LINEAR; - + o_grid = FALSE; o_box = FALSE; - + o_xmajortick = 10; o_ymajortick = 8; o_xminortick = 4; o_yminortick = 4; - + o_color = DEF_CURVE_CLR; o_symfreq = 1; o_symbol = -1; o_linestyle = SGP::LS_SOLID; - + o_xtlabel = TRUE; o_ytlabel = TRUE; o_xticks = BELOW; o_yticks = LEFT; - + o_legendbox = INSIDE; o_tag = FALSE; - + s_xtitle = FALSE; s_ytitle = FALSE; s_xcross = FALSE; @@ -217,8 +217,8 @@ EZPlot::initPlotSettings () s_xlegend = FALSE; s_ylegend = FALSE; s_textsize = FALSE; - - clr_axis = C_LTGRAY; // set fixed colors + + clr_axis = C_LTGRAY; // set fixed colors clr_title = C_RED; clr_label = C_BLUE; clr_legend = C_CYAN; @@ -374,7 +374,7 @@ EZPlot::getColor (unsigned int iCurve) const } - + int @@ -392,7 +392,7 @@ EZPlot::getSymbol (unsigned int iCurve) const } - + int @@ -410,7 +410,7 @@ EZPlot::getSymbolFreq (unsigned int iCurve) const } - + int @@ -428,7 +428,7 @@ EZPlot::getLinestyle (unsigned int iCurve) const } - + const std::string* @@ -446,12 +446,12 @@ EZPlot::getLegend (unsigned int iCurve) const } - + /* NAME -* plot Plots all curves collected by addCurves () +* plot Plots all curves collected by addCurves () * * SYNOPSIS * plot() @@ -468,30 +468,30 @@ EZPlot::plot (SGP* pSGP) { if (m_vecCurves.size() <= 0) return; - + m_pSGP = pSGP; m_pSGP->setWindow (0., 0., 1., 1.); - + if (s_textsize == TRUE) m_pSGP->setTextPointSize (v_textsize); charheight = m_pSGP->getCharHeight(); charwidth = m_pSGP->getCharWidth(); - double symheight = charheight * 0.3; // size of symbol in NDC + double symheight = charheight * 0.3; // size of symbol in NDC double symwidth = symheight; - + const EZPlotCurve& firstCurve = *m_vecCurves[0]; double xmin = firstCurve.x[0]; // extent of curves in world coord - double xmax = xmin; + double xmax = xmin; double ymin = firstCurve.y[0]; - double ymax = ymin; + double ymax = ymin; unsigned int iCurve; for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) { const EZPlotCurve* const pCurve = m_vecCurves [iCurve]; - + for (int ip = 0; ip < pCurve->m_iPointCount; ip++) { if (pCurve->x[ip] > xmax) xmax = pCurve->x[ip]; @@ -503,27 +503,27 @@ EZPlot::plot (SGP* pSGP) ymin = pCurve->y[ip]; } } - - // extend graph limits for user defined axis cross positions + + // extend graph limits for user defined axis cross positions if (s_xcross == TRUE) { if (v_xcross < xmin) xmin = v_xcross; else if (v_xcross > xmax) xmax = v_xcross; } - + if (s_ycross == TRUE) { if (v_ycross < ymin) ymin = v_ycross; else if (v_ycross > ymax) 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; @@ -532,8 +532,8 @@ EZPlot::plot (SGP* pSGP) xgw_max = v_xmax; 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; @@ -542,49 +542,49 @@ EZPlot::plot (SGP* pSGP) ygw_max = v_ymax; 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 - - // set the number of decimal point to users' setting or default + + // 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 if (s_lxfrac == TRUE) x_frac = v_lxfrac; else x_frac = -1; - + if (s_lyfrac == TRUE) y_frac = v_lyfrac; else y_frac = -1; - + 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; - - // 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; yp_max = yp_min + o_ylength; - + xp_min = clamp (xp_min, 0., 1.); xp_max = clamp (xp_max, 0., 1.); 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 + + // adjust frame for title title_row = ya_max;; if (c_title.length() > 0) @@ -595,9 +595,9 @@ EZPlot::plot (SGP* pSGP) ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel - // calculate legend box boundaries - int max_leg = 0; // longest legend in characters - int num_leg = 0; // number of legend titles + // calculate legend box boundaries + int max_leg = 0; // longest legend in characters + int num_leg = 0; // number of legend titles for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) { const std::string* pstrLegend = getLegend (iCurve); @@ -614,11 +614,11 @@ EZPlot::plot (SGP* pSGP) } } } - + if (num_leg > 0 && o_legendbox != NOLEGEND) { - double leg_width = (max_leg + 2) * charwidth; // size of legend box + 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; else { @@ -627,28 +627,28 @@ EZPlot::plot (SGP* pSGP) xa_max -= (leg_width + 0.5 * charwidth); } xl_min = xl_max - leg_width; - + if (s_ylegend == TRUE) yl_max = v_ylegend; else yl_max = ya_max; - + yl_min = yl_max - leg_height; - + m_pSGP->setColor (clr_legend); m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max); - - int iLegend = 0; // current legend position + + int iLegend = 0; // current legend position for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) { const std::string* pstrLegend = getLegend (iCurve); if (! pstrLegend || pstrLegend->length() == 0) continue; - + double xmin = xl_min + 1.0 * charwidth; double xmax = xl_max - 1.0 * charwidth; double y = yl_max - (2.0 + iLegend * 3) * charheight; - + m_pSGP->moveAbs (xmin, y + 0.5 * charheight); m_pSGP->drawText (pstrLegend->c_str()); @@ -670,22 +670,22 @@ EZPlot::plot (SGP* pSGP) symbol (iSymbol, symwidth, symheight); } } - ++iLegend; // move to next legend position + ++iLegend; // move to next legend position } - } // end legend printing - - // calculate the extent of the axes - + } // end legend printing + + // calculate the extent of the axes + /*-------------------------*/ /* adjust frame for labels */ /*-------------------------*/ - - // X-Label + + // X-Label if (c_xlabel.length() > 0) ya_min += 1.5 * charheight; - xlbl_row = xp_min; // put x-label on bottom of plot frame - - // Y-Label + xlbl_row = xp_min; // put x-label on bottom of plot frame + + // Y-Label if (c_ylabel.length() > 0) { m_pSGP->setTextAngle (HALFPI); @@ -704,19 +704,19 @@ EZPlot::plot (SGP* pSGP) } ylbl_col = xp_min; - + /*------------------------------*/ /* 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 = -0.5 * charheight; else if (o_xticks == ABOVE) xtl_ofs = 0.5 * charheight; - + if (o_yaxis == NOAXIS || o_ytlabel == FALSE) ytl_ofs = 0.0; else if (o_yticks == LEFT) { @@ -731,15 +731,15 @@ EZPlot::plot (SGP* pSGP) ytl_ofs = -xExtentMax; } else if (o_yticks == RIGHT) ytl_ofs = 1.5 * charwidth; - + xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label 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 + + // 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 += 1.5 * charheight; @@ -748,9 +748,9 @@ EZPlot::plot (SGP* pSGP) ya_min += 0.0; yt_min = ya_min + 1.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) { if (o_yticks == LEFT) { xa_min += 2*charwidth - ytl_ofs; // (2 + y_fldwid) * charwidth; @@ -762,59 +762,59 @@ EZPlot::plot (SGP* pSGP) } else xt_min = xa_min; - // decrease size of graph, if necessary, to accommadate space - // between axis boundary and boundary of ticks - double x_added_ticks = 0; // number of tick spaces added to axis + // decrease size of graph, if necessary, to accommadate space + // between axis boundary and boundary of ticks + double x_added_ticks = 0; // number of tick spaces added to axis double y_added_ticks = 0; if (o_xaxis == NOAXIS || o_xtlabel == FALSE) x_added_ticks = 0; if (o_yaxis == NOAXIS || o_ytlabel == FALSE) y_added_ticks = 0; - + if (o_grid == TRUE) { if (x_added_ticks < 0) x_added_ticks = 2; if (y_added_ticks < 0) y_added_ticks = 2; } - + if (x_added_ticks < 0) { if (o_yticks == LEFT || s_ycross) x_added_ticks = 1; else x_added_ticks = 2; } - + if (y_added_ticks < 0) { if (o_xticks == BELOW || s_xcross) y_added_ticks = 1; else y_added_ticks = 2; } - + xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks); yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks); - + xt_min += 0.5 * x_added_ticks * xn_tickinc; xt_max -= 0.5 * x_added_ticks * xn_tickinc; yt_min += 0.5 * y_added_ticks * yn_tickinc; yt_max -= 0.5 * y_added_ticks * yn_tickinc; - + xgn_min = xt_min; xgn_max = xt_max; ygn_min = yt_min; ygn_max = yt_max; - + //------------------------------------------------------------------------ - + m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min); m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min); - - // PLOT CURVES - + + // PLOT CURVES + m_pSGP->setLineStyle (SGP::LS_SOLID); drawAxes(); - + double clipRect[4]; @@ -824,7 +824,7 @@ EZPlot::plot (SGP* pSGP) for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) { const EZPlotCurve* const pCurve = m_vecCurves [iCurve]; - + m_pSGP->setColor (getColor (iCurve)); @@ -851,7 +851,7 @@ EZPlot::plot (SGP* pSGP) } x1 = x2; y1 = y2; - } + } } if (iSym > 0) { int iSymFreq = getSymbolFreq (iCurve); @@ -871,43 +871,43 @@ EZPlot::plot (SGP* pSGP) } } } - } + } } /* NAME -* drawAxes INTERNAL routine to draw axis & label them +* drawAxes INTERNAL routine to draw axis & label them * * SYNOPSIS * drawAxes() */ -void +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[256]; char *numstr; - + m_pSGP->setTextSize (charheight); m_pSGP->setTextColor (1, -1); - + if (o_xticks == ABOVE) xticklen = 0.5 * charheight; else if (o_xticks == BELOW) xticklen = -0.5 * charheight; - + if (o_yticks == RIGHT) yticklen = charwidth; else if (o_yticks == LEFT) yticklen = -charwidth; - + if (c_title.length() > 0) { double wText, hText; m_pSGP->setTextSize (charheight * 2.0); @@ -917,7 +917,7 @@ EZPlot::drawAxes() m_pSGP->drawText (c_title); m_pSGP->setTextSize (charheight); } - + if (o_grid == TRUE || o_box == TRUE) { m_pSGP->setColor (clr_axis); m_pSGP->moveAbs (xa_min, ya_min); @@ -926,30 +926,30 @@ EZPlot::drawAxes() m_pSGP->lineAbs (xa_min, ya_max); m_pSGP->lineAbs (xa_min, ya_min); } - - // calculate position of axes - - // x-axis - if (s_ycross == TRUE) { // convert users' world-coord - xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC + + // calculate position of axes + + // 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 = convertWorldToNDC_X (v_xcross);// axis to its NDC position + + // 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; - + /*-------------*/ /* draw x-axis */ /*-------------*/ - + if (o_xaxis == LINEAR) { - // draw axis line - + // draw axis line + m_pSGP->setColor (clr_axis); if (o_tag && !o_grid && !o_box && s_xcross) { m_pSGP->moveAbs (xa_min, xaxispos - charheight); @@ -961,7 +961,7 @@ EZPlot::drawAxes() m_pSGP->moveAbs (xa_max, xaxispos - charheight); m_pSGP->lineAbs (xa_max, xaxispos + charheight); } - + if (o_grid == TRUE) { m_pSGP->setColor (clr_grid); for (i = 0; i <= x_nint; i++) { @@ -980,7 +980,7 @@ EZPlot::drawAxes() m_pSGP->drawText (c_xlabel); m_pSGP->setTextSize (charheight); minorinc = xn_tickinc / (o_xminortick + 1); - + for (i = 0; i <= x_nint; i++) { x = xt_min + xn_tickinc * i; m_pSGP->setColor (clr_axis); @@ -1002,7 +1002,7 @@ EZPlot::drawAxes() if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc) axis_near = TRUE; } - + if (o_xtlabel == TRUE && axis_near == FALSE) { snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i); numstr = str_skip_head (str, " "); @@ -1013,15 +1013,15 @@ EZPlot::drawAxes() m_pSGP->drawText (numstr); } } - } // X - Axis - - + } // X - Axis + + /*--------*/ /* y-axis */ /*--------*/ - + if (o_yaxis == LINEAR) { - + m_pSGP->setColor (clr_axis); if (o_tag && !o_grid && !o_box && s_ycross) { m_pSGP->moveAbs (yaxispos - charwidth, ya_min); @@ -1033,7 +1033,7 @@ EZPlot::drawAxes() m_pSGP->moveAbs (yaxispos - charwidth, ya_max); m_pSGP->lineAbs (yaxispos + charwidth, ya_max); } - + if (o_grid == TRUE) { m_pSGP->setColor (clr_grid); for (i = 0; i <= y_nint; i++) { @@ -1056,7 +1056,7 @@ EZPlot::drawAxes() m_pSGP->setTextAngle (0.0); m_pSGP->setTextSize (charheight); minorinc = yn_tickinc / (o_yminortick + 1); - + for (i = 0; i <= y_nint; i++) { y = yt_min + yn_tickinc * i; m_pSGP->setColor (clr_axis); @@ -1090,16 +1090,16 @@ EZPlot::drawAxes() m_pSGP->drawText (str); } } - } // Y - Axis + } // Y - Axis } -void +void EZPlot::symbol (int sym, double symwidth, double symheight) { if (sym <= 0) return; - + if (sym == SB_CROSS) { m_pSGP->markerRel (0, 0); // m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight); @@ -1138,34 +1138,34 @@ EZPlot::symbol (int sym, double symwidth, double symheight) /* NAME -* axis_scale calculates graph axis scaling +* axis_scale calculates graph axis scaling * * SYNOPSIS: -* retval = axis_scale (min, max, nint, minp, maxp, nintp, -* rec_total, rec_frac) +* retval = axis_scale (min, max, nint, minp, maxp, nintp, +* rec_total, rec_frac) * * INPUT: -* double min Smallest value to plot -* double max Largest value to plot -* int nint Number of intervals desired +* double min Smallest value to plot +* double max Largest value to plot +* int nint Number of intervals desired * * OUTPUT: -* int retval FALSE if illegal parameters, else TRUE -* double *minp Minimum graph value -* double *maxp Maximum graph value -* int *nintp Number of intervals for graph +* int retval FALSE if illegal parameters, else TRUE +* double *minp Minimum graph value +* double *maxp Maximum graph value +* int *nintp Number of intervals for graph * int *rec_total Recommended field width for printing out the number -* int *rec_frac Recommended number of digits for print fraction +* int *rec_frac Recommended number of digits for print fraction */ -int +int EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp) { if (min >= max || nint < 1) { sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint); return (FALSE); } - + double eps = 0.025; double a = fabs(min); if (fabs(min) < fabs(max)) @@ -1207,47 +1207,47 @@ loop: scale = scale * 10.0; goto loop; } - + *minp *= scale; *maxp *= scale; - + return (TRUE); } /* NAME -* make_numfmt Make a numeric format string +* make_numfmt Make a numeric format string * * SYNOPSIS * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint) -* char *fmtstr Returned format string for printf() -* int *fldwid Returned field width -* int *nfrac If < 0, then calculate best number of -* fraction places & return that value -* If >= 0, then use that number of places -* double min Minimum value -* double max Maximum value -* int nint Number of intervals between min & max +* char *fmtstr Returned format string for printf() +* int *fldwid Returned field width +* int *nfrac If < 0, then calculate best number of +* fraction places & return that value +* If >= 0, then use that number of places +* double min Minimum value +* double max Maximum value +* int nint Number of intervals between min & max * * DESCRIPTION * This routine is written as an INTERNAL routine for EZPLOT */ -static inline double +static inline double my_trunc (double x) { double integer; - + modf (x, &integer); - + return (integer); } -void +void EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint) { int wid, frac, expon; - + double delta = (maxval - minval) / nint; double absmin = fabs(minval); double absmax = fabs(maxval); @@ -1256,44 +1256,44 @@ EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, doubl 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 - - if (*nfrac < 0) { // calculate frac - delta /= pow (10., floor(logt)); // scale delta + expon = 4; // E+00 + + if (*nfrac < 0) { // calculate frac + delta /= pow (10., floor(logt)); // scale delta frac = static_cast(fabs(my_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(my_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(fabs(my_trunc(log10(delta)))) + 1; - } else // use users' frac + } else // use users' frac frac = *nfrac; - + wid += 1 + frac; sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f"); } - + *fldwid = wid; *nfrac = frac; }