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