proc buildG {funcstr w3} {
global gwth ghght minx maxx numtc gres
destroy $w3.gcvs1
canvas $w3.gcvs1 -width $gwth -height $ghght \
-borderwidth 2 -relief sunken
pack $w3.gcvs1
set c0y $ghght
set c0x 0
set gres [expr {1.000*$gres}]
# Assume that input function uses "x" as independant variable
set funcstr [string map {x \$xi} $funcstr]
# make sure "exp" function not clobberd
set funcstr [string map {e\$xip exp} $funcstr]
# get all cartesian pairs to plot
# break domain into "$resolution" points
set xinc [expr {($maxx-$minx)/$gres}]
set xi $minx
set i 0
while {$xi <= $maxx} {
set garr($i,x) $xi
set garr($i,y) [expr $funcstr]
set xi [expr {$xi+$xinc}]
incr i
}
#now "i" is the number of array elements since it starts w/ 0 and goes to i-1
# turn cartesian pairs into canvas coordinates
# first find ymax and ymin
set maxy $garr(0,y)
set miny $garr(0,y)
for {set p 1} {$p<$i} {incr p} {
if {$garr($p,y)<$miny} then {set miny $garr($p,y)}
if {$garr($p,y)>$maxy} then {set maxy $garr($p,y)}
}
set deltay [expr {$maxy - $miny}]
set yscale 20
if {$deltay!=0} {set yscale [expr {1.00*$ghght/$deltay}]}
set xscale [expr {1.00*$gwth/($maxx-$minx)}]
for {set p 0} {$p < $i} {incr p} {
set cgarr($p,x) [expr {$c0x +($garr($p,x)-$minx)*$xscale}]
set cgarr($p,y) [expr {$c0y -($garr($p,y)-$miny)*$yscale}]
}
# create lines in canvas
set c $w3.gcvs1
# draw Yaxis
set xmd [expr {$gwth/2}]
set tcinc [expr {$ghght/$numtc}]
$c create line $xmd $c0y $xmd 0 -width 1 -fill white
for {set p 0} {$p<$numtc} {incr p} {
set tcy [expr {$ghght - $tcinc*$p}]
$c create line 0 $tcy $gwth $tcy -width 1 -fill white
set yval [format "%6.4g" [expr {$miny+$p*$tcinc/$yscale}]]
$c create text $xmd $tcy -text $yval -fill red -font {courier 8} -anchor w
}
for {set p 1} {$p < $i} {incr p} {
set q [expr {$p -1}]
$c create line $cgarr($q,x) $cgarr($q,y) $cgarr($p,x) $cgarr($p,y) -width 1
}
}