xref: /inferno-os/appl/math/hist0.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1implement Hist0;
2
3include "sys.m";
4	sys: Sys;
5	print: import sys;
6include "math.m";
7	math: Math;
8	fmin: import math;
9
10include "draw.m";
11include "tk.m";
12	tk: Tk;
13
14include "bufio.m";
15	bufio: Bufio;
16	Iobuf: import bufio;
17
18include "gr.m";
19	gr: GR;
20	Plot: import gr;
21
22Hist0: module{
23	init:	fn(nil: ref Draw->Context, argv: list of string);
24};
25
26
27plotfile(ctxt: ref Draw->Context, nil: list of string, filename: string){
28	p := gr->open(ctxt,filename);
29	input := bufio->open(filename,bufio->OREAD);
30	if(input==nil){
31		print("can't read %s",filename);
32		exit;
33	}
34
35	n := 0;
36	maxn := 100;
37	x := array[maxn] of real;
38	y := array[maxn] of real;
39	while(1){
40		xn := input.gett(" \t\n\r");
41		if(xn==nil)
42			break;
43		yn := input.gett(" \t\n\r");
44		if(yn==nil){
45			print("after reading %d pairs, saw singleton\n",n);
46			exit;
47		}
48		if(n>=maxn){
49			maxn *= 2;
50			newx := array[maxn] of real;
51			newy := array[maxn] of real;
52			for(i:=0; i<n; i++){
53				newx[i] = x[i];
54				newy[i] = y[i];
55			}
56			x = newx;
57			y = newy;
58		}
59		x[n] = real xn;
60		y[n] = real yn;
61		n++;
62	}
63	if(n==0){
64		print("empty input\n");
65		exit;
66	}
67
68	for(i:=0; i<n; i++){
69		h := 0.2;
70		if(i==0) h *= x[i+1]-x[i];
71		else if(i==n-1) h *= x[i]-x[i-1];
72		else h *= fmin(x[i+1]-x[i],x[i]-x[i-1]);
73		barx := array[] of{x[i]-h,x[i]-h,x[i]+h,x[i]+h,x[i]-h};
74		bary := array[] of{0.,y[i],y[i],0.,0.};
75		p.graph(barx,bary);
76	}
77	p.paint("",nil,"",nil);
78	p.bye();
79}
80
81init(ctxt: ref Draw->Context, argv: list of string){
82	sys = load Sys Sys->PATH;
83	math = load Math Math->PATH;
84	tk = load Tk Tk->PATH;
85	bufio = load Bufio Bufio->PATH;
86	if((gr = load GR GR->PATH) == nil){
87		sys->print("%s: Can't load gr\n",hd argv);
88		exit;
89	}
90
91	tkargs := "";
92	argv = tl argv;
93	if(argv != nil) {
94		tkargs = hd argv;
95		argv = tl argv;
96	}
97	if(argv!=nil)
98		plotfile(ctxt,argv,hd argv);
99}
100