cristy | 16631fc | 2009-10-16 14:54:23 +0000 | [diff] [blame] | 1 | package |
cristy | 16631fc | 2009-10-16 14:54:23 +0000 | [diff] [blame] | 2 | Turtle; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3 | |
cristy | 6071266 | 2009-10-21 00:51:53 +0000 | [diff] [blame] | 4 | # Written by jreed@itis.com, adapted by Cristy. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5 | |
| 6 | sub new |
| 7 | { |
| 8 | my $class = shift; |
| 9 | my $self = {}; |
| 10 | |
| 11 | @{$self}{qw(x y theta mirror)} = @_; |
| 12 | bless $self, $class; |
| 13 | } |
| 14 | |
| 15 | sub 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 | |
| 28 | sub turn |
| 29 | { |
| 30 | my $self = shift; |
| 31 | my $dtheta = shift; |
| 32 | |
| 33 | $self->{theta} += $dtheta*$self->{mirror}; |
| 34 | } |
| 35 | |
| 36 | sub state |
| 37 | { |
| 38 | my $self = shift; |
| 39 | |
| 40 | @{$self}{qw(x y theta mirror)}; |
| 41 | } |
| 42 | |
| 43 | sub setstate |
| 44 | { |
| 45 | my $self = shift; |
| 46 | |
| 47 | @{$self}{qw(x y theta mirror)} = @_; |
| 48 | } |
| 49 | |
| 50 | sub mirror |
| 51 | { |
| 52 | my $self = shift; |
| 53 | |
| 54 | $self->{mirror} *= -1; |
| 55 | } |
| 56 | |
| 57 | "Turtle.pm"; |