PlanePath/FlowsnakeCentres.pm

filter: Statementstmt 86.8% 251 / 289 Branchbran 80.3% 82 / 102 Conditioncond 92.3% 24 / 26 MC/DCmcdc 82.3% 14 / 17 Subroutinesub 84.3% 27 / 32 Podpod 100% 9 / 9 totaltotal 85.6% 407 / 475 CC 67 SCAR 27.0
CC
67
Cov
86%
rect_to_n_range
27.1
xy_to_n
19.3
n_to_xy
17.4
CRAP 14.9
« PlanePath/PyramidSides.pm ↑ Summary PlanePath/PentSpiral.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 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 # math-image --path=FlowsnakeCentres --lines --scale=10
20 #
21 # http://80386.nl/projects/flowsnake/
22 #
23  
24  
25 package Math::PlanePath::FlowsnakeCentres;
26 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use POSIX 'ceil';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use List::Util 'min'; # 'max'
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 *max = \&Math::PlanePath::_max;
31  
32 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 $VERSION = 128;
34 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 @ISA = ('Math::PlanePath');
36 2 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
37  
38 use Math::PlanePath::Base::Generic
39 2 'is_infinite',
40 'round_nearest',
41 2 ▶ 'xy_is_even';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 use Math::PlanePath::Base::Digits
43 2 'digit_split_lowtohigh',
44 2 ▶ 'round_up_pow';
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 2 ▶use Math::PlanePath::SacksSpiral;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 *_rect_to_radius_range = \&Math::PlanePath::SacksSpiral::_rect_to_radius_range;
48  
49 # uncomment this to run the ### lines
50 #use Devel::Comments;
51  
52  
53 2 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 2 use constant parameter_info_array => [ { name => 'arms',
56 share_key => 'arms_3',
57 display => 'Arms',
58 type => 'integer',
59 minimum => 1,
60 maximum => 3,
61 default => 1,
62 width => 1,
63 description => 'Arms',
64 2 ▶ } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 {
67 2 my @x_negative_at_n = (undef, 3, 1, 1);
68 sub x_negative_at_n {
69 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
70 0 return $x_negative_at_n[$self->{'arms'}];
71 }
72 }
73 {
74 2 my @y_negative_at_n = (undef, 8597, 7, 2);
75 sub y_negative_at_n {
76 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
77 0 return $y_negative_at_n[$self->{'arms'}];
78 }
79 }
80  
81 2 ▶use constant dx_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use constant dx_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 *_UNDOCUMENTED__dxdy_list = \&Math::PlanePath::_UNDOCUMENTED__dxdy_list_six;
86 {
87 2 my @_UNDOCUMENTED__dxdy_list_at_n = (undef, 10, 6, 8);
88 sub _UNDOCUMENTED__dxdy_list_at_n {
89 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
90 0 return $_UNDOCUMENTED__dxdy_list_at_n[$self->{'arms'}];
91 }
92 }
93 2 ▶use constant absdx_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 2 ▶use constant dsumxy_minimum => -2; # diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
95 2 ▶use constant dsumxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 2 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 2 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 2 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100  
101  
102 #------------------------------------------------------------------------------
103 # *
104 # / \
105 # / \
106 # *-----*
107 #
108 # (b/2)^2 + h^2 = s
109 # (1/2)^2 + h^2 = 1
110 # h^2 = 1 - 1/4
111 # h = sqrt(3)/2 = 0.866
112 #
113  
114 sub new {
115 31 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
116 31 ▶ $self->{'arms'} = max(1, min(3, $self->{'arms'} || 1));
Condition100%2/2
$self->{'arms'} || 1
l!l
1813
MC/DC100%1/1
$self->{'arms'} || 1
$self->{'arms'}
Truth table100%2/2
A
A= $self->{'arms'}
Aresult
11
00
117 31 return $self;
118 }
119  
120  
121 # # next_state length 84
122 # my @next_state = (0, 35,49,14, 0,70, 7, 0,21, 7,21,42,28, 7, # 0,7
123 # 14,49,63,28,14, 0,21, 14,35,21,35,56,42,21, # 14,21
124 # 28,63,77,42,28,14,35, 28,49,35,49,70,56,35, # 28,35
125 # 42,77, 7,56,42,28,49, 42,63,49,63, 0,70,49, # 42,49
126 # 56, 7,21,70,56,42,63, 56,77,63,77,14, 0,63, # 56,63
127 # 70,21,35, 0,70,56,77, 70, 7,77, 7,28,14,77); # 70,77
128 # my @digit_to_i = (0, 1, 0,-1,-1, 0, 1, 0, 1, 2, 3, 3, 2, 1, # 0,7
129 # 0, 0,-1,-1,-2,-2,-1, 0, 0, 1, 1, 0, 0,-1, # 14,21
130 # 0, -1,-1, 0,-1,-2,-2, 0,-1,-1,-2,-3,-2,-2, # 28,35
131 # 0, -1, 0, 1, 1, 0,-1, 0,-1,-2,-3,-3,-2,-1, # 42,49
132 # 0, 0, 1, 1, 2, 2, 1, 0, 0,-1,-1, 0, 0, 1, # 56,63
133 # 0, 1, 1, 0, 1, 2, 2, 0, 1, 1, 2, 3, 2,2); # 70,77
134 # my @digit_to_j = (0, 0, 1, 1, 2, 2, 1, 0, 0,-1,-1, 0, 0, 1, # 0,7
135 # 0, 1, 1, 0, 1, 2, 2, 0, 1, 1, 2, 3, 2, 2, # 14,21
136 # 0, 1, 0,-1,-1, 0, 1, 0, 1, 2, 3, 3, 2, 1, # 28,35
137 # 0, 0,-1,-1,-2,-2,-1, 0, 0, 1, 1, 0, 0,-1, # 42,49
138 # 0, -1,-1, 0,-1,-2,-2, 0,-1,-1,-2,-3,-2,-2, # 56,63
139 # 0, -1, 0, 1, 1, 0,-1, 0,-1,-2,-3,-3,-2,-1); # 70,77
140 # my @state_to_di = ( 1, 1, 0, 0,-1,-1, -1,-1, 0, 0, 1,1);
141 # my @state_to_dj = ( 0, 0, 1, 1, 1, 1, 0, 0,-1,-1,-1,-1);
142 #
143 #
144 # sub n_to_xy {
145 # my ($self, $n) = @_;
146 # ### Flowsnake n_to_xy(): $n
147 #
148 # if ($n < 0) { return; }
149 # if (is_infinite($n)) { return ($n,$n); }
150 #
151 # my $int = int($n);
152 # $n -= $int; # fraction part
153 # ### $int
154 # ### frac: $n
155 #
156 # my $state;
157 # {
158 # my $arm = _divrem_mutate ($int, $self->{'arms'});
159 # $state = 28 * $arm; # initial rotation
160 #
161 # # adjust so that for arms=2 point N=1 has $int==1
162 # # or for arms=3 then points N=1 and N=2 have $int==1
163 # if ($arm) { $int += 1; }
164 # }
165 # ### initial state: $state
166 #
167 # my $i = my $j = $int*0; # bignum zero
168 #
169 # foreach my $digit (reverse digit_split_lowtohigh($int,7)) { # high to low
170 # ### at: "state=$state digit=$digit i=$i,j=$j di=".$digit_to_i[$state+$digit]." dj=".$digit_to_j[$state+$digit]
171 #
172 # # i,j * (2+w), being 2*(i,j)+rot60(i,j)
173 # # then add low digit position
174 # #
175 # $state += $digit;
176 # ($i, $j) = (2*$i - $j + $digit_to_i[$state],
177 # 3*$j + $i + $digit_to_j[$state]);
178 # $state = $next_state[$state];
179 # }
180 # ### integer: "i=$i, j=$j"
181 #
182 # # fraction in final $state direction
183 # if ($n) {
184 # ### apply: "frac=$n state=$state"
185 # $state /= 7;
186 # $i = $n * $state_to_di[$state] + $i;
187 # $j = $n * $state_to_dj[$state] + $j;
188 # }
189 #
190 # ### ret: "$i, $j x=".(2*$i+$j)." y=$j"
191 # return (2*$i+$j,
192 # $j);
193 #
194 # }
195  
196 # 4-->5
197 # ^ ^
198 # / \
199 # 3--- 2 6--
200 # \
201 # v
202 # 0-->1
203 #
204  
205 2 my @digit_reverse = (0,1,1,0,0,0,1); # 1,2,6
206  
207 sub n_to_xy {
208 10907 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=16 SCAR=28.6
Pod documented
209 ### FlowsnakeCentres n_to_xy(): $n
210  
211 10907 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 10907 10907
212 10907 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 10907 10907
213  
214 # ENHANCE-ME: work $frac into initial $x,$y somehow
215 # my $frac;
216 # {
217 # my $int = int($n);
218 # $frac = $n - $int; # inherit possible BigFloat/BigRat
219 # $n = $int; # BigInt instead of BigFloat
220 # }
221 {
222 10907 my $int = int($n);
223 ### $int
224 ### $n
225 10907 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 63 10844 10907
226 63 my ($x1,$y1) = $self->n_to_xy($int);
227 63 my ($x2,$y2) = $self->n_to_xy($int+$self->{'arms'});
228 63 my $frac = $n - $int; # inherit possible BigFloat
229 63 my $dx = $x2-$x1;
230 63 my $dy = $y2-$y1;
231 63 return ($frac*$dx + $x1, $frac*$dy + $y1);
232 }
233 10844 $n = $int; # BigFloat int() gives BigInt, use that
234 }
235  
236 # arm as initial rotation
237 10844 my $rot = _divrem_mutate ($n, $self->{'arms'});
238  
239 10844 my @digits = digit_split_lowtohigh($n,7);
240 ### @digits
241  
242 10844 my $x = 0;
243 10844 my $y = 0;
244 {
245 # if (! @n || $digits[0] == 0) {
246 # $x = 2*$frac;
247 # } elsif ($digits[0] == 1) {
248 # $x = $frac;
249 # $y = -$frac;
250 # } elsif ($digits[0] == 2) {
251 # $x = -2*$frac;
252 # } elsif ($digits[0] == 3) {
253 # $x = $frac;
254 # $y = -$frac;
255 # } elsif ($digits[0] == 4) {
256 # $x = 2*$frac;
257 # } elsif ($digits[0] == 5) {
258 # $x = $frac;
259 # $y = -$frac;
260 # } elsif ($digits[0] == 6) {
261 # $x = -$frac;
262 # $y = -$frac;
263 # }
264  
265 10844 my $rev = 0;
266 10844 foreach my $digit (reverse @digits) { # high to low
267 ### $digit
268 53954 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) 23988 29966 53954
269 ### reverse: "$digit to ".(6 - $digit)
270 23988 $digit = 6 - $digit; # mutate the array
271 }
272 53954 $rev ^= $digit_reverse[$digit];
273 ### now rev: $rev
274 }
275 ### reversed n: @n
276 }
277  
278 10844 my ($ox,$oy,$sx,$sy);
279 10844 ▶ if ($rot == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rot == 0) { } 10844 0 10844
elsif ($rot == 1) { } 0 0 0
280 10844 $ox = 0;
281 10844 $oy = 0;
282 10844 $sx = 2;
283 10844 $sy = 0;
284 } elsif ($rot == 1) {
285 0 $ox = -1; # at +120
286 0 $oy = 1;
287 0 $sx = -1; # rot to +120
288 0 $sy = 1;
289 } else {
290 0 $ox = -2; # at 180
291 0 $oy = 0;
292 0 $sx = -1; # rot to +240
293 0 $sy = -1;
294 }
295  
296 10844 ▶ while (@digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits) 53954 10844 64798
297 53954 my $digit = shift @digits; # low to high
298 ### digit: "$digit $x,$y side $sx,$sy origin $ox,$oy"
299  
300 53954 ▶ if ($digit == 0) {
Branch: 7 branches
BranchTrueFalseTotal
if ($digit == 0) { } 22105 31849 53954
elsif ($digit == 1) { } 6019 25830 31849
elsif ($digit == 2) { } 5706 20124 25830
elsif ($digit == 3) { } 4049 16075 20124
elsif ($digit == 4) { } 3636 12439 16075
elsif ($digit == 5) { } 3524 8915 12439
elsif ($digit == 6) { } 8915 0 8915
301 22105 $x += (3*$sy - $sx)/2; # at -120
302 22105 $y += ($sx + $sy)/-2;
303  
304 } elsif ($digit == 1) {
305 6019 ($x,$y) = ((3*$y-$x)/2, # rotate -120
306 ($x+$y)/-2);
307 6019 $x += ($sx + 3*$sy)/2; # at -60
308 6019 $y += ($sy - $sx)/2;
309  
310 } elsif ($digit == 2) {
311 # centre
312  
313 } elsif ($digit == 3) {
314 4049 ($x,$y) = (($x+3*$y)/-2, # rotate +120
315 ($x-$y)/2);
316 4049 $x -= $sx; # at -180
317 4049 $y -= $sy;
318  
319 } elsif ($digit == 4) {
320 3636 $x += ($sx + 3*$sy)/-2; # at +120
321 3636 $y += ($sx - $sy)/2;
322  
323 } elsif ($digit == 5) {
324 3524 $x += ($sx - 3*$sy)/2; # at +60
325 3524 $y += ($sx + $sy)/2;
326  
327 } elsif ($digit == 6) {
328 8915 ($x,$y) = (($x+3*$y)/-2, # rotate +120
329 ($x-$y)/2);
330 8915 $x += $sx; # at X axis
331 8915 $y += $sy;
332 }
333  
334 53954 $ox += $sx;
335 53954 $oy += $sy;
336  
337 # 2*(sx,sy) + rot+60(sx,sy)
338 53954 ($sx,$sy) = ((5*$sx - 3*$sy) / 2,
339 ($sx + 5*$sy) / 2);
340 }
341  
342  
343 ### digits to: "$x,$y"
344 ### origin sum: "$ox,$oy"
345 ### origin rotated: (($ox-3*$oy)/2).','.(($ox+$oy)/2)
346 10844 $x += ($ox-3*$oy)/2; # rotate +60
347 10844 $y += ($ox+$oy)/2;
348  
349 ### final: "$x,$y"
350 10844 return ($x,$y);
351 }
352  
353 # all even points when arms==3
354 sub xy_is_visited {
355 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited: not called CC=2 SCAR=17.9
Pod documented
356 0 ▶ if ($self->{'arms'} == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'arms'} == 3) { } 0 0 0
357 0 return xy_is_even($self,$x,$y);
358 } else {
359 0 return defined($self->xy_to_n($x,$y));
360 }
361 }
362  
363 # 4-->5
364 # ^ ^ forw
365 # / \
366 # 3--- 2 6---
367 # \
368 # v
369 # 0-->1
370 #
371 # 5 3
372 # \ rev
373 # / \ / v
374 # --6 4 2
375 # /
376 # v
377 # 0-->1
378 #
379  
380 my @modulus_to_digit
381 2 = (0,3,1,2,4,6,5, 0,42,14,28, 0,56, 0, # 0 right forw 0
382 0,5,1,4,6,2,3, 0,42,14,70,14,14,28, # 14 +120 rev 1
383 6,3,5,4,2,0,1, 28,56,70, 0,28,42,28, # 28 left rev 2
384 4,5,3,2,6,0,1, 42,42,70,56,14,42,28, # 42 +60 forw 3
385 2,1,3,4,0,6,5, 56,56,14,42,70,56, 0, # 56 -60 rev 6
386 6,1,5,2,0,4,3, 28,56,70,14,70,70, 0, # 70 forw
387 );
388 sub xy_to_n {
389 167 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=18 SCAR=29.6
Pod documented
390 ### FlowsnakeCentres xy_to_n(): "$x, $y"
391  
392 167 $x = round_nearest($x);
393 167 $y = round_nearest($y);
394 167 ▶ if (($x ^ $y) & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x ^ $y) & 1) 0 167 167
395 ### odd x,y ...
396 0 return undef;
397 }
398  
399 167 my $level_limit = log($x*$x + 3*$y*$y + 1) * 0.835 * 2;
400 167 ▶ if (is_infinite($level_limit)) { return $level_limit; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level_limit)) 0 167 167
401  
402 167 my @digits;
403 my $arm;
404 167 my $state;
405 167 for (;;) {
406 766 ▶ if ($level_limit-- < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level_limit-- < 0) 0 766 766
407 ### oops, level limit ...
408 0 return undef;
409 }
410 766 ▶ if ($x == 0 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) 165 601 766
Condition100%3/3
$x == 0 and $y == 0
!ll&&!rl&&r
57526165
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
411 ### found first arm 0,0 ...
412 165 $arm = 0;
413 165 $state = 0;
414 165 last;
415 }
416 601 ▶ if ($x == -2 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == -2 and $y == 0) 2 599 601
Condition100%3/3
$x == -2 and $y == 0
!ll&&!rl&&r
59092
MC/DC100%2/2
$x == -2 and $y == 0
$x == -2 $y == 0
Truth table100%3/3
A and B
A= $x == -2B= $y == 0
ABresult
0X0
100
111
417 ### found second arm -2,0 ...
418 2 $arm = 1;
419 2 $state = 42;
420 2 last;
421 }
422 599 ▶ if ($x == -1 && $y == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == -1 and $y == -1) 0 599 599
Condition66%2/3
$x == -1 and $y == -1
!ll&&!rl&&r
567320
MC/DC0%0/2
$x == -1 and $y == -1
$x == -1 $y == -1
Truth table66%2/3
A and B
A= $x == -1B= $y == -1
ABresult
0X0
100
111
423 ### found third arm -1,-1 ...
424 0 $arm = 2;
425 0 $state = 70;
426 0 last;
427 }
428  
429 # if ((($x == -1 || $x == 1) && $y == -1)
430 # || ($x == 0 && $y == -2)) {
431 # ### below island ...
432 # return undef;
433 # }
434  
435 599 my $m = ($x + 2*$y) % 7;
436 ### at: "$x,$y digits=".join(',',@digits)
437 ### mod remainder: $m
438  
439 # 0,0 is m=0
440 599 ▶ if ($m == 2) { # 2,0 = 2
Branch: 6 branches
BranchTrueFalseTotal
if ($m == 2) { } 126 473 599
elsif ($m == 3) { } 109 364 473
elsif ($m == 1) { } 72 292 364
elsif ($m == 4) { } 81 211 292
elsif ($m == 6) { } 80 131 211
elsif ($m == 5) { } 74 57 131
441 126 $x -= 2;
442 } elsif ($m == 3) { # 1,1 = 1+2 = 3
443 109 $x -= 1;
444 109 $y -= 1;
445 } elsif ($m == 1) { # -1,1 = -1+2 = 1
446 72 $x += 1;
447 72 $y -= 1;
448 } elsif ($m == 4) { # 0,2 = 0+2*2 = 4
449 81 $y -= 2;
450 } elsif ($m == 6) { # 2,2 = 2+2*2 = 6
451 80 $x -= 2;
452 80 $y -= 2;
453 } elsif ($m == 5) { # 3,1 = 3+2*1 = 5
454 74 $x -= 3;
455 74 $y -= 1;
456 }
457 599 push @digits, $m;
458  
459 ### digit: "$m to $x,$y"
460 ### shrink to: ((3*$y + 5*$x) / 14).','.((5*$y - $x) / 14)
461 ### assert: (3*$y + 5*$x) % 14 == 0
462 ### assert: (5*$y - $x) % 14 == 0
463  
464 # shrink
465 599 ($x,$y) = ((3*$y + 5*$x) / 14,
466 (5*$y - $x) / 14);
467 }
468  
469 ### @digits
470 167 my $arms = $self->{'arms'};
471 167 ▶ if ($arm >= $arms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm >= $arms) 0 167 167
472 0 return undef;
473 }
474  
475 167 my $n = 0;
476 167 foreach my $m (reverse @digits) { # high to low
477 ### $m
478 ### digit: $modulus_to_digit[$state + $m]
479 ### state: $state
480 ### next state: $modulus_to_digit[$state+7 + $m]
481  
482 599 $n = 7*$n + $modulus_to_digit[$state + $m];
483 599 $state = $modulus_to_digit[$state+7 + $m];
484 }
485 ### final n along arm: $n
486  
487 167 return $n*$arms + $arm;
488 }
489  
490 # exact
491 sub rect_to_n_range {
492 137 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=27 SCAR=33.0
Pod documented
493 ### FlowsnakeCentres rect_to_n_range(): "$x1,$y1 $x2,$y2"
494  
495 137 my ($r_lo, $r_hi) = _rect_to_radius_range ($x1,$y1*sqrt(3), $x2,$y2*sqrt(3));
496 137 $r_hi *= 2;
497 137 my $level_plus_1 = ceil( log(max(1,$r_hi/4)) / log(sqrt(7)) ) + 2;
498 # return (0, 7**$level_plus_1);
499  
500  
501 137 my $level_limit = $level_plus_1;
502 ### $level_limit
503 137 ▶ if (is_infinite($level_limit)) { return ($level_limit,$level_limit); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level_limit)) 0 137 137
504  
505 137 $x1 = round_nearest ($x1);
506 137 $y1 = round_nearest ($y1);
507 137 $x2 = round_nearest ($x2);
508 137 $y2 = round_nearest ($y2);
509 137 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 3 134 137
510 137 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 137 137
511 ### sorted range: "$x1,$y1 $x2,$y2"
512  
513 my $rect_dist = sub {
514 20661 ▶ my ($x,$y) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
515 20661 ▶ my $xd = ($x < $x1 ? $x1 - $x
Branch: 2 branches
BranchTrueFalseTotal
$x < $x1 ? : 9036 11625 20661
$x > $x2 ? : 10726 899 11625
516 : $x > $x2 ? $x - $x2
517 : 0);
518 20661 ▶ my $yd = ($y < $y1 ? $y1 - $y
Branch: 2 branches
BranchTrueFalseTotal
$y < $y1 ? : 10249 10412 20661
$y > $y2 ? : 9342 1070 10412
519 : $y > $y2 ? $y - $y2
520 : 0);
521 20661 return ($xd*$xd + 3*$yd*$yd);
522 137 };
523  
524 137 my $arms = $self->{'arms'};
525 ### $arms
526 137 my $n_lo;
527 {
528 137 my @hypot = (6);
529 137 my $top = 0;
530 137 for (;;) {
531 434 ARM_LO: foreach my $arm (0 .. $arms-1) {
532 439 my $i = 0;
533 439 my @digits;
534 439 ▶ if ($top > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top > 0) { } 298 141 439
535 298 @digits = ((0)x($top-1), 1);
536 } else {
537 141 @digits = (0);
538 }
539  
540 439 for (;;) {
541 10802 my $n = 0;
542 10802 foreach my $digit (reverse @digits) { # high to low
543 45945 $n = 7*$n + $digit;
544 }
545 10802 $n = $n*$arms + $arm;
546 ### lo consider: "i=$i digits=".join(',',reverse @digits)." is n=$n"
547  
548 10802 my ($nx,$ny) = $self->n_to_xy($n);
549 10802 my $nh = &$rect_dist ($nx,$ny);
550 10802 ▶ if ($i == 0 && $nh == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and $nh == 0) 139 10663 10802
Condition100%3/3
$i == 0 and $nh == 0
!ll&&!rl&&r
70733590139
MC/DC100%2/2
$i == 0 and $nh == 0
$i == 0 $nh == 0
Truth table100%3/3
A and B
A= $i == 0B= $nh == 0
ABresult
0X0
100
111
551 ### lo found inside: $n
552 139 ▶ if (! defined $n_lo || $n < $n_lo) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $n_lo or $n < $n_lo) 137 2 139
Condition66%2/3
not defined $n_lo or $n < $n_lo
l!l&&r!l&&!r
13702
MC/DC50%1/2
not defined $n_lo or $n < $n_lo
not defined $n_lo $n < $n_lo
Truth table66%2/3
not(A) or B
A= not defined $n_loB= $n < $n_lo
ABresult
1X1
011
000
553 137 $n_lo = $n;
554 }
555 139 next ARM_LO;
556 }
557  
558 10663 ▶ if ($i == 0 || $nh > $hypot[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 or $nh > $hypot[$i]) { } 10017 646 10663
Condition100%3/3
$i == 0 or $nh > $hypot[$i]
l!l&&r!l&&!r
35906427646
MC/DC100%2/2
$i == 0 or $nh > $hypot[$i]
$i == 0 $nh > $hypot[$i]
Truth table100%3/3
A or B
A= $i == 0B= $nh > $hypot[$i]
ABresult
1X1
011
000
559 ### too far away: "nxy=$nx,$ny nh=$nh vs ".$hypot[$i]
560  
561 10017 ▶ while (++$digits[$i] > 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$digits[$i] > 6) 1489 9717 11206
562 1489 $digits[$i] = 0;
563 1489 ▶ if (++$i <= $top) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i <= $top) { } 1189 300 1489
564 ### backtrack up ...
565 } else {
566 ### not found within this top and arm, next arm ...
567 300 next ARM_LO;
568 }
569 }
570 } else {
571 ### lo descend ...
572 ### assert: $i > 0
573 646 $i--;
574 646 $digits[$i] = 0;
575 }
576 }
577 }
578  
579 # if an $n_lo was found on any arm within this $top then done
580 434 ▶ if (defined $n_lo) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n_lo) 137 297 434
581 137 last;
582 }
583  
584 ### lo extend top ...
585 297 ▶ if (++$top > $level_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$top > $level_limit) 0 297 297
586 ### nothing below level limit ...
587 0 return (1,0);
588 }
589 297 $hypot[$top] = 7 * $hypot[$top-1];
590 }
591 }
592  
593 137 my $n_hi = 0;
594 137 ARM_HI: foreach my $arm (reverse 0 .. $arms-1) {
595 141 my @digits = ((6) x $level_limit);
596 141 my $i = $#digits;
597 141 for (;;) {
598 9859 my $n = 0;
599 9859 foreach my $digit (reverse @digits) { # high to low
600 62798 $n = 7*$n + $digit;
601 }
602 9859 $n = $n*$arms + $arm;
603 ### hi consider: "arm=$arm i=$i digits=".join(',',reverse @digits)." is n=$n"
604  
605 9859 my ($nx,$ny) = $self->n_to_xy($n);
606 9859 my $nh = &$rect_dist ($nx,$ny);
607 9859 ▶ if ($i == 0 && $nh == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and $nh == 0) 139 9720 9859
Condition100%3/3
$i == 0 and $nh == 0
!ll&&!rl&&r
74012319139
MC/DC100%2/2
$i == 0 and $nh == 0
$i == 0 $nh == 0
Truth table100%3/3
A and B
A= $i == 0B= $nh == 0
ABresult
0X0
100
111
608 ### hi found inside: $n
609 139 ▶ if ($n > $n_hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $n_hi) 131 8 139
610 131 $n_hi = $n;
611 131 next ARM_HI;
612 }
613 }
614  
615 9728 ▶ if ($i == 0 || $nh > (6 * 7**$i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 or $nh > 6 * 7 ** $i) { } 8297 1431 9728
Condition100%3/3
$i == 0 or $nh > 6 * 7 ** $i
l!l&&r!l&&!r
232759701431
MC/DC100%2/2
$i == 0 or $nh > 6 * 7 ** $i
$i == 0 $nh > 6 * 7 ** $i
Truth table100%3/3
A or B
A= $i == 0B= $nh > 6 * 7 ** $i
ABresult
1X1
011
000
616 ### too far away: "$nx,$ny nh=$nh vs ".(6 * 7**$i)
617  
618 8297 ▶ while (--$digits[$i] < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$digits[$i] < 0) 889 8287 9176
619 889 $digits[$i] = 6;
620 889 ▶ if (++$i < $level_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i < $level_limit) { } 879 10 889
621 ### hi backtrack up ...
622 } else {
623 ### hi nothing within level limit for this arm ...
624 10 next ARM_HI;
625 }
626 }
627  
628 } else {
629 ### hi descend
630 ### assert: $i > 0
631 1431 $i--;
632 1431 $digits[$i] = 6;
633 }
634 }
635 }
636  
637 137 ▶ if ($n_hi == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_hi == 0) 7 130 137
638 ### oops, lo found but hi not found
639 7 $n_hi = $n_lo;
640 }
641  
642 137 return ($n_lo, $n_hi);
643 }
644  
645 #------------------------------------------------------------------------------
646 # levels
647  
648 # arms=1 arms=2
649 # level 1 0..6 = 7 0..13 = 14
650 # level 2 0..48 = 49 0..97 = 98
651 # 7^k-1 2*7^k-1
652  
653 # level 7^k points
654 # or arms*7^k
655 # counting from 0
656 sub level_to_n_range {
657 6 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
658 6 return (0, 7**$level * $self->{'arms'} - 1);
659 }
660 sub n_to_level {
661 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
662 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
663 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
664 0 $n = round_nearest($n);
665 0 _divrem_mutate ($n, $self->{'arms'});
666 0 my ($pow, $exp) = round_up_pow ($n+1, 7);
667 0 return $exp;
668 }
669  
670 #------------------------------------------------------------------------------
671 2 1;
672 __END__
« PlanePath/PyramidSides.pm ↑ Summary PlanePath/PentSpiral.pm »