xref: /plan9/sys/src/cmd/pic/circgen.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 #include	<stdio.h>
2 #include	"pic.h"
3 #include	"y.tab.h"
4 
circgen(int type)5 obj *circgen(int type)
6 {
7 	static double rad[2] = { HT2, WID2 };
8 	static double rad2[2] = { HT2, HT2 };
9 	int i, at, t, with, battr;
10 	double xwith, ywith;
11 	double r, r2, ddval, fillval;
12 	obj *p, *ppos;
13 	Attr *ap;
14 
15 	battr = at = 0;
16 	with = xwith = ywith = fillval = ddval = 0;
17 	t = (type == CIRCLE) ? 0 : 1;
18 	if (type == CIRCLE)
19 		r = r2 = getfval("circlerad");
20 	else if (type == ELLIPSE) {
21 		r = getfval("ellipsewid") / 2;
22 		r2 = getfval("ellipseht") / 2;
23 	}
24 	for (i = 0; i < nattr; i++) {
25 		ap = &attr[i];
26 		switch (ap->a_type) {
27 		case TEXTATTR:
28 			savetext(ap->a_sub, ap->a_val.p);
29 			break;
30 		case RADIUS:
31 			r = ap->a_val.f;
32 			break;
33 		case DIAMETER:
34 		case WIDTH:
35 			r = ap->a_val.f / 2;
36 			break;
37 		case HEIGHT:
38 			r2 = ap->a_val.f / 2;
39 			break;
40 		case SAME:
41 			r = rad[t];
42 			r2 = rad2[t];
43 			break;
44 		case WITH:
45 			with = ap->a_val.i;
46 			break;
47 		case AT:
48 			ppos = ap->a_val.o;
49 			curx = ppos->o_x;
50 			cury = ppos->o_y;
51 			at++;
52 			break;
53 		case INVIS:
54 			battr |= INVIS;
55 			break;
56 		case NOEDGE:
57 			battr |= NOEDGEBIT;
58 			break;
59 		case DOT:
60 		case DASH:
61 			battr |= ap->a_type==DOT ? DOTBIT : DASHBIT;
62 			if (ap->a_sub == DEFAULT)
63 				ddval = getfval("dashwid");
64 			else
65 				ddval = ap->a_val.f;
66 			break;
67 		case FILL:
68 			battr |= FILLBIT;
69 			if (ap->a_sub == DEFAULT)
70 				fillval = getfval("fillval");
71 			else
72 				fillval = ap->a_val.f;
73 			break;
74 		}
75 	}
76 	if (type == CIRCLE)
77 		r2 = r;	/* probably superfluous */
78 	if (with) {
79 		switch (with) {
80 		case NORTH:	ywith = -r2; break;
81 		case SOUTH:	ywith = r2; break;
82 		case EAST:	xwith = -r; break;
83 		case WEST:	xwith = r; break;
84 		case NE:	xwith = -r * 0.707; ywith = -r2 * 0.707; break;
85 		case SE:	xwith = -r * 0.707; ywith = r2 * 0.707; break;
86 		case NW:	xwith = r * 0.707; ywith = -r2 * 0.707; break;
87 		case SW:	xwith = r * 0.707; ywith = r2 * 0.707; break;
88 		}
89 		curx += xwith;
90 		cury += ywith;
91 	}
92 	if (!at) {
93 		if (isright(hvmode))
94 			curx += r;
95 		else if (isleft(hvmode))
96 			curx -= r;
97 		else if (isup(hvmode))
98 			cury += r2;
99 		else
100 			cury -= r2;
101 	}
102 	p = makenode(type, 2);
103 	p->o_val[0] = rad[t] = r;
104 	p->o_val[1] = rad2[t] = r2;
105 	if (r <= 0 || r2 <= 0) {
106 		ERROR "%s has invalid radius %g\n", (type==CIRCLE) ? "circle" : "ellipse", r<r2 ? r : r2 WARNING;
107 	}
108 	p->o_attr = battr;
109 	p->o_ddval = ddval;
110 	p->o_fillval = fillval;
111 	extreme(curx+r, cury+r2);
112 	extreme(curx-r, cury-r2);
113 	if (type == CIRCLE)
114 		dprintf("C %g %g %g\n", curx, cury, r);
115 	if (type == ELLIPSE)
116 		dprintf("E %g %g %g %g\n", curx, cury, r, r2);
117 	if (isright(hvmode))
118 		curx += r;
119 	else if (isleft(hvmode))
120 		curx -= r;
121 	else if (isup(hvmode))
122 		cury += r2;
123 	else
124 		cury -= r2;
125 	return(p);
126 }
127