/* If tick == 0, this works out a "nice" interval, so that there */
/* are between 3 and 7.5 major tick intervals in the input range */
/* "vmin" to "vmax". Using this value for the tick interval or   */
/* supplied value, it also computes "prec" which specifies       */
/* the number of places that should be written after the decimal */
/* point. The recommended number of subticks is returned in      */
/* "nsubt" unless the routine is entered with a non-zero value   */
/* of "nsubt". The output variable "mode" is set to 0 if         */
/* labels are to be written in floating-point format, or to 1 if */
/* they are to be written in fixed-point format.                 */
/* THIS CODE WAS ADAPTED FROM PLPLOT.LIB                         */

#include <math.h>
#include <exec/types.h>

void tik(double vmin, double vmax, double *tick, WORD *nsubt, BOOL *mode, WORD *prec)
{

double t1, t2, vmod;
LONG msd, np, ns;

      t1 = fabs(vmin);
      t2 = fabs(vmax);

      vmod = max(t1,t2);

      *mode = 0;

      if (vmod <= 1e-2 || vmod >= 1e3) *mode = 1;

      t1 = (double)log10(vmod);
      msd = (LONG)t1;

      t1 = (double)log10(fabs(vmax-vmin));
      np = (LONG)t1;
      t1 -= np;

      if (t1 > 0.7781512503)
        {
          t2 = 2.0 ;
          ns = 4;
        }
      else if (t1 > 0.4771212549)
        {
          t2 = 1.0 ;
          ns = 5;
        }
      else if (t1 > 0.1760912591)
        {
          t2 = 5.0;
          ns = 5;
          np--;
        }
      else
        {
          t2 = 1.0;
          ns = 4;
          np--;
        }

      *tick = t2 * pow(10.0,(double)np);

      if (vmin > vmax) *tick = -*tick;

      if (*nsubt == 0) *nsubt = ns;

      if (*mode != 0)
        *prec = msd - np;
      else
        *prec = max(-np,0);
}
