PlanePath/AlternateTerdragon.pm

filter: Statementstmt 83.0% 191 / 230 Branchbran 67.5% 50 / 74 Conditioncond 96.1% 25 / 26 MC/DCmcdc 92.8% 13 / 14 Subroutinesub 71.7% 28 / 39 Podpod 100% 16 / 16 totaltotal 80.9% 323 / 399 CC 50 SCAR 20.2
CC
50
Cov
81%
_xy_d_to_ndigits_and_arm
15.0
n_to_xy
12.2
n_to_level
12.0
CRAP 7.5
« PlanePath/DiagonalsAlternating.pm ↑ Summary PlanePath/Diagonals.pm »
1 # Copyright 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 package Math::PlanePath::AlternateTerdragon;
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 1 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use List::Util 'min'; # 'max'
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 *max = \&Math::PlanePath::_max;
25  
26 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
28  
29 use Math::PlanePath::Base::Generic
30 1 'is_infinite',
31 'round_nearest',
32 1 ▶ 'xy_is_even';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 use Math::PlanePath::Base::Digits
34 1 'digit_split_lowtohigh',
35 'digit_join_lowtohigh',
36 1 ▶ 'round_up_pow';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 $VERSION = 128;
40 1 @ISA = ('Math::PlanePath');
41  
42 1 ▶use Math::PlanePath::TerdragonMidpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # uncomment this to run the ### lines
45 # use Smart::Comments;
46  
47  
48 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 use constant parameter_info_array =>
50 [ { name => 'arms',
51 share_key => 'arms_6',
52 display => 'Arms',
53 type => 'integer',
54 minimum => 1,
55 maximum => 6,
56 default => 1,
57 width => 1,
58 description => 'Arms',
59 1 ▶ } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 sub x_negative {
62 2 ▶ my ($self) = @_;
Subroutine x_negative: called CC=1 SCAR=0.0
Pod documented
63 2 return ($self->{'arms'} >= 2);
64 }
65 {
66 1 my @x_negative_at_n = (undef, undef, 5, 5, 6, 7, 8);
67 sub x_negative_at_n {
68 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
69 0 return $x_negative_at_n[$self->{'arms'}];
70 }
71 }
72 {
73 1 my @y_negative_at_n = (undef, 6, 12, 18, 11, 9, 10);
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 $y_negative_at_n[$self->{'arms'}];
77 }
78 }
79 1 sub dx_minimum {
80 0 ▶ my ($self) = @_;
Subroutine dx_minimum: not called CC=2 SCAR=17.9
Pod documented
81 0 ▶ return ($self->{'arms'} == 1 ? -1 : -2);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'arms'} == 1 ? : 0 0 0
82 }
83 1 ▶use constant dx_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 sub sumxy_minimum {
88 0 ▶ my ($self) = @_;
Subroutine sumxy_minimum: not called CC=2 SCAR=17.9
Pod documented
89 # arm 0 and arm 1 are always above X+Y=0 opposite diagonal, which is +120 deg
90 0 ▶ return ($self->{'arms'} <= 2 ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'arms'} <= 2 ? : 0 0 0
91 }
92 sub diffxy_minimum {
93 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
94 # arm 0 remains below the X-Y leading diagonal, being +60 deg
95 0 ▶ return ($self->{'arms'} <= 1 ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'arms'} <= 1 ? : 0 0 0
96 }
97  
98 sub _UNDOCUMENTED__dxdy_list {
99 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list: not called CC=2 SCAR=17.9
100 0 ▶ return ($self->{'arms'} == 1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'arms'} == 1 ? : 0 0 0
101 ? Math::PlanePath::_UNDOCUMENTED__dxdy_list_three()
102 : Math::PlanePath::_UNDOCUMENTED__dxdy_list_six());
103 }
104 {
105 1 my @_UNDOCUMENTED__dxdy_list_at_n = (undef, 3, 7, 10, 7, 8, 5);
106 sub _UNDOCUMENTED__dxdy_list_at_n {
107 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
108 0 return $_UNDOCUMENTED__dxdy_list_at_n[$self->{'arms'}];
109 }
110 }
111 1 ▶use constant absdx_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 ▶use constant dsumxy_minimum => -2; # diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
113 1 ▶use constant dsumxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 # arms=1 curve goes at 0,120,240 degrees
118 # arms=2 second +60 to 60,180,300 degrees
119 # so when arms==1 dir maximum is 240 degrees
120 sub dir_maximum_dxdy {
121 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=2 SCAR=17.9
Pod documented
122 0 ▶ return ($self->{'arms'} == 1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'arms'} == 1 ? : 0 0 0
123 ? (-1,-1) # 0,2,4 only South-West
124 : ( 1,-1)); # rotated to 1,3,5 too South-East
125 }
126  
127 1 ▶use constant turn_any_straight => 0; # never straight
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129  
130 #------------------------------------------------------------------------------
131  
132 sub new {
133 20 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
134 20 ▶ $self->{'arms'} = max(1, min(6, $self->{'arms'} || 1));
Condition100%2/2
$self->{'arms'} || 1
l!l
146
MC/DC100%1/1
$self->{'arms'} || 1
$self->{'arms'}
Truth table100%2/2
A
A= $self->{'arms'}
Aresult
11
00
135 20 return $self;
136 }
137  
138 1 my @dir6_to_dx = (2, 1,-1,-2, -1, 1);
139 1 my @dir6_to_dy = (0, 1, 1, 0, -1,-1);
140  
141 sub n_to_xy {
142 28 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=12 SCAR=25.0
Pod documented
143 ### AlternateTerdragon n_to_xy(): $n
144  
145 28 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 28 28
146 28 ▶ if (is_infinite($n)) { return ($n, $n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 28 28
147  
148 28 my $zero = ($n * 0); # inherit bignum 0
149 28 my $i; # X
150 28 my $j = $zero; # +60
151 28 my $k = $zero; # +120
152 28 my $pow = $zero + 1; # inherit bignum 1
153  
154 # initial rotation from arm number
155 28 my $rot;
156 {
157 28 my $int = int($n);
158 28 $i = $n - $int; # frac, inherit possible BigFloat
159 28 $n = $int; # BigFloat int() gives BigInt, use that
160 28 $rot = _divrem_mutate ($n, $self->{'arms'});
161 }
162  
163 # even si = pow, sj = 0, sk = 0
164 # odd si = pow, sj = 0, sk = -pow
165  
166 28 my $even = 1;
167 28 my @n = digit_split_lowtohigh($n,3);
168 28 ▶ while (@n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@n) 82 12 94
169 82 my $digit = shift @n;
170 ### at: "$i, $j, $k even digit $digit"
171 82 ▶ if ($digit == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($digit == 1) { } 30 52 82
elsif ($digit == 2) { } 22 30 52
172 30 ($i,$j,$k) = ($pow-$j, -$k, $i); # rotate +120 and add
173 } elsif ($digit == 2) {
174 22 $j += $pow; # add rotated +60
175 }
176  
177 82 ▶ last unless @n;
Branch: 1 branch
BranchTrueFalseTotal
unless @n 16 66 82
178 66 $digit = shift @n;
179 66 ▶ if ($digit == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($digit == 1) { } 30 36 66
elsif ($digit == 2) { } 23 13 36
180 30 ($i,$j,$k) = ($pow+$k, $pow-$i, -$j); # rotate -120 and add
181 } elsif ($digit == 2) {
182 23 $i += $pow; # add * b
183 23 $k -= $pow;
184 }
185 66 $pow *= 3;
186 }
187  
188 ### final: "$i, $j, $k"
189 ### is: (2*$i + $j - $k).", ".($j+$k)
190  
191 ### $rot
192 28 ▶ if ($rot >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot >= 3) 0 28 28
193 0 ($i,$j,$k) = (-$i,-$j,-$k);
194 0 $rot -= 3;
195 }
196 28 ▶ if ($rot == 1) { ($i,$j,$k) = (-$k,$i,$j); } # rotate +60
Branch: 2 branches
BranchTrueFalseTotal
if ($rot == 1) { } 6 22 28
elsif ($rot == 2) { } 5 17 22
197 5 elsif ($rot == 2) { ($i,$j,$k) = (-$j,-$k, $i); } # rotate +128
198  
199 28 return (2*$i + $j - $k, $j+$k);
200 }
201  
202 # all even points when arms==6
203 sub xy_is_visited {
204 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited: not called CC=2 SCAR=17.9
Pod documented
205 0 ▶ if ($self->{'arms'} == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'arms'} == 6) { } 0 0 0
206 0 return xy_is_even($self,$x,$y);
207 } else {
208 0 return defined($self->xy_to_n($x,$y));
209 }
210 }
211  
212 sub xy_to_n {
213 20 ▶ return scalar((shift->xy_to_n_list(@_))[0]);
Subroutine xy_to_n: called CC=1 SCAR=0.0
Pod documented
214 }
215 sub xy_to_n_list {
216 35 ▶ my ($self, $x,$y) = @_;
Subroutine xy_to_n_list: called CC=8 SCAR=21.0
Pod documented
217 ### AlternateTerdragon xy_to_n_list(): "$x, $y"
218  
219 35 $x = round_nearest($x);
220 35 $y = round_nearest($y);
221 {
222 # nothing at an odd point, and trap overflows in $x+$y dividing out b
223 35 my $sum = abs($x) + abs($y);
224 35 ▶ if (is_infinite($sum)) { return $sum; } # infinity
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($sum)) 0 35 35
225 35 ▶ if ($sum % 2) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sum % 2) 0 35 35
226 }
227  
228 35 ▶ if ($x==0 && $y==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) 7 28 35
Condition66%2/3
$x == 0 and $y == 0
!ll&&!rl&&r
2807
MC/DC50%1/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table66%2/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
229 7 return 0 .. $self->{'arms'}-1;
230 }
231  
232 28 my $arms_count = $self->arms_count;
233 28 my $zero = ($x * 0 * $y); # inherit bignum 0
234  
235 28 my @n_list;
236 28 foreach my $d (0,1,2) {
237 84 my ($ndigits,$arm) = _xy_d_to_ndigits_and_arm($x,$y,$d);
238 84 ▶ next if $arm >= $arms_count;
Branch: 1 branch
BranchTrueFalseTotal
if $arm >= $arms_count 9 75 84
239 75 ▶ if ($arm & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 1) 19 56 75
240 ### flip ...
241 19 @$ndigits = map {2-$_} @$ndigits;
242 }
243 75 push @n_list,
244 digit_join_lowtohigh($ndigits, 3, $zero) * $arms_count + $arm;
245 }
246  
247 ### unsorted n_list: @n_list
248 28 return sort {$a<=>$b} @n_list;
249 }
250  
251 1 my @digit_to_x = ([0,2,1], [0,-1,-2], [0,-1, 1]);
252 1 my @digit_to_y = ([0,0,1], [0, 1, 0], [0,-1,-1]);
253  
254 # $d = 0,1,2 for segment leaving $x,$y at direction $d*120 degrees.
255 # For odd arms the digits are 0<->2 reversals.
256 sub _xy_d_to_ndigits_and_arm {
257 84 ▶ my ($x,$y, $d) = @_;
Subroutine _xy_d_to_ndigits_and_arm: called CC=15 SCAR=27.1
258 ### _xy_d_to_ndigits_and_arm(): "$x,$y d=$d"
259 84 my @ndigits;
260 my $arm;
261 84 for (;;) {
262 ### at: "$x,$y d=$d"
263 537 ▶ if ($x==0 && $y==0) { $arm = 2*$d; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) 56 481 537
Condition100%3/3
$x == 0 and $y == 0
!ll&&!rl&&r
477456
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
264 481 ▶ if ($d==2 && $x==1 && $y==1) { $arm = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($d == 2 and $x == 1 and $y == 1) 19 462 481
Condition100%6/6
$d == 2 and $x == 1 and $y == 1
!ll&&!rl&&r
460219
$d == 2 and $x == 1
!ll&&!rl&&r
32313721
MC/DC100%3/3
$d == 2 and $x == 1 and $y == 1
$d == 2 $x == 1 $y == 1
Truth table100%4/4
A and B and C
A= $d == 2B= $x == 1C= $y == 1
ABCresult
0XX0
10X0
1100
1111
265 462 ▶ if ($d==0 && $x==-2 && $y==0) { $arm = 3; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($d == 0 and $x == -2 and $y == 0) 5 457 462
Condition100%6/6
$d == 0 and $x == -2 and $y == 0
!ll&&!rl&&r
45435
$d == 0 and $x == -2
!ll&&!rl&&r
2991558
MC/DC100%3/3
$d == 0 and $x == -2 and $y == 0
$d == 0 $x == -2 $y == 0
Truth table100%4/4
A and B and C
A= $d == 0B= $x == -2C= $y == 0
ABCresult
0XX0
10X0
1100
1111
266 457 ▶ if ($d==1 && $x==1 && $y==-1) { $arm = 5; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($d == 1 and $x == 1 and $y == -1) 4 453 457
Condition100%6/6
$d == 1 and $x == 1 and $y == -1
!ll&&!rl&&r
44584
$d == 1 and $x == 1
!ll&&!rl&&r
29714812
MC/DC100%3/3
$d == 1 and $x == 1 and $y == -1
$d == 1 $x == 1 $y == -1
Truth table100%4/4
A and B and C
A= $d == 1B= $x == 1C= $y == -1
ABCresult
0XX0
10X0
1100
1111
267 453 my $a = $x % 3; # z mod b = -x mod 3
268 453 ▶ if ($a) { $a = 3-$a; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a) 310 143 453
269 453 push @ndigits, $a;
270  
271 453 ▶ if ($a==1) { $d = ($d-1) % 3; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a == 1) 194 259 453
272 ### a: $a
273 ### new d: $d
274  
275 453 $x -= $digit_to_x[$d]->[$a];
276 453 $y -= $digit_to_y[$d]->[$a];
277 ### subtract: "$digit_to_x[$d]->[$a],$digit_to_y[$d]->[$a] to $x,$y"
278  
279 ### assert: ($x+$y) % 2 == 0
280 ### assert: $x % 3 == 0
281 ### assert: ($y-$x/3) % 2 == 0
282 ### assert: (3*$y-$x) % 6 == 0
283  
284 453 ($x,$y) = (($x+$y)/2, # divide b = w6+1
285 ($y-$x/3)/2);
286  
287 453 $y = -$y;
288 453 $d = (-$d) % 3;
289 }
290 84 ▶ if (scalar(@ndigits) & 1) { $arm = (6-$arm) % 6; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ndigits & 1) 43 41 84
291 ### $arm
292 ### @ndigits
293 84 return (\@ndigits, $arm);
294 }
295  
296 # not exact
297 sub rect_to_n_range {
298 20 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=1 SCAR=0.0
Pod documented
299 ### AlternateTerdragon rect_to_n_range(): "$x1,$y1 $x2,$y2"
300 20 my $xmax = int(max(abs($x1),abs($x2)));
301 20 my $ymax = int(max(abs($y1),abs($y2)));
302 return (0,
303 ($xmax*$xmax + 3*$ymax*$ymax + 1)
304 * 2
305 20 * $self->{'arms'});
306 }
307  
308 1 my @digit_to_nextturn = (2,-2);
309 sub n_to_dxdy {
310 4 ▶ my ($self, $n) = @_;
Subroutine n_to_dxdy: called CC=9 SCAR=22.2
Pod documented
311 ### AlternateTerdragon n_to_dxdy(): $n
312  
313 4 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 4 4
314 0 return; # first direction at N=0
315 }
316 4 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 4 4
317 0 return ($n,$n);
318 }
319  
320 4 my $int = int($n); # integer part
321 4 $n -= $int; # fraction part
322  
323 # initial direction from arm
324 4 my $dir6 = _divrem_mutate ($int, $self->{'arms'});
325  
326 4 my @ndigits = digit_split_lowtohigh($int,3);
327 4 foreach my $i (0 .. $#ndigits) {
328 2 ▶ if ($ndigits[$i] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigits[$i] == 1) 1 1 2
329 1 ▶ $dir6 += 2*($i&1 ? -1 : 1); # count 1s for total turn
Branch: 1 branch
BranchTrueFalseTotal
$i & 1 ? : 0 1 1
330 }
331 }
332 4 $dir6 %= 6;
333 4 my $dx = $dir6_to_dx[$dir6];
334 4 my $dy = $dir6_to_dy[$dir6];
335  
336 4 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 3 1 4
337 ### fraction part: $n
338  
339 # find lowest non-2 digit, or zero if all 2s or no digits at all
340 3 my $above = scalar(@ndigits);
341 3 foreach my $i (0 .. $#ndigits) {
342 2 ▶ if ($ndigits[$i] != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigits[$i] != 2) 1 1 2
343 ### lowest non-2: "at i=$i digit=$ndigits[$i]"
344 1 $above = $ndigits[$i] ^ $i;
345 1 last;
346 }
347 }
348  
349 3 $dir6 = ($dir6 + $digit_to_nextturn[$above & 1]) % 6;
350 ### $above
351 ### $dir6
352  
353 3 $dx += $n*($dir6_to_dx[$dir6] - $dx);
354 3 $dy += $n*($dir6_to_dy[$dir6] - $dy);
355 }
356 4 return ($dx, $dy);
357 }
358  
359  
360 #-----------------------------------------------------------------------------
361 # eg. arms=5 0 .. 5*3^k step by 5s
362 # 1 .. 5*3^k+1 step by 5s
363 # 4 .. 5*3^k+4 step by 5s
364 #
365 sub level_to_n_range {
366 0 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: not called CC=1 SCAR=6.9
Pod documented
367 0 return (0, (3**$level + 1) * $self->{'arms'} - 1);
368 }
369 sub n_to_level {
370 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
371 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
372 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
373 0 $n = round_nearest($n);
374 0 _divrem_mutate ($n, $self->{'arms'});
375 0 my ($pow, $exp) = round_up_pow ($n, 3);
376 0 return $exp;
377 }
378  
379 1 1;
380 __END__
« PlanePath/DiagonalsAlternating.pm ↑ Summary PlanePath/Diagonals.pm »