1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: plotin.c,v 1.1 2003/02/22 02:07:26 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Program to convert plot(5) files to metafiles |
6 |
* |
7 |
* cc -o plotin plotin.c primout.o mfio.o syscalls.o misc.o -lm |
8 |
* |
9 |
* 5/20/85 |
10 |
*/ |
11 |
|
12 |
#include <string.h> |
13 |
|
14 |
#include "meta.h" |
15 |
#include "plot.h" |
16 |
|
17 |
#define getsi(f, m) ICONV(geti(f)-(m), size) |
18 |
|
19 |
|
20 |
|
21 |
char *progname; |
22 |
|
23 |
static int xmin = 0, ymin = 0, /* current space settings */ |
24 |
size = XYSIZE; |
25 |
|
26 |
static int curx = -1, /* current position */ |
27 |
cury = -1; |
28 |
|
29 |
static short curmod = 0; /* current line drawing mode */ |
30 |
|
31 |
static void convert(FILE *infp); |
32 |
static int geti(FILE *fp); |
33 |
static int quadr(int x, int y, int xp, int yp); |
34 |
static void step(int d); |
35 |
|
36 |
static void linemod(char s[]); |
37 |
static void erase(void); |
38 |
static void space(int xmn, int ymn, int xmx, int ymx); |
39 |
static void move(int x, int y); |
40 |
static void cont(int x, int y); |
41 |
static void line(int x0, int y0, int x1, int y1); |
42 |
static void label(register char *s); |
43 |
static void point(int x, int y); |
44 |
static void arc(int x, int y, int x0, int y0, int x1, int y1); |
45 |
static void circle(int x, int y, int r); |
46 |
|
47 |
|
48 |
int |
49 |
main( |
50 |
int argc, |
51 |
char **argv |
52 |
) |
53 |
{ |
54 |
FILE *fp; |
55 |
|
56 |
progname = *argv++; |
57 |
argc--; |
58 |
|
59 |
while (argc && **argv == '-') { |
60 |
switch (*(*argv+1)) { |
61 |
default: |
62 |
error(WARNING, "unknown option"); |
63 |
break; |
64 |
} |
65 |
argv++; |
66 |
argc--; |
67 |
} |
68 |
|
69 |
if (argc) |
70 |
while (argc) { |
71 |
fp = efopen(*argv, "r"); |
72 |
convert(fp); |
73 |
fclose(fp); |
74 |
argv++; |
75 |
argc--; |
76 |
} |
77 |
else |
78 |
convert(stdin); |
79 |
|
80 |
pglob(PEOF, 0200, NULL); |
81 |
|
82 |
return(0); |
83 |
} |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
void |
89 |
convert( /* convert to meta-file */ |
90 |
FILE *infp |
91 |
) |
92 |
{ |
93 |
char *fgets(), sbuf[BUFSIZ]; |
94 |
int command; |
95 |
int a1, a2, a3, a4, a5, a6; |
96 |
|
97 |
while ((command = getc(infp)) != EOF) |
98 |
switch (command) { |
99 |
case 'm': |
100 |
a1 = getsi(infp, xmin); |
101 |
a2 = getsi(infp, ymin); |
102 |
move(a1, a2); |
103 |
break; |
104 |
case 'n': |
105 |
a1 = getsi(infp, xmin); |
106 |
a2 = getsi(infp, ymin); |
107 |
cont(a1, a2); |
108 |
break; |
109 |
case 'p': |
110 |
a1 = getsi(infp, xmin); |
111 |
a2 = getsi(infp, ymin); |
112 |
point(a1, a2); |
113 |
break; |
114 |
case 'l': |
115 |
a1 = getsi(infp, xmin); |
116 |
a2 = getsi(infp, ymin); |
117 |
a3 = getsi(infp, xmin); |
118 |
a4 = getsi(infp, ymin); |
119 |
line(a1, a2, a3, a4); |
120 |
break; |
121 |
case 't': |
122 |
fgets(sbuf, sizeof(sbuf), infp); |
123 |
sbuf[strlen(sbuf)-1] = '\0'; |
124 |
label(sbuf); |
125 |
break; |
126 |
case 'a': |
127 |
a1 = getsi(infp, xmin); |
128 |
a2 = getsi(infp, ymin); |
129 |
a3 = getsi(infp, xmin); |
130 |
a4 = getsi(infp, ymin); |
131 |
a5 = getsi(infp, xmin); |
132 |
a6 = getsi(infp, ymin); |
133 |
arc(a1, a2, a3, a4, a5, a6); |
134 |
break; |
135 |
case 'c': |
136 |
a1 = getsi(infp, xmin); |
137 |
a2 = getsi(infp, ymin); |
138 |
a3 = getsi(infp, 0); |
139 |
circle(a1, a2, a3); |
140 |
break; |
141 |
case 'e': |
142 |
erase(); |
143 |
break; |
144 |
case 'f': |
145 |
fgets(sbuf, sizeof(sbuf), infp); |
146 |
linemod(sbuf); |
147 |
break; |
148 |
case 's': |
149 |
a1 = geti(infp); |
150 |
a2 = geti(infp); |
151 |
a3 = geti(infp); |
152 |
a4 = geti(infp); |
153 |
space(a1, a2, a3, a4); |
154 |
break; |
155 |
default: |
156 |
error(USER, "unknown command in convert"); |
157 |
break; |
158 |
} |
159 |
} |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
int |
165 |
geti( /* get two-byte integer from file */ |
166 |
register FILE *fp |
167 |
) |
168 |
{ |
169 |
register int i; |
170 |
|
171 |
i = getc(fp); |
172 |
i |= getc(fp) << 8; |
173 |
if (i & 0100000) |
174 |
i |= ~0177777; /* manual sign extend */ |
175 |
if (feof(fp)) |
176 |
error(USER, "unexpected end of file in geti"); |
177 |
|
178 |
return(i); |
179 |
} |
180 |
|
181 |
|
182 |
|
183 |
void |
184 |
move( /* move to new position */ |
185 |
register int x, |
186 |
register int y |
187 |
) |
188 |
{ |
189 |
curx = x; |
190 |
cury = y; |
191 |
} |
192 |
|
193 |
|
194 |
|
195 |
void |
196 |
cont( /* draw line to point */ |
197 |
register int x, |
198 |
register int y |
199 |
) |
200 |
{ |
201 |
plseg(curmod, curx, cury, x, y); |
202 |
curx = x; |
203 |
cury = y; |
204 |
} |
205 |
|
206 |
|
207 |
void |
208 |
point( /* draw a point */ |
209 |
register int x, |
210 |
register int y |
211 |
) |
212 |
{ |
213 |
plseg(0, x, y, x, y); |
214 |
curx = x; |
215 |
cury = y; |
216 |
} |
217 |
|
218 |
|
219 |
|
220 |
void |
221 |
line( /* draw a line segment */ |
222 |
int x0, |
223 |
int y0, |
224 |
int x1, |
225 |
int y1 |
226 |
) |
227 |
{ |
228 |
move(x0, y0); |
229 |
cont(x1, y1); |
230 |
} |
231 |
|
232 |
|
233 |
void |
234 |
label( /* print a label */ |
235 |
register char *s |
236 |
) |
237 |
{ |
238 |
pprim(PMSTR, 0, curx, cury, curx, cury, s); |
239 |
} |
240 |
|
241 |
|
242 |
|
243 |
static int del = 100; |
244 |
|
245 |
void |
246 |
step( |
247 |
int d |
248 |
) |
249 |
{ |
250 |
del = d; |
251 |
} |
252 |
|
253 |
void |
254 |
arc( |
255 |
int x, |
256 |
int y, |
257 |
int x0, |
258 |
int y0, |
259 |
int x1, |
260 |
int y1 |
261 |
) |
262 |
{ |
263 |
double pc; |
264 |
double sqrt(); |
265 |
int flg,m,xc,yc,xs,ys,qs,qf; |
266 |
float dx,dy,r; |
267 |
char use; |
268 |
dx = x-x0; |
269 |
dy = y-y0; |
270 |
r = dx*dx+dy*dy; |
271 |
pc = r; |
272 |
pc = sqrt(pc); |
273 |
flg = pc/4; |
274 |
if(flg == 0)step(1); |
275 |
else if(flg < del)step(flg); |
276 |
xc = xs = x0; |
277 |
yc = ys = y0; |
278 |
move(xs,ys); |
279 |
if(x0 == x1 && y0 == y1)flg=0; |
280 |
else flg=1; |
281 |
qs = quadr(x,y,x0,y0); |
282 |
qf = quadr(x,y,x1,y1); |
283 |
if(abs(x-x1) < abs(y-y1)){ |
284 |
use = 'x'; |
285 |
if(qs == 2 || qs ==3)m = -1; |
286 |
else m=1; |
287 |
} |
288 |
else { |
289 |
use = 'y'; |
290 |
if(qs > 2)m= -1; |
291 |
else m= 1; |
292 |
} |
293 |
while(1){ |
294 |
switch(use){ |
295 |
case 'x': |
296 |
if(qs == 2 || qs == 3)yc -= del; |
297 |
else yc += del; |
298 |
dy = yc-y; |
299 |
pc = r-dy*dy; |
300 |
xc = m*sqrt(pc)+x; |
301 |
if((x < xs && x >= xc) || ( x > xs && x <= xc) || |
302 |
(y < ys && y >= yc) || ( y > ys && y <= yc) ) |
303 |
{ |
304 |
if(++qs > 4)qs=1; |
305 |
if(qs == 2 || qs == 3)m= -1; |
306 |
else m=1; |
307 |
flg=1; |
308 |
} |
309 |
cont(xc,yc); |
310 |
xs = xc; |
311 |
ys = yc; |
312 |
if(qs == qf && flg == 1) |
313 |
switch(qf){ |
314 |
case 3: |
315 |
case 4: |
316 |
if(xs >= x1)return; |
317 |
continue; |
318 |
case 1: |
319 |
case 2: |
320 |
if(xs <= x1)return; |
321 |
} |
322 |
continue; |
323 |
case 'y': |
324 |
if(qs > 2)xc += del; |
325 |
else xc -= del; |
326 |
dx = xc-x; |
327 |
pc = r-dx*dx; |
328 |
yc = m*sqrt(pc)+y; |
329 |
if((x < xs && x >= xc) || ( x > xs && x <= xc ) || |
330 |
(y < ys && y >= yc) || (y > ys && y <= yc) ) |
331 |
{ |
332 |
if(++qs > 4)qs=1; |
333 |
if(qs > 2)m = -1; |
334 |
else m = 1; |
335 |
flg=1; |
336 |
} |
337 |
cont(xc,yc); |
338 |
xs = xc; |
339 |
ys = yc; |
340 |
if(qs == qf && flg == 1) |
341 |
switch(qs){ |
342 |
case 1: |
343 |
case 4: |
344 |
if(ys >= y1)return; |
345 |
continue; |
346 |
case 2: |
347 |
case 3: |
348 |
if(ys <= y1)return; |
349 |
} |
350 |
} |
351 |
} |
352 |
} |
353 |
|
354 |
int |
355 |
quadr( |
356 |
int x, |
357 |
int y, |
358 |
int xp, |
359 |
int yp |
360 |
) |
361 |
{ |
362 |
if(x < xp) |
363 |
if(y <= yp)return(1); |
364 |
else return(4); |
365 |
else if(x > xp) |
366 |
if(y < yp)return(2); |
367 |
else return(3); |
368 |
else if(y < yp)return(2); |
369 |
else return(4); |
370 |
} |
371 |
|
372 |
|
373 |
void |
374 |
circle( |
375 |
int x, |
376 |
int y, |
377 |
int r |
378 |
) |
379 |
{ |
380 |
arc(x,y,x+r,y,x+r,y); |
381 |
} |
382 |
|
383 |
|
384 |
|
385 |
void |
386 |
erase(void) /* erase plot */ |
387 |
{ |
388 |
pglob(PEOP, 0200, NULL); |
389 |
} |
390 |
|
391 |
|
392 |
void |
393 |
linemod( /* set line mode according to s */ |
394 |
char s[] |
395 |
) |
396 |
{ |
397 |
switch (s[2]) { |
398 |
case 'l': /* solid */ |
399 |
curmod = 0; |
400 |
break; |
401 |
case 't': /* dotted */ |
402 |
curmod = 1; |
403 |
break; |
404 |
case 'n': /* long dashed */ |
405 |
curmod = 2; |
406 |
break; |
407 |
case 'o': /* short dashed (dot dashed) */ |
408 |
curmod = 3; |
409 |
break; |
410 |
default: |
411 |
error(WARNING, "unknown line mode in linemod"); |
412 |
break; |
413 |
} |
414 |
} |
415 |
|
416 |
|
417 |
void |
418 |
space( /* change space */ |
419 |
int xmn, |
420 |
int ymn, |
421 |
int xmx, |
422 |
int ymx |
423 |
) |
424 |
{ |
425 |
if (xmn >= xmx || ymn >= ymx) |
426 |
error(USER, "illegal space specification in space"); |
427 |
|
428 |
xmin = xmn; |
429 |
ymin = ymn; |
430 |
size = min(xmx-xmn, ymx-ymn); |
431 |
} |