1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: ezsupport.cpp,v 1.1 2000/06/19 18:05:03 kevin Exp $
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License (version 2) as
9 ** published by the Free Software Foundation.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ******************************************************************************/
28 * axis_scale calculates graph axis scaling
31 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
32 * rec_total, rec_frac)
35 * double min Smallest value to plot
36 * double max Largest value to plot
37 * int nint Number of intervals desired
40 * int retval FALSE if illegal parameters, else TRUE
41 * double *minp Minimum graph value
42 * double *maxp Maximum graph value
43 * int *nintp Number of intervals for graph
44 * int *rec_total Recommended field width for printing out the number
45 * int *rec_frac Recommended number of digits for print fraction
49 axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
51 double eps, a, scale, mina, maxa, d, j, e, f, v, wdt, g, h;
53 if (min >= max || nint < 1) {
54 sys_error (ERR_WARNING, "Invalid params: min=%lf, min=%lf, num intervals=%d [axis_scale]", min, max, nint);
60 if (fabs(min) < fabs(max))
62 scale = pow (10.0, floor(log10(a)));
66 d = (maxa - mina) / nint;
69 f = d / pow (10.0, e);
73 else if (f < sqrt (10.0))
75 else if (f < sqrt (50.0))
77 wdt = v * pow (10.0, e);
78 g = floor (mina / wdt);
79 if (fabs(g + 1 - mina / wdt) < j)
82 h = floor (maxa / wdt) + 1.0;
83 if (fabs(maxa / wdt + 1 - h) < j)
86 *nintp = static_cast<int>(h - g);
87 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
100 * make_numfmt Make a numeric format string
103 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
104 * char *fmtstr Returned format string for printf()
105 * int *fldwid Returned field width
106 * int *nfrac If < 0, then calculate best number of
107 * fraction places & return that value
108 * If >= 0, then use that number of places
109 * double min Minimum value
110 * double max Maximum value
111 * int nint Number of intervals between min & max
114 * This routine is written as an INTERNAL routine for EZPLOT
122 double frac = modf (x, &integer);
128 make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
130 int wid, frac, expon;
132 double delta = (maxval - minval) / nint;
133 double absmin = fabs(minval);
134 double absmax = fabs(maxval);
135 double logt = log10( max(absmin, absmax) );
137 if (fabs(logt) >= 6) { /* use exponential format */
139 expon = 5; /* E+102 */
141 expon = 4; /* E+00 */
143 if (*nfrac < 0) { /* calculate frac */
144 delta /= pow (10., floor(logt)); /* scale delta */
145 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
147 frac = 1; /* to be safe, add decimal pt */
148 } else /* use users' frac */
151 wid = 2 + frac + expon;
152 if (minval < 0. || maxval < 0.)
154 snprintf (fmtstr, sizeof(fmtstr), "%%%d.%dle", wid, frac);
155 } else { /* use fixed format */
156 wid = static_cast<int>(trunc(logt)) + 1;
159 if (minval < 0. || maxval < 0.)
162 if (*nfrac < 0) { /* calculate frac */
163 if (delta >= 0.999999)
164 frac = 1; /* add a decimal pt to be safe */
166 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
167 } else /* use users' frac */
171 snprintf (fmtstr, sizeof(fmtstr), "%%%d.%dlf", wid, frac);
184 int i, nint, wid, frac;
187 printf ("Enter min, max, & number of intervals -- ");
188 scanf ("%lf %lf %d", &min, &max, &nint);
190 frac = -1; /* let makefmt determine fraction */
192 makefmt (fmtstr, &wid, &frac, min, max, nint);
194 printf ("Format string = %s\n", fmtstr);
196 xinc = (max - min) / nint;
199 for (i = 0; i <= nint; i++) {
208 ezplot_1d (double *y, int n)
212 for (int i = 0; i < n; i++)