#ifndef lint
static char *rc="$Header: /mit/hst/src/plot/RCS/drawAxis.c,v 2.1 90/08/18 18:37:59 tldavis Exp $";
#endif
/*Copyright 1988 by Timothy L. Davis
 *
 *Draws Axis in either horizontal or vertical direction with hash marks
 *as requested.  For use by the Plot widget.
 *
 *$Source: /mit/hst/src/plot/RCS/drawAxis.c,v $
 *$Log:	drawAxis.c,v $
 * Revision 2.1  90/08/18  18:37:59  tldavis
 * Small bugfixes?
 * 
 * Revision 2.0  90/08/06  18:00:05  tldavis
 * Final X11R3 version.
 * 
 * Revision 1.6  89/02/22  12:27:28  tldavis
 * X11R3 version (no saves of old data).
 * 
 * Revision 1.5  88/10/10  01:22:21  tldavis
 * Fixed positioning of numbers.
 * 
 * Revision 1.4  88/10/04  08:21:59  tldavis
 * *** empty log message ***
 * 
 * Revision 1.3  88/08/16  16:03:11  tldavis
 * *** empty log message ***
 * 
 * Revision 1.2  88/08/03  18:22:20  tldavis
 * First successful compile of X11 version.
 * 
 * Revision 1.1  88/08/03  14:54:55  tldavis
 * Initial revision
 * 
 */
#include <math.h>
#include <stdio.h>
#include <X11/Xlib.h>

#define MIN_SPACE 4

static double myfabs(x) 
     double x;
{
  return(((x) > -0.001 && (x) < 0.001) ? 1.0 : fabs(x));
}

/*PUBLIC*/ double
drawAxis(dpy, window, gc, smallFont, startX, startY, endX, endY, min, max, tic)
Display *dpy;
Window window;
GC gc;
XFontStruct *smallFont;
int startX, startY, endX, endY;
double min, max;
int tic;
{
  double ticint, sfX, sfY;
  double order, temp;
  int x, y; /* screen coordinates */
  double next;
  char s[80];
  int xNumOffset, yNumOffset;
  int nextnumloc, lastnumloc;
  int font_width = smallFont ? XTextWidth(smallFont, "0", 1) : 5;
  int font_height = 10;

  if (startX==endX) ticint = fabs((max - min)*MIN_SPACE*5./(endY-startY));
  else if (startY==endY) ticint = (max - min)*MIN_SPACE*5./(endX-startX);
  else { fprintf(stderr, "in drawAxis: Axis must not be diagonal\n"); }

  sfX = (endX-startX)/(max - min);
  sfY = (endY-startY)/(max - min);

  /* determine tic interval based on a round number (10, 25, 50...) */
  order =  pow(10.0, floor(log10(ticint)));
  temp = ticint/order;
  if (temp <= 1.0001) temp = 1.;
  else if (temp <= 2.5001) temp = 2.5;
  else if (temp <= 5.0001) temp = 5.0;
  else temp = 10.0;
  ticint = temp*order;
  if (startX==endX) {
    xNumOffset = (tic>0 ? -tic*5*font_width : 0) - tic*5;
    yNumOffset = font_height/2; 
  } else { /* startY must == endY */
    xNumOffset = -2*font_width;
    /*||| trying to get x axis right. */
    yNumOffset = (tic<0 ? tic*font_height : font_height) + tic*5 ; 
    nextnumloc = startX + 2*font_width;
    lastnumloc = endX - (4+3)*(font_width);
  }

  sprintf(s, "%4.2f    ",min);
  XDrawImageString(dpy,window,gc,startX+xNumOffset, startY+yNumOffset, s, 5);
  sprintf(s, "%4.2f    ",max);
  XDrawImageString(dpy,window,gc,endX+xNumOffset, endY+yNumOffset, s, 5);
  XDrawLine(dpy,window,gc, startX, startY, endX, endY);
  XDrawLine(dpy,window,gc, startX, startY,(int)(startX-sfY/myfabs(sfY)*tic*8),
	    (int)(startY-sfX/myfabs(sfX)*tic*8.));
  XDrawLine(dpy,window,gc, endX, endY, (int)(endX-sfY/myfabs(sfY)*tic*8.),
	    (int)(endY-sfX/myfabs(sfX)*tic*8.));


  for (next=ticint*ceil(min/ticint); next < max; next += ticint) {
    x = (next-min)*sfX+startX;
    y = (next-min)*sfY+startY;
    XDrawLine(dpy,window,gc, x, y,(int)(x-sfY/myfabs(sfY)*tic*4.), 
	      (int)(y-sfX/myfabs(sfX)*tic*4.));
    x += xNumOffset;
    y += yNumOffset;
    if (startX==endX || (x >= nextnumloc && x <= lastnumloc)) {
	sprintf(s, "%f    ", next);
	XDrawImageString(dpy,window,gc, x, y, s, 4);
	nextnumloc = x+5*font_width;
      }
  }
  ticint *= 0.2;
  for (next=ticint*ceil(min/ticint); next < max; next += ticint) {
    x = (next-min)*sfX+startX;
    y = (next-min)*sfY+startY;
    XDrawLine(dpy,window,gc, x, y,(int)(x-sfY/myfabs(sfY)*tic*2.), 
	  (int)(y-sfX/myfabs(sfX)*tic*2.));
  }
  return(sfX ? sfX : sfY);
}
