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