PlanePath/QuintetCurve.pm

filter: Statementstmt 84.6% 121 / 143 Branchbran 84.0% 42 / 50 Conditioncond 80.0% 12 / 15 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 70.5% 12 / 17 Podpod 100% 6 / 6 totaltotal 82.8% 198 / 239 CC 33 SCAR 23.2
CC
33
Cov
84%
n_to_xy
18.0
n_to_level
12.0
_UNDOCUMENTED__n_to_turn_LSR
9.3
CRAP 10.2
« PlanePath/PeanoCurve.pm ↑ Summary PlanePath/DekkingCurve.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
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 # Boundary of unit squares:
20 # 2*(4*3^n+1) cf A199108 = 4*3^n+1
21 #
22 # QuintetCurve unit squares boundary
23 # 12,28,76,220,652
24 # match 12,28,76,220,652
25 # [HALF]
26 # A079003 a(n) = 4*3^(n-2)+2
27  
28  
29  
30 package Math::PlanePath::QuintetCurve;
31 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 $VERSION = 128;
36  
37 # inherit: new(), rect_to_n_range(), arms_count(), n_start(),
38 # parameter_info_array(), xy_is_visited()
39 1 ▶use Math::PlanePath::QuintetCentres;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 @ISA = ('Math::PlanePath::Base::NSEW',
42 'Math::PlanePath::QuintetCentres');
43  
44 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
46  
47 use Math::PlanePath::Base::Generic
48 1 'is_infinite',
49 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 use Math::PlanePath::Base::Digits
51 1 'digit_split_lowtohigh',
52 1 ▶ 'round_up_pow';
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 # uncomment this to run the ### lines
55 # use Smart::Comments;
56  
57  
58 {
59 1 my @x_negative_at_n = (undef, 513, 9, 2, 2);
60 sub x_negative_at_n {
61 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
62 0 return $x_negative_at_n[$self->{'arms'}];
63 }
64 }
65 {
66 1 my @y_negative_at_n = (undef, 2, 4, 6, 3);
67 sub y_negative_at_n {
68 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
69 0 return $y_negative_at_n[$self->{'arms'}];
70 }
71 }
72 {
73 1 my @_UNDOCUMENTED__dxdy_list_at_n = (undef, 8, 5, 5, 4);
74 sub _UNDOCUMENTED__dxdy_list_at_n {
75 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
76 0 return $_UNDOCUMENTED__dxdy_list_at_n[$self->{'arms'}];
77 }
78 }
79  
80 # N=4 first straight, then for other arms 18,27,36
81 # must override base Math::PlanePath::QuintetCentres
82 1 sub _UNDOCUMENTED__turn_any_straight_at_n {
83 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_straight_at_n: not called CC=1 SCAR=6.9
84 # arms=1 4 only first arm has origin 0
85 # arms=2 7
86 # arms=3 10
87 # arms=4 13
88 0 return 3*$self->arms_count + 1;
89 }
90  
91  
92 #------------------------------------------------------------------------------
93 1 my @dir4_to_dx = (1,0,-1,0);
94 1 my @dir4_to_dy = (0,1,0,-1);
95 1 my @digit_reverse = (0,1,0,0,1,0);
96  
97 sub n_to_xy {
98 23106 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=18 SCAR=28.9
Pod documented
99 ### QuintetCurve n_to_xy(): $n
100  
101 23106 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 23106 23106
102 0 return;
103 }
104 23106 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 23106 23106
105 0 return ($n,$n);
106 }
107  
108 23106 my $arms = $self->{'arms'};
109 23106 my $int = int($n);
110 23106 $n -= $int; # fraction part
111  
112 23106 my $rot = _divrem_mutate ($int,$arms);
113 23106 ▶ if ($rot) { $int += 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rot) 4086 19020 23106
114  
115 23106 my @digits = digit_split_lowtohigh($int,5);
116 23106 my @sx;
117 my @sy;
118 {
119 23106 my $sy = 0 * $int; # inherit bignum 0
120 23106 my $sx = 1 + $sy; # inherit bignum 1
121 23106 foreach (@digits) {
122 111004 push @sx, $sx;
123 111004 push @sy, $sy;
124  
125 # 2*(sx,sy) + rot+90(sx,sy)
126 111004 ($sx,$sy) = (2*$sx - $sy,
127 2*$sy + $sx);
128 }
129 # ### @digits
130 # my $rev = 0;
131 # for (my $i = $#digits; $i >= 0; $i--) { # high to low
132 # ### digit: $digits[$i]
133 # if ($rev) {
134 # ### reverse: "$digits[$i] to ".(5 - $digits[$i])
135 # $digits[$i] = (5 - $digits[$i]) % 5;
136 # }
137 # # $rev ^= $digit_reverse[$digits[$i]];
138 # ### now rev: $rev
139 }
140 # ### reversed n: @digits
141  
142  
143 23106 my $x = 0;
144 23106 my $y = 0;
145 23106 my $rev = 0;
146  
147 23106 ▶ while (defined (my $digit = pop @digits)) { # high to low
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $digit = pop @digits)) 111004 23106 134110
148 111004 my $sx = pop @sx;
149 111004 my $sy = pop @sy;
150 ### at: "$x,$y digit $digit side $sx,$sy"
151  
152 111004 ▶ if ($rot & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 2) 40646 70358 111004
153 40646 ($sx,$sy) = (-$sx,-$sy);
154 }
155 111004 ▶ if ($rot & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 1) 50817 60187 111004
156 50817 ($sx,$sy) = (-$sy,$sx);
157 }
158  
159 111004 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) { } 50220 60784 111004
160 50220 ▶ if ($digit == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($digit == 0) { } 10196 40024 50220
elsif ($digit == 1) { } 7815 32209 40024
elsif ($digit == 2) { } 6008 26201 32209
elsif ($digit == 3) { } 5782 20419 26201
161 10196 $rev = 0;
162 10196 $rot++;
163  
164 } elsif ($digit == 1) {
165 7815 $x -= $sy;
166 7815 $y += $sx;
167 7815 $rot++;
168  
169 } elsif ($digit == 2) {
170 6008 $x += -2*$sy;
171 6008 $y += 2*$sx;
172  
173 } elsif ($digit == 3) {
174 5782 $x += $sx - 2*$sy; # add 2*rot-90(side) + side
175 5782 $y += $sy + 2*$sx;
176 5782 $rot--;
177 5782 $rev = 0;
178  
179 } else { # $digit == 4
180 20419 $x += $sx - $sy; # add rot-90(side) + side
181 20419 $y += $sy + $sx;
182 }
183  
184 } else {
185 # normal
186  
187 60784 ▶ if ($digit == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($digit == 0) { } 14451 46333 60784
elsif ($digit == 1) { } 15237 31096 46333
elsif ($digit == 2) { } 9148 21948 31096
elsif ($digit == 3) { } 9516 12432 21948
188  
189 } elsif ($digit == 1) {
190 15237 $x += $sx;
191 15237 $y += $sy;
192 15237 $rot--;
193 15237 $rev = 1;
194  
195 } elsif ($digit == 2) {
196 9148 $x += $sx + $sy; # add side + rot-90(side)
197 9148 $y += $sy - $sx;
198  
199 } elsif ($digit == 3) {
200 9516 $x += 2*$sx + $sy;
201 9516 $y += 2*$sy - $sx;
202 9516 $rot++;
203  
204 } else { # $digit == 4
205 12432 $x += 2*$sx;
206 12432 $y += 2*$sy;
207 12432 $rot++;
208 12432 $rev = 1;
209 }
210 }
211  
212 # lowest non-zero digit determines the direction
213 111004 ▶ if ($digit != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit != 0) 86357 24647 111004
214 ### frac_dir at non-zero: $rot
215 }
216 }
217  
218 ### final: "$x,$y"
219 ### $rot
220 23106 $rot &= 3;
221 23106 return ($n * $dir4_to_dx[$rot] + $x,
222 $n * $dir4_to_dy[$rot] + $y);
223 }
224  
225 # up upl left
226 1 my @attempt_x = (0, 0, -1, -1);
227 1 my @attempt_y = (0, 1, 1, 0);
228 sub xy_to_n {
229 2562 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=6 SCAR=18.0
Pod documented
230 ### QuintetCurve xy_to_n(): "$x, $y"
231  
232 2562 $x = round_nearest($x);
233 2562 $y = round_nearest($y);
234  
235 2562 my ($n, $cx, $cy);
236 2562 foreach my $i (0, 1, 2, 3) {
237 6097 ▶ if (defined ($n = $self->SUPER::xy_to_n($x + $attempt_x[$i],
Branch: 1 branch
BranchTrueFalseTotal
if (defined($n = $self->SUPER::xy_to_n($x + $attempt_x[$i], $y + $attempt_y[$i])) and () = $self->n_to_xy($n) and $x == $cx and $y == $cy) 2562 3535 6097
Condition88%8/9
defined($n = $self->SUPER::xy_to_n($x + $attempt_x[$i], $y + $attempt_y[$i])) and () = $self->n_to_xy($n) and $x == $cx and $y == $cy
!ll&&!rl&&r
236511702562
defined($n = $self->SUPER::xy_to_n($x + $attempt_x[$i], $y + $attempt_y[$i])) and () = $self->n_to_xy($n) and $x == $cx
!ll&&!rl&&r
13722283732
defined($n = $self->SUPER::xy_to_n($x + $attempt_x[$i], $y + $attempt_y[$i])) and () = $self->n_to_xy($n)
!ll&&!rl&&r
13705960
MC/DC75%3/4
defined($n = $self->SUPER::xy_to_n($x + $attempt_x[$i], $y + $attempt_y[$i])) and () = $self->n_to_xy($n) and $x == $cx and $y == $cy
defined($n = $self->SUPER::xy_to_n($x + $attempt_x[$i], $y + $attempt_y[$i])) () = $self->n_to_xy($n) $x == $cx $y == $cy
Truth table80%4/5
A and B and C and D
A= defined($n = $self->SUPER::xy_to_n($x + $attempt_x[$i], $y + $attempt_y[$i]))B= () = $self->n_to_xy($n)C= $x == $cxD= $y == $cy
ABCDresult
0XXX0
10XX0
110X0
11100
11111
238 $y + $attempt_y[$i]))
239 && (($cx,$cy) = $self->n_to_xy($n))
240 && $x == $cx
241 && $y == $cy) {
242 2562 return $n;
243 }
244 }
245 0 return undef;
246 }
247  
248 #------------------------------------------------------------------------------
249 # levels
250  
251 # arms=1 arms=2 arms=3 arms=4
252 # level 0 0..1 = 2 0..2 = 2+1=3 0..3 = 2+1+1=4 0..4 = 2+1+1+1=5
253 # level 1 0..5 = 6 0..10 = 6+5=11 0..15 = 6+5+5=16 0..20 = 6+5+5+5=21
254 # level 2 0..25 = 26 0..50 = 26+25=51 0..75 = 26+25+25=76 0..100 = 26+25+25+25=101
255 # 5^k 2*5^k 3*5^k 4*5^k
256 #
257 sub level_to_n_range {
258 7 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
259 7 return (0, 5**$level * $self->{'arms'});
260 }
261 sub n_to_level {
262 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
263 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
264 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
265 0 $n = round_nearest($n);
266 0 $n += $self->{'arms'}-1; # division rounding up
267 0 _divrem_mutate ($n, $self->{'arms'});
268 0 my ($pow, $exp) = round_up_pow ($n, 5);
269 0 return $exp;
270 }
271  
272  
273 #------------------------------------------------------------------------------
274  
275 # R,L,L,S
276 #
277 # forward reverse
278 # 0 forward 0 forward
279 # 1 reverse 1 reverse
280 # 2 forward 2 reverse
281 # 3 forward 3 forward
282 # 4 reverse 4 reverse
283 {
284 # 1 2 3 4
285 1 my @_UNDOCUMENTED__n_to_turn_LSR = (-1, 1, 1, 0, # forward no low zeros
286 -1, 1, 0, 0, # forward low zeros
287 0,-1,-1, 1, # reverse
288 0, 0,-1, 1);
289 sub _UNDOCUMENTED__n_to_turn_LSR {
290 998 ▶ my ($self, $n) = @_;
Subroutine _UNDOCUMENTED__n_to_turn_LSR: called CC=9 SCAR=22.3
291 ### _UNDOCUMENTED__n_to_turn_LSR(): $n
292  
293 998 $n += $self->{'arms'}-1; # division rounding up
294 998 _divrem_mutate ($n, $self->{'arms'});
295 998 ▶ if ($n < 1 || is_infinite($n)) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1 or is_infinite($n)) 0 998 998
Condition33%1/3
$n < 1 or is_infinite($n)
l!l&&r!l&&!r
00998
MC/DC0%0/2
$n < 1 or is_infinite($n)
$n < 1 is_infinite($n)
Truth table33%1/3
A or B
A= $n < 1B= is_infinite($n)
ABresult
1X1
011
000
296  
297 998 my $any_low_zeros;
298 my $low;
299 998 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 1244 0 1244
300 1244 ▶ last if ($low = _divrem_mutate($n,5));
Branch: 1 branch
BranchTrueFalseTotal
if $low = _divrem_mutate($n, 5) 998 246 1244
301 246 $any_low_zeros = 1;
302 }
303 ### $low
304 ### $any_low_zeros
305  
306 998 my $non_two = 0;
307 998 ▶ while (($non_two = _divrem_mutate($n,5)) == 2) {}
Branch: 1 branch
BranchTrueFalseTotal
if (($non_two = _divrem_mutate($n, 5)) == 2) 248 998 1246
308 ### $non_two
309  
310 998 ▶ $low = $low - 1
Branch: 2 branches
BranchTrueFalseTotal
$any_low_zeros ? : 199 799 998
$non_two == 1 || $non_two == 4 ? : 497 501 998
Condition100%3/3
$non_two == 1 or $non_two == 4
l!l&&r!l&&!r
262235501
MC/DC100%2/2
$non_two == 1 or $non_two == 4
$non_two == 1 $non_two == 4
Truth table100%3/3
A or B
A= $non_two == 1B= $non_two == 4
ABresult
1X1
011
000
311 + ($any_low_zeros ? 4 : 0) # low zeros
312 + ($non_two == 1 || $non_two == 4 ? 8 : 0); # reverse
313 ### lookup: $low
314 998 return $_UNDOCUMENTED__n_to_turn_LSR[$low];
315 }
316 }
317  
318  
319 #------------------------------------------------------------------------------
320 1 1;
321 __END__
« PlanePath/PeanoCurve.pm ↑ Summary PlanePath/DekkingCurve.pm »