Searching the house for the third $100 scientific calculator to be lost this year, so my son could complete his homework, I gave up and knocked together the following little doohickey, just ripe for extension.

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

y=x^2 or y=x**2 or y=3x^2
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.

*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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.