PlanePath/QuintetReplicate.pm

filter: Statementstmt 74.6% 112 / 150 Branchbran 64.0% 32 / 50 Conditioncond 100% 14 / 14 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 90.0% 18 / 20 Podpod 100% 6 / 6 totaltotal 75.7% 187 / 247 CC 35 SCAR 23.2
CC
35
Cov
74%
rect_to_n_range
42.0
n_to_level
12.0
n_to_xy
11.2
CRAP 10.2
« PlanePath/R5DragonMidpoint.pm ↑ Summary PlanePath/PeanoCurve.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 # math-image --path=QuintetReplicate --lines --scale=10
20 # math-image --path=QuintetReplicate --output=numbers --all
21 # math-image --path=QuintetReplicate,numbering_type=rotate --output=numbers --all
22 # math-image --path=QuintetReplicate --expression='5**i'
23  
24 package Math::PlanePath::QuintetReplicate;
25 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 $VERSION = 128;
30 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 @ISA = ('Math::PlanePath');
32  
33 use Math::PlanePath::Base::Generic
34 1 'is_infinite',
35 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 use Math::PlanePath::Base::Digits
37 1 'round_up_pow',
38 'digit_split_lowtohigh',
39 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # uncomment this to run the ### lines
42 # use Smart::Comments;
43  
44  
45 1 use constant parameter_info_array =>
46 [ { name => 'numbering_type',
47 display => 'Numbering',
48 share_key => 'numbering_type_rotate',
49 type => 'enum',
50 default => 'fixed',
51 choices => ['fixed','rotate'],
52 choices_display => ['Fixed','Rotate'],
53 description => 'Fixed or rotating sub-part numbering.',
54 },
55 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use constant xy_is_visited => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use constant x_negative_at_n => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use constant y_negative_at_n => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 #------------------------------------------------------------------------------
63 sub new {
64 4 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
65 4 ▶ $self->{'numbering_type'} ||= 'fixed'; # default
Condition100%2/2
$self->{"numbering_type"} ||= "fixed"
l!l
22
MC/DC100%1/1
$self->{"numbering_type"} ||= "fixed"
$self->{"numbering_type"}
Truth table100%2/2
A
A= $self->{"numbering_type"}
Aresult
11
00
66 4 return $self;
67 }
68  
69 # 10 7
70 # 2 8 5 6
71 # 3 0 1 9
72 # 4
73  
74 # my @digit_to_xbx = (0,1,0,-1,0);
75 # my @digit_to_xby = (0,0,-1,0,1);
76 # my @digit_to_y = (0,0,1,0,-1);
77 # my @digit_to_yby = (0,0,1,0,-1);
78 # $x += $bx * $digit_to_xbx[$digit] + $by * $digit_to_xby[$digit];
79 # $y += $bx * $digit_to_ybx[$digit] + $by * $digit_to_yby[$digit];
80  
81 sub _digits_rotate_lowtohigh {
82 262 ▶ my ($aref) = @_;
Subroutine _digits_rotate_lowtohigh: called CC=3 SCAR=11.0
83 262 my $rot = 0;
84 262 foreach my $digit (reverse @$aref) { # high to low
85 712 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) 619 93 712
86 619 $rot += $digit-1;
87 619 $digit = ($rot % 4) + 1; # mutate $aref
88 }
89 }
90 }
91 sub _digits_unrotate_lowtohigh {
92 647 ▶ my ($aref) = @_;
Subroutine _digits_unrotate_lowtohigh: called CC=3 SCAR=11.0
93 647 my $rot = 0;
94 647 foreach my $digit (reverse @$aref) { # high to low
95 1802 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) 1585 217 1802
96 1585 $digit = ($digit-1-$rot) % 4; # mutate $aref
97 1585 $rot += $digit;
98 1585 $digit++;
99 }
100 }
101 }
102  
103 sub n_to_xy {
104 250 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=10 SCAR=24.2
Pod documented
105 ### QuintetReplicate n_to_xy(): $n
106  
107 250 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 250 250
108 0 return;
109 }
110 250 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 250 250
111 0 return ($n,$n);
112 }
113  
114 # any value in long frac lines like this?
115 {
116 250 my $int = int($n);
117 250 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 250 250
118 0 my ($x1,$y1) = $self->n_to_xy($int);
119 0 my ($x2,$y2) = $self->n_to_xy($int+1);
120 0 my $frac = $n - $int; # inherit possible BigFloat
121 0 my $dx = $x2-$x1;
122 0 my $dy = $y2-$y1;
123 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
124 }
125 250 $n = $int; # BigFloat int() gives BigInt, use that
126 }
127  
128 250 my $x = my $y = my $by = ($n * 0); # inherit bignum 0
129 250 my $bx = $x+1; # inherit bignum 1
130  
131 250 my @digits = digit_split_lowtohigh($n,5);
132 250 ▶ if ($self->{'numbering_type'} eq 'rotate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numbering_type"} eq "rotate") 125 125 250
133 125 _digits_rotate_lowtohigh(\@digits);
134 }
135 250 foreach my $digit (@digits) {
136 ### $digit
137 ### $bx
138 ### $by
139  
140 688 ▶ if ($digit == 1) {
Branch: 4 branches
BranchTrueFalseTotal
if ($digit == 1) { } 150 538 688
elsif ($digit == 2) { } 150 388 538
elsif ($digit == 3) { } 150 238 388
elsif ($digit == 4) { } 150 88 238
141 150 $x += $bx;
142 150 $y += $by;
143 } elsif ($digit == 2) {
144 150 $x -= $by; # i*(bx+i*by) = rotate +90
145 150 $y += $bx;
146 } elsif ($digit == 3) {
147 150 $x -= $bx; # -1*(bx+i*by) = rotate 180
148 150 $y -= $by;
149 } elsif ($digit == 4) {
150 150 $x += $by; # -i*(bx+i*by) = rotate -90
151 150 $y -= $bx;
152 }
153  
154 # power (bx,by) = (bx + i*by)*(i+2)
155 #
156 688 ($bx,$by) = (2*$bx-$by, 2*$by+$bx);
157 }
158  
159 250 return ($x, $y);
160 }
161  
162 # digit modulus 2Y+X mod 5
163 # 2 2
164 # 3 0 1 1 0 4
165 # 4 3
166 #
167 1 my @modulus_to_x = (0,-1,0,0,1);
168 1 my @modulus_to_y = (0,0,1,-1,0);
169 1 my @modulus_to_digit = (0,3,2,4,1);
170  
171 sub xy_to_n {
172 770 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=6 SCAR=17.9
Pod documented
173 ### QuintetReplicate xy_to_n(): "$x, $y"
174  
175 770 $x = round_nearest ($x);
176 770 $y = round_nearest ($y);
177  
178 770 foreach my $overflow (2*$x + 2*$y, 2*$x - 2*$y) {
179 1540 ▶ if (is_infinite($overflow)) { return $overflow; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($overflow)) 0 1540 1540
180 }
181  
182 770 my $zero = ($x * 0 * $y); # inherit bignum 0
183 770 my @n; # digits low to high
184  
185 770 ▶ while ($x || $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x or $y) 2180 770 2950
Condition100%3/3
$x or $y
l!l&&r!l&&!r
1680500770
MC/DC100%2/2
$x or $y
$x $y
Truth table100%3/3
A or B
A= $xB= $y
ABresult
1X1
011
000
186 ### at: "$x,$y"
187  
188 2180 my $m = (2*$y - $x) % 5;
189 ### $m
190 ### digit: $modulus_to_digit[$m]
191  
192 2180 push @n, $modulus_to_digit[$m];
193  
194 2180 $x -= $modulus_to_x[$m];
195 2180 $y -= $modulus_to_y[$m];
196 ### modulus shift to: "$x,$y"
197  
198 # div i+2,
199 # = (i*y + x) * (i-2)/-5
200 # = (-y -2*y*i + x*i -2*x) / -5
201 # = (y + 2*y*i - x*i + 2*x) / 5
202 # = (2x+y + (2*y-x)i) / 5
203 #
204 # ### assert: ((2*$x + $y) % 5) == 0
205 # ### assert: ((2*$y - $x) % 5) == 0
206  
207 2180 ($x,$y) = ((2*$x + $y) / 5,
208 (2*$y - $x) / 5);
209 }
210 770 ▶ if ($self->{'numbering_type'} eq 'rotate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numbering_type"} eq "rotate") 385 385 770
211 385 _digits_unrotate_lowtohigh(\@n);
212 }
213 770 return digit_join_lowtohigh (\@n, 5, $zero);
214 }
215  
216 # level min x^2+y^2 for N >= 5^k
217 # 0 1 at 1,0
218 # 1 2 at 1,1 factor 2
219 # 2 5 at 1,2 factor 2.5
220 # 3 16 at 0,4 factor 3.2
221 # 4 65 at -4,7 factor 4.0625
222 # 5 296 at -14,10 factor 4.55384615384615
223 # 6 1405 at -37,6 factor 4.74662162162162
224 # 7 6866 at -79,-25 factor 4.88683274021352
225 #
226 # not exact
227 sub rect_to_n_range {
228 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=6 SCAR=37.4
Pod documented
229  
230 0 $x1 = abs($x1);
231 0 $x2 = abs($x2);
232 0 $y1 = abs($y1);
233 0 $y2 = abs($y2);
234 0 ▶ if ($x1 < $x2) { $x1 = $x2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < $x2) 0 0 0
235 0 ▶ if ($y1 < $y2) { $y1 = $y2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < $y2) 0 0 0
236 0 my $rsquared = $x1*$x1 + $y1*$y1;
237 0 ▶ if (is_infinite($rsquared)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($rsquared)) 0 0 0
238 0 return (0, $rsquared);
239 }
240  
241 0 my $x = 1;
242 0 my $y = 0;
243 0 for (my $level = 1; ; $level++) {
244 # (x+iy)*(2+i)
245 0 ($x,$y) = (2*$x - $y, $x + 2*$y);
246 0 ▶ if (abs($x) >= abs($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $x >= abs $y) { } 0 0 0
247 0 $x -= ($x<=>0);
248 } else {
249 0 $y -= ($y<=>0);
250 }
251  
252 0 ▶ unless ($x*$x + $y*$y <= $rsquared) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x * $x + $y * $y <= $rsquared) 0 0 0
253 0 return (0, 5**$level - 1);
254 }
255 }
256 }
257  
258 #------------------------------------------------------------------------------
259 # levels
260  
261 sub level_to_n_range {
262 2 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
263 2 return (0, 5**$level - 1);
264 }
265 sub n_to_level {
266 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
267 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
268 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
269 0 $n = round_nearest($n);
270 0 my ($pow, $exp) = round_up_pow ($n+1, 5);
271 0 return $exp;
272 }
273  
274  
275 #------------------------------------------------------------------------------
276  
277 # Return true if $n is on the boundary of $level.
278 #
279 sub _UNDOCUMENTED__n_is_boundary_level {
280 250 ▶ my ($self, $n, $level) = @_;
Subroutine _UNDOCUMENTED__n_is_boundary_level: called CC=9 SCAR=22.0
281  
282 ### _UNDOCUMENTED__n_is_boundary_level(): "n=$n"
283  
284 250 my @digits = digit_split_lowtohigh($n,5);
285 ### @digits
286 250 ▶ if ($self->{'numbering_type'} eq 'fixed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numbering_type"} eq "fixed") 125 125 250
287 125 _digits_unrotate_lowtohigh(\@digits);
288 ### @digits
289 }
290  
291 # no high 0 digit (and nothing too big)
292 250 ▶ if (@digits != $level) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits != $level) 50 200 250
293 50 return 0;
294 }
295  
296 # no 0 digit anywhere else
297 200 ▶ if (grep {$_==0} @digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ == 0;} @digits) 72 128 200
298 72 return 0;
299 }
300  
301 # skip high digit and all 1 digits
302 128 pop @digits;
303 128 @digits = grep {$_ != 1} @digits;
304  
305 128 ▶ for (my $i = 0; $i < $#digits; $i++) { # low to high
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#digits) 72 104 176
306 72 ▶ if (($digits[$i+1] == 3 && $digits[$i] <= 3) # 33, 32
Branch: 1 branch
BranchTrueFalseTotal
if ($digits[$i + 1] == 3 and $digits[$i] <= 3 or $digits[$i + 1] == 4 and $digits[$i] == 4) 24 48 72
Condition100%9/9
$digits[$i + 1] == 3 and $digits[$i] <= 3 or $digits[$i + 1] == 4 and $digits[$i] == 4
l!l&&r!l&&!r
161343
$digits[$i + 1] == 3 and $digits[$i] <= 3
!ll&&!rl&&r
48816
$digits[$i + 1] == 4 and $digits[$i] == 4
!ll&&!rl&&r
32168
MC/DC50%2/4
$digits[$i + 1] == 3 and $digits[$i] <= 3 or $digits[$i + 1] == 4 and $digits[$i] == 4
$digits[$i + 1] == 3 $digits[$i] <= 3 $digits[$i + 1] == 4 $digits[$i] == 4
Truth table42%3/7
A and B or C and D
A= $digits[$i + 1] == 3B= $digits[$i] <= 3C= $digits[$i + 1] == 4D= $digits[$i] == 4
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
307 || ($digits[$i+1] == 4 && $digits[$i] == 4)) { # 44
308 ### no, pair at: $i
309 24 return 0;
310 }
311 }
312 104 return 1;
313 }
314  
315  
316 #------------------------------------------------------------------------------
317 1 1;
318 __END__
« PlanePath/R5DragonMidpoint.pm ↑ Summary PlanePath/PeanoCurve.pm »