/* This draws a vertical line from (wx,wy1) to (wx,wy2)           */
/*  which represents the vertical axis of a 3-d graph with data   */
/*  values from "vmin" to "vmax". Depending on "opt", 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: Draws left-hand axis*/
/* C: Draws right-hand axis*/
/* I: Inverts tick marks (i.e. drawn to the left)  */
/* L: Logarithmic axes, major ticks at decades, minor ticks at units*/
/* M: Write numeric label on right axis*/
/* N: Write numeric label on left axis*/
/* S: Draw minor tick marks  */
/* T: Draw major tick marks  */
/* U: Writes left-hand label*/
/* V: Writes right-hand label*/

#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 plzbx(opt,label,right,dx,dy,wx,wy1,wy2,vmin,vmax,tick,nsub)
char *opt, *label;
float dx, dy, wx, wy1, wy2, vmin, vmax, tick;
int nsub, right;
{
      char string[40];
      int lb,lc,li,ll,lm,ln,ls,lt,lu,lv;
      int i, mode, prec;
      int nsub1;
      float xpmm, ypmm, defmaj, defmin, tick1;
      float pos, tn, tp, temp;
      float dwy, lambda, diag, major, minor, xmajor, xminor;
      float ymajor, yminor, dxm, dym, xscl, xoff, yscl, yoff;
               
      dwy = wy2 - wy1;

/* Tick and subtick sizes in device coords */

      gpixmm(&xpmm,&ypmm);
      gmaj(&defmaj,&major);
      gmin(&defmin,&minor);
      
      tick1=tick;
      nsub1=nsub;

      lb=strpos(opt,'B') != -1 || strpos(opt,'b') != -1;
      lc=strpos(opt,'C') != -1 || strpos(opt,'c') != -1;
      li=strpos(opt,'I') != -1 || strpos(opt,'i') != -1;
      ll=strpos(opt,'L') != -1 || strpos(opt,'l') != -1;
      lm=strpos(opt,'M') != -1 || strpos(opt,'m') != -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;
      lv=strpos(opt,'V') != -1 || strpos(opt,'v') != -1;

      if (lu && !right) plztx("h",dx,dy,wx,wy1,wy2,5.0,0.5,0.5,label);
      if (lv && right) plztx("h",dx,dy,wx,wy1,wy2,-5.0,0.5,0.5,label);

      if (right && !lc) return;
      if (!right && !lb) return;

      if (ll) tick1 = 1.0;
      if (lt) pldtik(vmin,vmax,&tick1,&nsub1,&mode,&prec);

      if ( (li && !right) || (!li && right) ) {
        minor = -minor;
        major = -major;
      }  

      gwm(&xscl,&xoff,&yscl,&yoff);
      dxm = dx * xscl;
      dym = dy * yscl;
      diag = sqrt(dxm*dxm + dym*dym);

      xminor = minor * dxm/diag;
      xmajor = major * dxm/diag;
      yminor = minor * dym/diag;
      ymajor = major * dym/diag;

/* Draw the line */

      movwor(wx,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);
                plstik(wcmmx(wx),wcmmy(wy1+lambda*dwy),xminor,yminor);
              }
            }
          }
          else  {
            for (i=1; i<= nsub1-1; i++)  {
              temp=tp+i*(tn-tp)/nsub1;
              if (betw(temp,vmin,vmax)) {
                lambda = (temp-vmin)/(vmax-vmin);
                plstik(wcmmx(wx),wcmmy(wy1+lambda*dwy),xminor,yminor);
              }
            }
          }
        }
        temp=tn;
        if (betw(temp,vmin,vmax)) {
          lambda = (temp-vmin)/(vmax-vmin);
          plstik(wcmmx(wx),wcmmy(wy1+lambda*dwy),xmajor,ymajor);
          tp=tn;
          goto lab2;
        }
      }

      drawor(wx,wy2);

/* Label the line */

      if (ln && lt) {
        tp=tick1*floor(vmin/tick1);
lab82:
        tn=tp+tick1;
        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 && !right) plztx("v",dx,dy,wx,wy1,wy2,0.5,pos,1.0,string);
          if (lm && right) plztx("v",dx,dy,wx,wy1,wy2,-0.5,pos,0.0,string);
          tp=tn;
          goto lab82;
        }
      }
}
