PlanePath/SquareReplicate.pm

filter: Statementstmt 62.4% 83 / 133 Branchbran 37.5% 12 / 32 Conditioncond 22.2% 2 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 85.7% 18 / 21 Podpod 100% 6 / 6 totaltotal 59.2% 122 / 206 CC 25 SCAR 28.6
CC
25
Cov
56%
xy_to_n
72.0
rect_to_n_range
20.0
n_to_level
12.0
CRAP 17.5
« NumSeq/PlanePathCoord.pm ↑ Summary PlanePath/UlamWarburton.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
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=SquareReplicate --lines --scale=10
20 # math-image --path=SquareReplicate --all --output=numbers_dash --size=80x50
21 # math-image --path=SquareReplicate,numbering_type=rotate-4 --all --output=numbers --size=48x9
22  
23  
24 package Math::PlanePath::SquareReplicate;
25 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 $VERSION = 128;
30 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 @ISA = ('Math::PlanePath');
32  
33 use Math::PlanePath::Base::Generic
34 2 'is_infinite',
35 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 use Math::PlanePath::Base::Digits
37 2 'round_down_pow','round_up_pow',
38 2 ▶ 'digit_split_lowtohigh','digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # uncomment this to run the ### lines
41 # use Smart::Comments;
42  
43  
44 2 use constant parameter_info_array =>
45 [ { name => 'numbering_type',
46 display => 'Numbering',
47 type => 'enum',
48 default => 'fixed',
49 choices => ['fixed','rotate-4','rotate-8'],
50 choices_display => ['Fixed','Rotate 4','Rotate 8'],
51 description => 'Fixed or rotating sub-part numbering.',
52 },
53 2 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 2 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use constant xy_is_visited => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use constant ddiffxy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use constant dir_maximum_dxdy => (0,-1); # South
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 # these don't vary with numbering_type since initial N=0to9 same
61 2 ▶use constant x_negative_at_n => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use constant y_negative_at_n => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 #------------------------------------------------------------------------------
65  
66 sub new {
67 35 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
68 35 ▶ $self->{'numbering_type'} ||= 'fixed'; # default
Condition100%2/2
$self->{"numbering_type"} ||= "fixed"
l!l
332
MC/DC100%1/1
$self->{"numbering_type"} ||= "fixed"
$self->{"numbering_type"}
Truth table100%2/2
A
A= $self->{"numbering_type"}
Aresult
11
00
69 35 return $self;
70 }
71  
72 sub _digits_rotate_lowtohigh {
73 20535 ▶ my ($self, $aref) = @_;
Subroutine _digits_rotate_lowtohigh: called CC=4 SCAR=13.9
74 20535 my $rot = 0;
75 20535 ▶ my $mask = ($self->{'numbering_type'} eq 'rotate-4' ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'numbering_type'} eq 'rotate-4' ? : 13961 6574 20535
76 20535 foreach my $digit (reverse @$aref) {
77 78590 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) 72130 6460 78590
78 72130 $digit--;
79 72130 my $delta_rot = $digit - ($digit & $mask);
80 72130 $digit = (($digit + $rot) % 8) + 1; # mutate $aref
81 72130 $rot += $delta_rot;
82 }
83 }
84 }
85 sub _digits_unrotate_lowtohigh {
86 13154 ▶ my ($self, $aref) = @_;
Subroutine _digits_unrotate_lowtohigh: called CC=4 SCAR=13.9
87 ### _digits_unrotate_lowtohigh(): @$aref
88 13154 my $rot = 0;
89 13154 ▶ my $mask = ($self->{'numbering_type'} eq 'rotate-4' ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'numbering_type'} eq 'rotate-4' ? : 6580 6574 13154
90 13154 foreach my $digit (reverse @$aref) {
91 ### at: "digit=$digit rot=$rot"
92 50910 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) 46706 4204 50910
93 46706 $digit = ($digit-1 - $rot) % 8; # mutate $aref
94 ### new digit 0-based: $digit
95 46706 $rot += $digit - ($digit & $mask);
96 ### $rot
97 46706 $digit++;
98 ### new digit 1-based: $digit
99 }
100 }
101 }
102  
103 # 4 3 2
104 # 5 0 1
105 # 6 7 8
106 #
107 2 my @digit_to_x = (0,1, 1,0,-1, -1, -1, 0, 1);
108 2 my @digit_to_y = (0,0, 1,1, 1, 0, -1,-1,-1);
109  
110 sub n_to_xy {
111 7381 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=6 SCAR=20.5
Pod documented
112 ### SquareReplicate n_to_xy(): $n
113  
114 7381 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 7381 7381
115 7381 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 7381 7381
116  
117 {
118 7381 my $int = int($n);
119 ### $int
120 ### $n
121 7381 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 7381 7381
122 0 my ($x1,$y1) = $self->n_to_xy($int);
123 0 my ($x2,$y2) = $self->n_to_xy($int+1);
124 0 my $frac = $n - $int; # inherit possible BigFloat
125 0 my $dx = $x2-$x1;
126 0 my $dy = $y2-$y1;
127 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
128 }
129 7381 $n = $int; # BigFloat int() gives BigInt, use that
130 }
131  
132 7381 my $x = my $y = ($n * 0); # inherit bignum 0
133 7381 my $len = ($x + 1); # inherit bignum 1
134  
135 7381 my @digits = digit_split_lowtohigh($n,9);
136 7381 ▶ if ($self->{'numbering_type'} ne 'fixed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numbering_type"} ne "fixed") 7381 0 7381
137 7381 _digits_rotate_lowtohigh($self, \@digits, 1);
138 }
139 7381 foreach my $digit (@digits) {
140 ### at: "$x,$y digit=$digit"
141 27680 $x += $digit_to_x[$digit] * $len;
142 27680 $y += $digit_to_y[$digit] * $len;
143 27680 $len *= 3;
144 }
145 ### final: "$x,$y"
146 7381 return ($x,$y);
147 }
148  
149 # mod digit
150 # 5 3 4 4 3 2 (x mod 3) + 3*(y mod 3)
151 # 2 0 1 5 0 1
152 # 8 6 7 6 7 8
153 #
154 2 my @mod_to_digit = (0,1,5, 3,2,4, 7,8,6);
155  
156 sub xy_to_n {
157 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: not called CC=8 SCAR=42.8
Pod documented
158 ### SquareReplicate xy_to_n(): "$x, $y"
159  
160 0 $x = round_nearest ($x);
161 0 $y = round_nearest ($y);
162  
163 0 my ($len,$level_limit);
164 {
165 0 my $xa = abs($x);
166 0 my $ya = abs($y);
167 0 ▶ ($len,$level_limit) = round_down_pow (2*($xa > $ya ? $xa : $ya) || 1, 3);
Branch: 1 branch
BranchTrueFalseTotal
$xa > $ya ? : 0 0 0
Condition0%0/2
2 * ($xa > $ya ? $xa : $ya) || 1
l!l
00
MC/DC0%0/1
2 * ($xa > $ya ? $xa : $ya) || 1
2 * ($xa > $ya ? $xa : $ya)
Truth table0%0/2
A
A= 2 * ($xa > $ya ? $xa : $ya)
Aresult
11
00
168 ### $level_limit
169 ### $len
170 }
171 0 $level_limit += 2;
172 0 ▶ if (is_infinite($level_limit)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level_limit)) 0 0 0
173 0 return $level_limit;
174 }
175  
176 0 my $zero = ($x * 0 * $y); # inherit bignum 0
177 0 my @n; # digits low to high
178 0 ▶ while ($x || $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x or $y) 0 0 0
Condition0%0/3
$x or $y
l!l&&r!l&&!r
000
MC/DC0%0/2
$x or $y
$x $y
Truth table0%0/3
A or B
A= $xB= $y
ABresult
1X1
011
000
179 0 ▶ if ($level_limit-- < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level_limit-- < 0) 0 0 0
180 ### oops, level limit reached ...
181 0 return undef;
182 }
183 0 my $m = ($x % 3) + 3*($y % 3);
184 0 my $digit = $mod_to_digit[$m];
185 0 push @n, $digit;
186 ### at: "$x,$y m=$m digit=$digit"
187  
188 0 $x -= $digit_to_x[$digit];
189 0 $y -= $digit_to_y[$digit];
190 ### subtract: "$digit_to_x[$digit],$digit_to_y[$digit] to $x,$y"
191  
192 ### assert: $x!=$x || $x % 3 == 0
193 ### assert: $y!=$y || $y % 3 == 0
194 0 $x /= 3;
195 0 $y /= 3;
196 }
197 ### n from xy: @n
198 0 ▶ if ($self->{'numbering_type'} ne 'fixed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numbering_type"} ne "fixed") 0 0 0
199 0 _digits_rotate_lowtohigh($self, \@n, -1);
200 ### @n
201 }
202 0 return digit_join_lowtohigh (\@n, 9, $zero);
203 }
204  
205 # level N Xmax
206 # 1 9^1-1 1
207 # 2 9^2-1 1+3
208 # 3 9^3-1 1+3+9
209 # X <= 3^0+3^1+...+3^(level-1)
210 # X <= 1 + 3^0+3^1+...+3^(level-1)
211 # X <= (3^level - 1)/2
212 # 2*X+1 <= 3^level
213 # level >= log3(2*X+1)
214 #
215 # X < 1 + 3^0+3^1+...+3^(level-1)
216 # X < 1 + (3^level - 1)/2
217 # (3^level - 1)/2 > X-1
218 # 3^level - 1 > 2*X-2
219 # 3^level > 2*X-1
220 #
221 # not exact
222 sub rect_to_n_range {
223 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=4 SCAR=30.0
Pod documented
224 ### SquareReplicate rect_to_n_range(): "$x1,$y1 $x2,$y2"
225  
226 0 my $max = abs(round_nearest($x1));
227 0 foreach ($y1, $x2, $y2) {
228 0 my $m = abs(round_nearest($_));
229 0 ▶ if ($m > $max) { $max = $m }
Branch: 1 branch
BranchTrueFalseTotal
if ($m > $max) 0 0 0
230 }
231 0 ▶ my ($pow) = round_down_pow (2*($max||1)-1, 3);
Condition0%0/2
$max || 1
l!l
00
MC/DC0%0/1
$max || 1
$max
Truth table0%0/2
A
A= $max
Aresult
11
00
232 0 return (0, 9*$pow*$pow - 1); # 9^level-1
233 }
234  
235 #-----------------------------------------------------------------------------
236 # level_to_n_range()
237  
238 # shared by Math::PlanePath::WunderlichMeander and more
239 sub level_to_n_range {
240 5 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
241 5 return (0, 9**$level - 1);
242 }
243 sub n_to_level {
244 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
245 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
246 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
247 0 $n = round_nearest($n);
248 0 my ($pow, $exp) = round_up_pow ($n+1, 9);
249 0 return $exp;
250 }
251  
252 #-----------------------------------------------------------------------------
253 2 1;
254 __END__
« NumSeq/PlanePathCoord.pm ↑ Summary PlanePath/UlamWarburton.pm »