#!/usr/bin/perl -w use strict; use constant pi => 3.14159265358979; use Tk; use Tk::WorldCanvas; my $top = new MainWindow( -width => 800, -height => 600 ); my $cvs = $top->WorldCanvas( -width => 800, -height => 550 ); $cvs->center( 0, 0 ); my $frm = $top->Canvas( -width => 800, -height => 20 ); my $txt = $frm->Entry( -width => 40 ); my $min = $frm->Entry( -width => 5 ); my $max = $frm->Entry( -width => 5 ); my $stp = $frm->Entry( -width => 5 ); my $clr = $frm->Button( -text => 'Clear' ); $clr->bind( '' => [ sub { $cvs->delete($_) for $cvs->find('all') } ] ); $txt->bind( '', [ \&evaluate ] ); $frm->pack( -side => 'bottom' ); $min->pack( -side => 'left' ); $max->pack( -side => 'left' ); $stp->pack( -side => 'left' ); $clr->pack( -side => 'right' ); $txt->pack( -side => 'right' ); $cvs->pack( -side => 'top' ); $min->insert( 0, "0" ); $max->insert( 0, "100" ); $stp->insert( 0, "1" ); MainLoop; sub evaluate { my $text = $txt->get(); my $func = lc $text; $func =~ s/(\d+)(pi|x)/$1 * $2/g; $func =~ s/([xy])/\$$1/g; $func =~ s/\^/**/g; my $mini = $min->get(); my $maxi = $max->get(); my $step = $stp->get(); #my $size = 1; my @points = (); for ( my $i = $mini ; $i <= $maxi ; $i += $step ) { my $y = 0; my $x = $i; eval "$func"; # warn "$func: $x, $y\n" if !($x % 50); push @points, $x, $y; # $cvs->createRectangle( # 800 * $x/$maxx, 300 - $y, # 800 * $x/$maxx + $size, 300 - $y + $size, # -width => 1); } $cvs->createLine( @points, -smooth => 1 ); $cvs->viewFit('all'); } __END__