line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Turtle; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GENE'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Turtle Movement and State Operations |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1249
|
use Moo; |
|
1
|
|
|
|
|
11359
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
1892
|
use POSIX qw( ceil ); |
|
1
|
|
|
|
|
6485
|
|
|
1
|
|
|
|
|
10
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.0201'; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
1423
|
use constant K => 3.14159265358979323846 / 180; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1020
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub _init_x { |
15
|
2
|
|
|
2
|
|
10
|
my $self = shift; |
16
|
2
|
|
|
|
|
32
|
return $self->width / 2; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _init_y { |
20
|
2
|
|
|
2
|
|
11
|
my $self = shift; |
21
|
2
|
|
|
|
|
40
|
return $self->height / 2; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _init_heading { |
25
|
2
|
|
|
2
|
|
2573
|
my $self = shift; |
26
|
2
|
|
|
|
|
26
|
return -90 % 360; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has width => ( |
31
|
|
|
|
|
|
|
is => 'rw', |
32
|
|
|
|
|
|
|
default => sub { 500 }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has height => ( |
36
|
|
|
|
|
|
|
is => 'rw', |
37
|
|
|
|
|
|
|
default => sub { 500 }, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has x => ( |
42
|
|
|
|
|
|
|
is => 'rw', |
43
|
|
|
|
|
|
|
lazy => 1, |
44
|
|
|
|
|
|
|
builder => \&_init_x, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has y => ( |
48
|
|
|
|
|
|
|
is => 'rw', |
49
|
|
|
|
|
|
|
lazy => 1, |
50
|
|
|
|
|
|
|
builder => \&_init_y, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has heading => ( |
54
|
|
|
|
|
|
|
is => 'rw', |
55
|
|
|
|
|
|
|
builder => \&_init_heading, |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
has pen_status => ( |
60
|
|
|
|
|
|
|
is => 'rw', |
61
|
|
|
|
|
|
|
default => sub { 1 }, # Pen down |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
has pen_color => ( |
65
|
|
|
|
|
|
|
is => 'rw', |
66
|
|
|
|
|
|
|
default => sub { 'black' }, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
has pen_size => ( |
70
|
|
|
|
|
|
|
is => 'rw', |
71
|
|
|
|
|
|
|
default => sub { 1 }, |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub home { |
76
|
1
|
|
|
1
|
1
|
2850
|
my $self = shift; |
77
|
1
|
|
|
|
|
4
|
$self->x( $self->_init_x ); |
78
|
1
|
|
|
|
|
9
|
$self->y( $self->_init_y ); |
79
|
1
|
|
|
|
|
7
|
$self->heading( $self->_init_heading ); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub pen_up { |
84
|
1
|
|
|
1
|
1
|
2757
|
my $self = shift; |
85
|
1
|
|
|
|
|
5
|
$self->pen_status(0); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub pen_down { |
90
|
1
|
|
|
1
|
1
|
449
|
my $self = shift; |
91
|
1
|
|
|
|
|
6
|
$self->pen_status(1); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub right { |
96
|
2
|
|
|
2
|
1
|
907
|
my $self = shift; |
97
|
2
|
|
50
|
|
|
7
|
my $degrees = shift // 0; |
98
|
2
|
|
|
|
|
12
|
$self->heading( ( $self->heading + $degrees ) % 360 ); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub left { |
103
|
1
|
|
|
1
|
1
|
443
|
my $self = shift; |
104
|
1
|
|
50
|
|
|
5
|
my $degrees = shift // 0; |
105
|
1
|
|
|
|
|
7
|
$self->heading( ( $self->heading - $degrees ) % 360 ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub position { |
110
|
3
|
|
|
3
|
1
|
500
|
my $self = shift; |
111
|
3
|
|
|
|
|
55
|
return ceil($self->x), ceil($self->y); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub get_state { |
116
|
2
|
|
|
2
|
1
|
8
|
my $self = shift; |
117
|
|
|
|
|
|
|
return |
118
|
2
|
|
|
|
|
37
|
ceil($self->x), |
119
|
|
|
|
|
|
|
ceil($self->y), |
120
|
|
|
|
|
|
|
$self->heading, |
121
|
|
|
|
|
|
|
$self->pen_status, |
122
|
|
|
|
|
|
|
$self->pen_color, |
123
|
|
|
|
|
|
|
$self->pen_size; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub set_state { |
128
|
1
|
|
|
1
|
1
|
442
|
my $self = shift; |
129
|
1
|
|
|
|
|
5
|
my ( $x, $y, $heading, $pen_status, $pen_color, $pen_size ) = @_; |
130
|
1
|
|
|
|
|
25
|
$self->x($x); |
131
|
1
|
|
|
|
|
21
|
$self->y($y); |
132
|
1
|
|
|
|
|
11
|
$self->heading($heading); |
133
|
1
|
|
|
|
|
4
|
$self->pen_status($pen_status); |
134
|
1
|
|
|
|
|
5
|
$self->pen_color($pen_color); |
135
|
1
|
|
|
|
|
4
|
$self->pen_size($pen_size); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub forward { |
140
|
2
|
|
|
2
|
1
|
13
|
my $self = shift; |
141
|
2
|
|
50
|
|
|
8
|
my $step = shift // 1; |
142
|
|
|
|
|
|
|
|
143
|
2
|
|
|
|
|
37
|
my $x = $step * cos( $self->heading * K ); |
144
|
2
|
|
|
|
|
11
|
my $y = $step * sin( $self->heading * K ); |
145
|
|
|
|
|
|
|
|
146
|
2
|
|
|
|
|
45
|
my $xo = $self->x; |
147
|
2
|
|
|
|
|
49
|
my $yo = $self->y; |
148
|
|
|
|
|
|
|
|
149
|
2
|
|
|
|
|
47
|
$self->x( $x + $xo ); |
150
|
2
|
|
|
|
|
56
|
$self->y( $y + $yo ); |
151
|
|
|
|
|
|
|
|
152
|
2
|
50
|
|
|
|
53
|
return $self->pen_status == 1 |
153
|
|
|
|
|
|
|
? ( ceil($xo), ceil($yo), ceil($self->x), ceil($self->y), $self->pen_color, $self->pen_size ) |
154
|
|
|
|
|
|
|
: undef; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub backward { |
159
|
1
|
|
|
1
|
1
|
1114
|
my $self = shift; |
160
|
1
|
|
|
|
|
2
|
my $step = shift; |
161
|
1
|
|
|
|
|
3
|
$self->forward( - $step ) |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub mirror { |
166
|
1
|
|
|
1
|
1
|
924
|
my $self = shift; |
167
|
1
|
|
|
|
|
7
|
$self->heading( $self->heading * -1 ); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub goto { |
172
|
1
|
|
|
1
|
1
|
925
|
my $self = shift; |
173
|
1
|
|
|
|
|
2
|
my ( $x, $y ) = @_; |
174
|
|
|
|
|
|
|
|
175
|
1
|
|
|
|
|
25
|
my $xo = $self->x; |
176
|
1
|
|
|
|
|
21
|
my $yo = $self->y; |
177
|
|
|
|
|
|
|
|
178
|
1
|
|
|
|
|
22
|
$self->x($x); |
179
|
1
|
|
|
|
|
19
|
$self->y($y); |
180
|
|
|
|
|
|
|
|
181
|
1
|
50
|
|
|
|
27
|
return $self->pen_status == 1 |
182
|
|
|
|
|
|
|
? ( ceil($xo), ceil($yo), ceil($self->x), ceil($self->y), $self->pen_color, $self->pen_size ) |
183
|
|
|
|
|
|
|
: undef; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
__END__ |