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