PlanePath/CellularRule57.pm

filter: Statementstmt 91.8% 146 / 159 Branchbran 87.5% 70 / 80 Conditioncond 94.4% 17 / 18 MC/DCmcdc 90.9% 10 / 11 Subroutinesub 86.9% 20 / 23 Podpod 100% 6 / 6 totaltotal 90.5% 269 / 297 CC 47 SCAR 27.6
CC
47
Cov
91%
xy_to_n
28.0
n_to_xy
15.0
rect_to_n_range
12.0
CRAP 15.8
« PlanePath/DragonCurve.pm ↑ Summary PlanePath/TriangularHypot.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  
20 package Math::PlanePath::CellularRule57;
21 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 $VERSION = 128;
26 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 @ISA = ('Math::PlanePath');
28 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
29  
30 use Math::PlanePath::Base::Generic
31 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use Math::PlanePath::CellularRule54;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 *_rect_for_V = \&Math::PlanePath::CellularRule54::_rect_for_V;
35  
36 # uncomment this to run the ### lines
37 #use Smart::Comments;
38  
39  
40 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 use constant parameter_info_array =>
44 [ { name => 'mirror',
45 display => 'Mirror',
46 type => 'boolean',
47 default => 0,
48 description => 'Mirror to "rule 99" instead.',
49 },
50 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
51 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 sub x_negative_at_n {
54 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=2 SCAR=17.9
Pod documented
55 0 ▶ return $self->n_start + ($self->{'mirror'} ? 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mirror'} ? : 0 0 0
56 }
57 1 ▶use constant sumxy_minimum => 0; # triangular X>=-Y so X+Y>=0
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use constant diffxy_maximum => 0; # triangular X<=Y so X-Y<=0
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use constant dx_maximum => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use constant dy_minimum => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 sub absdx_minimum {
64 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=2 SCAR=17.9
Pod documented
65 0 ▶ return ($self->{'mirror'} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mirror'} ? : 0 0 0
66 }
67 1 ▶use constant dsumxy_maximum => 3; # straight East dX=+3
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use constant ddiffxy_maximum => 3; # straight East dX=+3
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use constant dir_maximum_dxdy => (-1,0); # supremum, West and dY=+1 up
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71  
72 #------------------------------------------------------------------------------
73  
74 sub new {
75 2 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=2 SCAR=7.6
Pod documented
76 2 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 0 2 2
77 0 $self->{'n_start'} = $self->default_n_start;
78 }
79 2 return $self;
80 }
81  
82 # left
83 # even y=3 5
84 # 5 12
85 # 7 23
86 # 9 38
87 # [1,2,3,4], [5,12,23,38]
88 #
89 # N = (2 d^2 + d + 2)
90 # = (2*$d**2 + $d + 2)
91 # = ((2*$d + 1)*$d + 2)
92 # d = -1/4 + sqrt(1/2 * $n + -15/16)
93 # = (-1 + 4*sqrt(1/2 * $n + -15/16)) / 4
94 # = (sqrt(8*$n-15)-1)/4
95 # with Y=2*d+1
96  
97 # row 19, d=9
98 # N=173 to N=181 is 9 cells rem=0..8 is d-1
99 # 1/3 section 3 cells rem=0,1,2 floor((d-1)/3)
100 # 2/3 section 6 cells
101 # right solid N=191 to N=200 is 10 of is rem<d
102 #
103 # row 21, d=10
104 # 1/3 section 4 cells rem=0,1,2,3 floor((d-1)/3)
105 # 2/3 section 6 cells
106 #
107 # row 23, d=11
108 # 1/3 section 4 cells rem=0,1,2,3 floor((d-1)/3)
109 # 2/3 section 7 cells
110 #
111 # row 25, d=12
112 # 2/3 section 8 cells
113 #
114 # row 27, d=13
115 # 2/3 section 8 cells
116 #
117 # row 29, d=14
118 # 2/3 section 9 cells floor(2d/3)
119 #
120 # row 31, d=15
121 # 2/3 section 10 cells floor(2d/3)
122 #
123 #
124 # row 18 d=8
125 # odd 1/3 section 4 cells (d+4)/3
126 #
127 # row 20 d=9
128 # odd 1/3 section 4 cells
129 #
130 # row 22 d=10
131 # odd 1/3 section 4 cells
132 #
133 # row 23 d=11
134 # odd 1/3 section 5 cells
135  
136  
137 sub n_to_xy {
138 51 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=15 SCAR=27.1
Pod documented
139 ### CellularRule57 n_to_xy(): $n
140  
141 51 $n = $n - $self->{'n_start'} + 1; # to N=1 basis, and warn if $n undef
142 51 my $frac;
143 {
144 51 my $int = int($n);
145 51 $frac = $n - $int;
146 51 $n = $int; # BigFloat int() gives BigInt, use that
147 51 ▶ if (2*$frac >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $frac >= 1) 0 51 51
148 0 $frac -= 1;
149 0 $n += 1;
150 }
151 # -0.5 <= $frac < 0.5
152 ### assert: 2*$frac >= -1
153 ### assert: 2*$frac < 1
154 }
155  
156 51 ▶ if ($n <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 1) 10 41 51
157 10 ▶ if ($n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) { } 4 6 10
158 4 return (0,0);
159 } else {
160 6 return;
161 }
162 }
163  
164 # d is the two-row group number, y=2*d+1, where n belongs
165 #
166 41 my $d = int( (_sqrtint(8*$n-15)-1)/4 );
167 41 $n -= ((2*$d + 1)*$d + 2); # remainder
168 ### $d
169 ### remainder: $n
170  
171 41 ▶ if ($self->{'mirror'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mirror'}) { } 20 21 41
172 20 ▶ if ($n <= $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $d) 7 13 20
173 ### right solid: $n
174 7 return ($frac + $n - 2*$d - 1,
175 2*$d+1);
176 }
177 13 $n -= $d+1;
178  
179 13 ▶ if ($n < int(2*$d/3)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < int 2 * $d / 3) 1 12 13
180 ### right 2/3: $n
181 1 return ($frac + int(3*$n/2) - $d + 1,
182 2*$d+1);
183 }
184 12 $n -= int(2*$d/3);
185  
186 12 ▶ if ($n < int(($d+2)/3)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < int(($d + 2) / 3)) 2 10 12
187 ### left 1/3: $n
188 2 return ($frac + 3*$n + ((2+$d)%3),
189 2*$d+1);
190 }
191 10 $n -= int(($d+2)/3);
192  
193 10 ▶ if ($n < $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $d) 3 7 10
194 ### left solid: $n
195 3 return ($frac + $n + $d+2,
196 2*$d+1);
197 }
198 7 $n -= $d;
199  
200 7 ▶ if ($n < int((2*$d+5)/3)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < int((2 * $d + 5) / 3)) 5 2 7
201 ### odd 2/3: $n
202 5 return ($frac + int((3*$n)/2) - $d + - 1,
203 2*$d+2);
204 }
205 2 $n -= int((2*$d+5)/3);
206  
207 ### odd 1/3: $n
208 2 return ($frac + 3*$n + ($d%3) + 1,
209 2*$d+2);
210  
211 } else {
212 21 ▶ if ($n < $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $d) 3 18 21
213 ### left solid: $n
214 3 return ($frac + $n - 2*$d - 1,
215 2*$d+1);
216 }
217 18 $n -= $d;
218  
219 18 ▶ if ($n < int(($d+2)/3)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < int(($d + 2) / 3)) 2 16 18
220 ### left 1/3: $n
221 2 return ($frac + 3*$n - $d + 1,
222 2*$d+1);
223 }
224 16 $n -= int(($d+2)/3);
225  
226 16 ▶ if ($n < int(2*$d/3)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < int 2 * $d / 3) 1 15 16
227 ### right 2/3: $n
228 1 return ($frac + $n + int(($n+(-$d%3))/2) + 1,
229 2*$d+1);
230 }
231 15 $n -= int(2*$d/3);
232  
233 15 ▶ if ($n <= $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $d) 7 8 15
234 ### right solid: $n
235 7 return ($frac + $d + $n + 1,
236 2*$d+1);
237 }
238 8 $n -= $d+1;
239  
240 8 ▶ if ($n < int(($d+4)/3)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < int(($d + 4) / 3)) 5 3 8
241 ### odd 1/3: $n
242 5 return ($frac + 3*$n - $d - 1,
243 2*$d+2);
244 }
245 3 $n -= int(($d+4)/3);
246  
247 ### odd 2/3: $n
248 3 return ($frac + $n + int(($n+((1-$d)%3))/2) + 1,
249 2*$d+2);
250 }
251 }
252  
253 sub xy_to_n {
254 992 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=28 SCAR=33.3
Pod documented
255 992 $x = round_nearest ($x);
256 992 $y = round_nearest ($y);
257 ### CellularRule57 xy_to_n(): "$x,$y"
258  
259 992 ▶ if ($y < 0
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0 or $x < -$y or $x > $y) 480 512 992
Condition83%5/6
$y < 0 or $x < -$y or $x > $y
l!l&&r!l&&!r
240240512
$y < 0 or $x < -$y
l!l&&r!l&&!r
0240752
MC/DC66%2/3
$y < 0 or $x < -$y or $x > $y
$y < 0 $x < -$y $x > $y
Truth table75%3/4
A or B or C
A= $y < 0B= $x < -$yC= $x > $y
ABCresult
1XX1
01X1
0011
0000
260 || $x < -$y
261 || $x > $y) {
262 ### outside pyramid region ...
263 480 return undef;
264 }
265  
266 512 ▶ if ($self->{'mirror'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mirror'}) { } 256 256 512
267 # mirrored, rule 99
268  
269 256 ▶ if ($y % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y % 2) { } 136 120 256
270 136 my $d = ($y+1)/2;
271 ### odd row, solids, d: $d
272  
273 136 ▶ if ($x < -$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < -$d) 28 108 136
274 28 return ($y+1)*$y/2 + $x + 1 + $self->{'n_start'};
275 }
276 108 ▶ if ($x < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x < 0) { } 36 72 108
elsif ($x > $d) { } 28 44 72
277 ### mirror left 2 of 3 ...
278 36 ▶ if (($x += $d+2) % 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x += $d + 2) % 3) 24 12 36
279 24 return ($y+1)*$y/2 + $x-int($x/3) - $d + $self->{'n_start'} - 1;
280 }
281 } elsif ($x > $d) {
282 28 return ($y+1)*$y/2 + $x - $d + $self->{'n_start'};
283 } else {
284 ### mirror right 1 of 3 ...
285 44 $x += 2-$d;
286 44 ▶ unless ($x % 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x % 3) 12 32 44
287 12 return ($y+1)*$y/2 + $x/3 + $self->{'n_start'};
288 }
289 }
290  
291 } else {
292 ### even row, sparse ...
293 120 my $d = $y/2;
294 120 ▶ if ($x >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 0) { } 64 56 120
295 ### mirror sparse right 1 of 3 ...
296 64 ▶ if ($x <= $d # only to half way
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $d and ($x -= $d) % 3 == 0) 15 49 64
Condition100%3/3
$x <= $d and ($x -= $d) % 3 == 0
!ll&&!rl&&r
282115
MC/DC100%2/2
$x <= $d and ($x -= $d) % 3 == 0
$x <= $d ($x -= $d) % 3 == 0
Truth table100%3/3
A and B
A= $x <= $dB= ($x -= $d) % 3 == 0
ABresult
0X0
100
111
297 && (($x -= $d) % 3) == 0) {
298 15 return ($y+1)*$y/2 + $x/3 + $self->{'n_start'};
299 }
300 } else { # $x < 0
301 ### mirror sparse left 2 of 3 ...
302 56 ▶ if ($x >= -$d # only to half way
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= -$d and ($x += $d + 1) % 3) 21 35 56
Condition100%3/3
$x >= -$d and ($x += $d + 1) % 3
!ll&&!rl&&r
28721
MC/DC100%2/2
$x >= -$d and ($x += $d + 1) % 3
$x >= -$d ($x += $d + 1) % 3
Truth table100%3/3
A and B
A= $x >= -$dB= ($x += $d + 1) % 3
ABresult
0X0
100
111
303 && (($x += $d+1) % 3)) {
304 21 return ($y+1)*$y/2 + $x-int($x/3) - $d + $self->{'n_start'} - 1;
305 }
306 }
307 }
308 } else {
309 # unmirrored, rule 57
310  
311 256 ▶ if ($y % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y % 2) { } 136 120 256
312 136 my $d = ($y+1)/2;
313 ### odd row, solids, d: $d
314  
315 136 ▶ if ($x <= -$d) {
Branch: 3 branches
BranchTrueFalseTotal
if ($x <= -$d) { } 36 100 136
elsif ($x <= 0) { } 36 64 100
elsif ($x >= $d) { } 36 28 64
316 ### solid left ...
317 36 ▶ if ($x < -$d) { # always skip the -$d cell
Branch: 1 branch
BranchTrueFalseTotal
if ($x < -$d) 28 8 36
318 28 return ($y+1)*$y/2 + $x + 1 + $self->{'n_start'};
319 }
320 } elsif ($x <= 0) {
321 ### 1 of 3 ...
322 36 ▶ unless (($x += $d+1) % 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($x += $d + 1) % 3) 12 24 36
323 12 return ($y+1)*$y/2 + $x/3 - $d + $self->{'n_start'};
324 }
325 } elsif ($x >= $d) {
326 ### solid right ...
327 36 return ($y+1)*$y/2 + $x - $d + $self->{'n_start'};
328 } else {
329 ### 2 of 3 ...
330 28 $x += 1-$d;
331 28 ▶ if ($x % 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 3) 16 12 28
332 16 return ($y+1)*$y/2 + $x-int($x/3) + $self->{'n_start'};
333 }
334 }
335  
336 } else {
337 ### even row, sparse ...
338  
339 120 my $d = $y/2;
340 120 ▶ if ($x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 0) { } 56 64 120
341 ### right 2 of 3 ...
342 56 ▶ if ($x <= $d # goes to half way only
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $d and ($x -= $d + 1) % 3) 21 35 56
Condition100%3/3
$x <= $d and ($x -= $d + 1) % 3
!ll&&!rl&&r
28721
MC/DC100%2/2
$x <= $d and ($x -= $d + 1) % 3
$x <= $d ($x -= $d + 1) % 3
Truth table100%3/3
A and B
A= $x <= $dB= ($x -= $d + 1) % 3
ABresult
0X0
100
111
343 && (($x -= $d+1) % 3)) {
344 21 return ($y+1)*$y/2 + $x-int($x/3) + 1 + $self->{'n_start'};
345 }
346 } else { # $x <= 0
347 ### left 1 of 3 ...
348 64 ▶ if (($x += $d) >= 0 # goes to half way only
Branch: 1 branch
BranchTrueFalseTotal
if (($x += $d) >= 0 and not $x % 3) 15 49 64
Condition100%3/3
($x += $d) >= 0 and not $x % 3
!ll&&!rl&&r
282115
MC/DC100%2/2
($x += $d) >= 0 and not $x % 3
($x += $d) >= 0 not $x % 3
Truth table100%3/3
A and not(B)
A= ($x += $d) >= 0B= not $x % 3
ABresult
0X0
100
111
349 && ! ($x % 3)) {
350 15 return ($y+1)*$y/2 + $x/3 - $d + $self->{'n_start'};
351 }
352 }
353 }
354 }
355 256 return undef;
356 }
357  
358 # left edge ((2*$d + 1)*$d + 2)
359 # where y=2*d+1
360 # d=floor((y-1)/2)
361 # left N = (2*floor((y-1)/2) + 1)*floor((y-1)/2) + 2
362 # = (yodd + 1)*yodd/2 + 2
363  
364  
365 # not exact
366 sub rect_to_n_range {
367 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=3 SCAR=24.8
Pod documented
368 ### CellularRule57 rect_to_n_range(): "$x1,$y1, $x2,$y2"
369  
370 0 ▶ ($x1,$y1, $x2,$y2) = _rect_for_V ($x1,$y1, $x2,$y2)
Branch: 1 branch
BranchTrueFalseTotal
() = _rect_for_V($x1, $y1, $x2, $y2) or return 1, 0 0 0 0
371 or return (1,0); # rect outside pyramid
372  
373 0 my $zero = ($x1 * 0 * $y1 * $x2 * $y2); # inherit bignum
374  
375 0 $y1 -= ! ($y1 % 2);
376 0 $y2 -= ! ($y2 % 2);
377 return ($zero + ($y1 < 1
378 ? $self->{'n_start'}
379 : ($y1-1)*$y1/2 + 1 + $self->{'n_start'}),
380 0 ▶ $zero + ($y2+2)*($y2+1)/2 + $self->{'n_start'});
Branch: 1 branch
BranchTrueFalseTotal
$y1 < 1 ? : 0 0 0
381 }
382  
383 1 1;
384 __END__
« PlanePath/DragonCurve.pm ↑ Summary PlanePath/TriangularHypot.pm »