PlanePath/ZOrderCurve.pm

filter: Statementstmt 58.7% 97 / 165 Branchbran 26.9% 14 / 52 Conditioncond 61.5% 8 / 13 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 73.0% 19 / 26 Podpod 100% 9 / 9 totaltotal 55.3% 151 / 273 CC 34 SCAR 33.5
CC
34
Cov
52%
n_to_dxdy
110.0
rect_to_n_range
56.0
n_to_xy
10.3
CRAP 28.4
« PlanePath/HypotOctant.pm ↑ Summary PlanePath/QuadricIslands.pm »
1 # Copyright 2010, 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=ZOrderCurve,radix=3 --all --output=numbers
20 # math-image --path=ZOrderCurve --values=Fibbinary --text
21 #
22 # increment N+1 changes low 1111 to 10000
23 # X bits change 011 to 000, no carry, decreasing by number of low 1s
24 # Y bits change 011 to 100, plain +1
25 #
26 # cf A105186 replace odd position ternary digits with 0
27 #
28  
29  
30 package Math::PlanePath::ZOrderCurve;
31 11 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 11 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 11 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 11 $VERSION = 128;
37 11 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 11 @ISA = ('Math::PlanePath');
39  
40 use Math::PlanePath::Base::Generic
41 11 'is_infinite',
42 11 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 use Math::PlanePath::Base::Digits
44 11 'parameter_info_array',
45 'round_up_pow',
46 'digit_split_lowtohigh',
47 11 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 11 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
49  
50 # uncomment this to run the ### lines
51 #use Smart::Comments;
52  
53  
54 11 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 11 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 11 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 11 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1;
58  
59 11 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 11 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 11 ▶use constant absdx_minimum => 1; # X coord always changes
Subroutine BEGIN: called CC=1 SCAR=0.0
62 11 ▶use constant dsumxy_maximum => 1; # forward straight only
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 sub dir_maximum_dxdy {
65 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=1 SCAR=6.9
Pod documented
66 0 return (1, 1 - $self->{'radix'}); # SE diagonal
67 }
68  
69 sub turn_any_straight {
70 0 ▶ my ($self) = @_;
Subroutine turn_any_straight: not called CC=1 SCAR=6.9
Pod documented
71 0 return ($self->{'radix'} != 2); # radix=2 never straight
72 }
73 sub _UNDOCUMENTED__turn_any_left_at_n {
74 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_left_at_n: not called CC=1 SCAR=6.9
75 0 return $self->{'radix'} - 1;
76 }
77 sub _UNDOCUMENTED__turn_any_right_at_n {
78 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: not called CC=1 SCAR=6.9
79 0 return $self->{'radix'};
80 }
81  
82  
83 #------------------------------------------------------------------------------
84  
85 sub new {
86 10 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=3 SCAR=11.0
Pod documented
87  
88 10 my $radix = $self->{'radix'};
89 10 ▶ if (! defined $radix || $radix <= 2) { $radix = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $radix or $radix <= 2) 6 4 10
Condition100%3/3
not defined $radix or $radix <= 2
l!l&&r!l&&!r
514
MC/DC100%2/2
not defined $radix or $radix <= 2
not defined $radix $radix <= 2
Truth table100%3/3
not(A) or B
A= not defined $radixB= $radix <= 2
ABresult
1X1
011
000
90 10 $self->{'radix'} = $radix;
91  
92 10 return $self;
93 }
94  
95 sub n_to_xy {
96 1292 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=9 SCAR=23.4
Pod documented
97 ### ZOrderCurve n_to_xy(): $n
98 1292 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 1292 1292
99 0 return;
100 }
101 1292 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 1292 1292
102 0 return ($n,$n);
103 }
104  
105 1292 my $int = int($n);
106 1292 $n -= $int; # fraction part
107  
108 1292 my $radix = $self->{'radix'};
109 1292 my @ndigits = digit_split_lowtohigh ($int, $radix);
110 ### @ndigits
111 1292 ▶ unless ($#ndigits & 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#ndigits & 1) 559 733 1292
112 559 push @ndigits, 0; # pad @ndigits to an even number of digits
113 }
114  
115 1292 my @xdigits;
116 my @ydigits;
117 1292 ▶ while (@ndigits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ndigits) 3046 1292 4338
118 3046 push @xdigits, shift @ndigits; # low to high
119 3046 push @ydigits, shift @ndigits; # low to high
120 }
121 ### @xdigits
122 ### @ydigits
123  
124 1292 my $zero = ($int * 0); # inherit bigint 0
125 1292 my $x = digit_join_lowtohigh (\@xdigits, $radix, $zero);
126 1292 my $y = digit_join_lowtohigh (\@ydigits, $radix, $zero);
127  
128 1292 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 1 1291 1292
129 # fraction part
130 1 my $dx = 1;
131 1 my $dy = $zero;
132 1 my $radix_minus_1 = $radix - 1;
133 1 foreach my $i (0 .. $#xdigits) { # low to high
134 1 ▶ if ($xdigits[$i] != $radix_minus_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xdigits[$i] != $radix_minus_1) 1 0 1
135 ### lowest non-9 is an X digit, so dx=1 dy=0,-R+1,-R^2+1,etc
136 1 last;
137 }
138 0 $dy = ($dy * $radix) - $radix_minus_1; # 1-$radix**$i
139 0 ▶ if ($ydigits[$i] != $radix_minus_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ydigits[$i] != $radix_minus_1) 0 0 0
140 ### lowest non-9 is a Y digit, so dy=1, dx=-R+1,-R^2+1,etc
141 0 $dx = $dy;
142 0 $dy = 1;
143 0 last;
144 }
145 }
146 ### $dx
147 ### $dy
148 1 $x = $n*$dx + $x;
149 1 $y = $n*$dy + $y;
150 }
151  
152 1292 return ($x, $y);
153 }
154  
155 sub n_to_dxdy {
156 0 ▶ my ($self, $n) = @_;
Subroutine n_to_dxdy: not called CC=10 SCAR=47.0
Pod documented
157 ### ZOrderCurve n_to_xy(): $n
158  
159 0 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
160 0 return;
161 }
162  
163 0 my $int = int($n);
164 0 $n -= $int; # fraction part
165  
166 0 ▶ if (is_infinite($int)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($int)) 0 0 0
167 0 return ($int,$int);
168 }
169  
170 0 my $radix = $self->{'radix'};
171 0 my $digit = _divrem_mutate($int,$radix); # lowest digit of N
172 0 ▶ if ($digit < $radix - 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit < $radix - 2) 0 0 0
173 # N an integer at lowdigit<radix-2, so dx=1,dy=0
174 0 return (1, 0);
175 }
176  
177 0 my $radix_minus_1 = $radix - 1;
178 0 my $scan_for_dx = ($digit == $radix_minus_1);
179 0 ▶ unless ($scan_for_dx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scan_for_dx) 0 0 0
180 ### assert: $digit == $radix-2
181 0 ▶ unless ($n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 0 0
182 # N an integer with lowdigit==radix-2, so dx=1,dy=0
183 0 return (1, 0);
184 }
185 # scan digits for next_dx,next_dy
186 }
187  
188 0 my $power = $radix + ($int*0); # $radix**$i, inherit bigint
189  
190 0 for (;;) {
191 0 ▶ if (_divrem_mutate($int,$radix) != $radix_minus_1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_divrem_mutate($int, $radix) != $radix_minus_1) 0 0 0
192 ### lowest non-9 is a Y digit, so dy=1, dx=-R+1,-R^2+1,etc
193 0 ▶ if ($scan_for_dx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scan_for_dx) { } 0 0 0
194 # scanned for dx=1-power,dy=1 have nextdx=1,nextdy=0
195 # frac*(nextdx-dx) + dx = n*(1-(1-power))+(1-power)
196 # = n*(1-1+power))+1-power
197 # = n*power+1-power
198 # = (n-1)*power+1
199 # frac*(nextdy-dy) + dy = n*(0-1) + 1
200 # = 1-n
201 0 return (($n-1)*$power + 1,
202 1-$n);
203  
204 } else {
205 # scanned for nextdx=1-power,nextdy=1 have dx=1,dy=0
206 # frac*(nextdx-dx) + dx = n*((1-power)-1)+1
207 # = n*(1-power-1)+1
208 # = n*-power+1
209 # = 1 - n*power
210 # frac*(nextdy-dy) + dy = n*(1-0) + 0
211 # = n
212 0 return (1 - $n*$power,
213 $n);
214 }
215 }
216  
217 0 ▶ if (_divrem_mutate($int,$radix) != $radix_minus_1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_divrem_mutate($int, $radix) != $radix_minus_1) 0 0 0
218 ### lowest non-9 is an X digit, so dx=1 dy=0,-R+1,-R^2+1,etc
219 0 $power -= 1;
220 0 ▶ if ($scan_for_dx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scan_for_dx) { } 0 0 0
221 # scanned for dx=1,dy=1-power have nextdx=1,nextdy=0
222 # frac*(nextdx-dx) + dx = n*(1-1)+1
223 # = 1
224 # frac*(nextdy-dy) + dy = n*(0-(1-power)) + (1-power)
225 # = n*(-1+power) + 1-power
226 # = -n + n*power + 1 - power
227 # = 1-n + (n-1)*power
228 # = (n-1)*(power-1)
229 0 return (1,
230 ($n-1) * $power);
231 } else {
232 # scanned for nextdx=1,nextdy=1-power have dx=1,dy=0
233 # frac*(nextdx-dx) + dx = n*(1-1) + 1
234 # = 1
235 # frac*(nextdy-dy) + dy = n*((1-power) - 0) + 0
236 # = n*(1-power)
237 0 return (1,
238 -$n*$power);
239 }
240 }
241  
242 0 $power *= $radix;
243 }
244 }
245  
246 sub xy_to_n {
247 2 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=5 SCAR=18.0
Pod documented
248 ### ZOrderCurve xy_to_n(): "$x, $y"
249  
250 2 $x = round_nearest ($x);
251 2 $y = round_nearest ($y);
252 2 ▶ if ($x < 0 || $y < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 2 2
Condition33%1/3
$x < 0 or $y < 0
l!l&&r!l&&!r
002
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
253 2 ▶ if (is_infinite($x)) { return $x; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 2 2
254 2 ▶ if (is_infinite($y)) { return $y; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 2 2
255  
256 2 my $radix = $self->{'radix'};
257 2 my $zero = ($x * 0 * $y); # inherit bigint 0
258  
259 2 my @x = digit_split_lowtohigh($x,$radix);
260 2 my @y = digit_split_lowtohigh($y,$radix);
261 2 return digit_join_lowtohigh ([ _digit_interleave (\@x, \@y) ],
262 $radix,
263 $zero);
264 }
265  
266 # return list of @$xaref interleaved with @$yaref
267 # ($xaref->[0], $yaref->[0], $xaref->[1], $yaref->[1], ...)
268 #
269 sub _digit_interleave {
270 202 ▶ my ($xaref, $yaref) = @_;
Subroutine _digit_interleave: called CC=4 SCAR=13.9
271 202 my @ret;
272 202 foreach my $i (0 .. max($#$xaref,$#$yaref)) {
273 794 ▶ push @ret, $xaref->[$i] || 0;
Condition100%2/2
$xaref->[$i] || 0
l!l
409385
MC/DC100%1/1
$xaref->[$i] || 0
$xaref->[$i]
Truth table100%2/2
A
A= $xaref->[$i]
Aresult
11
00
274 794 ▶ push @ret, $yaref->[$i] || 0;
Condition100%2/2
$yaref->[$i] || 0
l!l
291503
MC/DC100%1/1
$yaref->[$i] || 0
$yaref->[$i]
Truth table100%2/2
A
A= $yaref->[$i]
Aresult
11
00
275 }
276 202 return @ret;
277 }
278  
279 # exact
280 sub rect_to_n_range {
281 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=7 SCAR=40.3
Pod documented
282  
283 0 $x1 = round_nearest ($x1);
284 0 $y1 = round_nearest ($y1);
285 0 $x2 = round_nearest ($x2);
286 0 $y2 = round_nearest ($y2);
287  
288 0 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); } # x1 smaller
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 0 0
289 0 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # y1 smaller
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 0 0
290  
291 0 ▶ if ($y2 < 0 || $x2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < 0 or $x2 < 0) 0 0 0
Condition0%0/3
$y2 < 0 or $x2 < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$y2 < 0 or $x2 < 0
$y2 < 0 $x2 < 0
Truth table0%0/3
A or B
A= $y2 < 0B= $x2 < 0
ABresult
1X1
011
000
292 0 return (1, 0); # rect all negative, no N
293 }
294  
295 0 ▶ if ($x1 < 0) { $x1 *= 0; } # "*=" to preserve bigint x1 or y1
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 0 0 0
296 0 ▶ if ($y1 < 0) { $y1 *= 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 0 0
297  
298 # monotonic increasing in X and Y directions, so this is exact
299 0 return ($self->xy_to_n ($x1, $y1),
300 $self->xy_to_n ($x2, $y2));
301 }
302  
303 #------------------------------------------------------------------------------
304 # levels
305  
306 # arms=1
307 # level 1 0..0 = 1
308 # level 1 0..3 = 4
309 # level 2 0..15 = 16
310 # 4^k-1
311  
312 # shared by Math::PlanePath::GrayCode and others
313 sub level_to_n_range {
314 5 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
315 5 return (0, $self->{'radix'}**(2*$level) - 1);
316 }
317 sub n_to_level {
318 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=2 SCAR=17.9
Pod documented
319 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
320 0 $n = round_nearest($n);
321 0 my ($pow, $exp) = round_up_pow ($n+1, $self->{'radix'}*$self->{'radix'});
322 0 return $exp;
323 }
324  
325 #------------------------------------------------------------------------------
326 11 1;
327 __END__
« PlanePath/HypotOctant.pm ↑ Summary PlanePath/QuadricIslands.pm »