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