10 double tickval[MAXTICK]; /* tick values (one axis at a time */
11 char *tickstr[MAXTICK]; /* and labels */
14 int tlist = 0; /* 1 => explicit values given */
15 int toffside = 0; /* no ticks on these sides */
16 int goffside = 0; /* no ticks on grid on these sides */
18 double ticklen = TICKLEN; /* default tick length */
19 int autoticks = LEFT|BOT;
20 int autodir = 0; /* set LEFT, etc. if automatic ticks go in */
22 void savetick(double f, char *s) /* remember tick location and label */
25 ERROR "too many ticks (%d)", MAXTICK FATAL;
31 void dflt_tick(double f)
34 savetick(f, tostring("%g"));
36 savetick(f, tostring("\\%g"));
39 void tickside(int n) /* remember which side these ticks/gridlines go on */
44 void tickoff(int side) /* remember explicit sides */
49 void gridtickoff(void) /* turn grid ticks off on the side previously specified (ugh) */
54 void setlist(void) /* remember that there was an explicit list */
59 void tickdir(int dir, double val, int explicit) /* remember in/out [expr] */
66 void ticks(void) /* set autoticks after ticks statement */
68 /* was there an explicit "ticks [side] off"? */
70 autoticks &= ~toffside;
71 /* was there an explicit list? (eg "ticks at ..." or "ticks from ...") */
73 if (tside & (BOT|TOP))
74 autoticks &= ~(BOT|TOP);
75 if (tside & (LEFT|RIGHT))
76 autoticks &= ~(LEFT|RIGHT);
78 /* was there a side without a list? (eg "ticks left in") */
79 if (tside && !tlist) {
82 if (tside & (BOT|TOP))
83 autoticks = (autoticks & ~(BOT|TOP)) | (tside & (BOT|TOP));
84 if (tside & (LEFT|RIGHT))
85 autoticks = (autoticks & ~(LEFT|RIGHT)) | (tside & (LEFT|RIGHT));
87 tlist = tside = toffside = goffside = 0;
91 double modfloor(double f, double t)
94 return floor(f/t) * t;
97 double modceil(double f, double t)
100 return ceil(f/t) * t;
103 double xtmin, xtmax; /* range of ticks */
105 double xquant, xmult; /* quantization & scale for auto x ticks */
106 double yquant, ymult;
109 void do_autoticks(Obj *p) /* make set of ticks for default coord only */
115 fprintf(tfd, "Autoticks:\t# x %g..%g, y %g..%g",
116 p->pt.x, p->pt1.x, p->pt.y, p->pt1.y);
117 fprintf(tfd, "; xt %g,%g, yt %g,%g, xq,xm = %g,%g, yq,ym = %g,%g\n",
118 xtmin, xtmax, ytmin, ytmax, xquant, xmult, yquant, ymult);
119 if ((autoticks & (BOT|TOP)) && p->pt1.x >= p->pt.x) { /* make x ticks */
125 else if ((p->log & XFLAG) && xu/xl >= lograt) {
126 for (x = q; x < xu; x *= 10) {
129 logtick(2*x, xl, xu);
130 logtick(5*x, xl, xu);
134 xl = modceil(xtmin - q/100, q);
135 xu = modfloor(xtmax + q/100, q) + q/2;
136 for (x = xl; x <= xu; x += q)
139 tside = autoticks & (BOT|TOP);
142 if ((autoticks & (LEFT|RIGHT)) && p->pt1.y >= p->pt.y) { /* make y ticks */
148 else if ((p->log & YFLAG) && xu/xl >= lograt) {
149 for (x = q; x < xu; x *= 10) {
152 logtick(2*x, xl, xu);
153 logtick(5*x, xl, xu);
157 xl = modceil(ytmin - q/100, q);
158 xu = modfloor(ytmax + q/100, q) + q/2;
159 for (x = xl; x <= xu; x += q)
162 tside = autoticks & (LEFT|RIGHT);
167 void logtick(double v, double lb, double ub)
169 float slop = 1.0; /* was 1.001 */
171 if (slop * lb <= v && ub >= slop * v)
175 Obj *setauto(void) /* compute new min,max, and quant & mult */
179 if ((q = lookup("lograt",0)) != NULL)
181 for (p = objlist; p; p = p->next)
182 if (p->type == NAME && strcmp(p->name,dflt_coord) == 0)
185 if ((p->log & XFLAG) && p->pt1.x/p->pt.x >= lograt)
189 if ((p->log & YFLAG) && p->pt1.y/p->pt.y >= lograt)
197 void autoside(Obj *p, int side)
199 double r, s, d, ub, lb;
202 xtmin = lb = p->pt.x;
203 xtmax = ub = p->pt1.x;
205 ytmin = lb = p->pt.y;
206 ytmax = ub = p->pt1.y;
209 return; /* cop out on little ranges */
228 void autolog(Obj *p, int side)
230 double r, s, t, ub, lb;
234 xtmin = lb = p->pt.x;
235 xtmax = ub = p->pt1.x;
236 flg = p->coord & XFLAG;
238 ytmin = lb = p->pt.y;
239 ytmax = ub = p->pt1.y;
240 flg = p->coord & YFLAG;
242 for (s = 1; lb * s < 1; s *= 10)
246 for (r = 1; 10 * r < lb; r *= 10)
248 for (t = 1; t < ub; t *= 10)
259 else if (lb >= 2 * r)
263 else if (ub * 2 <= t)
275 void iterator(double from, double to, int op, double by, char *fmt) /* create an iterator */
279 /* should validate limits, etc. */
282 dprintf("iterate from %g to %g by %g, op = %c, fmt=%s\n",
283 from, to, by, op, fmt ? fmt : "");
287 for (x = from; x <= to + (SLOP-1) * by; x += by)
289 savetick(x, tostring(fmt));
294 for (x = from; x >= to; x -= by)
296 savetick(x, tostring(fmt));
301 for (x = from; x <= SLOP * to; x *= by)
303 savetick(x, tostring(fmt));
308 for (x = from; x >= to; x /= by)
310 savetick(x, tostring(fmt));
319 void ticklist(Obj *p, int explicit) /* fire out the accumulated ticks */
320 /* 1 => list, 0 => auto */
324 fprintf(tfd, "Ticks_%s:\n\tticklen = %g\n", p->name, ticklen);
325 print_ticks(TICKS, explicit, p, "ticklen", "");
328 void print_ticks(int type, int explicit, Obj *p, char *lenstr, char *descstr)
330 int i, logflag, inside;
334 for (i = 0; i < ntick; i++) /* any ticks given explicitly? */
335 if (tickstr[i] != NULL)
337 if (i >= ntick && type == TICKS) /* no, so use values */
338 for (i = 0; i < ntick; i++) {
339 if (tickval[i] >= 0.0)
340 sprintf(buf, "%g", tickval[i]);
342 sprintf(buf, "\\-%g", -tickval[i]);
343 tickstr[i] = tostring(buf);
346 for (i = 0; i < ntick; i++) {
347 if (tickstr[i] != NULL) {
348 sprintf(buf, tickstr[i], tickval[i]);
350 tickstr[i] = tostring(buf);
353 logflag = sidelog(p->log, tside);
354 for (i = 0; i < ntick; i++) {
356 halfrange(p, tside, tv);
359 ERROR "can't take log of tick value %g", tv FATAL;
363 inside = LEFT|RIGHT|TOP|BOT;
365 inside = (tick_dir == IN) ? tside : 0;
369 maketick(type, p->name, BOT, inside, tv, tickstr[i], lenstr, descstr);
371 maketick(type, p->name, TOP, inside, tv, tickstr[i], lenstr, descstr);
373 maketick(type, p->name, LEFT, inside, tv, tickstr[i], lenstr, descstr);
375 maketick(type, p->name, RIGHT, inside, tv, tickstr[i], lenstr, descstr);
384 void maketick(int type, char *name, int side, int inflag, double val, char *lab, char *lenstr, char *descstr)
388 fprintf(tfd, "\tline %s ", descstr);
393 td = inflag ? "up" : "down";
394 fprintf(tfd, "%s %s from (x_%s(%g),0)", td, lenstr, name, val);
397 td = inflag ? "down" : "up";
398 fprintf(tfd, "%s %s from (x_%s(%g),frameht)", td, lenstr, name, val);
401 td = inflag ? "right" : "left";
402 fprintf(tfd, "%s %s from (0,y_%s(%g))", td, lenstr, name, val);
405 td = inflag ? "left" : "right";
406 fprintf(tfd, "%s %s from (framewid,y_%s(%g))", td, lenstr, name, val);
410 if (type == GRID && (side & goffside)) /* wanted no ticks on grid */
412 sidestr = tick_dir == IN ? "start" : "end";
414 /* BUG: should fix size of lab here */
415 double wid = strlen(lab)/7.5 + (tick_dir == IN ? 0 : 0.1); /* estimate width at 15 chars/inch */
418 /* can drop "box invis" with new pic */
419 fprintf(tfd, "\tbox invis \"%s\" ht .25 wid 0 with .n at last line.%s",
423 fprintf(tfd, "\tbox invis \"%s\" ht .2 wid 0 with .s at last line.%s",
427 fprintf(tfd, "\t\"%s \" wid %.2f rjust at last line.%s",
431 fprintf(tfd, "\t\" %s\" wid %.2f ljust at last line.%s",
435 /* BUG: works only if "down x" comes before "at wherever" */
443 void griddesc(Attr *a)
448 void gridlist(Obj *p)
452 if ((tside & (BOT|TOP)) || tside == 0)
453 framestr = "frameht";
455 framestr = "framewid";
456 fprintf(tfd, "Grid_%s:\n", p->name);
458 print_ticks(GRID, 0, p, framestr, desc_str(grid_desc));
465 char *desc_str(Attr *a) /* convert DOT to "dotted", etc. */
467 static char buf[50], *p;
472 case DOT: p = "dotted"; break;
473 case DASH: p = "dashed"; break;
474 case INVIS: p = "invis"; break;
477 if (a->fval != 0.0) {
478 sprintf(buf, "%s %g", p, a->fval);
485 sidelog(int logflag, int side) /* figure out whether to scale a side */
487 if ((logflag & XFLAG) && ((side & (BOT|TOP)) || side == 0))
489 else if ((logflag & YFLAG) && (side & (LEFT|RIGHT)))