#define MAXTICK 200
int ntick = 0;
double tickval[MAXTICK]; /* tick values (one axis at a time */
char *tickstr[MAXTICK]; /* and labels */
int tside = 0;
int tlist = 0; /* 1 => explicit values given */
int toffside = 0; /* no ticks on these sides */
int goffside = 0; /* no ticks on grid on these sides */
int tick_dir = OUT;
double ticklen = TICKLEN; /* default tick length */
int autoticks = LEFT|BOT;
int autodir = 0; /* set LEFT, etc. if automatic ticks go in */
void ticks(void) /* set autoticks after ticks statement */
{
/* was there an explicit "ticks [side] off"? */
if (toffside)
autoticks &= ~toffside;
/* was there an explicit list? (eg "ticks at ..." or "ticks from ...") */
if (tlist) {
if (tside & (BOT|TOP))
autoticks &= ~(BOT|TOP);
if (tside & (LEFT|RIGHT))
autoticks &= ~(LEFT|RIGHT);
}
/* was there a side without a list? (eg "ticks left in") */
if (tside && !tlist) {
if (tick_dir == IN)
autodir |= tside;
if (tside & (BOT|TOP))
autoticks = (autoticks & ~(BOT|TOP)) | (tside & (BOT|TOP));
if (tside & (LEFT|RIGHT))
autoticks = (autoticks & ~(LEFT|RIGHT)) | (tside & (LEFT|RIGHT));
}
tlist = tside = toffside = goffside = 0;
tick_dir = OUT;
}
if (side == 'x') {
xtmin = lb = p->pt.x;
xtmax = ub = p->pt1.x;
} else {
ytmin = lb = p->pt.y;
ytmax = ub = p->pt1.y;
}
if (ub <= lb)
return; /* cop out on little ranges */
d = ub - lb;
r = s = 1;
while (d * s < 10)
s *= 10;
d *= s;
while (10 * r < d)
r *= 10;
if (r > d/3)
r /= 2;
else if (r <= d/6)
r *= 2;
if (side == 'x') {
xquant = r / s;
} else {
yquant = r / s;
}
}
void autolog(Obj *p, int side)
{
double r, s, t, ub, lb;
int flg;
if (side == 'x') {
xtmin = lb = p->pt.x;
xtmax = ub = p->pt1.x;
flg = p->coord & XFLAG;
} else {
ytmin = lb = p->pt.y;
ytmax = ub = p->pt1.y;
flg = p->coord & YFLAG;
}
for (s = 1; lb * s < 1; s *= 10)
;
lb *= s;
ub *= s;
for (r = 1; 10 * r < lb; r *= 10)
;
for (t = 1; t < ub; t *= 10)
;
if (side == 'x')
xquant = r / s;
else
yquant = r / s;
if (flg)
return;
if (ub / lb < 100) {
if (lb >= 5 * r)
r *= 5;
else if (lb >= 2 * r)
r *= 2;
if (ub * 5 <= t)
t /= 5;
else if (ub * 2 <= t)
t /= 2;
if (side == 'x') {
xtmin = r / s;
xtmax = t / s;
} else {
ytmin = r / s;
ytmax = t / s;
}
}
}
void iterator(double from, double to, int op, double by, char *fmt) /* create an iterator */
{
double x;
/* should validate limits, etc. */
/* punt for now */
dprintf("iterate from %g to %g by %g, op = %c, fmt=%s\n",
from, to, by, op, fmt ? fmt : "");
switch (op) {
case '+':
case ' ':
for (x = from; x <= to + (SLOP-1) * by; x += by)
if (fmt)
savetick(x, tostring(fmt));
else
dflt_tick(x);
break;
case '-':
for (x = from; x >= to; x -= by)
if (fmt)
savetick(x, tostring(fmt));
else
dflt_tick(x);
break;
case '*':
for (x = from; x <= SLOP * to; x *= by)
if (fmt)
savetick(x, tostring(fmt));
else
dflt_tick(x);
break;
case '/':
for (x = from; x >= to; x /= by)
if (fmt)
savetick(x, tostring(fmt));
else
dflt_tick(x);
break;
}
if (fmt)
free(fmt);
}
void ticklist(Obj *p, int explicit) /* fire out the accumulated ticks */
/* 1 => list, 0 => auto */
{
if (p == NULL)
return;
fprintf(tfd, "Ticks_%s:\n\tticklen = %g\n", p->name, ticklen);
print_ticks(TICKS, explicit, p, "ticklen", "");
}
void print_ticks(int type, int explicit, Obj *p, char *lenstr, char *descstr)
{
int i, logflag, inside;
char buf[100];
double tv;
for (i = 0; i < ntick; i++) /* any ticks given explicitly? */
if (tickstr[i] != NULL)
break;
if (i >= ntick && type == TICKS) /* no, so use values */
for (i = 0; i < ntick; i++) {
if (tickval[i] >= 0.0)
sprintf(buf, "%g", tickval[i]);
else
sprintf(buf, "\\-%g", -tickval[i]);
tickstr[i] = tostring(buf);
}
else
for (i = 0; i < ntick; i++) {
if (tickstr[i] != NULL) {
sprintf(buf, tickstr[i], tickval[i]);
free(tickstr[i]);
tickstr[i] = tostring(buf);
}
}
logflag = sidelog(p->log, tside);
for (i = 0; i < ntick; i++) {
tv = tickval[i];
halfrange(p, tside, tv);
if (logflag) {
if (tv <= 0.0)
ERROR "can't take log of tick value %g", tv FATAL;
logit(tv);
}
if (type == GRID)
inside = LEFT|RIGHT|TOP|BOT;
else if (explicit)
inside = (tick_dir == IN) ? tside : 0;
else
inside = autodir;
if (tside & BOT)
maketick(type, p->name, BOT, inside, tv, tickstr[i], lenstr, descstr);
if (tside & TOP)
maketick(type, p->name, TOP, inside, tv, tickstr[i], lenstr, descstr);
if (tside & LEFT)
maketick(type, p->name, LEFT, inside, tv, tickstr[i], lenstr, descstr);
if (tside & RIGHT)
maketick(type, p->name, RIGHT, inside, tv, tickstr[i], lenstr, descstr);
if (tickstr[i]) {
free(tickstr[i]);
tickstr[i] = NULL;
}
}
ntick = 0;
}
char *desc_str(Attr *a) /* convert DOT to "dotted", etc. */
{
static char buf[50], *p;
if (a == NULL)
return p = "";
switch (a->type) {
case DOT: p = "dotted"; break;
case DASH: p = "dashed"; break;
case INVIS: p = "invis"; break;
default: p = "";
}
if (a->fval != 0.0) {
sprintf(buf, "%s %g", p, a->fval);
return buf;
} else
return p;
}
sidelog(int logflag, int side) /* figure out whether to scale a side */
{
if ((logflag & XFLAG) && ((side & (BOT|TOP)) || side == 0))
return 1;
else if ((logflag & YFLAG) && (side & (LEFT|RIGHT)))
return 1;
else
return 0;
}