I'm sure there are more full-featured ones out there, but this is quite handy in that it'll let you enter functions in the form
i.e. no need to enter perl magic sigils, and the use of ^ as exponentiation (sorry if you needed exclusive-or - this ain't that kind of cal!). Oh, it also supports 'pi', for trigonometric functions, and multiplicands do not need the * sign, as long as the first is a number.y=x^2 or y=x**2 or y=3x^2
*dreams of a scientific calculator with built-in perl interpreter*
#!/usr/bin/perl -wT use strict; use constant pi => 3.14159265358979; use Tk; my $top = new MainWindow(-width => 800, -height => 600); my $cvs = $top->Canvas(-width => 800, -height => 550); 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('<Button-1>' => [ sub { $cvs->delete($_) for $cvs->find('al +l') } ]); $txt->bind('<KeyPress-Return>', [ \&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 $minx = $min->get(); my $maxx = $max->get(); my $step = $stp->get(); my $size = 1; for (my $x = $minx; $x <= $maxx; $x += $step) { my $y; eval "$func"; # warn "$func: $x, $y\n" if !($x % 50); $cvs->createRectangle( 800 * $x/$maxx, 300 - $y, 800 * $x/$maxx + $size, 300 - $y + $size, -width => 1); } $txt->delete(0, length $text); } __END__
In reply to Cheap'n'cheerful Graph Drawer by moot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |