/* 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.                 */
                                                                
#include "plplot.h"
#include <math.h>

void pldtik(vmin, vmax, tick, nsubt, mode, prec)
float vmin, vmax, *tick;
int *nsubt, *mode, *prec;
{
      
      float t1, t2, vmod;
      int msd, np, ns;

      vmod = max(fabs(vmin),fabs(vmax));
      *mode = 0;
      if (vmod < 1.0e-2 || vmod > 1.0e6) *mode = 1;
      t1 = (float)log10(vmod);
      msd = (int)t1;

      t1 = (float)log10(fabs(vmax-vmin));
      np = (int)t1;
      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 = np-1;
      }
      else  {
        t2 = 2.0;
        ns = 4;
        np = np-1;
      }

      if (*tick == 0.0) {
        *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);
      }
      else  {
        t1 = (float)log10(fabs(*tick));
        np = (int)t1;
        if (*mode != 0) 
          *prec = msd - np + 1;
        else
          *prec = max(-np+1,0);
      }
}

