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