PlanePath/PeanoCurve.pm

filter: Statementstmt 82.4% 169 / 205 Branchbran 50.0% 36 / 72 Conditioncond 38.8% 7 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 70.8% 17 / 24 Podpod 100% 7 / 7 totaltotal 70.6% 238 / 337 SCAR 45.3
CC
46
Cov
72%
xy_to_n
27.6
rect_to_n_range
22.0
n_to_xy
10.3
CRAP 93.1
« PlanePath/VogelFloret.pm ↑ Summary PlanePath/HypotOctant.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 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 # cf
20 #
21 # http://www.cut-the-knot.org/Curriculum/Geometry/PeanoComplete.shtml
22 # Java applet, directions in 9 sub-parts
23 #
24 # math-image --path=PeanoCurve,radix=5 --all --output=numbers
25 # math-image --path=PeanoCurve,radix=5 --lines
26 #
27 # T = 0.a1 a2 ...
28 # X = 0.b1 b2 ...
29 # Y = 0.c1 c2 ...
30 #
31 # b1=a1
32 # c1 = a2 comp(a1)
33 # b2 = a3 comp(a2)
34 # c2 = a4 comp(a1+a3)
35 #
36 # bn = a[2n-1] comp a2+a4+...+a[2n-2]
37 # cn = a[2n] comp a1+a3+...+a[2n-1]
38 #
39 # Brouwer(?) no continuous one-to-one between R and RxR, so line and plane
40 # are distinguished.
41 #
42  
43  
44 package Math::PlanePath::PeanoCurve;
45 4 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 #use List::Util 'max';
48 4 *max = \&Math::PlanePath::_max;
49  
50 4 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 $VERSION = 127;
52 4 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 4 @ISA = ('Math::PlanePath');
54  
55 use Math::PlanePath::Base::Generic
56 4 'is_infinite',
57 4 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 use Math::PlanePath::Base::Digits
59 4 'round_down_pow',
60 'digit_split_lowtohigh',
61 4 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 4 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 # uncomment this to run the ### lines
65 # use Smart::Comments;
66  
67  
68 4 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 4 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 4 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 4 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1;
72  
73 4 use constant parameter_info_array =>
74 [ { name => 'radix',
75 display => 'Radix',
76 share_key => 'radix_3',
77 type => 'integer',
78 minimum => 2,
79 default => 3,
80 width => 3,
81 4 ▶ } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 # shared by WunderlichSerpentine
84 sub dx_minimum {
85 0 ▶ my ($self) = @_;
Subroutine dx_minimum: not called CC=2 SCAR=17.9
Pod documented
86 0 ▶ return ($self->{'radix'} % 2
Branch: 1 branch
BranchTrueFalseTotal
$self->{'radix'} % 2 ? : 0 0 0
87 ? -1 # odd
88 : undef); # even, unlimited
89 }
90 sub dx_maximum {
91 0 ▶ my ($self) = @_;
Subroutine dx_maximum: not called CC=2 SCAR=17.9
Pod documented
92 0 ▶ return ($self->{'radix'} % 2
Branch: 1 branch
BranchTrueFalseTotal
$self->{'radix'} % 2 ? : 0 0 0
93 ? 1 # odd
94 : undef); # even, unlimited
95 }
96  
97 # shared by WunderlichSerpentine
98 sub _UNDOCUMENTED__dxdy_list {
99 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list: not called CC=2 SCAR=17.9
100 0 ▶ return ($self->{'radix'} % 2
Branch: 1 branch
BranchTrueFalseTotal
$self->{'radix'} % 2 ? : 0 0 0
101 ? Math::PlanePath::Base::NSEW->_UNDOCUMENTED__dxdy_list
102 : ()); # even, unlimited
103 }
104 # *--- b^2-1 -- b^2 ---- b^2+b-1 = (b+1)b-1
105 # | |
106 # *-------
107 # |
108 # 0 ----- b
109 #
110 sub _UNDOCUMENTED__dxdy_list_at_n {
111 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
112 0 return ($self->{'radix'} + 1) * $self->{'radix'} - 1;
113 }
114  
115 # shared by WunderlichSerpentine
116 4 *dy_minimum = \&dx_minimum;
117 4 *dy_maximum = \&dx_maximum;
118  
119 4 *dsumxy_minimum = \&dx_minimum;
120 4 *dsumxy_maximum = \&dx_maximum;
121  
122 4 *ddiffxy_minimum = \&dx_minimum;
123 4 *ddiffxy_maximum = \&dx_maximum;
124  
125 sub dir_maximum_dxdy {
126 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=2 SCAR=17.9
Pod documented
127 0 ▶ return ($self->{'radix'} % 2
Branch: 1 branch
BranchTrueFalseTotal
$self->{'radix'} % 2 ? : 0 0 0
128 ? (0,-1) # odd, South
129 : (0,0)); # even, supremum
130 }
131  
132 sub _UNDOCUMENTED__turn_any_left_at_n {
133 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_left_at_n: not called CC=1 SCAR=6.9
134 0 return $self->{'radix'} - 1;
135 }
136 sub _UNDOCUMENTED__turn_any_right_at_n {
137 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: not called CC=2 SCAR=17.9
138 return ($self->{'radix'} == 2 ? 5
139 0 ▶ : 2*$self->{'radix'} - 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'radix'} == 2 ? : 0 0 0
140 }
141  
142  
143 #------------------------------------------------------------------------------
144  
145 sub new {
146 7 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=3 SCAR=11.0
Pod documented
147  
148 7 ▶ if (! $self->{'radix'} || $self->{'radix'} < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"radix"} or $self->{"radix"} < 2) 6 1 7
Condition66%2/3
not $self->{"radix"} or $self->{"radix"} < 2
l!l&&r!l&&!r
601
MC/DC50%1/2
not $self->{"radix"} or $self->{"radix"} < 2
not $self->{"radix"} $self->{"radix"} < 2
Truth table66%2/3
not(A) or B
A= not $self->{"radix"}B= $self->{"radix"} < 2
ABresult
1X1
011
000
149 6 $self->{'radix'} = 3;
150 }
151 7 return $self;
152 }
153  
154 sub n_to_xy {
155 5 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=10 SCAR=23.3
Pod documented
156 ### PeanoCurve n_to_xy(): $n
157 5 ▶ if ($n < 0) { # negative
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 5 5
158 0 return;
159 }
160 5 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 5 5
161 0 return ($n,$n);
162 }
163  
164 {
165 # ENHANCE-ME: for odd radix the ends join and the direction can be had
166 # without a full N+1 calculation
167 5 my $int = int($n);
168 ### $int
169 ### $n
170 5 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 1 4 5
171 1 my ($x1,$y1) = $self->n_to_xy($int);
172 1 my ($x2,$y2) = $self->n_to_xy($int+1);
173 1 my $frac = $n - $int; # inherit possible BigFloat
174 1 my $dx = $x2-$x1;
175 1 my $dy = $y2-$y1;
176 1 return ($frac*$dx + $x1, $frac*$dy + $y1);
177 }
178 4 $n = $int; # BigFloat int() gives BigInt, use that
179 }
180  
181 4 my $radix = $self->{'radix'};
182 4 ▶ my @ndigits = digit_split_lowtohigh($n,$radix)
Branch: 1 branch
BranchTrueFalseTotal
my(@ndigits) = digit_split_lowtohigh($n, $radix) or return 0, 0 0 4 4
183 or return (0,0);
184  
185 # high to low style
186 #
187 4 my $radix_minus_1 = $radix - 1;
188 4 my $xk = 0;
189 4 my $yk = 0;
190 4 my @ydigits;
191 my @xdigits;
192  
193 4 $#ndigits |= 1; # ensure even number of entries
194 4 ▶ $ndigits[-1] ||= 0; # possible 0 as extra high digit
Condition50%1/2
$ndigits[-1] ||= 0
l!l
04
MC/DC0%0/1
$ndigits[-1] ||= 0
$ndigits[-1]
Truth table50%1/2
A
A= $ndigits[-1]
Aresult
11
00
195 ### @ndigits
196  
197 4 foreach my $i (reverse 0 .. ($#ndigits >> 1)) {
198 ### $i
199 {
200 390 my $ndigit = pop @ndigits; # high to low
201 390 $xk ^= $ndigit;
202 390 ▶ $ydigits[$i] = ($yk & 1 ? $radix_minus_1-$ndigit : $ndigit);
Branch: 1 branch
BranchTrueFalseTotal
$yk & 1 ? : 385 5 390
203 }
204 390 ▶ @ndigits || last;
Branch: 1 branch
BranchTrueFalseTotal
@ndigits or last 0 390 390
205 {
206 390 my $ndigit = pop @ndigits;
207 390 $yk ^= $ndigit;
208 390 ▶ $xdigits[$i] = ($xk & 1 ? $radix_minus_1-$ndigit : $ndigit);
Branch: 1 branch
BranchTrueFalseTotal
$xk & 1 ? : 2 388 390
209 }
210 }
211  
212 ### @xdigits
213 ### @ydigits
214 4 my $zero = ($n * 0); # inherit bignum 0
215 4 return (digit_join_lowtohigh(\@xdigits, $radix, $zero),
216 digit_join_lowtohigh(\@ydigits, $radix, $zero));
217  
218  
219  
220 # low to high style
221 #
222 # my $x = my $y = ($n * 0); # inherit bignum 0
223 # my $power = 1 + $x; # inherit bignum 1
224 #
225 # while (@ndigits) { # N digits low to high
226 # ### $power
227 # {
228 # my $ndigit = shift @ndigits; # low to high
229 # if ($ndigit & 1) {
230 # $y = $power-1 - $y; # 99..99 - Y
231 # }
232 # $x += $power * $ndigit;
233 # }
234 # @ndigits || last;
235 # {
236 # my $ndigit = shift @ndigits; # low to high
237 # $y += $power * $ndigit;
238 # $power *= $radix;
239 #
240 # if ($ndigit & 1) {
241 # $x = $power-1 - $x;
242 # }
243 # }
244 # }
245 # return ($x, $y);
246 }
247  
248 sub xy_to_n {
249 1 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=10 SCAR=33.2
Pod documented
250 ### PeanoCurve xy_to_n(): "$x, $y"
251  
252 1 $x = round_nearest ($x);
253 1 $y = round_nearest ($y);
254  
255 1 ▶ if ($x < 0 || $y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 1 1
Condition33%1/3
$x < 0 or $y < 0
l!l&&r!l&&!r
001
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
256 0 return undef;
257 }
258 1 ▶ if (is_infinite($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 1 1
259 0 return $x;
260 }
261 1 ▶ if (is_infinite($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 1 1
262 0 return $y;
263 }
264  
265 1 my $radix = $self->{'radix'};
266 1 my $zero = ($x * 0 * $y); # inherit bignum 0
267  
268 1 my @x = digit_split_lowtohigh ($x, $radix);
269 1 my @y = digit_split_lowtohigh ($y, $radix);
270  
271 1 my $radix_minus_1 = $radix - 1;
272 1 my $xk = 0;
273 1 my $yk = 0;
274  
275 1 my @n; # stored low to high, generated from high to low
276 1 my $i_high = max($#x,$#y);
277 1 my $npos = 2*$i_high+1;
278  
279 1 foreach my $i (reverse 0 .. $i_high) { # high to low
280 {
281 0 ▶ my $digit = $y[$i] || 0;
Condition0%0/2
$y[$i] || 0
l!l
00
MC/DC0%0/1
$y[$i] || 0
$y[$i]
Truth table0%0/2
A
A= $y[$i]
Aresult
11
00
282 0 ▶ if ($yk & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yk & 1) 0 0 0
283 0 $digit = $radix_minus_1 - $digit; # reverse digit
284 }
285 0 $n[$npos--] = $digit;
286 0 $xk ^= $digit;
287 }
288 {
289 0 ▶ my $digit = $x[$i] || 0;
Condition0%0/2
$x[$i] || 0
l!l
00
MC/DC0%0/1
$x[$i] || 0
$x[$i]
Truth table0%0/2
A
A= $x[$i]
Aresult
11
00
290 0 ▶ if ($xk & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xk & 1) 0 0 0
291 0 $digit = $radix_minus_1 - $digit; # reverse digit
292 }
293 0 $n[$npos--] = $digit;
294 0 $yk ^= $digit;
295 }
296 }
297 1 return digit_join_lowtohigh (\@n, $radix, $zero);
298 }
299  
300 # exact
301 sub rect_to_n_range {
302 1 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=19 SCAR=30.9
Pod documented
303  
304 1 $x1 = round_nearest ($x1);
305 1 $y1 = round_nearest ($y1);
306 1 $x2 = round_nearest ($x2);
307 1 $y2 = round_nearest ($y2);
308 1 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 1 1
309 1 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 1 1
310 ### rect_to_n_range(): "$x1,$y1 to $x2,$y2"
311  
312 1 ▶ if ($x2 < 0 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0) 0 1 1
Condition33%1/3
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
001
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
313 0 return (1, 0);
314 }
315  
316 1 my $radix = $self->{'radix'};
317  
318 1 my ($power, $level) = round_down_pow (max($x2,$y2), $radix);
319 1 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 1 1
320 0 return (0, $level);
321 }
322  
323 1 my $n_power = $power * $power * $radix;
324 1 my $max_x = 0;
325 1 my $max_y = 0;
326 1 my $max_n = 0;
327 1 my $max_xk = 0;
328 1 my $max_yk = 0;
329  
330 1 my $min_x = 0;
331 1 my $min_y = 0;
332 1 my $min_n = 0;
333 1 my $min_xk = 0;
334 1 my $min_yk = 0;
335  
336 # l<=c<h doesn't overlap c1<=c<=c2 if
337 # l>c2 or h-1<c1
338 # l>c2 or h<=c1
339 # so does overlap if
340 # l<=c2 and h>c1
341 #
342 1 my $radix_minus_1 = $radix - 1;
343 my $overlap = sub {
344 5 ▶ my ($c,$ck,$digit, $c1,$c2) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
345 5 ▶ if ($ck & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ck & 1) 1 4 5
346 1 $digit = $radix_minus_1 - $digit;
347 }
348 ### overlap consider: "inv".($ck&1)."digit=$digit ".($c+$digit*$power)."<=c<".($c+($digit+1)*$power)." cf $c1 to $c2 incl"
349 5 ▶ return ($c + $digit*$power <= $c2
Condition66%2/3
$c + $digit * $power <= $c2 && $c + ($digit + 1) * $power > $c1
!ll&&!rl&&r
104
MC/DC50%1/2
$c + $digit * $power <= $c2 && $c + ($digit + 1) * $power > $c1
$c + $digit * $power <= $c2 $c + ($digit + 1) * $power > $c1
Truth table66%2/3
A && B
A= $c + $digit * $power <= $c2B= $c + ($digit + 1) * $power > $c1
ABresult
0X0
100
111
350 && $c + ($digit+1)*$power > $c1);
351 1 };
352  
353 1 ▶ while ($level-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level-- >= 0) 1 1 2
354 ### $power
355 ### $n_power
356 ### $max_n
357 ### $min_n
358 {
359 1 my $digit;
360 1 ▶ for ($digit = $radix_minus_1; $digit > 0; $digit--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit > 0) 2 0 2
361 2 ▶ last if &$overlap ($max_y,$max_yk,$digit, $y1,$y2);
Branch: 1 branch
BranchTrueFalseTotal
if &$overlap($max_y, $max_yk, $digit, $y1, $y2) 1 1 2
362 }
363 1 $max_n += $n_power * $digit;
364 1 $max_xk ^= $digit;
365 1 ▶ if ($max_yk&1) { $digit = $radix_minus_1 - $digit; }
Branch: 1 branch
BranchTrueFalseTotal
if ($max_yk & 1) 0 1 1
366 1 $max_y += $power * $digit;
367 ### max y digit (complemented): $digit
368 ### $max_y
369 ### $max_n
370 }
371 {
372 1 my $digit;
373 1 ▶ for ($digit = 0; $digit < $radix_minus_1; $digit++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit < $radix_minus_1) 1 0 1
374 1 ▶ last if &$overlap ($min_y,$min_yk,$digit, $y1,$y2);
Branch: 1 branch
BranchTrueFalseTotal
if &$overlap($min_y, $min_yk, $digit, $y1, $y2) 1 0 1
375 }
376 1 $min_n += $n_power * $digit;
377 1 $min_xk ^= $digit;
378 1 ▶ if ($min_yk&1) { $digit = $radix_minus_1 - $digit; }
Branch: 1 branch
BranchTrueFalseTotal
if ($min_yk & 1) 0 1 1
379 1 $min_y += $power * $digit;
380 ### min y digit (complemented): $digit
381 ### $min_y
382 ### $min_n
383 }
384  
385 1 $n_power = int($n_power/$radix);
386 {
387 1 my $digit;
388 1 ▶ for ($digit = $radix_minus_1; $digit > 0; $digit--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit > 0) 1 0 1
389 1 ▶ last if &$overlap ($max_x,$max_xk,$digit, $x1,$x2);
Branch: 1 branch
BranchTrueFalseTotal
if &$overlap($max_x, $max_xk, $digit, $x1, $x2) 1 0 1
390 }
391 1 $max_n += $n_power * $digit;
392 1 $max_yk ^= $digit;
393 1 ▶ if ($max_xk&1) { $digit = $radix_minus_1 - $digit; }
Branch: 1 branch
BranchTrueFalseTotal
if ($max_xk & 1) 1 0 1
394 1 $max_x += $power * $digit;
395 ### max x digit (complemented): $digit
396 ### $max_x
397 ### $max_n
398 }
399 {
400 1 my $digit;
401 1 ▶ for ($digit = 0; $digit < $radix_minus_1; $digit++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit < $radix_minus_1) 1 0 1
402 1 ▶ last if &$overlap ($min_x,$min_xk,$digit, $x1,$x2);
Branch: 1 branch
BranchTrueFalseTotal
if &$overlap($min_x, $min_xk, $digit, $x1, $x2) 1 0 1
403 }
404 1 $min_n += $n_power * $digit;
405 1 $min_yk ^= $digit;
406 1 ▶ if ($min_xk&1) { $digit = $radix_minus_1 - $digit; }
Branch: 1 branch
BranchTrueFalseTotal
if ($min_xk & 1) 0 1 1
407 1 $min_x += $power * $digit;
408 ### min x digit (complemented): $digit
409 ### $min_x
410 ### $min_n
411 }
412  
413 1 $power = int($power/$radix);
414 1 $n_power = int($n_power/$radix);
415 }
416 ### is: "$min_n at $min_x,$min_y to $max_n at $max_x,$max_y"
417 1 return ($min_n, $max_n);
418 }
419  
420 #------------------------------------------------------------------------------
421 # levels
422  
423 4 ▶use Math::PlanePath::ZOrderCurve;
Subroutine BEGIN: called CC=1 SCAR=0.0
424 4 *level_to_n_range = \&Math::PlanePath::ZOrderCurve::level_to_n_range;
425 4 *n_to_level = \&Math::PlanePath::ZOrderCurve::n_to_level;
426  
427 #------------------------------------------------------------------------------
428 4 1;
429 __END__
« PlanePath/VogelFloret.pm ↑ Summary PlanePath/HypotOctant.pm »