| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $params{"counterclock"} and $params{"counterclock"} != 0
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
| |||||||||||||||||||||||||||||
| 212 | 192 | ▶ | $self->{corner_right} = (defined $params{corner_right} and $params{corner_right} != 0)? 1: 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $params{"corner_right"} and $params{"corner_right"} != 0
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
| |||||||||||||||||||||||||||||
| 213 | 192 | ▶ | $self->{corner_bottom} = (defined $params{corner_bottom} and $params{corner_bottom} != 0)? 1: 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $params{"corner_bottom"} and $params{"corner_bottom"} != 0
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
| |||||||||||||||||||||||||||||
| 214 | 192 | ▶ | $self->{inward} = (defined $params{inward} and $params{inward} != 0)? 1: 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $params{"inward"} and $params{"inward"} != 0
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
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 192 | ▶ | return $self->_set_inward() if ($self->{inward} == 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 244 | 96 | ▶ | $start_y = $height - 1 if ($corner_bottom); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 96 | $rotate = ($corner_bottom << 1) | ($corner_bottom ^ $corner_right); | |||||||||||||||||||||||||||
| 247 | 96 | ▶ | $rotate ^= 2 if ($counterclock); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 96 | push @direction, splice(@direction, 0, $rotate); | |||||||||||||||||||||||||||
| 249 | 96 | ▶ | @direction = reverse @direction if ($counterclock); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 289 | { | ||||||||||||||||||||||||||||
| 290 | # | ||||||||||||||||||||||||||||
| 291 | # Adjust the starting corner if it's an even side. | ||||||||||||||||||||||||||||
| 292 | # | ||||||||||||||||||||||||||||
| 293 | 32 | ▶ | if (($width & 1) == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | { | ||||||||||||||||||||||||||||
| 295 | 16 | ▶ | $start_x++ if ($corner_right); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 16 | ▶ | $start_y++ if ($corner_bottom); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 314 | { | ||||||||||||||||||||||||||||
| 315 | 16 | $pace_x--; | |||||||||||||||||||||||||||
| 316 | 16 | ▶ | $start_x++ if (($height & 1) == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 32 | ▶ | $start_x = ($width - 1) - $start_x if ($corner_right); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 32 | ▶ | $start_y++ if (($height & 1) == 0 and ($corner_right ^ $counterclock) == 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
($height & 1) == 0 and ($corner_right ^ $counterclock) == 1
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
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 32 | $rotate = $corner_right << 1; | |||||||||||||||||||||||||||
| 324 | 32 | ▶ | $rotate ^= 1 if ($counterclock); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 332 | { | ||||||||||||||||||||||||||||
| 333 | 16 | $pace_y--; | |||||||||||||||||||||||||||
| 334 | 16 | ▶ | $start_y++ if (($width & 1) == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 32 | ▶ | $start_y = ($height - 1) - $start_y if ($corner_bottom); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 32 | ▶ | $start_x++ if (($width & 1) == 0 and ($corner_bottom ^ $counterclock) == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
($width & 1) == 0 and ($corner_bottom ^ $counterclock) == 0
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
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 32 | $rotate = ($corner_bottom << 1) | 1; | |||||||||||||||||||||||||||
| 342 | 32 | ▶ | $rotate ^= 3 if ($counterclock); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 96 | push @direction, splice(@direction, 0, $rotate); | |||||||||||||||||||||||||||
| 346 | 96 | ▶ | @direction = reverse @direction if ($counterclock); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||