PlanePath/KochSquareflakes.pm

filter: Statementstmt 86.8% 217 / 250 Branchbran 77.0% 74 / 96 Conditioncond 92.8% 13 / 14 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 92.0% 23 / 25 Podpod 100% 5 / 5 totaltotal 84.3% 335 / 397 CC 55 SCAR 30.9
CC
55
Cov
84%
rect_to_n_range
72.0
n_to_xy
24.0
xy_to_n
23.1
CRAP 22.0
« PlanePath/MPeaks.pm ↑ Summary PlanePath/SierpinskiCurveStair.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 it
6 # under the terms of the GNU General Public License as published by the Free
7 # 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 package Math::PlanePath::KochSquareflakes;
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 $VERSION = 128;
25 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 @ISA = ('Math::PlanePath');
27 1 *_divrem = \&Math::PlanePath::_divrem;
28  
29 use Math::PlanePath::Base::Generic
30 1 'is_infinite',
31 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 use Math::PlanePath::Base::Digits
33 1 'round_down_pow',
34 1 ▶ 'digit_split_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # uncomment this to run the ### lines
37 #use Devel::Comments;
38  
39  
40 1 ▶use constant n_frac_discontinuity => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 use constant parameter_info_array =>
43 [ { name => 'inward',
44 display => 'Inward',
45 type => 'boolean',
46 default => 0,
47 description => 'Whether to direct the sides of the square inward, rather than outward.',
48 1 ▶ } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 1 ▶use constant x_negative_at_n => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant y_negative_at_n => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use constant sumabsxy_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use constant rsquared_minimum => 0.5; # minimum X=0.5,Y=0.5
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # jump across rings is South-West, so
56 1 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use constant dsumxy_maximum => 2; # diagonal NE
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 # N=1,2,3,4 gcd(1/2,1/2) = 1/2
64 1 ▶use constant gcdxy_minimum => 1/2;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 1 ▶use constant turn_any_straight => 0; # never straight
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68  
69 #------------------------------------------------------------------------------
70  
71 # level 0 inner square
72 # sidelen = 4^level
73 # ring points 4*4^level
74 # Nend = 4 * [ 1 + ... + 4^level ]
75 # = 4 * (4^(level+1) - 1) / 3
76 # = (4^(level+2) - 4) / 3
77 # Nstart = Nend(level-1) + 1
78 # = (4^(level+1) - 4) / 3 + 1
79 # = (4^(level+1) - 4 + 3) / 3
80 # = (4^(level+1) - 1) / 3
81 #
82 # level Nstart Nend
83 # 0 (4-1)/3=1 (16-4)/3=12/3=4
84 # 1 (16-1)/3=15/3=5 (64-4)/3=60/3=20
85 # 2 (64-1)/3=63/3=21 (256-4)/3=252/3=84
86 # 3 (256-1)/3=255/3=85
87 #
88  
89 sub n_to_xy {
90 190 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=24 SCAR=31.8
Pod documented
91 ### KochSquareflakes n_to_xy(): $n
92 190 ▶ if ($n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 190 190
93  
94 190 my $frac;
95 {
96 190 my $int = int($n);
97 190 $frac = $n - $int;
98 190 $n = $int; # BigFloat int() gives BigInt, use that
99 }
100  
101 # (4^(level+1) - 1) / 3 = N
102 # 4^(level+1) - 1 = 3*N
103 # 4^(level+1) = 3*N+1
104 #
105 190 my ($pow,$level) = round_down_pow (3*$n + 1, 4);
106 ### $level
107 ### $pow
108 190 ▶ if (is_infinite($level)) { return ($level,$level); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 190 190
109  
110 # Nstart = (4^(level+1)-1)/3 with $power=4^(level+1) here
111 #
112 190 $n -= ($pow-1)/3;
113  
114 ### base: ($pow-1)/3
115 ### next base would be: (4*$pow-1)/3
116 ### n remainder from base: $n
117  
118 190 my $sidelen = $pow/4;
119 190 (my $rot, $n) = _divrem ($n, $sidelen); # high part is rot
120 ### $sidelen
121 ### n remainder: $n
122 ### $rot
123  
124 ### assert: $n>=0
125 ### assert: $n < 4 ** $level
126  
127 190 my @horiz = (1);
128 190 my @diag = (1);
129 190 my $i = 0;
130 190 ▶ while (--$level > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$level > 0) 386 190 576
131 386 $horiz[$i+1] = 2*$horiz[$i] + 2*$diag[$i];
132 386 $diag[$i+1] = $horiz[$i] + 2*$diag[$i];
133 ### horiz: $horiz[$i+1]
134 ### diag: $diag[$i+1]
135 386 $i++;
136 }
137  
138 ### horiz: join(', ',@horiz)
139 ### $i
140 190 my $x
141 = my $y
142 = ($n * 0) + $horiz[$i]/-2; # inherit bignum
143 190 ▶ if ($rot & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 1) 87 103 190
144 87 ($x,$y) = (-$y,$x);
145 }
146 190 ▶ if ($rot & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 2) 87 103 190
147 87 $x = -$x;
148 87 $y = -$y;
149 }
150 190 $rot *= 2;
151  
152 190 my $inward = $self->{'inward'};
153 190 my @digits = digit_split_lowtohigh($n,4);
154  
155 190 ▶ while ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 386 190 576
156 386 $i--;
157 386 ▶ my $digit = $digits[$i] || 0;
Condition100%2/2
$digits[$i] || 0
l!l
237149
MC/DC100%1/1
$digits[$i] || 0
$digits[$i]
Truth table100%2/2
A
A= $digits[$i]
Aresult
11
00
158  
159 386 my ($dx, $dy, $drot);
160 386 ▶ if ($digit == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($digit == 0) { } 149 237 386
elsif ($digit == 1) { } 72 165 237
elsif ($digit == 2) { } 72 93 165
elsif ($digit == 3) { } 93 0 93
161 149 $dx = 0;
162 149 $dy = 0;
163 149 $drot = 0;
164 } elsif ($digit == 1) {
165 72 ▶ if ($rot & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 1) { } 16 56 72
166 16 $dx = $diag[$i];
167 16 $dy = $diag[$i];
168 } else {
169 56 $dx = $horiz[$i];
170 56 $dy = 0;
171 }
172 72 ▶ $drot = ($inward ? 1 : -1);
Branch: 1 branch
BranchTrueFalseTotal
$inward ? : 36 36 72
173 } elsif ($digit == 2) {
174 72 ▶ if ($rot & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 1) { } 16 56 72
175 16 ▶ if ($inward) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inward) { } 8 8 16
176 8 $dx = $diag[$i];
177 8 $dy = $diag[$i] + $horiz[$i];
178 } else {
179 8 $dx = $diag[$i] + $horiz[$i];
180 8 $dy = $diag[$i];
181 }
182 } else {
183 56 $dx = $horiz[$i] + $diag[$i];
184 56 $dy = $diag[$i];
185 56 ▶ unless ($inward) { $dy = -$dy; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($inward) 28 28 56
186 }
187 72 ▶ $drot = ($inward ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$inward ? : 36 36 72
188 } elsif ($digit == 3) {
189 93 ▶ if ($rot & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 1) { } 16 77 93
190 16 $dx = $dy = $diag[$i] + $horiz[$i];
191 } else {
192 77 $dx = $horiz[$i] + 2*$diag[$i];
193 77 $dy = 0;
194 }
195 93 $drot = 0;
196 }
197 ### delta: "$dx,$dy rot=$rot drot=$drot"
198  
199 386 ▶ if ($rot & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 2) 165 221 386
200 165 ($dx,$dy) = (-$dy,$dx);
201 }
202 386 ▶ if ($rot & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 4) 165 221 386
203 165 $dx = -$dx;
204 165 $dy = -$dy;
205 }
206 ### delta with rot: "$dx,$dy"
207  
208 386 $x += $dx;
209 386 $y += $dy;
210 386 $rot += $drot;
211 }
212  
213 {
214 190 my $dx = $frac;
215 190 ▶ my $dy = ($rot & 1 ? $frac : 0);
Branch: 1 branch
BranchTrueFalseTotal
$rot & 1 ? : 80 110 190
216 190 ▶ if ($rot & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 2) 87 103 190
217 87 ($dx,$dy) = (-$dy,$dx);
218 }
219 190 ▶ if ($rot & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 4) 87 103 190
220 87 $dx = -$dx;
221 87 $dy = -$dy;
222 }
223 190 $x = $dx + $x;
224 190 $y = $dy + $y;
225 }
226  
227 190 return ($x,$y);
228 }
229  
230 1 my @inner_to_n = (1,2,4,3);
231  
232 sub xy_to_n {
233 880 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=23 SCAR=31.4
Pod documented
234 ### KochSquareflakes xy_to_n(): "$x, $y"
235  
236 # +/- 0.75
237 880 ▶ if (4*$x < 3 && 4*$y < 3 && 4*$x >= -3 && 4*$y >= -3) {
Branch: 1 branch
BranchTrueFalseTotal
if (4 * $x < 3 and 4 * $y < 3 and 4 * $x >= -3 and 4 * $y >= -3) 0 880 880
Condition88%8/9
4 * $x < 3 and 4 * $y < 3 and 4 * $x >= -3 and 4 * $y >= -3
!ll&&!rl&&r
860200
4 * $x < 3 and 4 * $y < 3 and 4 * $x >= -3
!ll&&!rl&&r
64022020
4 * $x < 3 and 4 * $y < 3
!ll&&!rl&&r
420220240
MC/DC0%0/4
4 * $x < 3 and 4 * $y < 3 and 4 * $x >= -3 and 4 * $y >= -3
4 * $x < 3 4 * $y < 3 4 * $x >= -3 4 * $y >= -3
Truth table80%4/5
A and B and C and D
A= 4 * $x < 3B= 4 * $y < 3C= 4 * $x >= -3D= 4 * $y >= -3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
238 0 return $inner_to_n[($x >= 0) + 2*($y >= 0)];
239 }
240  
241 880 $x = round_nearest($x);
242 880 $y = round_nearest($y);
243  
244 # quarter curve segment and high digit
245 880 my $n;
246 {
247 880 my $negx = -$x;
248 880 ▶ if (($y > 0 ? $x > $y : $x >= $y)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y > 0 ? $x > $y : $x >= $y) { } 440 440 880
$y > 0 ? : 420 460 880
249 ### below leading diagonal ...
250 440 ▶ if ($negx > $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negx > $y) { } 220 220 440
251 ### bottom quarter ...
252 220 $n = 1;
253 } else {
254 ### right quarter ...
255 220 $n = 2;
256 220 ($x,$y) = ($y, $negx); # rotate -90
257 }
258 } else {
259 ### above leading diagonal
260 440 ▶ if ($y > $negx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $negx) { } 220 220 440
261 ### top quarter ...
262 220 $n = 3;
263 220 $x = $negx; # rotate 180
264 220 $y = -$y;
265 } else {
266 ### right quarter ...
267 220 $n = 4;
268 220 ($x,$y) = (-$y, $x); # rotate +90
269 }
270 }
271 }
272 880 $y = -$y;
273 ### rotate to: "$x,$y n=$n"
274  
275 880 ▶ if (is_infinite($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 880 880
276 0 return $x;
277 }
278 880 ▶ if (is_infinite($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 880 880
279 0 return $y;
280 }
281  
282 880 my @horiz;
283 my @diag;
284 880 my $horiz = 1;
285 880 my $diag = 1;
286 880 for (;;) {
287 1664 push @horiz, $horiz;
288 1664 push @diag, $diag;
289 1664 my $offset = $horiz+$diag;
290 1664 my $nextdiag = $offset + $diag; # horiz + 2*diag
291 ### $horiz
292 ### $diag
293 ### $offset
294 ### $nextdiag
295  
296 1664 ▶ if ($y <= $nextdiag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $nextdiag) 880 784 1664
297 ### found level at: "top=$nextdiag vs y=$y"
298 880 $y -= $offset;
299 880 $x += $offset;
300 880 last;
301 }
302 784 $horiz = 2*$offset; # 2*horiz+2*diag
303 784 $diag = $nextdiag;
304 }
305 ### base subtract to: "$x,$y"
306  
307  
308 880 ▶ if ($self->{'inward'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inward"}) 440 440 880
309 440 $y = -$y;
310 ### inward invert to: "$x,$y"
311 }
312  
313 ### origin based side: "$x,$y horiz=$horiz diag=$diag with levels ".scalar(@horiz)
314  
315 # loop 4*1, 4*4, 4*4^2 etc, extra +1 on the digits to include that in the sum
316 #
317 880 my $slope;
318 880 ▶ while (@horiz) {
Branch: 1 branch
BranchTrueFalseTotal
if (@horiz) 1664 880 2544
319 ### at: "$x,$y slope=".($slope||0)." n=$n"
320 1664 $horiz = pop @horiz;
321 1664 $diag = pop @diag;
322 1664 $n *= 4;
323  
324 1664 ▶ if ($slope) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slope) { } 336 1328 1664
325 336 ▶ if ($y < $diag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $diag) { } 152 184 336
326 ### slope digit 0 ...
327 152 $n += 1;
328 } else {
329 184 $x -= $diag;
330 184 $y -= $diag;
331 ### slope not digit 0, move to: "$x,$y"
332  
333 184 ▶ if ($y < $horiz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $horiz) { } 80 104 184
334 ### digit 1 ...
335 80 $n += 2;
336 80 ($x,$y) = ($y, -$x); # rotate -90
337 80 $slope = 0;
338 } else {
339 104 $y -= $horiz;
340 ### slope not digit 1, move to: "$x,$y"
341  
342 104 ▶ if ($x < $horiz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $horiz) { } 48 56 104
343 ### digit 2 ...
344 48 $n += 3;
345 48 $slope = 0;
346  
347 } else {
348 ### digit 3 ...
349 56 $n += 4;
350 56 $x -= $horiz;
351 }
352 }
353 }
354  
355 } else {
356 1328 ▶ if ($x < $horiz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $horiz) { } 384 944 1328
357 ### digit 0 ...
358 384 $n += 1;
359 } else {
360 944 $x -= $horiz;
361 ### not digit 0, move to: "$x,$y"
362  
363 944 ▶ if ($x < $diag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $diag) { } 280 664 944
364 ### digit 1 ...
365 280 $n += 2;
366 280 $slope = 1;
367 } else {
368 664 $x -= $diag;
369 ### not digit 1, move to: "$x,$y"
370  
371 664 ▶ if ($x < $diag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $diag) { } 280 384 664
372 ### digit 2 ...
373 280 $n += 3;
374 280 $slope = 1;
375 280 ($x,$y) = ($diag-$y, $x); # offset and rotate +90
376  
377 } else {
378 ### digit 3 ...
379 384 $n += 4;
380 384 $x -= $diag;
381 }
382 }
383 }
384 }
385 }
386 ### final: "$x,$y n=$n"
387  
388 880 ▶ if ($x == 0 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) { } 160 720 880
Condition100%3/3
$x == 0 and $y == 0
!ll&&!rl&&r
368352160
MC/DC100%2/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table100%3/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
389 160 return $n;
390 } else {
391 720 return undef;
392 }
393 }
394  
395 # not exact
396 sub rect_to_n_range {
397 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=8 SCAR=42.8
Pod documented
398 ### KochSquareflakes rect_to_n_range(): "$x1,$y1 $x2,$y2"
399  
400 0 foreach ($x1,$y1, $x2,$y2) {
401 0 ▶ if (is_infinite($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($_)) 0 0 0
402 0 return (0, $_);
403 }
404 0 $_ = abs(round_nearest($_));
405 }
406 0 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 0 0
407 0 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 0 0
408 0 ▶ my $max = ($x2 > $y2 ? $x2 : $y2);
Branch: 1 branch
BranchTrueFalseTotal
$x2 > $y2 ? : 0 0 0
409  
410 # Nend = 4 * [ 1 + ... + 4^level ]
411 # = 4 + 16 + ... + 4^(level+1)
412 #
413 0 my $horiz = 4;
414 0 my $diag = 3;
415 0 my $nhi = 4;
416 0 for (;;) {
417 0 $nhi += 1;
418 0 $nhi *= 4;
419 0 my $nextdiag = $horiz + 2*$diag;
420 0 ▶ if (($self->{'inward'} ? $horiz : $nextdiag) >= 2*$max) {
Branch: 2 branches
BranchTrueFalseTotal
if (($self->{"inward"} ? $horiz : $nextdiag) >= 2 * $max) 0 0 0
$self->{'inward'} ? : 0 0 0
421 0 return (1, $nhi);
422 }
423 0 $horiz = $nextdiag + $horiz; # 2*$horiz + 2*$diag;
424 0 $diag = $nextdiag;
425 }
426 }
427  
428  
429 #------------------------------------------------------------------------------
430 # Nstart = (4^(k+1) - 1)/3
431 # Nend = Nstart(k+1) - 1
432 # = (4*4^(k+1) - 1)/3 - 1
433 # = (4*4^(k+1) - 1 - 3)/3
434 # = (4*4^(k+1) - 4)/3
435 # = 4*(4^(k+1) - 1)/3
436 # = 4*Nstart(k)
437  
438 sub level_to_n_range {
439 10 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
440 10 my $n_lo = (4**($level+1) - 1)/3;
441 10 return ($n_lo, 4*$n_lo);
442 }
443 sub n_to_level {
444 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
445 0 ▶ if ($n < 1) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 0 0
446 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
447 0 my ($pow,$exp) = round_down_pow (3*$n + 1, 4);
448 0 return $exp-1;
449 }
450  
451 #------------------------------------------------------------------------------
452 1 1;
453 __END__
« PlanePath/MPeaks.pm ↑ Summary PlanePath/SierpinskiCurveStair.pm »