PlanePath/CornerReplicate.pm

filter: Statementstmt 68.5% 124 / 181 Branchbran 52.8% 37 / 70 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 90.0% 18 / 20 Podpod 100% 4 / 4 totaltotal 65.6% 191 / 291 CC 42 SCAR 36.6
CC
42
Cov
64%
n_to_dxdy
156.0
rect_to_n_range
19.5
xy_to_n
9.2
CRAP 38.8
« PlanePath/ComplexPlus.pm ↑ Summary PlanePath/WythoffArray.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 package Math::PlanePath::CornerReplicate;
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 #use List::Util 'max';
23 1 *max = \&Math::PlanePath::_max;
24  
25 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 $VERSION = 128;
27 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 @ISA = ('Math::PlanePath');
29 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
30  
31 use Math::PlanePath::Base::Generic
32 1 'is_infinite',
33 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 use Math::PlanePath::Base::Digits
35 1 'round_down_pow',
36 'bit_split_lowtohigh',
37 1 ▶ 'digit_split_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # uncomment this to run the ### lines
40 # use Smart::Comments;
41  
42  
43 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1;
47  
48 1 ▶use constant dy_maximum => 1; # dY=1,-1,-3,-7,-15,etc only
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant dsumxy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant ddiffxy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant dir_maximum_dxdy => (2,-1); # ESE
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use constant turn_any_straight => 0; # never straight
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54  
55 #------------------------------------------------------------------------------
56 1 my @digit_to_x = (0,1,1,0);
57 1 my @digit_to_y = (0,0,1,1);
58  
59 sub n_to_xy {
60 286 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=5 SCAR=16.2
Pod documented
61 ### CornerReplicate n_to_xy(): $n
62  
63 286 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 286 286
64 286 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 286 286
65  
66 {
67 286 my $int = int($n);
68 ### $int
69 ### $n
70 286 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 60 226 286
71 60 my ($x1,$y1) = $self->n_to_xy($int);
72 60 my ($x2,$y2) = $self->n_to_xy($int+1);
73 60 my $frac = $n - $int; # inherit possible BigFloat
74 60 my $dx = $x2-$x1;
75 60 my $dy = $y2-$y1;
76 60 return ($frac*$dx + $x1, $frac*$dy + $y1);
77 }
78 226 $n = $int; # BigFloat int() gives BigInt, use that
79 }
80  
81 226 my $x = my $y = ($n * 0); # inherit bignum 0
82 226 my $len = $x + 1; # inherit bignum 1
83  
84 226 foreach my $digit (digit_split_lowtohigh($n,4)) {
85 ### at: "$x,$y digit=$digit"
86  
87 1024 $x += $digit_to_x[$digit] * $len;
88 1024 $y += $digit_to_y[$digit] * $len;
89 1024 $len *= 2;
90 }
91  
92 ### final: "$x,$y"
93 226 return ($x,$y);
94 }
95  
96 1 my @digit_to_next_dx = (1, 0, -1, -1);
97 1 my @digit_to_next_dy = (0, 1, 0, 0);
98  
99 # use Smart::Comments;
100 sub n_to_dxdy {
101 0 ▶ my ($self, $n) = @_;
Subroutine n_to_dxdy: not called CC=12 SCAR=50.5
Pod documented
102 ### CornerReplicate n_to_dxdy(): $n
103  
104 0 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
105 0 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
106  
107 0 my $zero = $n * 0;
108 0 my $int = int($n);
109 0 $n -= $int; # fractional part
110  
111 0 my $digit = _divrem_mutate($int,4);
112 ### low digit: $digit
113  
114 0 ▶ if ($digit == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == 0) 0 0 0
115 # N = "...0" eg. N=0
116 # ^
117 # | this dX=1,dY=0
118 # N---* next dX=0,dY=1
119 # dX = dXthis*(1-frac) + dXnext*frac
120 # = 1*(1-frac) + 0*frac
121 # = 1-frac
122 # dY = dYthis*(1-frac) + dYnext*frac
123 # = 0*(1-frac) + 1*frac
124 # = frac
125 0 return (1-$n,$n);
126 }
127  
128 0 ▶ if ($digit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == 1) 0 0 0
129 # N = "...1" eg. N=1
130 # <---*
131 # | this dX=0,dY=1
132 # N next dX=-1,dY=0
133 # dX = dXthis*(1-frac) + dXnext*frac
134 # = 0*(1-frac) + -1*frac
135 # = -frac
136 # dY = dYthis*(1-frac) + dYnext*frac
137 # = 1*(1-frac) + 0*frac
138 # = 1-frac
139 0 return (-$n,1-$n);
140 }
141  
142 0 my ($dx,$dy);
143 0 ▶ if ($digit == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == 2) { } 0 0 0
144 # N="...2"
145 # *---N this dX=-1, dY=0
146 # \ next dX=power, dY=power
147 # \
148 # power part for next only needed if $n fractional
149 0 $dx = -1;
150 0 $dy = 0;
151  
152 0 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
153 # N = "[digit]333..3332"
154 0 (my $exp, $digit) = _count_low_base4_3s($int);
155  
156 0 ▶ if ($digit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == 1) 0 0 0
157 # N = "1333..3332" so N=6, N=30, N=126, ...
158 # ^
159 # | this dX=-1, dY=0
160 # *---N next dX=0, dY=+1
161 # dX = dXthis*(1-frac) + dXnext*frac
162 # = -1*(1-frac) + 0*frac
163 # = frac-1
164 # dY = dYthis*(1-frac) + dYnext*frac
165 # = 0*(1-frac) + 1*frac
166 # = frac
167 0 return ($n-1, $n);
168 }
169  
170 0 my $next_dx = (2+$zero) ** ($exp+1);
171 0 my $next_dy;
172 ### power: $dx
173  
174 0 ▶ if ($digit) { # $digit == 2
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 0 0 0
175 # N = "2333..3332" so N=10, N=14, N=62, ...
176 # *---N this dX=-1, dY=0
177 # / next dX=-2^k, dY=-(2^k-1)=1-2^k
178 # /
179 0 $next_dx = -$next_dx;
180 0 $next_dy = $next_dx+1;
181 } else { # $digit == 0
182 # N = "0333..3332" so N=2, N=14, N=62, ...
183 # *---N this dX=-1, dY=0
184 # \ next dX=+2^k, dY=-(2^k-1)=1-2^k
185 # \
186 0 $next_dy = 1-$next_dx;
187 }
188  
189 0 my $f1 = 1-$n;
190 0 $dx = $f1*$dx + $n*$next_dx;
191 0 $dy = $f1*$dy + $n*$next_dy;
192 }
193  
194 } else { # $digit == 3
195 0 my ($exp, $digit) = _count_low_base4_3s($int);
196 ### $exp
197 ### $digit
198  
199 0 ▶ if ($digit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit == 1) 0 0 0
200 # N = "1333..333" eg. N=31
201 # N+1 = "2000..000" eg. N=32
202 # *--->
203 # | this dX=0, dY=+1
204 # N next dX=+1, dY=0
205 # dX = dXthis*(1-frac) + dXnext*frac
206 # = 0*(1-frac) + 1*frac
207 # = frac
208 # dY = dYthis*(1-frac) + dYnext*frac
209 # = 1*(1-frac) + 0*frac
210 # = 1-frac
211 0 return ($n, 1-$n);
212 }
213  
214 0 $dx = (2+$zero) ** ($exp+1);
215 ### power: $dx
216 0 ▶ if ($digit) { # $digit == 2
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 0 0 0
217 # N = "2333..333" so N=11, N=47, N=191
218 # N
219 # / this dX=-2^k, dY=-(2^k-1)=1-2^k
220 # / next dX=1, dY=0
221 # *->
222 0 $dx = -$dx;
223 0 $dy = $dx+1;
224 } else { # $digit == 0
225 # N = "0333..333" so N=3, N=15, N=63, ...
226 # N
227 # \ this dX=2^k, dY=-(2^k-1)=1-2^k
228 # \ next dX=1, dY=0
229 # *->
230 0 $dy = 1-$dx;
231 }
232  
233 0 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
234 # dX*(1-frac) + nextdX*frac
235 # dY*(1-frac) + nextdY*frac
236 # nextdX=1, nextdY=0
237 0 my $f1 = 1-$n;
238 0 $dx = $f1*$dx + $n;
239 0 $dy = $f1*$dy;
240 }
241 }
242 ### final: "$dx,$dy"
243 0 return ($dx,$dy);
244 }
245  
246 # Return ($count,$digit) where $count is how many trailing 3s on $n
247 # (possibly 0), and $digit is the next digit above those 3s.
248 sub _count_low_base4_3s {
249 0 ▶ my ($n) = @_;
Subroutine _count_low_base4_3s: not called CC=2 SCAR=17.9
250 0 my $count =0;
251 0 for (;;) {
252 0 my $digit = _divrem_mutate($n,4);
253 0 ▶ if ($digit != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit != 3) 0 0 0
254 0 return ($count,$digit);
255 }
256 0 $count++;
257 }
258 }
259  
260 # my @yx_to_digit = ([0,1],
261 # [3,2]);
262 sub xy_to_n {
263 66 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=8 SCAR=22.2
Pod documented
264 ### CornerReplicate xy_to_n(): "$x, $y"
265  
266 66 $x = round_nearest ($x);
267 66 $y = round_nearest ($y);
268 66 ▶ if ($x < 0 || $y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 66 66
Condition33%1/3
$x < 0 or $y < 0
l!l&&r!l&&!r
0066
MC/DC0%0/2
$x < 0 or $y < 0
$x < 0 $y < 0
Truth table33%1/3
A or B
A= $x < 0B= $y < 0
ABresult
1X1
011
000
269 0 return undef;
270 }
271 66 ▶ if (is_infinite($x)) { return $x; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 66 66
272 66 ▶ if (is_infinite($y)) { return $y; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 66 66
273  
274 66 my @xbits = bit_split_lowtohigh($x);
275 66 my @ybits = bit_split_lowtohigh($y);
276  
277 66 my $n = ($x * 0 * $y); # inherit bignum 0
278 66 foreach my $i (reverse 0 .. max($#xbits,$#ybits)) { # high to low
279 280 $n *= 4;
280 280 ▶ my $ydigit = $ybits[$i] || 0;
Condition100%2/2
$ybits[$i] || 0
l!l
139141
MC/DC100%1/1
$ybits[$i] || 0
$ybits[$i]
Truth table100%2/2
A
A= $ybits[$i]
Aresult
11
00
281 280 ▶ $n += 2*$ydigit + (($xbits[$i]||0) ^ $ydigit);
Condition100%2/2
$xbits[$i] || 0
l!l
166114
MC/DC100%1/1
$xbits[$i] || 0
$xbits[$i]
Truth table100%2/2
A
A= $xbits[$i]
Aresult
11
00
282 }
283 66 return $n;
284 }
285  
286 # these tables generated by tools/corner-replicate-table.pl
287 1 my @min_digit = (0,0,1, 0,0,1, 3,2,2);
288 1 my @max_digit = (0,1,1, 3,3,2, 3,3,2);
289  
290 # exact
291 sub rect_to_n_range {
292 83 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=19 SCAR=29.7
Pod documented
293 ### CornerReplicate rect_to_n_range(): "$x1,$y1 $x2,$y2"
294  
295 83 $x1 = round_nearest ($x1);
296 83 $y1 = round_nearest ($y1);
297 83 $x2 = round_nearest ($x2);
298 83 $y2 = round_nearest ($y2);
299 83 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 83 83
300 83 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 83 83
301 ### rect: "X = $x1 to $x2, Y = $y1 to $y2"
302  
303 83 ▶ if ($x2 < 0 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0) 0 83 83
Condition33%1/3
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
0083
MC/DC0%0/2
$x2 < 0 or $y2 < 0
$x2 < 0 $y2 < 0
Truth table33%1/3
A or B
A= $x2 < 0B= $y2 < 0
ABresult
1X1
011
000
304 ### rectangle outside first quadrant ...
305 0 return (1, 0);
306 }
307  
308 83 my ($len, $level) = round_down_pow (max($x2,$y2), 2);
309 ### $len
310 ### $level
311 83 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 83 83
312 0 return (0,$level);
313 }
314  
315 83 my $n_min = my $n_max
316 = my $x_min = my $y_min
317 = my $x_max = my $y_max
318 = ($x1 * 0 * $x2 * $y1 * $y2); # inherit bignum 0
319  
320 83 ▶ while ($level-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level-- >= 0) 310 83 393
321 ### $level
322  
323 {
324 310 my $x_cmp = $x_max + $len;
325 310 my $y_cmp = $y_max + $len;
326 310 ▶ my $digit = $max_digit[($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0)
Branch: 4 branches
BranchTrueFalseTotal
$x1 >= $x_cmp ? : 166 144 310
$x2 >= $x_cmp ? : 18 126 144
$y1 >= $y_cmp ? : 139 171 310
$y2 >= $y_cmp ? : 16 155 171
327 + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0)];
328 310 $n_max = 4*$n_max + $digit;
329 310 ▶ if ($digit_to_x[$digit]) { $x_max += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_x[$digit]) 175 135 310
330 310 ▶ if ($digit_to_y[$digit]) { $y_max += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_y[$digit]) 155 155 310
331  
332 # my $key = ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0)
333 # + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0);
334 ### max ...
335 ### len: sprintf "%#X", $len
336 ### $x_cmp
337 ### $y_cmp
338 # ### $key
339 ### $digit
340 ### n_max: sprintf "%#X", $n_max
341 ### $x_max
342 ### $y_max
343 }
344  
345 {
346 310 my $x_cmp = $x_min + $len;
347 310 my $y_cmp = $y_min + $len;
348 310 ▶ my $digit = $min_digit[($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0)
Branch: 4 branches
BranchTrueFalseTotal
$x1 >= $x_cmp ? : 166 144 310
$x2 >= $x_cmp ? : 20 124 144
$y1 >= $y_cmp ? : 139 171 310
$y2 >= $y_cmp ? : 20 151 171
349 + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0)];
350 310 $n_min = 4*$n_min + $digit;
351 310 ▶ if ($digit_to_x[$digit]) { $x_min += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_x[$digit]) 166 144 310
352 310 ▶ if ($digit_to_y[$digit]) { $y_min += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_y[$digit]) 139 171 310
353  
354 # my $key = ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0)
355 # + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0);
356 ### min ...
357 ### len: sprintf "%#X", $len
358 ### $x_cmp
359 ### $y_cmp
360 # ### $key
361 ### $digit
362 ### n_min: sprintf "%#X", $n_min
363 ### $x_min
364 ### $y_min
365 }
366 310 $len /= 2;
367 }
368  
369 83 return ($n_min, $n_max);
370 }
371  
372 #------------------------------------------------------------------------------
373 # levels
374  
375 1 ▶use Math::PlanePath::HilbertCurve;
Subroutine BEGIN: called CC=1 SCAR=0.0
376 1 *level_to_n_range = \&Math::PlanePath::HilbertCurve::level_to_n_range;
377 1 *n_to_level = \&Math::PlanePath::HilbertCurve::n_to_level;
378  
379 #------------------------------------------------------------------------------
380 1 1;
381 __END__
« PlanePath/ComplexPlus.pm ↑ Summary PlanePath/WythoffArray.pm »