PlanePath/AnvilSpiral.pm

filter: Statementstmt 92.1% 129 / 140 Branchbran 91.6% 33 / 36 Conditioncond 50.0% 2 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 81.4% 22 / 27 Podpod 100% 6 / 6 totaltotal 89.7% 193 / 215 CC 21 SCAR 13.8
CC
21
Cov
91%
rect_to_n_range
8.0
n_to_xy
7.0
x_negative_at_n
6.0
CRAP 4.0
« PlanePath/ImaginaryBase.pm ↑ Summary PlanePath/AztecDiamondRings.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-PlanePath is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # math-image --path=AnvilSpiral --all --output=numbers_dash
20 # math-image --path=AnvilSpiral,wider=3 --all --output=numbers_dash
21  
22 package Math::PlanePath::AnvilSpiral;
23 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 #use List::Util 'min','max';
26 1 *min = \&Math::PlanePath::_min;
27 1 *max = \&Math::PlanePath::_max;
28  
29 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 $VERSION = 128;
31 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 @ISA = ('Math::PlanePath');
33 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
34  
35 use Math::PlanePath::Base::Generic
36 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38  
39 # pentagonal N = (3k-1)*k/2
40 # preceding
41 # Np = (3k-1)*k/2 - 1
42 # = (3k^2 - k - 2)/2
43 # = (3k+2)(k-1)/2
44 #
45  
46  
47 # parameters "wider","n_start"
48 1 ▶use Math::PlanePath::SquareSpiral;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 *parameter_info_array
50 = \&Math::PlanePath::SquareSpiral::parameter_info_array;
51 1 ▶use constant xy_is_visited => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use constant dx_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,0, # E # no N,S
58 1,1, # NE
59 -1,1, # NW
60 -1,0, # W
61 -1,-1, # SW
62 1 ▶ 1,-1); # SE
Subroutine BEGIN: called CC=1 SCAR=0.0
63 # last NW at lower right
64 # 2w+4 ------- w+1
65 # \ /
66 # * 0---- w *
67 # / \
68 # 2w+6 ---------- 3w+10 w=3; 1+3*w+10=20
69 #
70 sub x_negative_at_n {
71 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=2 SCAR=17.9
Pod documented
72 0 ▶ return $self->n_start + ($self->{'wider'} ? 0 : 3);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'wider'} ? : 0 0 0
73 }
74 sub y_negative_at_n {
75 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
76 0 return $self->n_start + 2*$self->{'wider'} + 6;
77 }
78 sub _UNDOCUMENTED__dxdy_list_at_n {
79 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
80 0 return $self->n_start + 3*$self->{'wider'} + 10;
81 }
82  
83 1 ▶use constant absdx_minimum => 1; # abs(dX)=1 always
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use constant dsumxy_minimum => -2; # diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use constant dsumxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 sub _UNDOCUMENTED__turn_any_left_at_n {
91 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_left_at_n: not called CC=2 SCAR=17.9
92 # left turn at w when w!=0 or at w+1 when w==0
93 0 ▶ return $self->n_start + ($self->{'wider'} || 1);
Condition0%0/2
$self->{'wider'} || 1
l!l
00
MC/DC0%0/1
$self->{'wider'} || 1
$self->{'wider'}
Truth table0%0/2
A
A= $self->{'wider'}
Aresult
11
00
94 }
95 sub _UNDOCUMENTED__turn_any_right_at_n {
96 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: not called CC=1 SCAR=6.9
97 0 return $self->n_start + 2*$self->{'wider'} + 5;
98 }
99  
100  
101 #------------------------------------------------------------------------------
102  
103 sub new {
104 20 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=3 SCAR=11.0
Pod documented
105  
106 # parameters
107 20 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 8 12 20
108 8 $self->{'n_start'} = $self->default_n_start;
109 }
110 20 ▶ $self->{'wider'} ||= 0; # default
Condition100%2/2
$self->{"wider"} ||= 0
l!l
155
MC/DC100%1/1
$self->{"wider"} ||= 0
$self->{"wider"}
Truth table100%2/2
A
A= $self->{"wider"}
Aresult
11
00
111  
112 20 return $self;
113 }
114  
115 # [1,2,3,4],[1,12,35,70] # horizontal
116 # N = (6 d^2 - 7 d + 2)
117 # = (6*$d**2 - 7*$d + 2)
118 # = ((6*$d - 7)*$d + 2)
119 # d = 7/12 + sqrt(1/6 * $n + 1/144)
120 # = (7 + 12*sqrt(1/6 * $n + 1/144))/12
121 # = (7 + sqrt(144/6*$n + 1))/12
122 # = (7 + sqrt(24*$n + 1))/12
123 #
124 # wider=1
125 # [1,2,3,4],[1+1,12+1+2,35+1+2+2,70+1+2+2+2]
126 # N = (6 d^2 - 5 d + 1)
127 # d = 5/12 + sqrt(1/6 * $n + 1/144)
128 #
129 # wider=2
130 # [1,2,3,4],[1+2,12+2+4,35+2+4+4,70+2+4+4+4]
131 # N = (6 d^2 - 3 d)
132 # d = 3/12 + sqrt(1/6 * $n + 9/144)
133 #
134 # wider=3
135 # [1,2,3,4],[1+3,12+3+6,35+3+6+6,70+3+6+6+6]
136 # N = (6 d^2 - d - 1)
137 # d = 1/12 + sqrt(1/6 * $n + 25/144)
138 #
139 # wider=4
140 # [1,2,3,4],[1+4,12+4+8,35+4+8+8,70+4+8+8+8]
141 # N = (6 d^2 + d - 2)
142 # d = -1/12 + sqrt(1/6 * $n + 49/144) # 49=7*7=(2w-1)*(2w-1)
143 #
144 # in general
145 # N = (6 d^2 - (7-2w) d + 2-w)
146 # = (6d - (7-2w)) d + 2-w
147 # = (6d - 7 + 2w))*d + 2-w
148 # d = (7-2w)/12 + sqrt(1/6 * $n + (w-1)^2/144)
149 # = [ 7-2w + 12*sqrt(1/6 * $n + (w-1)^2/144) ] / 12
150 # = [ 7-2w + sqrt(144/6*$n + (w-1)^2) ] / 12
151 # = [ 7-2w + sqrt(24*$n + (w-1)^2) ] / 12
152  
153  
154  
155 sub n_to_xy {
156 9234 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=7 SCAR=19.5
Pod documented
157 ### AnvilSpiral n_to_xy(): $n
158  
159 9234 $n = $n - $self->{'n_start'}; # to N=0 basis, warning if $n==undef
160 9234 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 9234 9234
161 9234 my $w = $self->{'wider'};
162 9234 my $w_right = int($w/2);
163 9234 my $w_left = $w - $w_right;
164 ### $w
165 ### $w_left
166 ### $w_right
167  
168 9234 ▶ if ($n <= $w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $w) 114 9120 9234
169 ### centre horizontal
170 114 return ($n - $w_left, # N=0 at $w_left
171 0);
172 }
173  
174 9120 my $d = int((_sqrtint(24*($n+1) + (2*$w-1)**2) + 7-2*$w) / 12);
175 ### ($n+1)
176 ### $d
177 ### d frac: ((sqrt(int(24*($n+1)) + (2*$w-1)**2) + 7-2*$w) / 12)
178 ### d sqrt add: ($w-1)*($w-1)
179 ### d const part: 7-2*$w
180  
181 9120 $n -= (6*$d - 7 + 2*$w)*$d + 2-$w;
182 ### base: (6*$d - 7 + 2*$w)*$d + 2-$w
183 ### remainder: $n
184  
185 9120 ▶ if ($n <= 5*$d+$w-2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 5 * $d + $w - 2) 4268 4852 9120
186 4268 ▶ if ($n+1 <= $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n + 1 <= $d) { } 874 3394 4268
187 ### upper right slope ...
188 874 return ($n + $d + $w_right,
189 $n+1);
190 } else {
191 ### top ...
192 3394 return (-$n + 3*$d + $w_right - 2,
193 $d);
194 }
195 }
196  
197 4852 $n -= 7*$d + $w - 2;
198 4852 ▶ if ($n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 0) 1320 3532 4852
199 ### left slopes: $n
200 1320 return (-abs($n+$d) - $d - $w_left,
201 -$n - $d);
202 }
203  
204 3532 $n -= 4*$d + $w;
205 3532 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) { } 2944 588 3532
206 ### bottom ...
207 2944 return ($n + 2*$d + $w_right,
208 -$d);
209 } else {
210 ### right lower ...
211 588 return (-$n + 2*$d + $w_right,
212 $n - $d);
213 }
214 }
215  
216 sub xy_to_n {
217 9234 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=4 SCAR=13.9
Pod documented
218 ### AnvilSpiral xy_to_1 n(): "$x, $y"
219  
220 9234 $x = round_nearest ($x);
221 9234 $y = round_nearest ($y);
222  
223 9234 my $w = $self->{'wider'};
224 9234 my $w_right = int($w/2);
225 9234 my $w_left = $w - $w_right;
226 ### $w
227 ### $w_left
228 ### $w_right
229  
230 9234 my $abs_y = abs($y);
231 9234 ▶ if ($x-$w_right >= 2*$abs_y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x - $w_right >= 2 * $abs_y) 1327 7907 9234
232 ### right slopes: "d=".($x-$w_right - $abs_y)
233 1327 my $d = $x-$w_right - $abs_y; # zero based
234 return ((6*$d + 5 + 2*$w)*$d + $w
235 + $y
236 1327 + $self->{'n_start'});
237 }
238  
239 7907 ▶ if ($x+$w_left < -2*$abs_y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x + $w_left < -2 * $abs_y) 1178 6729 7907
240 ### left slopes: "d=".($x+$w_left + $abs_y)
241 1178 my $d = $x+$w_left + $abs_y; # negative, and zero based
242 return ((6*$d + 1 - 2*$w)*$d
243 - $y
244 1178 + $self->{'n_start'});
245 }
246  
247 6729 ▶ if ($y > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > 0) { } 3547 3182 6729
248 ### top horizontal ...
249 return ((6*$y - 4 + 2*$w)*$y - $w
250 + $w_right-$x
251 3547 + $self->{'n_start'});
252 } else {
253 ### bottom horizontal ...
254 # y negative
255 return ((6*$y - 2 - 2*$w)*$y
256 + $x+$w_left
257 3182 + $self->{'n_start'});
258 }
259 }
260  
261 # uncomment this to run the ### lines
262 #use Smart::Comments;
263  
264 # ...-78-77-76-75-74
265 # /
266 # 43-42-41-40-39-38 73
267 # / /
268 # 17-16-15-14 37 72
269 # / / /
270 # -3--2 13 36 71
271 # / / / /
272 # 1 12 35 70
273 #
274 # column X=2, dmin decreasing until Y=1=floor(x/2)
275 # column X=3, dmin decreasing until Y=2=ceil(x/2)
276 # so x1 - min(y2,int((x1+1)/2))
277 #
278 #
279 # column Xmax=2, dmax increasing down until x2-y1
280 #
281 # horizontal Y>=0 N increases left and right of X=Y*3/2
282 # so candidate max at top-left x1,y2 or top-right x2,y2
283 #
284 # horizontal Y<0 N increases left and right of X=-Y*3/2
285 # so candidate max at bottom-left x1,y1 or bottom-right x2,y1
286 #
287 # vertical Y>=0 N increases above and below Y=ceil(X/2)
288 # so candidate max at top-right or bottom-right, or Y=0
289 #
290 # vertical Y<0 N increases above and below Y=ceil(X/2)
291 # so candidate max at top-right or bottom-right, or Y=0
292 #
293 # int(($y2+1)/2), $y2
294 # int(($y1+1)/2), $y1
295 #
296 # my @corners = ($self->xy_to_n($x1,$y1),
297 # $self->xy_to_n($x2,$y1),
298 # $self->xy_to_n($x1,$y2),
299 # $self->xy_to_n($x2,$y2));
300 # return (($x_zero && $y_zero ? 1 : min (@corners)),
301 # max (@corners,
302 # ($y_zero ? ($self->xy_to_n($x1,0),
303 # $self->xy_to_n($x2,0)) : ())));
304  
305  
306  
307  
308 # not exact
309 sub rect_to_n_range {
310 18468 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=8 SCAR=20.8
Pod documented
311 ### AnvilSpiral rect_to_n_range(): "$x1,$y1 $x2,$y2"
312  
313 18468 my $w = $self->{'wider'};
314 18468 my $w_right = int($w/2);
315 18468 my $w_left = $w - $w_right;
316  
317 18468 $x1 = round_nearest($x1);
318 18468 $x2 = round_nearest($x2);
319 18468 $y1 = round_nearest($y1);
320 18468 $y2 = round_nearest($y2);
321  
322 18468 my $x_zero = (($x1<0) != ($x2<0));
323 18468 my $y_zero = (($y1<0) != ($y2<0));
324 ### $x_zero
325 ### $y_zero
326  
327 18468 $x1 += $w_left;
328 18468 $x2 += $w_left;
329  
330 18468 ▶ if ($x1 < 0) { $x1 = $w-$x1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 7598 10870 18468
331 18468 ▶ if ($x2 < 0) { $x2 = $w-$x2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0) 3799 14669 18468
332 18468 $y1 = abs($y1);
333 18468 $y2 = abs($y2);
334  
335 18468 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 8106 10362 18468
336 18468 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 8839 9629 18468
337  
338 18468 ▶ if ($x_zero) { $x1 = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x_zero) 4621 13847 18468
339 18468 ▶ if ($y_zero) { $y1 = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y_zero) 4186 14282 18468
340  
341 ### abs: "$x1,$y1 $x2,$y2"
342 ### d1 slope max y: int(($x1+1)/2)
343 ### d1 slope: $x1 - min($y2,int(($x1+1)/2))
344  
345 # --------*
346 # /
347 # /
348 # * <-y=0
349 # x=0....w
350 #
351 # d=x-w-y on the slope
352 # d=y on the top horizontal
353 #
354 18468 my $d1 = min ($x1-$w - min($y2,int(($x1-$w+1)/2)) - 1,
355 $y2);
356 18468 my $d2 = 1 + max ($x2-$w - $y1,
357 $y2);
358 ### $d1
359 ### $d2
360 ### d2 right slope would be: $x2-$w_right - $y2
361  
362 # d1==0 is the centre horizontal
363 #
364  
365 return ($d1 <= 0
366 ? $self->{'n_start'}
367 : (6*$d1 - 7 + 2*$w)*$d1 + 1-$w + $self->{'n_start'},
368  
369 18468 ▶ (6*$d2 - 6 + 2*$w)*$d2 - $w + $self->{'n_start'});
Branch: 1 branch
BranchTrueFalseTotal
$d1 <= 0 ? : 12933 5535 18468
370 }
371  
372 1 1;
373 __END__
« PlanePath/ImaginaryBase.pm ↑ Summary PlanePath/AztecDiamondRings.pm »