#!/usr/bin/perl -w # # "spiral.pl" (a number of other programs call this) # # Draws one or more spirals using the Logo library # # 070108 by liverpole # # Strict use strict; use warnings; # Libraries use lib "."; use Language::Logo; use Data::Dumper; use File::Basename; use Getopt::Long; # Globals my $iam = basename $0; my $b_toroid = 0; my $b_reflect = 0; my $b_debug = 0; my $bg = ""; my $host = ""; my $b_hide = 0; my $color = ""; my $update = ""; my $width = ""; my $maxsize = 512; my $minsize = 8; my $minang = -1; my $incr = 2; my $startsize = 0; my $nticks = 0; my $tickcnt = 0; my $pos = 0; my $syntax = " syntax: $iam [switches] Uses Logo to create a spiral with the specified update and angle. The update specifies the update interval of the gui (iterations per second between 1 (very fast) and 1000 (very slow)). Switches: -d ............ starts the Logo server in debug mode -t ............ 'toroidal' -- sets wrap to 1 -r ............ 'reflect' -- sets wrap to 2 -h ..... connects to server on given host (':' = localhost) -x ............ hides the turtle -a ...... use a random angle from to the given -b .... specifies the Canvas background color -c .... specifies the pen color -u ... specifies the update speed -w .... specifies the pen width -s ..... specify the starting size -i ..... increment the size each time by -n ... change the color after 'ticks' -p ...... make the starting position (X, Y) "; # Command-line my $result = GetOptions( "d" => \$b_debug, "x" => \$b_hide, "t" => \$b_toroid, "r" => \$b_reflect, "b=s" => \$bg, "c=s" => \$color, "h=s" => \$host, "u=s" => \$update, "w=s" => \$width, "a=s" => \$minang, "s=s" => \$startsize, "i=s" => \$incr, "n=s" => \$nticks, "p=s" => \$pos, ); $result or die $syntax; (my $angle = shift) or die $syntax; # Main program # Create Logo object my @opts = (debug => $b_debug); $bg and push @opts, bg => $bg; # Initial canvas color $host and push @opts, host => $host; # Connect to an existing server my $lo = new Logo(@opts); # Issue logo commands $update and $lo->cmd("update $update"); if ($startsize) { my $half = $startsize / 2; my $quarter = $half / 2; $lo->cmd("rt 90; bk $quarter; lt 90; bk $half"); } $width and $lo->cmd("pensize $width"); $color and $lo->cmd("color $color"); $b_hide and $lo->cmd("ht"); $b_toroid and $lo->cmd("wrap 1"); $b_reflect and $lo->cmd("wrap 2"); if ($pos) { ($pos =~ /(\d+),(\d+)/) or die "$iam: invalid position '$pos'\n"; my ($x, $y) = ($1, $2); $lo->cmd("xy $x $y"); } $lo->cmd("pendown"); # Create spiral spiral($startsize, $angle); # Disconnect from main client $host or $lo->disconnect("Type [RETURN] to disconnect ... "); # Subroutines sub spiral { my ($size, $angle) = @_; while (($incr <= 0 and $size >= $minsize) || ($incr >= 0 and $size <= $maxsize)) { $lo->cmd("forward $size"); # Move & draw turtle if ($minang >= 0) { $lo->cmd("rt random $minang $angle"); # Make a random turn } else { $lo->cmd("rt $angle"); # Turn degrees } # Change pen color randomly if ($nticks and ++$tickcnt >= $nticks) { $lo->cmd("color random"); $tickcnt = 0; } $size += $incr; } }