Tour/Spiral.pm

filter: Statementstmt 97.9% 146 / 149 Branchbran 97.3% 74 / 76 Conditioncond 77.7% 14 / 18 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 94.8% 256 / 270 CC 46 SCAR 25.0
CC
46
Cov
96%
_set_outward
21.0
_set
11.2
next
8.0
CRAP 12.1
« Tour/RandomWalk.pm ↑ Summary Tour/Serpentine.pm »
1 package Array::Tour::Spiral;
2  
3 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use base q(Array::Tour);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Array::Tour qw(:directions :status);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 our $VERSION = '0.06';
11  
12 =head1 NAME
13  
14 Array::Tour::Spiral - Return coordinates to take a spiral path.
15  
16 =head1 SYNOPSIS
17  
18 use Array::Tour::Spiral qw(:directions);
19  
20 my $spiral = Array::Tour::Spiral->new(
21 dimensions => [5, 5],
22 counterclock => $counterclock,
23 corner_right => $corner_right,
24 corner_bottom => $corner_bottom
25 inward => $inward);
26  
27 Creates the object with its attributes. The attributes are:
28  
29 =over 4
30  
31 =item dimensions
32  
33 Set the size of the grid:
34  
35 my $spath1 = Array::Tour::Spiral->new(dimensions => [16, 16]);
36  
37 If the grid is going to be square, a single integer is sufficient:
38  
39 my $spath1 = Array::Tour::Spiral->new(dimensions => 16);
40  
41 =item counterclock corner_bottom corner_right inward
42  
43 I<Default values: 0.> All are boolean values that affect the starting
44 point and the direction of the spiral path. By default, the spiral is
45 generated outwards from the center, using the upper left corner (if
46 there is a choice), in a clockwise direction. See the Examples section
47 to see what effects the different combinations produce.
48  
49 =back
50  
51 =head1 PREREQUISITES
52  
53 Perl 5.8 or later. This is the version of perl under which this module
54 was developed.
55  
56 =head1 DESCRIPTION
57  
58 A simple iterator that will return the coordinates of the next cell if
59 one were to tour a matrice's cells in a spiral path.
60  
61 =head2 Spiral Object Methods
62  
63 =head3 direction
64  
65 $dir = $tour->direction()
66  
67 Return the direction we just walked.
68  
69 Overrides Array::Tour's direction() method.
70  
71 =cut
72  
73 sub direction()
74 0 ▶{
Subroutine direction: not called CC=2 SCAR=17.9
Pod documented
75 0 my $self = shift;
76 0 ▶ return ($self->{status} == STOP)? undef: ${$self->{direction}}[0];
Branch: 1 branch
BranchTrueFalseTotal
$self->{'status'} == 2 ? : 0 0 0
77 }
78  
79 =head3 next()
80  
81 Returns an array reference to the next coordinates to use. Returns
82 undef if there is no next cell to visit.
83  
84 my $ctr = 1;
85 my $tour = Array::Tour::Spiral->new(dimensions => 64);
86  
87 while (my $cref = $tour->next())
88 {
89 my($x_coord, $y_coord, $z_coord) = @{$cref};
90 $grid[$y_coord, $x_coord] = isprime($ctr++);
91 }
92  
93 The above example generates Ulam's Spiral
94 L<http://en.wikipedia.org/wiki/Ulam_spiral> in the array @grid.
95  
96 Overrides Array::Tour's next() method.
97  
98 =cut
99  
100 sub next()
101 7776 ▶{
Subroutine next: called CC=8 SCAR=20.8
Pod documented
102 7776 my $self = shift;
103  
104 7776 ▶ return undef unless ($self->has_next());
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_next 192 7584 7776
105  
106 #
107 # Set up the conditions for the pacing.
108 # The first pacing value is incremented by one for inward
109 # spirals because by the time it is used for the second time
110 # it won't be shortened by a perpendicular branch of the walk.
111 #
112 7584 ▶ if ($self->{tourstatus} == START)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'tourstatus'} == 0) { } 192 7392 7584
113 {
114 192 $self->{tourstatus} = TOURING;
115 192 $self->{pacer} = ${$self->{pacing}}[0];
116 192 ▶ ${$self->{pacing}}[0] += 1 if ($self->{inward});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inward"} 96 96 192
117 }
118 else
119 {
120 #
121 # Pace off in the current direction.
122 #
123 7392 my $direction = ${$self->{direction}}[0];
124 7392 ▶ ${$self->{position}}[(($direction & (North | South)) == 0)? 0: 1] +=
Branch: 2 branches
BranchTrueFalseTotal
($direction & 33) == 0 ? : 3696 3696 7392
($direction & 5) == 0 ? : 3696 3696 7392
125 (($direction & (North | West)) == 0)? 1: -1;
126  
127 #
128 # Will the next pace be in a different direction?
129 #
130 7392 ▶ if (--$self->{pacer} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"pacer"} == 0) 1888 5504 7392
131 {
132 1888 $self->{pacer} = ${$self->{pacing}}[1];
133 1888 ${$self->{pacing}}[0] += $self->{pacechange};
134
135 #
136 # Rotate to the next pacing length and the next direction.
137 #
138 1888 push @{$self->{pacing}}, shift @{$self->{pacing}};
139 1888 push @{$self->{direction}}, shift @{$self->{direction}};
140 }
141  
142 }
143  
144 7584 ▶ $self->{tourstatus} = STOP if (++$self->{odometer} == $self->{tourlength});
Branch: 1 branch
BranchTrueFalseTotal
if ++$self->{"odometer"} == $self->{"tourlength"} 192 7392 7584
145 7584 return $self->adjusted_position();
146 }
147  
148 =head3 anti_spiral()
149  
150 $larips = $spiral->anti_spiral();
151  
152 Return a new object that follows the same path as the original object,
153 reversing the inward/outward direction.
154  
155 =cut
156  
157 sub anti_spiral()
158 96 ▶{
Subroutine anti_spiral: called CC=3 SCAR=11.0
Pod documented
159 96 my $self = shift;
160 96 my %anti_self;
161 96 my @dimensions = @{ $self->{dimensions} };
162  
163 96 $anti_self{dimensions} = $self->{dimensions};
164 96 $anti_self{counterclock} = $self->{counterclock} ^ 1;
165 96 $anti_self{inward} = $self->{inward} ^ 1;
166  
167 96 my $width = $dimensions[0];
168 96 my $height = $dimensions[1];
169  
170 96 ▶ if ($width == $height)
Branch: 2 branches
BranchTrueFalseTotal
if ($width == $height) { } 32 64 96
elsif ($width > $height) { } 32 32 64
171 {
172 32 my $is_even = $height & 1;
173 32 $anti_self{corner_right} = $is_even ^ $self->{counterclock} ^ $self->{corner_bottom} ^ 1;
174 32 $anti_self{corner_bottom} = $is_even ^ $self->{counterclock} ^ $self->{corner_right};
175 }
176 elsif ($width > $height)
177 {
178 32 my $is_even = $height & 1;
179 32 $anti_self{corner_right} = $is_even ^ $self->{corner_right};
180 32 $anti_self{corner_bottom} = $is_even ^ $self->{counterclock} ^ $self->{corner_bottom};
181 }
182 else #$width < $height
183 {
184 32 my $is_even = $width & 1;
185 32 $anti_self{corner_right} = $is_even ^ $self->{counterclock} ^ $self->{corner_right};
186 32 $anti_self{corner_bottom} = $is_even ^ $self->{corner_bottom};
187 }
188  
189 96 return Array::Tour::Spiral->new(%anti_self);
190 }
191  
192 =head3 _set()
193  
194 $self->_set(%parameters);
195  
196 Override Array::Tour's _set() method for one that can handle
197 our parameters.
198 =cut
199  
200 sub _set()
201 192 ▶{
Subroutine _set: called CC=11 SCAR=24.2
202 192 my $self = shift;
203 192 my(%params) = @_;
204  
205 192 warn "Unknown paramter $_" foreach (grep{$_ !~ /inward|counterclock|corner_right|corner_bottom/} (keys %params));
206  
207 #
208 # Set counterclock, corner_right, corner_bottom, and inward
209 # to 0/1 values.
210 #
211 192 ▶ $self->{counterclock} = (defined $params{counterclock} and $params{counterclock} != 0)? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'counterclock'} && $params{'counterclock'} != 0 ? : 96 96 192
Condition66%2/3
defined $params{"counterclock"} and $params{"counterclock"} != 0
!ll&&!rl&&r
09696
MC/DC50%1/2
defined $params{"counterclock"} and $params{"counterclock"} != 0
defined $params{"counterclock"} $params{"counterclock"} != 0
Truth table66%2/3
A and B
A= defined $params{"counterclock"}B= $params{"counterclock"} != 0
ABresult
0X0
100
111
212 192 ▶ $self->{corner_right} = (defined $params{corner_right} and $params{corner_right} != 0)? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'corner_right'} && $params{'corner_right'} != 0 ? : 96 96 192
Condition66%2/3
defined $params{"corner_right"} and $params{"corner_right"} != 0
!ll&&!rl&&r
09696
MC/DC50%1/2
defined $params{"corner_right"} and $params{"corner_right"} != 0
defined $params{"corner_right"} $params{"corner_right"} != 0
Truth table66%2/3
A and B
A= defined $params{"corner_right"}B= $params{"corner_right"} != 0
ABresult
0X0
100
111
213 192 ▶ $self->{corner_bottom} = (defined $params{corner_bottom} and $params{corner_bottom} != 0)? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'corner_bottom'} && $params{'corner_bottom'} != 0 ? : 96 96 192
Condition66%2/3
defined $params{"corner_bottom"} and $params{"corner_bottom"} != 0
!ll&&!rl&&r
09696
MC/DC50%1/2
defined $params{"corner_bottom"} and $params{"corner_bottom"} != 0
defined $params{"corner_bottom"} $params{"corner_bottom"} != 0
Truth table66%2/3
A and B
A= defined $params{"corner_bottom"}B= $params{"corner_bottom"} != 0
ABresult
0X0
100
111
214 192 ▶ $self->{inward} = (defined $params{inward} and $params{inward} != 0)? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'inward'} && $params{'inward'} != 0 ? : 96 96 192
Condition66%2/3
defined $params{"inward"} and $params{"inward"} != 0
!ll&&!rl&&r
09696
MC/DC50%1/2
defined $params{"inward"} and $params{"inward"} != 0
defined $params{"inward"} $params{"inward"} != 0
Truth table66%2/3
A and B
A= defined $params{"inward"}B= $params{"inward"} != 0
ABresult
0X0
100
111
215  
216 192 ▶ return $self->_set_inward() if ($self->{inward} == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inward"} == 1 96 96 192
217 96 return $self->_set_outward();
218 }
219  
220 =head3 _set_inward()
221  
222 $self->_set_inward();
223  
224 Set the attributes knowing that the spiral path goes inward.
225 =cut
226  
227 sub _set_inward()
228 96 ▶{
Subroutine _set_inward: called CC=6 SCAR=17.9
229 96 my $self = shift;
230 96 my @dimensions = @{ $self->{dimensions} };
231 96 my $width = $dimensions[0];
232 96 my $height = $dimensions[1];
233 96 my $counterclock = $self->{counterclock};
234 96 my $corner_bottom = $self->{corner_bottom};
235 96 my $corner_right = $self->{corner_right};
236 96 my $pace_x = $width - 1;
237 96 my $pace_y = $height - 1;
238 96 my @direction = (East, South, West, North);
239 96 my($start_x, $start_y) = (0, 0);
240 96 my $rotate;
241  
242 96 $self->{pacechange} = -1;
243 96 ▶ $start_x = $width - 1 if ($corner_right);
Branch: 1 branch
BranchTrueFalseTotal
if $corner_right 48 48 96
244 96 ▶ $start_y = $height - 1 if ($corner_bottom);
Branch: 1 branch
BranchTrueFalseTotal
if $corner_bottom 48 48 96
245  
246 96 $rotate = ($corner_bottom << 1) | ($corner_bottom ^ $corner_right);
247 96 ▶ $rotate ^= 2 if ($counterclock);
Branch: 1 branch
BranchTrueFalseTotal
if $counterclock 48 48 96
248 96 push @direction, splice(@direction, 0, $rotate);
249 96 ▶ @direction = reverse @direction if ($counterclock);
Branch: 1 branch
BranchTrueFalseTotal
if $counterclock 48 48 96
250  
251 96 $self->{direction} = \@direction;
252 96 ▶ $self->{pacing} = (($direction[0] & (West | East)) == 0)? [$pace_y, $pace_x]: [$pace_x, $pace_y];
Branch: 1 branch
BranchTrueFalseTotal
($direction[0] & 132) == 0 ? : 48 48 96
253 96 $self->{start} = [$start_x, $start_y];
254 96 $self->{position} = [$start_x, $start_y];
255 96 $self->{rotate} = $rotate;
256  
257 96 return $self;
258 }
259  
260 =head3 _set_outward()
261  
262 $self->_set_outward();
263  
264 Set the attributes knowing that the spiral path goes outward.
265 =cut
266  
267 sub _set_outward()
268 96 ▶{
Subroutine _set_outward: called CC=21 SCAR=30.4
269 96 my $self = shift;
270 96 my @dimensions = @{ $self->{dimensions} };
271 96 my $width = $dimensions[0];
272 96 my $height = $dimensions[1];
273 96 my $counterclock = $self->{counterclock};
274 96 my $corner_bottom = $self->{corner_bottom};
275 96 my $corner_right = $self->{corner_right};
276 96 my($pace_x, $pace_y) = (1, 1);
277 96 my @direction = (East, South, West, North);
278 96 my($start_x, $start_y, $rotate);
279  
280 96 $self->{pacechange} = 1;
281  
282 #
283 # Find the starting corner.
284 #
285 96 $start_x = ($width-1)/2;
286 96 $start_y = ($height-1)/2;
287  
288 96 ▶ if ($width == $height)
Branch: 2 branches
BranchTrueFalseTotal
if ($width == $height) { } 32 64 96
elsif ($width > $height) { } 32 32 64
289 {
290 #
291 # Adjust the starting corner if it's an even side.
292 #
293 32 ▶ if (($width & 1) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (($width & 1) == 0) 16 16 32
294 {
295 16 ▶ $start_x++ if ($corner_right);
Branch: 1 branch
BranchTrueFalseTotal
if $corner_right 8 8 16
296 16 ▶ $start_y++ if ($corner_bottom);
Branch: 1 branch
BranchTrueFalseTotal
if $corner_bottom 8 8 16
297 }
298  
299 #
300 # Circling clockwise from top left to bottom left the
301 # corner flags are [00], [01], [11], [10]. In other
302 # words, a two bit Gray code that converts to 0..3,
303 # which we'll use to rotate the direction list.
304 #
305 32 $rotate = ($corner_bottom << 1) | ($corner_bottom ^ $corner_right);
306 32 ▶ $rotate ^= 2 if ($counterclock);
Branch: 1 branch
BranchTrueFalseTotal
if $counterclock 16 16 32
307 }
308 elsif ($width > $height) # X-axis is the major axis.
309 {
310 32 $pace_x += $width - $height;
311 32 $start_x = $start_y;
312  
313 32 ▶ if (($corner_right ^ $corner_bottom) == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (($corner_right ^ $corner_bottom) == 1) 16 16 32
314 {
315 16 $pace_x--;
316 16 ▶ $start_x++ if (($height & 1) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ($height & 1) == 0 8 8 16
317 }
318  
319 32 ▶ $start_x = ($width - 1) - $start_x if ($corner_right);
Branch: 1 branch
BranchTrueFalseTotal
if $corner_right 16 16 32
320  
321 32 ▶ $start_y++ if (($height & 1) == 0 and ($corner_right ^ $counterclock) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if ($height & 1) == 0 and ($corner_right ^ $counterclock) == 1 8 24 32
Condition100%3/3
($height & 1) == 0 and ($corner_right ^ $counterclock) == 1
!ll&&!rl&&r
1688
MC/DC100%2/2
($height & 1) == 0 and ($corner_right ^ $counterclock) == 1
($height & 1) == 0 ($corner_right ^ $counterclock) == 1
Truth table100%3/3
A and B
A= ($height & 1) == 0B= ($corner_right ^ $counterclock) == 1
ABresult
0X0
100
111
322  
323 32 $rotate = $corner_right << 1;
324 32 ▶ $rotate ^= 1 if ($counterclock);
Branch: 1 branch
BranchTrueFalseTotal
if $counterclock 16 16 32
325 }
326 else # Y-axis is the major axis.
327 {
328 32 $pace_y += $height - $width;
329 32 $start_y = $start_x;
330  
331 32 ▶ if (($corner_bottom ^ $corner_right) == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (($corner_bottom ^ $corner_right) == 1) 16 16 32
332 {
333 16 $pace_y--;
334 16 ▶ $start_y++ if (($width & 1) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ($width & 1) == 0 8 8 16
335 }
336  
337 32 ▶ $start_y = ($height - 1) - $start_y if ($corner_bottom);
Branch: 1 branch
BranchTrueFalseTotal
if $corner_bottom 16 16 32
338  
339 32 ▶ $start_x++ if (($width & 1) == 0 and ($corner_bottom ^ $counterclock) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ($width & 1) == 0 and ($corner_bottom ^ $counterclock) == 0 8 24 32
Condition100%3/3
($width & 1) == 0 and ($corner_bottom ^ $counterclock) == 0
!ll&&!rl&&r
1688
MC/DC100%2/2
($width & 1) == 0 and ($corner_bottom ^ $counterclock) == 0
($width & 1) == 0 ($corner_bottom ^ $counterclock) == 0
Truth table100%3/3
A and B
A= ($width & 1) == 0B= ($corner_bottom ^ $counterclock) == 0
ABresult
0X0
100
111
340  
341 32 $rotate = ($corner_bottom << 1) | 1;
342 32 ▶ $rotate ^= 3 if ($counterclock);
Branch: 1 branch
BranchTrueFalseTotal
if $counterclock 16 16 32
343 }
344  
345 96 push @direction, splice(@direction, 0, $rotate);
346 96 ▶ @direction = reverse @direction if ($counterclock);
Branch: 1 branch
BranchTrueFalseTotal
if $counterclock 48 48 96
347  
348 96 $self->{direction} = \@direction;
349 96 ▶ $self->{pacing} = (($direction[0] & (West | East)) == 0)? [$pace_y, $pace_x]: [$pace_x, $pace_y];
Branch: 1 branch
BranchTrueFalseTotal
($direction[0] & 132) == 0 ? : 48 48 96
350 96 $self->{start} = [$start_x, $start_y];
351 96 $self->{position} = [$start_x, $start_y];
352 96 $self->{rotate} = $rotate;
353  
354 96 return $self;
355 }
356 6 1;
357 __END__
« Tour/RandomWalk.pm ↑ Summary Tour/Serpentine.pm »