#!/usr/bin/perl ## Sierpinski's Triangle ## use Tk; $width = 800; $height = 500; my $main = MainWindow->new(); my $canvas = $main->Canvas( -width=>$width, -height=>$height, -background=>"black"); $canvas->pack( -expand=>1,-fill=>'both' ); # the coderefs take ( x, y ) @iterative_set = ( sub{ my @pt = (400,50); return( ($pt[0] + $_[0] )/2, ( $pt[1] + $_[1] )/2 ) }, sub{ my @pt = (50,400); return( ($pt[0] + $_[0] )/2, ( $pt[1] + $_[1] )/2 ) }, sub{ my @pt = (750,400); return( ($pt[0] + $_[0] )/2, ( $pt[1] + $_[1] )/2 ) }, ); # start coordinates $x = 40; $y = 400; $iter = 0; # weed out transients while( $iter++ != 100 ) { ( $x, $y ) = $iterative_set[ int rand 3 ]->( $x, $y ); } $iter = 0; while( $iter++ < 10000 ) { ( $x, $y ) = $iterative_set[ int rand 3 ]->( $x, $y ); $canvas->createRectangle( int $x, int $y, int $x, int $y, -outline=>undef, -fill=>'white' ); } MainLoop;