#!/usw/tcl8.0/bin/wish #An interactive graph plotter v1.0 #C.Rajkumar Madhuram proc screen {} { global fr2 fr3 typesel function ascale minx maxx miny maxy numsamples global wantscale frame .plot set fr1 .plot.f1 set fr2 .plot.f2 set fr3 .plot.f3 set fr4 .plot.demo set fr5 .plot.remark frame $fr1 -relief raised -borderwidth 3 -background #535380 label $fr1.lab1 -text "PlotIt! 1.0" -background #535280 \ -foreground #aaeca6 label $fr1.lab2 -text "(c) C.Rajkumar Madhuram" -background #535280 \ -foreground #aaeca6 pack $fr1.lab1 -side left pack $fr1.lab2 -side right -fill both pack $fr1 -fill both frame $fr2 -relief ridge -borderwidth 5 -background #6b7787 canvas $fr2.c -width 600 -height 400 -background #ffffff pack $fr2.c pack $fr2 -fill both set typesel 0 frame $fr3 -relief ridge -borderwidth 3 -background #6b7787 frame $fr3.f1 -relief flat radiobutton $fr3.f1.b1 -text "XY" -variable typesel -value 0 \ -command {$fr3.f2.e3 configure -state disabled} radiobutton $fr3.f1.b2 -text "Polar" -variable typesel -value 1 \ -command {$fr3.f2.e3 configure -state normal} button $fr3.f1.b3 -text "GO" -command { plot } \ -highlightbackground #00ff00 label $fr3.f1.l1 -text "Expr : " entry $fr3.f1.e1 -textvariable function -background #e4b7b5 $fr3.f1.e1 insert 1 x pack $fr3.f1.b1 $fr3.f1.b2 $fr3.f1.b3 $fr3.f1.l1 -side left pack $fr3.f1.e1 -side left -expand yes -fill both pack $fr3.f1 -fill both frame $fr3.f2 -relief flat set minx -5 set maxx 5 set numsamples 600.0 label $fr3.f2.l1 -text "Min x/theta : " entry $fr3.f2.e1 -width 10 -textvariable minx label $fr3.f2.l2 -text "Max x/theta : " entry $fr3.f2.e2 -width 10 -textvariable maxx label $fr3.f2.l3 -text "samples : " entry $fr3.f2.e3 -width 10 -textvariable numsamples -state disabled pack $fr3.f2.l1 $fr3.f2.e1 $fr3.f2.l2 $fr3.f2.e2 \ $fr3.f2.l3 $fr3.f2.e3 -side left pack $fr3.f2 -fill both frame $fr3.f3 -relief flat set ascale 0 button $fr3.f3.b1 -text "To Manual" -width 8 -command { if $ascale==0 { $fr3.f3.b1 configure -text "To Auto" $fr3.f3.e1 configure -state normal $fr3.f3.e2 configure -state normal set ascale 1 } else { $fr3.f3.b1 configure -text "To Manual" $fr3.f3.e1 configure -state disabled $fr3.f3.e2 configure -state disabled set ascale 0 } } label $fr3.f3.l1 -text "Min y/r : " entry $fr3.f3.e1 -width 10 -textvariable miny -state disabled label $fr3.f3.l2 -text "Max y/r : " entry $fr3.f3.e2 -width 10 -textvariable maxy -state disabled checkbutton $fr3.f3.cb1 -text "Scale" -variable wantscale \ -command { if $typesel==0 {plot_xy} } set wantscale 1 pack $fr3.f3.b1 $fr3.f3.l1 $fr3.f3.e1 \ $fr3.f3.l2 $fr3.f3.e2 $fr3.f3.cb1 -side left -expand yes pack $fr3.f3 -fill both pack $fr3 -fill both frame $fr4 -relief ridge -borderwidth 3 -background #6b7787 frame $fr4.f1 -relief raised -borderwidth 5 -background #6b7787 button $fr4.f1.b1 -text "Demo 1" -background #ed7fa0 -foreground #e2f3a0 \ -command { set typesel 0 $fr3.f1.b1 invoke if $ascale==1 { $fr3.f3.b1 invoke } set function "sin(x)" set maxx 6.28 set minx -6.28 set wantscale 1 set remark "Plotting... Please wait"; update plot_xy set remark "A sinusoid with period = PI" } button $fr4.f1.b2 -text "Demo 2" -background #ed7fa0 -foreground #e2f3a0 \ -command { set typesel 0 $fr3.f1.b1 invoke if $ascale==1 { $fr3.f3.b1 invoke } set function "exp(x)" set maxx 3 set minx -3 set wantscale 1 set remark "Plotting... Please wait"; update plot_xy set remark "An exponentially increasing function" } button $fr4.f1.b3 -text "Demo 3" -background #ed7fa0 -foreground #e2f3a0 \ -command { set typesel 1 $fr3.f1.b2 invoke if $ascale==1 { $fr3.f3.b1 invoke } set function "80+50*sin(6*x)" set numsamples 1200 set maxx 31.4 set minx 0 set wantscale 1 set remark "Plotting... Please wait"; update plot_polar set remark "A polar plot" } button $fr4.f1.b4 -text "Demo 4" -background #ed7fa0 -foreground #e2f3a0 \ -command { set typesel 0 $fr3.f1.b1 invoke set function "sin(x)+sin(1.1*x)" set maxx 100.0 set minx -100.0 if $ascale==0 { $fr3.f3.b1 invoke } set miny -2.5 set maxy 2.5 set wantscale 1 set remark "Plotting... Please wait"; update plot_xy set remark "Demonstration of Resonance Effect" } button $fr4.f1.b5 -text "Demo 5" -background #ed7fa0 -foreground #e2f3a0 \ -command { set typesel 0 $fr3.f1.b1 invoke set function "sin(x)/x" set maxx 20.0 set minx -20.0 if $ascale==1 { $fr3.f3.b1 invoke } set wantscale 1 set remark "Plotting... Please wait"; update plot_xy set remark "The sinc function" } button $fr4.f1.b6 -text "Demo 6" -background #ed7fa0 -foreground #e2f3a0 \ -command { set typesel 1 $fr3.f1.b2 invoke if $ascale==1 { $fr3.f3.b1 invoke } set function "2*x" set numsamples 2500 set maxx 80.0 set minx 0 set remark "Plotting... Please wait"; update plot_polar set remark "A Spiral" } pack $fr4.f1.b1 $fr4.f1.b2 $fr4.f1.b3 $fr4.f1.b4 $fr4.f1.b5 $fr4.f1.b6 \ -side left -expand yes -fill both pack $fr4.f1 -fill both pack $fr4 -fill both frame $fr5 -relief flat -borderwidth 1 -background #6b7787 label $fr5.rem -text "" -textvariable remark -relief sunken pack $fr5.rem -side left -fill both -expand yes pack $fr5 -fill both pack .plot } proc draw_xy_axes {} { global fr2 $fr2.c delete all #$fr2.c create line 0 200 600 200 -fill #000000 #$fr2.c create line 300 0 300 400 -fill #000000 } proc draw_polar_axes {} { global fr2 $fr2.c delete all $fr2.c create line 0 200 600 200 -fill #000000 $fr2.c create line 300 0 300 400 -fill #000000 for { set i 1 } { $i < 5 } { incr i } { $fr2.c create oval [expr 300-$i*75] [expr 200+$i*75] [expr 300+$i*75] \ [expr 200-$i*75] -outline #000000 } } proc fudge_function {} { global function outfunc regsub -all {([^e]|^)x} $function {\1$x} outfunc } proc draw_scale_xy {} { global fr2 minx maxx miny maxy global scrxl scrxr scryt scryb set nxpoints 8 set nypoints 6 set yincr [expr ($maxy-$miny)/$nypoints.0] set xincr [expr ($maxx-$minx)/$nxpoints.0] for {set i 1} {$i<$nypoints} {incr i} { set ypos [expr 400.0-(400.0/$nypoints.0)*$i] set yval [format "%+7.5f" [expr $miny+$i*$yincr]] $fr2.c create line 0 $ypos 10 $ypos -fill #6725a0 $fr2.c create text 15 [expr $ypos-7] -text $yval -anchor nw \ -justify left -fill #6725a0 \ -font "-adobe-new century schoolbook-medium-r-normal--10-100-75-75-p-60-iso8859-1" } for {set i 1} {$i<$nxpoints} {incr i} { set xpos [expr (600.0/$nxpoints.0)*$i] set xval [format "%+7.5f" [expr $minx+($i*$xincr)]] $fr2.c create line $xpos 0 $xpos 10 -fill #6725a0 $fr2.c create text $xpos 15 -text $xval -anchor n\ -justify left -fill #6725a0 \ -font "-adobe-new century schoolbook-medium-r-normal--10-100-75-75-p-60-iso8859-1" } } proc plot_xy {} { global outfunc numsamples minx maxx miny maxy fr2 ascale global scrxl scrxr scryt scryb draw_xy_axes fudge_function set scrxl 0.0 set scrxr 599.0 set scryt 0 set scryb 399 set y 0 set xincr [expr ($maxx - $minx)/($scrxr - $scrxl + 1)] set x $minx set errflag 1 set count 1 set highval -1E50 set lowval 1E50 for { set i $scrxl } { $i<=$scrxr } { set i [expr $i+1] } { if [catch { set y [expr $outfunc] }]==0 { set errflag 0 set yflag($count) 1 set yval($count) $y if $count==1 { set highval $y set lowval $y } else { if $highval<$y { set highval $y } if $lowval>$y { set lowval $y } } } else { set yflag($count) 0 set yval($count) 100000 } incr count set x [expr $minx+$xincr*$count] } set yflag($count) 0 if $errflag==1 { puts "\nWarning : Please check your function. There could be a syntax error \n\ or it is everywhere undefined in the domain\n" } else { if $ascale==0 { set maxy $highval; set miny $lowval } if $maxy==$miny { set maxy [expr $miny+1] } set a [expr ($scryb-$scryt)/($miny-$maxy)] set b [expr $scryb-($a*$miny)] set oldflag 0 for { set i 1 } { $i<$count } { incr i } { set scrx [expr $i-1] set y $yval($i) set thisflag $yflag($i) set scry [expr ($a*$y)+$b] if ($oldflag==1)&&($thisflag==1) { #$fr2.c create oval [expr $i-1] [expr 201-$y] [expr $i+1] [expr 199-$y] \ # -outline #ff0000 $fr2.c create line $scrx $scry $oldx $oldy -fill #ff0000 -width 3 } set oldx $scrx set oldy $scry set oldflag $thisflag } if ($minx<0)&&($maxx>0) { set npix [expr $scrxr-$scrxl+1] set xrange [expr $maxx-$minx] set xpixno [expr -$minx*$npix/$xrange] $fr2.c create line $xpixno $scryt $xpixno $scryb -fill #000000 -width 1 } if ($miny<0)&&($maxy>0) { set npix [expr $scryb-$scryt+1] set yrange [expr $maxy-$miny] set ypixno [expr $scryb+1+$miny*$npix/$yrange] $fr2.c create line $scrxl $ypixno $scrxr $ypixno -fill #000000 -width 1 } } global wantscale if $wantscale==1 {draw_scale_xy} after 6000 { set remark "Please send comments to rmadhuram@yahoo.com"; update } } proc plot_polar {} { global outfunc numsamples minx maxx fr2 draw_polar_axes fudge_function set y 0 for { set i 0 } { $i<$numsamples } { incr i } { set x [expr $minx + [expr [expr [expr $maxx - $minx]*$i]/$numsamples]] catch { set y [expr $outfunc] } set x1 [expr $y*cos($x)] set y1 [expr $y*sin($x)] $fr2.c create oval [expr 299+$x1] [expr 201-$y1] [expr 301+$x1] \ [expr 199-$y1] -outline #ff0000 } after 6000 { set remark "Please send comments to rmadhuram@yahoo.com"; update } } proc plot {} { global typesel remark set remark "Plotting... Please wait"; update if $typesel==0 { plot_xy } else { plot_polar } set remark "Please send comments to rmadhuram@yahoo.com"; update } global remark set remark "Click on GO or any of the DEMO to start" screen
Make your own free website on Tripod.com