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