xref: /plan9-contrib/sys/src/cmd/graph/graph.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <stdio.h>
4 #include "iplot.h"
5 #define	INF	1.e+37
6 #define	F	.25
7 
8 struct xy {
9 	int	xlbf;		/*flag:explicit lower bound*/
10 	int 	xubf;		/*flag:explicit upper bound*/
11 	int	xqf;		/*flag:explicit quantum*/
12 	double (*xf)(double);	/*transform function, e.g. log*/
13 	float	xa,xb;		/*scaling coefficients*/
14 	float	xlb,xub;	/*lower and upper bound*/
15 	float	xquant;		/*quantum*/
16 	float	xoff;		/*screen offset fraction*/
17 	float	xsize;		/*screen fraction*/
18 	int	xbot,xtop;	/*screen coords of border*/
19 	float	xmult;		/*scaling constant*/
20 } xd,yd;
21 struct val {
22 	float xv;
23 	float yv;
24 	int lblptr;
25 } *xx;
26 
27 char *labels;
28 int labelsiz;
29 
30 int tick = 50;
31 int top = 4000;
32 int bot = 200;
33 float absbot;
34 int	n;
35 int	erasf = 1;
36 int	gridf = 2;
37 int	symbf = 0;
38 int	absf = 0;
39 int	transf;
40 int	equf;
41 int	brkf;
42 int	ovlay = 1;
43 float	dx;
44 char	*plotsymb;
45 
46 #define BSIZ 80
47 char	labbuf[BSIZ];
48 char	titlebuf[BSIZ];
49 
50 char *modes[] = {
51 	"disconnected",
52 	"solid",
53 	"dotted",
54 	"dotdashed",
55 	"shortdashed",
56 	"longdashed"
57 };
58 int mode = 1;
59 double ident(double x){
60 	return(x);
61 }
62 
63 struct z {
64 	float lb,ub,mult,quant;
65 };
66 init(struct xy *);
67 setopt(int, char *[]);
68 readin(void);
69 transpose(void);
70 getlim(struct xy *, struct val *);
71 equilibrate(struct xy *, struct xy *);
72 scale(struct xy *);
73 limread(struct xy *, int *, char ***);
74 numb(float *, int *, char ***);
75 copystring(int);
76 struct z setloglim(int, int, float, float);
77 struct z setlinlim(int, int, float, float);
78 axes(void);
79 setmark(int *, struct xy *);
80 submark(int *, int *, float, struct xy *);
81 plot(void);
82 getfloat(float *);
83 getstring(void);
84 title(void);
85 badarg(void);
86 conv(float, struct xy *, int *);
87 symbol(int, int, int);
88 axlab(char, struct xy *, char *);
89 main(int argc,char *argv[]){
90 
91 	openpl();
92 	range(0,0,4096,4096);
93 	init(&xd);
94 	init(&yd);
95 	xd.xsize = yd.xsize = 1.;
96 	xx = (struct val *)malloc((unsigned)sizeof(struct val));
97 	labels = malloc(1);
98 	labels[labelsiz++] = 0;
99 	setopt(argc,argv);
100 	if(erasf)
101 		erase();
102 	readin();
103 	transpose();
104 	getlim(&xd,(struct val *)&xx->xv);
105 	getlim(&yd,(struct val *)&xx->yv);
106 	if(equf) {
107 		equilibrate(&xd,&yd);
108 		equilibrate(&yd,&xd);
109 	}
110 	scale(&xd);
111 	scale(&yd);
112 	axes();
113 	title();
114 	plot();
115 	closepl();
116 	exits(0);
117 }
118 
119 init(struct xy *p){
120 	p->xf = ident;
121 	p->xmult = 1;
122 }
123 
124 setopt(int argc, char *argv[]){
125 	char *p1, *p2;
126 	float temp;
127 
128 	xd.xlb = yd.xlb = INF;
129 	xd.xub = yd.xub = -INF;
130 	while(--argc > 0) {
131 		argv++;
132 again:		switch(argv[0][0]) {
133 		case '-':
134 			argv[0]++;
135 			goto again;
136 		case 'l': /* label for plot */
137 			p1 = titlebuf;
138 			if (argc>=2) {
139 				argv++;
140 				argc--;
141 				p2 = argv[0];
142 				while (*p1++ = *p2++);
143 			}
144 			break;
145 
146 		case 'd':	/*disconnected,obsolete option*/
147 		case 'm': /*line mode*/
148 			mode = 0;
149 			if(!numb(&temp,&argc,&argv))
150 				break;
151 			if(temp>=sizeof(modes)/sizeof(*modes))
152 				mode = 1;
153 			else if(temp>=-1)
154 				mode = temp;
155 			break;
156 
157 		case 'o':
158 			if(numb(&temp,&argc,&argv) && temp>=1)
159 				ovlay = temp;
160 			break;
161 		case 'a': /*automatic abscissas*/
162 			absf = 1;
163 			dx = 1;
164 			if(!numb(&dx,&argc,&argv))
165 				break;
166 			if(numb(&absbot,&argc,&argv))
167 				absf = 2;
168 			break;
169 
170 		case 's': /*save screen, overlay plot*/
171 			erasf = 0;
172 			break;
173 
174 		case 'g': /*grid style 0 none, 1 ticks, 2 full*/
175 			gridf = 0;
176 			if(!numb(&temp,&argc,&argv))
177 				temp = argv[0][1]-'0';	/*for caompatibility*/
178 			if(temp>=0&&temp<=2)
179 				gridf = temp;
180 			break;
181 
182 		case 'c': /*character(s) for plotting*/
183 			if(argc >= 2) {
184 				symbf = 1;
185 				plotsymb = argv[1];
186 				argv++;
187 				argc--;
188 			}
189 			break;
190 
191 		case 't':	/*transpose*/
192 			transf = 1;
193 			break;
194 		case 'e':	/*equal scales*/
195 			equf = 1;
196 			break;
197 		case 'b':	/*breaks*/
198 			brkf = 1;
199 			break;
200 		case 'x':	/*x limits */
201 			limread(&xd,&argc,&argv);
202 			break;
203 		case 'y':
204 			limread(&yd,&argc,&argv);
205 			break;
206 		case 'h': /*set height of plot */
207 			if(!numb(&yd.xsize, &argc,&argv))
208 				badarg();
209 			break;
210 		case 'w': /*set width of plot */
211 			if(!numb(&xd.xsize, &argc, &argv))
212 				badarg();
213 			break;
214 		case 'r': /* set offset to right */
215 			if(!numb(&xd.xoff, &argc, &argv))
216 				badarg();
217 			break;
218 		case 'u': /*set offset up the screen*/
219 			if(!numb(&yd.xoff,&argc,&argv))
220 				badarg();
221 			break;
222 		default:
223 			badarg();
224 		}
225 	}
226 }
227 
228 limread(struct xy *p, int *argcp, char ***argvp){
229 	if(*argcp>1 && (*argvp)[1][0]=='l') {
230 		(*argcp)--;
231 		(*argvp)++;
232 		p->xf = log10;
233 	}
234 	if(!numb(&p->xlb,argcp,argvp))
235 		return;
236 	p->xlbf = 1;
237 	if(!numb(&p->xub,argcp,argvp))
238 		return;
239 	p->xubf = 1;
240 	if(!numb(&p->xquant,argcp,argvp))
241 		return;
242 	p->xqf = 1;
243 }
244 
245 isdigit(char c){
246 	return '0'<=c && c<='9';
247 }
248 numb(float *np, int *argcp, char ***argvp){
249 	register char c;
250 
251 	if(*argcp <= 1)
252 		return(0);
253 	while((c=(*argvp)[1][0]) == '+')
254 		(*argvp)[1]++;
255 	if(!(isdigit(c) || c=='-'&&(*argvp)[1][1]<'A' || c=='.'))
256 		return(0);
257 	*np = atof((*argvp)[1]);
258 	(*argcp)--;
259 	(*argvp)++;
260 	return(1);
261 }
262 
263 readin(void){
264 	register t,i;
265 	struct val *temp;
266 
267 	if(absf==1) {
268 		if(xd.xlbf)
269 			absbot = xd.xlb;
270 		else if(xd.xf==log10)
271 			absbot = 1;
272 	}
273 	for(;;) {
274 		temp = (struct val *)realloc((char*)xx,
275 			(unsigned)(n+ovlay)*sizeof(struct val));
276 		if(temp==0)
277 			return;
278 		xx = temp;
279 		if(absf)
280 			xx[n].xv = n*dx/ovlay + absbot;
281 		else
282 			if(!getfloat(&xx[n].xv))
283 				return;
284 		for(i=0;i<ovlay;i++) {
285 			xx[n+i].xv = xx[n].xv;
286 			if(!getfloat(&xx[n+i].yv))
287 				return;
288 			xx[n+i].lblptr = -1;
289 			t = getstring();
290 			if(t>0)
291 				xx[n+i].lblptr = copystring(t);
292 			if(t<0 && i+1<ovlay)
293 				return;
294 		}
295 		n += ovlay;
296 		if(t<0)
297 			return;
298 	}
299 }
300 
301 transpose(void){
302 	register i;
303 	float f;
304 	struct xy t;
305 	if(!transf)
306 		return;
307 	t = xd; xd = yd; yd = t;
308 	for(i= 0;i<n;i++) {
309 		f = xx[i].xv; xx[i].xv = xx[i].yv; xx[i].yv = f;
310 	}
311 }
312 
313 copystring(int k){
314 	register char *temp;
315 	register i;
316 	int q;
317 
318 	temp = realloc(labels,(unsigned)(labelsiz+1+k));
319 	if(temp==0)
320 		return(0);
321 	labels = temp;
322 	q = labelsiz;
323 	for(i=0;i<=k;i++)
324 		labels[labelsiz++] = labbuf[i];
325 	return(q);
326 }
327 
328 float modceil(float f, float t){
329 
330 	t = fabs(t);
331 	return(ceil(f/t)*t);
332 }
333 
334 float
335 modfloor(float f, float t){
336 	t = fabs(t);
337 	return(floor(f/t)*t);
338 }
339 
340 getlim(struct xy *p, struct val *v){
341 	register i;
342 
343 	i = 0;
344 	do {
345 		if(!p->xlbf && p->xlb>v[i].xv)
346 			p->xlb = v[i].xv;
347 		if(!p->xubf && p->xub<v[i].xv)
348 			p->xub = v[i].xv;
349 		i++;
350 	} while(i < n);
351 }
352 
353 setlim(struct xy *p){
354 	float t,delta,sign;
355 	struct z z;
356 	int mark[50];
357 	float lb,ub;
358 	int lbf,ubf;
359 
360 	lb = p->xlb;
361 	ub = p->xub;
362 	delta = ub-lb;
363 	if(p->xqf) {
364 		if(delta*p->xquant <=0 )
365 			badarg();
366 		return;
367 	}
368 	sign = 1;
369 	lbf = p->xlbf;
370 	ubf = p->xubf;
371 	if(delta < 0) {
372 		sign = -1;
373 		t = lb;
374 		lb = ub;
375 		ub = t;
376 		t = lbf;
377 		lbf = ubf;
378 		ubf = t;
379 	}
380 	else if(delta == 0) {
381 		if(ub > 0) {
382 			ub = 2*ub;
383 			lb = 0;
384 		}
385 		else
386 			if(lb < 0) {
387 				lb = 2*lb;
388 				ub = 0;
389 			}
390 			else {
391 				ub = 1;
392 				lb = -1;
393 			}
394 	}
395 	if(p->xf==log10 && lb>0 && ub>lb) {
396 		z = setloglim(lbf,ubf,lb,ub);
397 		p->xlb = z.lb;
398 		p->xub = z.ub;
399 		p->xmult *= z.mult;
400 		p->xquant = z.quant;
401 		if(setmark(mark,p)<2) {
402 			p->xqf = lbf = ubf = 1;
403 			lb = z.lb; ub = z.ub;
404 		} else
405 			return;
406 	}
407 	z = setlinlim(lbf,ubf,lb,ub);
408 	if(sign > 0) {
409 		p->xlb = z.lb;
410 		p->xub = z.ub;
411 	} else {
412 		p->xlb = z.ub;
413 		p->xub = z.lb;
414 	}
415 	p->xmult *= z.mult;
416 	p->xquant = sign*z.quant;
417 }
418 
419 struct z
420 setloglim(int lbf, int ubf, float lb, float ub){
421 	float r,s,t;
422 	struct z z;
423 
424 	for(s=1; lb*s<1; s*=10) ;
425 	lb *= s;
426 	ub *= s;
427 	for(r=1; 10*r<=lb; r*=10) ;
428 	for(t=1; t<ub; t*=10) ;
429 	z.lb = !lbf ? r : lb;
430 	z.ub = !ubf ? t : ub;
431 	if(ub/lb<100) {
432 		if(!lbf) {
433 			if(lb >= 5*z.lb)
434 				z.lb *= 5;
435 			else if(lb >= 2*z.lb)
436 				z.lb *= 2;
437 		}
438 		if(!ubf) {
439 			if(ub*5 <= z.ub)
440 				z.ub /= 5;
441 			else if(ub*2 <= z.ub)
442 				z.ub /= 2;
443 		}
444 	}
445 	z.mult = s;
446 	z.quant = r;
447 	return(z);
448 }
449 
450 struct z
451 setlinlim(int lbf, int ubf, float xlb, float xub){
452 	struct z z;
453 	float r,s,delta;
454 	float ub,lb;
455 
456 loop:
457 	ub = xub;
458 	lb = xlb;
459 	delta = ub - lb;
460 	/*scale up by s, a power of 10, so range (delta) exceeds 1*/
461 	/*find power of 10 quantum, r, such that delta/10<=r<delta*/
462 	r = s = 1;
463 	while(delta*s < 10)
464 		s *= 10;
465 	delta *= s;
466 	while(10*r < delta)
467 		r *= 10;
468 	lb *= s;
469 	ub *= s;
470 	/*set r=(1,2,5)*10**n so that 3-5 quanta cover range*/
471 	if(r>=delta/2)
472 		r /= 2;
473 	else if(r<delta/5)
474 		r *= 2;
475 	z.ub = ubf? ub: modceil(ub,r);
476 	z.lb = lbf? lb: modfloor(lb,r);
477 	if(!lbf && z.lb<=r && z.lb>0) {
478 		xlb = 0;
479 		goto loop;
480 	}
481 	else if(!ubf && z.ub>=-r && z.ub<0) {
482 		xub = 0;
483 		goto loop;
484 	}
485 	z.quant = r;
486 	z.mult = s;
487 	return(z);
488 }
489 
490 scale(struct xy *p){
491 	float edge;
492 
493 	setlim(p);
494 	edge = top-bot;
495 	p->xa = p->xsize*edge/((*p->xf)(p->xub) - (*p->xf)(p->xlb));
496 	p->xbot = bot + edge*p->xoff;
497 	p->xtop = p->xbot + (top-bot)*p->xsize;
498 	p->xb = p->xbot - (*p->xf)(p->xlb)*p->xa + .5;
499 }
500 
501 equilibrate(struct xy *p, struct xy *q){
502 	if(p->xlbf||	/* needn't test xubf; it implies xlbf*/
503 	   q->xubf&&q->xlb>q->xub)
504 		return;
505 	if(p->xlb>q->xlb) {
506 		p->xlb = q->xlb;
507 		p->xlbf = q->xlbf;
508 	}
509 	if(p->xub<q->xub) {
510 		p->xub = q->xub;
511 		p->xubf = q->xubf;
512 	}
513 }
514 
515 axes(void){
516 	register i;
517 	int mark[50];
518 	int xn, yn;
519 	if(gridf==0)
520 		return;
521 
522 	line(xd.xbot,yd.xbot,xd.xtop,yd.xbot);
523 	vec(xd.xtop,yd.xtop);
524 	vec(xd.xbot,yd.xtop);
525 	vec(xd.xbot,yd.xbot);
526 
527 	xn = setmark(mark,&xd);
528 	for(i=0; i<xn; i++) {
529 		if(gridf==2)
530 			line(mark[i],yd.xbot,mark[i],yd.xtop);
531 		if(gridf==1) {
532 			line(mark[i],yd.xbot,mark[i],yd.xbot+tick);
533 			line(mark[i],yd.xtop-tick,mark[i],yd.xtop);
534 		}
535 	}
536 	yn = setmark(mark,&yd);
537 	for(i=0; i<yn; i++) {
538 		if(gridf==2)
539 			line(xd.xbot,mark[i],xd.xtop,mark[i]);
540 		if(gridf==1) {
541 			line(xd.xbot,mark[i],xd.xbot+tick,mark[i]);
542 			line(xd.xtop-tick,mark[i],xd.xtop,mark[i]);
543 		}
544 	}
545 }
546 
547 setmark(int *xmark, struct xy *p){
548 	int xn = 0;
549 	float x,xl,xu;
550 	float q;
551 	if(p->xf==log10&&!p->xqf) {
552 		for(x=p->xquant; x<p->xub; x*=10) {
553 			submark(xmark,&xn,x,p);
554 			if(p->xub/p->xlb<=100) {
555 				submark(xmark,&xn,2*x,p);
556 				submark(xmark,&xn,5*x,p);
557 			}
558 		}
559 	} else {
560 		xn = 0;
561 		q = p->xquant;
562 		if(q>0) {
563 			xl = modceil(p->xlb+q/6,q);
564 			xu = modfloor(p->xub-q/6,q)+q/2;
565 		} else {
566 			xl = modceil(p->xub-q/6,q);
567 			xu = modfloor(p->xlb+q/6,q)-q/2;
568 		}
569 		for(x=xl; x<=xu; x+=fabs(p->xquant))
570 			xmark[xn++] = (*p->xf)(x)*p->xa + p->xb;
571 	}
572 	return(xn);
573 }
574 submark(int *xmark, int *pxn, float x, struct xy *p){
575 	if(1.001*p->xlb < x && .999*p->xub > x)
576 		xmark[(*pxn)++] = log10(x)*p->xa + p->xb;
577 }
578 
579 plot(void){
580 	int ix,iy;
581 	int i,j;
582 	int conn;
583 
584 	for(j=0;j<ovlay;j++) {
585 		switch(mode) {
586 		case -1:
587 			pen(modes[j%(sizeof modes/sizeof *modes-1)+1]);
588 			break;
589 		case 0:
590 			break;
591 		default:
592 			pen(modes[mode]);
593 		}
594 		conn = 0;
595 		for(i=j; i<n; i+=ovlay) {
596 			if(!conv(xx[i].xv,&xd,&ix) ||
597 			   !conv(xx[i].yv,&yd,&iy)) {
598 				conn = 0;
599 				continue;
600 			}
601 			if(mode!=0) {
602 				if(conn != 0)
603 					vec(ix,iy);
604 				else
605 					move(ix,iy);
606 				conn = 1;
607 			}
608 			conn &= symbol(ix,iy,xx[i].lblptr);
609 		}
610 	}
611 	pen(modes[1]);
612 }
613 
614 conv(float xv, struct xy *p, int *ip){
615 	long ix;
616 	ix = p->xa*(*p->xf)(xv*p->xmult) + p->xb;
617 	if(ix<p->xbot || ix>p->xtop)
618 		return(0);
619 	*ip = ix;
620 	return(1);
621 }
622 
623 getfloat(float *p){
624 	register i;
625 
626 	i = scanf("%f",p);
627 	return(i==1);
628 }
629 getstring(void){
630 	register i;
631 	char junk[20];
632 	i = scanf("%1s",labbuf);
633 	if(i==-1)
634 		return(-1);
635 	switch(*labbuf) {
636 	default:
637 		if(!isdigit(*labbuf)) {
638 			ungetc(*labbuf,stdin);
639 			i = scanf("%s",labbuf);
640 			break;
641 		}
642 	case '.':
643 	case '+':
644 	case '-':
645 		ungetc(*labbuf,stdin);
646 		return(0);
647 	case '"':
648 		i = scanf("%[^\"\n]",labbuf);
649 		scanf("%[\"]",junk);
650 		break;
651 	}
652 	if(i==-1)
653 		return(-1);
654 	return(strlen(labbuf));
655 }
656 
657 
658 symbol(int ix, int iy, int k){
659 
660 	if(symbf==0&&k<0) {
661 		if(mode==0)
662 			point(ix,iy);
663 		return(1);
664 	}
665 	else {
666 		move(ix,iy);
667 		text(k>=0?labels+k:plotsymb);
668 		move(ix,iy);
669 		return(!brkf|k<0);
670 	}
671 }
672 
673 title(void){
674 	char buf[BSIZ+100];
675 	buf[0] = ' ';
676 	buf[1] = ' ';
677 	buf[2] = ' ';
678 	strcpy(buf+3,titlebuf);
679 	if(erasf&&gridf) {
680 		axlab('x',&xd,buf);
681 		strcat(buf,",");
682 		axlab('y',&yd,buf);
683 	}
684 	move(xd.xbot,yd.xbot-60);
685 	text(buf);
686 }
687 
688 axlab(char c, struct xy *p, char *b){
689 	char *dir;
690 	dir = p->xlb<p->xub? "<=": ">=";
691 	sprintf(b+strlen(b), " %g %s %c%s %s %g", p->xlb/p->xmult,
692 		dir, c, p->xf==log10?" (log)":"", dir, p->xub/p->xmult);
693 }
694 
695 badarg(void){
696 	fprintf(stderr,"graph: error in arguments\n");
697 	closepl();
698 	exits("bad arg");
699 }
700