home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
qtawk
/
graph.exp
< prev
next >
Wrap
Text File
|
1990-04-23
|
3KB
|
132 lines
# greph - processor for a graph-drawing language
# input: data and specification of a graph
# output: data plotted in specified area
# AWK page 137
BEGIN { #set frame dimensions ...
ht = 23; wid = 79; # height and width
ox = 6; oy = 2; # offset for x and y axes
number = /^{_f}({_e})?$/; # use built-in sub-expression for real numbers
}
$1 == "label" { # for bottom
sub(/^ *label */,"");
botlab = $0;
next;
}
$1 == "bottom" && $2 == "ticks" { # ticks for x-axis
for ( i = 3 ; i <= NF ; i++ ) bticks[++nb] = $i;
next;
}
$1 == "left" && $2 == "ticks" { # ticks for y-axis
for ( i = 3 ; i <= NF ; i++ ) lticks[++nl] = $i;
next;
}
$1 == "range" { # xmin ymin xmax ymax
xmin = $2; ymin = $3; xmax = $4; ymax = $5;
next;
}
$1 == "height" { ht = $2; next; }
$1 == "width" { wid = $2; next; }
$1 ~~ number && $2 ~~ number { # pair of numbers
nd++; # count number of data points
x[nd] = $1; y[nd] = $2;
ch[nd] = $3; # optional plotting character
next;
}
$1 ~~ number && $2 !~ number { # single number
nd++; # count number of data points
x[nd] = nd; y[nd] = $1; ch[nd] = $2;
next;
}
END { # draw graph
if ( xmin == "" ) { # no range was given
xmin = xmax = x[1];
ymin = ymax = y[1];
for ( i = 2 ; i <= nd ; i++ ) {
if ( x[i] < xmin ) xmin = x[i];
if ( x[i] > xmax ) xmax = x[i];
if ( y[i] < ymin ) ymin = y[i];
if ( y[i] > ymax ) ymax = y[i];
}
}
frame; ticks; label; data; draw;
}
function frame() { # create frame for graph
local i, j;
for ( i = ox+1 ; i < wid ; i++ ) plot(i,oy,"─"); # bottom
for ( i = ox+1 ; i < wid ; i++ ) plot(i,ht-1,"─"); # top
for ( i = oy+1 ; i < ht ; i++ ) plot(ox,i,"│"); # left
for ( i = oy+1 ; i < ht ; i++ ) plot(wid-1,i,"│"); # right
plot(ox,ht-1,"┌");
plot(wid-1,ht-1,"┐");
plot(ox,oy,"└");
plot(wid-1,oy,"┘");
}
function ticks() { # create tick marks for both axes
local i;
local xys;
for ( i = 1 ; i <= nb ; i++ ) {
xys = xscale(bticks[i]);
plot(xys,oy,"┼");
splot(xys - 1,1,bticks[i]);
}
for ( i = 1 ; i <= nl ; i++ ) {
xys = yscale(lticks[i]);
plot(ox,xys,"┼");
splot(0,xys,lticks[i]);
}
}
function label() { # center label under x-axis
splot((wid + ox - length(botlab))/2,0,botlab);
}
function data() { # create data points
local i;
for ( i = 1 ; i <= nd ; i++ )
plot(xscale(x[i]),yscale(y[i]), ch[i] ? ch[i] : "*");
# plot(xscale(x[i]),yscale(y[i]),"*");
}
function draw() { # print graph from array
local i, j;
for ( i = ht - 1 ; i >= 0 ; i-- ) {
for ( j = 0 ; j < wid ; j++ )
printf( j in array && i in array[j] ? array[j][i] : " " );
printf("\n");
}
}
function xscale(x) { # scale x-value
return int((x - xmin + 0.0)/(xmax - xmin) * (wid - 1.0 - ox) + ox + 0.5);
}
function yscale(y) { # scale y-value
return int((y - ymin + 0.0)/(ymax - ymin) * (ht - 1.0 - oy) + oy + 0.5);
}
function plot(x,y,c) { # put character c in array
array[x][y] = c;
}
function splot(x,y,s) { # put string s in array
local i, n = length(s);
local str = s "";
for ( i = 0 ; i < n ; i++ )
array[x+i][y] = substr(str,i+1,1);
}