2008/05/16

The Labs.Com Issue_09_GD_L-systems
Last update 1999/02/20

TPJ: Issue_09_GD_L-systems

This is a collection of programs published by The Perl Journal. You can download all source-code also from TPJ: Programs.
  1. Turtle.pm
  2. lsys.pl.txt
  3. diagram.gif
  4. diagram2.gif
  5. flower.gif
  6. flower2.gif
  7. flower3.gif
  8. tree1.gif
  9. tree10.gif
  10. tree11.gif
  11. tree2.gif
  12. tree3.gif
  13. tree4.gif
  14. tree5.gif
  15. tree6.gif
  16. tree7.gif
  17. tree8.gif
  18. tree9.gif
  19. More Samples on GD_L-systems
Issue_09_GD_L-systems
1. Turtle.pm
Download Turtle.pm

 package Turtle; 
 sub new 
 { 
 my $class=shift; 
 my $self={}; 
 @{$self}{qw(x y theta mirror)}=@_; 
 bless $self, $class; 
 } 
 sub forward 
 { 
 my $self=shift; 
 my ($r, $what)=@_; 
 my ($newx, $newy)=($self->{x}+$r* sin($self->{theta}), 
                    $self->{y}+$r*-cos($self->{theta})); 
 if ($what) 
         { 
         &$what($self->{x}, $self->{y}, $newx, $newy);        # D\ 
 o something related to motion 
         }                                                             \ 
                                    # According to the coderef passed i\ 
 n 
 ($self->{x}, $self->{y})=($newx, $newy);                        \ 
 # And change the old coords 
 } 
  
 sub turn 
 { 
 my $self=shift; 
 my $dtheta=shift; 
 $self->{theta}+=$dtheta*$self->{mirror}; 
 } 
 sub state 
 { 
 my $self=shift; 
 @{$self}{qw(x y theta mirror)}; 
 } 
 sub setstate 
 { 
 my $self=shift; 
 @{$self}{qw(x y theta mirror)}=@_; 
 } 
 sub mirror 
 { 
 my $self=shift; 
 $self->{mirror}*=-1; 
 } 
 "Turtle.pm"; 

Issue_09_GD_L-systems
2. lsys.pl.txt

Download lsys.pl.txt

 #!/usr/bin/perl 
 use GD; 
 use Turtle; 
  
 sub flower 
 { 
 my $flower=shift; 
 my ($width, $height)=$flower->getBounds(); 
 my ($x,$y)=$turtle->state(); 
 $im->copy($flower, $x-$width/2, $y-$height/2, 0, 0, $width, $height\ 
 ); 
 } 
  
 #################################### 
 # This hash translates from turtle 
 # symbols into actual Perl code. 
 #################################### 
 # S => Step Forward 
 # - => Turn Counter-clockwise 
 # + => Turn Clockwise 
 # f,g,h => Flower 
 # M => Mirror 
 # [ => Begin Branch 
 # ] => End Branch 
 # { => Begin polygon 
 # } => End polygon 
 %translate=( 
 'S' => sub{$turtle->forward($changes->{"distance"}, $changes-\ 
 >{"motionsub"});}, 
 '-' => sub{$turtle->turn(-$changes->{"dtheta"});}, 
 '+' => sub{$turtle->turn($changes->{"dtheta"});}, 
 'M' => sub{$turtle->mirror();}, 
 '[' => sub{push(@statestack, [$turtle->state()]);}, 
 ']' => sub{$turtle->setstate(@{pop(@statestack)});}, 
 '{' => sub{$poly=new GD::Polygon; $changes=\%polychanges;}, 
 '}' => sub{$im->filledPolygon($poly, $light_green); undef $poly;\ 
  $changes=\%stemchanges;}, 
 'f' => sub{flower($flower);}, 
 'g' => sub{flower($flower2);}, 
 'h' => sub{flower($flower3);}, 
 ); 
 ###################################### 
 # And this one controls the actual 
 # development of the L-system itself 
 ###################################### 
  
 %rule=( 
 'A'=>'GS[---fA][++MB]', 
 'B'=>'C', 
 'C'=>'A', 
 'f'=>'g', 
 'g'=>'h', 
 'h'=>'', 
 'G'=>'HS', 
 'H'=>'IS', 
 'I'=>'GLMS', 
 'L'=>'[{S+S+S+S+S+S}]', 
 ); 
 # Set some parameters 
 %stemchanges=(distance => 2.8, dtheta => .06, motionsub => su\ 
 b{$im->line(@_, $dark_green);}); 
 %polychanges=(distance => 3, dtheta =>  .4, motionsub => sub{\ 
 $poly->addPt(@_[0..1]);}); 
 $changes=\%stemchanges; 
 $repetitions=10; 
 $imagesize=400; 
 # Create the main image 
 $im=new GD::Image($imagesize, $imagesize); 
 # Allocate some colors for it 
 $white=$im->colorAllocate(255,255,255); 
 $dark_green=$im->colorAllocate(0,128,0); 
 $light_green=$im->colorAllocate(0,192,0); 
 # Create the flower images 
 open(IN, "flower.gif") or die $!; 
 $flower=newFromGif GD::Image(IN); 
 close(IN); 
 open(IN, "flower2.gif") or die $!; 
 $flower2=newFromGif GD::Image(IN); 
 close(IN); 
 open(IN, "flower3.gif") or die $!; 
 $flower3=newFromGif GD::Image(IN); 
 close(IN); 
 # Create the turtle, at the midpoint of the bottom 
 # edge of the image, pointing up. 
  
 $turtle=new Turtle($imagesize/2, $imagesize, 0, 1); 
  
 # Finally, actually apply the rules to the axiom 
  
 $string="A"; # Axiom 
 for (1..$repetitions) 
         { 
         $string =~ s/./defined ($rule{$&}) ? $rule{$&} : $&/eg; 
         print "Finished pass $_...\n"; 
         } 
 foreach $cmd (split(//, $string)) 
         { 
         if ($translate{$cmd}) {&{$translate{$cmd}}();} 
         } 
 print "Executed...\n"; 
 #$im->transparent($white); 
 open(OUT, ">tree.gif") or die $!; 
 print OUT $im->gif; 
 close(OUT); 
 print "Done.\n"; 

Issue_09_GD_L-systems
3. diagram.gif

Issue_09_GD_L-systems
4. diagram2.gif

Issue_09_GD_L-systems
5. flower.gif

Issue_09_GD_L-systems
6. flower2.gif

Issue_09_GD_L-systems
7. flower3.gif

Issue_09_GD_L-systems
8. tree1.gif

Issue_09_GD_L-systems
9. tree10.gif

Issue_09_GD_L-systems
10. tree11.gif

Issue_09_GD_L-systems
11. tree2.gif

Issue_09_GD_L-systems
12. tree3.gif

Issue_09_GD_L-systems
13. tree4.gif

Issue_09_GD_L-systems
14. tree5.gif

Issue_09_GD_L-systems
15. tree6.gif

Issue_09_GD_L-systems
16. tree7.gif

Issue_09_GD_L-systems
17. tree8.gif

Issue_09_GD_L-systems
18. tree9.gif

Issue_09_GD_L-systems
19. More Samples on GD_L-systems

  • Issue_09_GD_L-systems

                                                                                                                                   

Last update 1999/02/20

All Rights Reserved - (C) 1997 - 2008 by The Labs.Com

Top of Page

The Labs.Com