This article about drawing sunflower seeds using PostScript inspired me to do a re-write using Perl Tk. The idea is to move in a spiral dropping seeds every 2.39996322972865 radians. This angle is based on the Golden Ratio.
#!/usr/bin/perl use strict; use diagnostics; use Tk; use constant TWO_PI => 8 * atan2(1, 1); use constant PHI => 0.5 * (sqrt(5) + 1); use constant GOLDEN => (TWO_PI) - (TWO_PI) / PHI; use constant DEFAULT_SEEDSIZE => 5; use constant DEFAULT_BEGIN => 3; use constant DEFAULT_END => 180; use constant DEFAULT_DIVERGENCE => 0.5; use constant DEFAULT_RATE => 5.0; my $box = 400; my ($seedsize, $begin, $end, $divergence, $rate); reset_defaults(); my $main = MainWindow->new(); my $canvas = $main->Canvas(-background => 'white', -height => $box, -w +idth => $box); $canvas->pack(); my $frame_0 = $main->Frame()->pack(-side => 'bottom', -anchor => 'w'); my $redraw_button = $frame_0->Button(-text => "Redraw", -command => \&set_new_values) ->pack(-side => 'left'); my $reset_button = $frame_0->Button(-text => "Reset defaults", -command => \&reset_defaults) ->pack(-side => 'left'); my $exit_button = $frame_0->Button(-text => "Exit", -command => sub {$main->destroy;}) ->pack(-side => 'right'); my $frame_1 = $main->Frame()->pack(-side => 'bottom', -anchor => 'w'); $frame_1->Label(-text => 'Rate:', -width => 12, -anchor => 'e')->pack( +-side => 'left'); my $rateEnt = $frame_1->Entry (-width => 9 , -justify => 'right' , -va +lidate => 'focus', -textvariable => \$rate) ->pack ( -side => 'left' +); $frame_1->Label(-text => 'Divergence:', -width => 12, -anchor => 'e')- +>pack(-side => 'left'); my $divEnt = $frame_1->Entry (-width => 9 , -justify => 'right', -vali +date => 'focus', -textvariable => \$divergence) ->pack ( -side => 'le +ft' ); my $frame_2 = $main->Frame()->pack(-side => 'bottom', -anchor => 'w'); $frame_2->Label(-text => 'Seed size:', -width => 12, -anchor => 'e')-> +pack(-side => 'left'); my $seedsizeEnt = $frame_2->Entry (-width => 9 , -justify => 'right' , + -validate => 'focus', -textvariable => \$seedsize) ->pack ( -side => + 'left' ); my $frame_3 = $main->Frame()->pack(-side => 'bottom', -anchor => 'w'); $frame_3->Label(-text => 'Begin angle:', -width => 12, -anchor => 'e') +->pack(-side => 'left'); my $beginEnt = $frame_3->Entry (-width => 9 , -justify => 'right' , -v +alidate => 'focus', -textvariable => \$begin) ->pack ( -side => 'left +' ); $frame_3->Label(-text => 'End angle:', -width => 12, -anchor => 'e')-> +pack(-side => 'left'); my $endEnt = $frame_3->Entry (-width => 9 , -justify => 'right' , -val +idate => 'focus', -textvariable => \$end) ->pack ( -side => 'left' ); drawseeds(); # $canvas->postscript(-width => $box, -height => $box, -file => "test. +eps"); MainLoop; ###################################################################### +######### sub drawseeds { for (my $theta = $begin * TWO_PI; $theta <= $end * TWO_PI; $theta += + GOLDEN) { my $radius = $rate * $theta ** $divergence; my ($x, $y) = translate(r2p($radius, $theta)); if ($radius > $box / 2) { print "Radius = $radius,\tTheta = $theta,\tx=$x,\ty=$y\n"; last; } drawseed($x, $y); } } sub drawseed { my ($x, $y) = @_; $canvas->createOval($x - ($seedsize / 2), $y - ($seedsize / 2), $x + ($seedsize / 2), $y + ($seedsize / 2), -fill => 'black', -outline => 'black', -tag => 'seed'); } sub translate { my ($x, $y) = @_; return ($box / 2 + $x, $box / 2 - $y); } sub r2p { my ($radius, $theta) = @_; return ($radius * cos $theta, $radius * sin $theta); } sub reset_defaults { $seedsize = DEFAULT_SEEDSIZE; $begin = DEFAULT_BEGIN; $end = DEFAULT_END; $divergence = DEFAULT_DIVERGENCE; $rate = DEFAULT_RATE; } sub set_new_values { $seedsize = $seedsizeEnt->get(); $begin = $beginEnt->get(); $end = $endEnt->get(); $divergence = $divEnt->get(); $rate = $rateEnt->get(); $canvas->delete("seed"); drawseeds(); }

Janitored by Arunbear - added readmore tags


In reply to Drawing sunflower seeds by perlmoth

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.