1package 2 Turtle; 3 4# Written by jreed@itis.com, adapted by Cristy. 5 6sub new 7{ 8 my $class = shift; 9 my $self = {}; 10 11 @{$self}{qw(x y theta mirror)} = @_; 12 bless $self, $class; 13} 14 15sub forward 16{ 17 my $self = shift; 18 my ($r, $what) = @_; 19 my ($newx, $newy)=($self->{x}+$r* sin($self->{theta}), 20 $self->{y}+$r*-cos($self->{theta})); 21 if ($what) { 22 &$what($self->{x}, $self->{y}, $newx, $newy); # motion 23 } 24 # According to the coderef passed in 25 ($self->{x}, $self->{y})=($newx, $newy); # change the old coords 26} 27 28sub turn 29{ 30 my $self = shift; 31 my $dtheta = shift; 32 33 $self->{theta} += $dtheta*$self->{mirror}; 34} 35 36sub state 37{ 38 my $self = shift; 39 40 @{$self}{qw(x y theta mirror)}; 41} 42 43sub setstate 44{ 45 my $self = shift; 46 47 @{$self}{qw(x y theta mirror)} = @_; 48} 49 50sub mirror 51{ 52 my $self = shift; 53 54 $self->{mirror} *= -1; 55} 56 57"Turtle.pm"; 58