8 int whatpos(obj *p, int corner, double *px, double *py);
9 void makeattr(int type, int sub, YYSTYPE val);
10 YYSTYPE getblk(obj *, char *);
13 setdir(int n) /* set direction (hvmode) from LEFT, RIGHT, etc. */
16 case UP: hvmode = U_DIR; break;
17 case DOWN: hvmode = D_DIR; break;
18 case LEFT: hvmode = L_DIR; break;
19 case RIGHT: hvmode = R_DIR; break;
25 curdir(void) /* convert current dir (hvmode) to RIGHT, LEFT, etc. */
28 case R_DIR: return RIGHT;
29 case L_DIR: return LEFT;
30 case U_DIR: return UP;
31 case D_DIR: return DOWN;
33 ERROR "can't happen curdir" FATAL;
38 getcomp(obj *p, int t) /* return component of a position */
53 return 2 * p->o_val[0];
56 return p->o_val[0] - p->o_x;
68 return 2 * p->o_val[1];
71 return p->o_val[1] - p->o_y;
82 ERROR "you asked for a weird dimension or position" WARNING;
89 void exprsave(double f)
91 exprlist[nexpr++] = f;
94 char *sprintgen(char *fmt)
98 sprintf(buf, fmt, exprlist[0], exprlist[1], exprlist[2], exprlist[3], exprlist[4]);
101 return tostring(buf);
104 void makefattr(int type, int sub, double f) /* double attr */
108 makeattr(type, sub, val);
111 void makeoattr(int type, obj *o) /* obj* attr */
115 makeattr(type, 0, val);
118 void makeiattr(int type, int i) /* int attr */
122 makeattr(type, 0, val);
125 void maketattr(int sub, char *p) /* text attribute: takes two */
129 makeattr(TEXTATTR, sub, val);
132 void addtattr(int sub) /* add text attrib to existing item */
134 attr[nattr-1].a_sub |= sub;
137 void makevattr(char *p) /* varname attribute */
141 makeattr(VARNAME, 0, val);
144 void makeattr(int type, int sub, YYSTYPE val) /* add attribute type and val */
146 if (type == 0 && val.i == 0) { /* clear table for next stat */
150 if (nattr >= nattrlist)
151 attr = (Attr *) grow((char *)attr, "attr", nattrlist += 100, sizeof(Attr));
152 dprintf("attr %d: %d %d %d\n", nattr, type, sub, val.i);
153 attr[nattr].a_type = type;
154 attr[nattr].a_sub = sub;
155 attr[nattr].a_val = val;
159 void printexpr(double f) /* print expression for debugging */
164 void printpos(obj *p) /* print position for debugging */
166 printf("%g, %g\n", p->o_x, p->o_y);
169 char *tostring(char *s)
173 p = malloc(strlen(s)+1);
175 ERROR "out of space in tostring on %s", s FATAL;
180 obj *makepos(double x, double y) /* make a position cell */
184 p = makenode(PLACE, 0);
190 obj *makebetween(double f, obj *p1, obj *p2) /* make position between p1 and p2 */
194 dprintf("fraction = %.2f\n", f);
195 p = makenode(PLACE, 0);
196 p->o_x = p1->o_x + f * (p2->o_x - p1->o_x);
197 p->o_y = p1->o_y + f * (p2->o_y - p1->o_y);
201 obj *getpos(obj *p, int corner) /* find position of point */
205 whatpos(p, corner, &x, &y);
206 return makepos(x, y);
209 int whatpos(obj *p, int corner, double *px, double *py) /* what is the position (no side effect) */
213 x1 = y1 = 0.0; /* Botch? (gcc) */
215 dprintf("whatpos %p %d %d\n", (void*)p, p->o_type, corner);
218 if (p->o_type != PLACE && p->o_type != MOVE) {
229 case NORTH: y += y1 / 2; break;
230 case SOUTH: y -= y1 / 2; break;
231 case EAST: x += x1 / 2; break;
232 case WEST: x -= x1 / 2; break;
233 case NE: x += x1 / 2; y += y1 / 2; break;
234 case SW: x -= x1 / 2; y -= y1 / 2; break;
235 case SE: x += x1 / 2; y -= y1 / 2; break;
236 case NW: x -= x1 / 2; y += y1 / 2; break;
238 if (p->o_type == BLOCK)
239 return whatpos(objlist[(int)p->o_val[2]], START, px, py);
241 if (p->o_type == BLOCK)
242 return whatpos(objlist[(int)p->o_val[3]], END, px, py);
248 if (p->o_attr & CW_ARC) {
249 x = p->o_val[2]; y = p->o_val[3];
255 if (p->o_attr & CW_ARC) {
258 x = p->o_val[2]; y = p->o_val[3];
262 if (corner == START || corner == END)
264 x1 = y1 = sqrt((x1-x)*(x1-x) + (y1-y)*(y1-y));
269 case NORTH: y += y1; break;
270 case SOUTH: y -= y1; break;
271 case EAST: x += x1; break;
272 case WEST: x -= x1; break;
273 case NE: x += 0.707 * x1; y += 0.707 * y1; break;
274 case SE: x += 0.707 * x1; y -= 0.707 * y1; break;
275 case NW: x -= 0.707 * x1; y += 0.707 * y1; break;
276 case SW: x -= 0.707 * x1; y -= 0.707 * y1; break;
283 case START: break; /* already in place */
284 case END: x = x1; y = y1; break;
285 default: /* change! */
286 case CENTER: x = (x+x1)/2; y = (y+y1)/2; break;
287 case NORTH: if (y1 > y) { x = x1; y = y1; } break;
288 case SOUTH: if (y1 < y) { x = x1; y = y1; } break;
289 case EAST: if (x1 > x) { x = x1; y = y1; } break;
290 case WEST: if (x1 < x) { x = x1; y = y1; } break;
294 /* really ought to be same as line... */
297 dprintf("whatpos returns %g %g\n", x, y);
303 obj *gethere(void) /* make a place for curx,cury */
305 dprintf("gethere %g %g\n", curx, cury);
306 return(makepos(curx, cury));
309 obj *getlast(int n, int t) /* find n-th previous occurrence of type t */
315 for (i = nobj-1; i >= 0; i--) {
317 if (p->o_type == BLOCKEND) {
324 continue; /* not there yet */
325 dprintf("got a last of x,y= %g,%g\n", p->o_x, p->o_y);
328 ERROR "there is no %dth last", n WARNING;
332 obj *getfirst(int n, int t) /* find n-th occurrence of type t */
338 for (i = 0; i < nobj; i++) {
340 if (p->o_type == BLOCK && t != BLOCK) { /* skip whole block */
347 continue; /* not there yet */
348 dprintf("got a first of x,y= %g,%g\n", p->o_x, p->o_y);
351 ERROR "there is no %dth ", n WARNING;
355 double getblkvar(obj *p, char *s) /* find variable s2 in block p */
363 obj *getblock(obj *p, char *s) /* find variable s in block p */
371 YYSTYPE getblk(obj *p, char *s) /* find union type for s in p */
376 if (p->o_type != BLOCK) {
377 ERROR ".%s is not in that block", s WARNING;
380 for (stp = p->o_symtab; stp != NULL; stp = stp->s_next)
381 if (strcmp(s, stp->s_name) == 0) {
382 dprintf("getblk %s found x,y= %g,%g\n",
383 s, (stp->s_val.o)->o_x, (stp->s_val.o)->o_y);
386 ERROR "there is no .%s in that []", s WARNING;
390 obj *fixpos(obj *p, double x, double y)
392 dprintf("fixpos returns %g %g\n", p->o_x + x, p->o_y + y);
393 return makepos(p->o_x + x, p->o_y + y);
396 obj *addpos(obj *p, obj *q)
398 dprintf("addpos returns %g %g\n", p->o_x+q->o_x, p->o_y+q->o_y);
399 return makepos(p->o_x+q->o_x, p->o_y+q->o_y);
402 obj *subpos(obj *p, obj *q)
404 dprintf("subpos returns %g %g\n", p->o_x-q->o_x, p->o_y-q->o_y);
405 return makepos(p->o_x-q->o_x, p->o_y-q->o_y);
408 obj *makenode(int type, int n)
412 p = (obj *) calloc(1, sizeof(obj) + (n-1)*sizeof(ofloat));
414 ERROR "out of space in makenode" FATAL;
423 ntext1 = ntext; /* ready for next caller */
424 if (nobj >= nobjlist)
425 objlist = (obj **) grow((char *) objlist, "objlist",
426 nobjlist *= 2, sizeof(obj *));
431 void extreme(double x, double y) /* record max and min x and y values */