/* This draws a sloping line from (wx1,wy1) to (wx2,wy2)             */
/*  which represents an axis of a 3-d graph with data values from    */
/*  "vmin" to "vmax". Depending on "opt", vertical ticks and/or      */
/*  subticks are placed on the line at major tick interval "tick"    */
/*  with "nsub" subticks between major ticks. If "tick" and/or       */
/*  "nsub" is zero, automatic tick positions are computed            */

/* B: Draw box boundary                                              */
/* I: Inverts tick marks (i.e. drawn downwards)                      */
/* L: Logarithmic axes, major ticks at decades, minor ticks at units */
/* N: Write numeric label                                            */
/* T: Draw major tick marks                                          */
/* S: Draw minor tick marks                                          */
/* U: Write label on line                                            */

#include "plplot.h"
#include <stdio.h>
#include <math.h>

#define  betw(c,a,b)  ((a <= c && c <= b) || (b <= c && c <= a))

static float xlog[8] = 
  {0.301030,0.477121,0.602060,0.698970,0.778151,0.845098,0.903090,0.954243};

void plxybx(opt,label,wx1,wy1,wx2,wy2,vmin,vmax,tick,nsub,nolast)
char *opt, *label;
float wx1, wy1, wx2, wy2, vmin, vmax, tick;
int nsub, nolast;
{
      char string[40];
      int lb,li,ll,ln,ls,lt,lu;
      int major, minor, mode, prec;
      int i, i1, i2, i3, i4;
      int nsub1;
      float xpmm, ypmm, defmaj, defmin, htmaj, htmin, tick1;
      float pos, tn, tp, temp;
      float dwx, dwy, lambda;

      dwx = wx2 - wx1;
      dwy = wy2 - wy1;

/* Tick and subtick sizes in device coords */

      gpixmm(&xpmm,&ypmm);
      gmaj(&defmaj,&htmaj);
      gmin(&defmin,&htmin);
      
      major=max(round(htmaj*ypmm),1);
      minor=max(round(htmin*ypmm),1);

      tick1=tick;
      nsub1=nsub;

      lb=strpos(opt,'B')!=-1||strpos(opt,'b')!=-1;
      li=strpos(opt,'I')!=-1||strpos(opt,'i')!=-1;
      ll=strpos(opt,'L')!=-1||strpos(opt,'l')!=-1;
      ln=strpos(opt,'N')!=-1||strpos(opt,'n')!=-1;
      ls=strpos(opt,'S')!=-1||strpos(opt,'s')!=-1;
      lt=strpos(opt,'T')!=-1||strpos(opt,'t')!=-1;
      lu=strpos(opt,'U')!=-1||strpos(opt,'u')!=-1;

      if (lu) plxytx(wx1,wy1,wx2,wy2,3.2,0.5,0.5,label);
      if (!lb) return;
   
      if (ll) tick1 = 1.0;
      if (lt) pldtik(vmin,vmax,&tick1,&nsub1,&mode,&prec);

      if (li) {
        i1 = minor;
        i2 = 0;
        i3 = major;
        i4 = 0;
      }
      else  {
        i1 = 0;
        i2 = minor;
        i3 = 0;
        i4 = major;
      }  

/* Draw the line */

      movwor(wx1,wy1);
      if (lt) {
        tp=tick1*floor(vmin/tick1);
lab2:
        tn=tp+tick1;
        if (ls) {
          if (ll) {
            for (i=0; i<=7; i++)  {
              temp=tp+xlog[i];
              if (betw(temp,vmin,vmax)) {
                lambda = (temp-vmin)/(vmax-vmin);
                plxtik(wcpcx(wx1+lambda*dwx),wcpcy(wy1+lambda*dwy),i1,i2);
              }
            }
          }
          else  {
            for(i=1; i <= nsub1-1; i++)  {
              temp=tp+i*(tn-tp)/nsub1;
              if (betw(temp,vmin,vmax)) {
                lambda = (temp-vmin)/(vmax-vmin);
                plxtik(wcpcx(wx1+lambda*dwx),wcpcy(wy1+lambda*dwy),i1,i2);
              }
            }
          }
        }
        temp=tn;
        if (betw(temp,vmin,vmax)) {
          lambda = (temp-vmin)/(vmax-vmin);
          plxtik(wcpcx(wx1+lambda*dwx),wcpcy(wy1+lambda*dwy),i3,i4);
          tp=tn;
          goto lab2;
        }
      }

      drawor(wx2,wy2);

/* Label the line */

      if (ln && lt) {
        tp=tick1*floor(vmin/tick1);
lab82:
        tn=tp+tick1;
        if (nolast && !betw(tn+tick1,vmin,vmax)) return;
        if (betw(tn,vmin,vmax)) {
          if (!ll) 
            plform(tn,mode,prec,string);
          else 
            sprintf(string,"10\\u%-d",round(tn));
          pos=(tn-vmin)/(vmax-vmin);
          if (ln) plxytx(wx1,wy1,wx2,wy2,1.5,pos,0.5,string);
          tp=tn;
          goto lab82;
        }
      }
}
