Kickin' it old school
I'm sure you remember the good ol' days of typing programs straight out of a book hoping to see something which looks like this:
*
* * *
* * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * ** * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * ** *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * ** *
* * * * * * * * *
* * * * * * * * * *
* * * * * * ** * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * *
* * *
*
Here's that code translated to Perl.
#!/usr/bin/env perl
use strict;
use warnings;
# http://www.atariarchives.org/basicgames/showpage.php?page=167
use List::Util 'max';
my $r = 30;
my @f = (
sub { $r * exp( -$_[0]**2 / 100 ) },
sub { $r * ( cos( $_[0] / 16 ) + 2 ) },
sub { $r - $r * sin( $_[0] / 18 ) },
sub { $r * exp( -cos( $_[0] / 16 ) ) - $r },
sub { $r * sin( $_[0] / 10 ) },
);
my $f = $f[ rand @f ];
for my $x ( 0 .. ( $r * 2 ) / 1.5 ) {
$x = $x * 1.5 - $r;
my $l = 0;
my $y1 = 5 * int( sqrt( $r**2 - $x * $x ) / 5 );
my @line;
for ( 0 .. ( $y1 * 2 ) / 5 ) {
my $y = -( ( $_ * 5 ) - $y1 );
my $z = int( 25 + $f->( sqrt( $x * $x + $y * $y ) ) - .7 * $y );
next if $z <= $l;
$l = $z;
push @line => $z;
}
my $string = ' ' x max @line;
substr $string, $_, 1, '*' foreach @line; # PRINT TAB(Z);"*";
print "$string\n";
}
Kinda reminds me of Text::AsciiTeX. Ascii Science UNITE!
Neat! I did Nicomachus. I moved the "yes or no" logic into a subroutine and re-used it for the "try another". Otherwise, I guess we'd have to hit C-c to exit.