PlanePath/ImaginaryHalf.pm

filter: Statementstmt 74.2% 124 / 167 Branchbran 40.0% 16 / 40 Conditioncond 53.5% 15 / 28 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 73.9% 17 / 23 Podpod 100% 8 / 8 totaltotal 66.0% 187 / 283 CC 34 SCAR 22.2
CC
34
Cov
66%
_UNDOCUMENTED__turn_any_left_at_n
30.0
_UNDOCUMENTED__turn_any_right_at_n
30.0
n_to_xy
7.3
CRAP 9.2
« PlanePath/DekkingCurve.pm ↑ Summary PlanePath/HIndexing.pm »
1 # Copyright 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 package Math::PlanePath::ImaginaryHalf;
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 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 #use List::Util 'max';
24 1 *max = \&Math::PlanePath::_max;
25  
26 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 $VERSION = 128;
28 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 @ISA = ('Math::PlanePath');
30 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
31  
32 use Math::PlanePath::Base::Generic
33 1 'is_infinite',
34 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 use Math::PlanePath::Base::Digits
36 1 'digit_split_lowtohigh',
37 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 ▶use Math::PlanePath::ImaginaryBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 *_negaradix_range_digits_lowtohigh
41 = \&Math::PlanePath::ImaginaryBase::_negaradix_range_digits_lowtohigh;
42  
43 # uncomment this to run the ### lines
44 #use Smart::Comments;
45  
46  
47 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad12;
50  
51 1 use constant parameter_info_array =>
52 [ Math::PlanePath::Base::Digits::parameter_info_radix2(),
53 {
54 name => 'digit_order',
55 share_key => 'digit_order_XYX',
56 display => 'Digit Order',
57 type => 'enum',
58 default => 'XYX',
59 choices => ['XYX',
60 'XXY',
61 'YXX',
62 'XnYX',
63 'XnXY',
64 'YXnX',
65 ],
66 },
67 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 {
70 1 my %x_negative_at_n = (XYX => 2,
71 XXY => 1,
72 YXX => 2,
73 XnYX => 0,
74 XnXY => 0,
75 YXnX => 1,
76 );
77 sub x_negative_at_n {
78 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
79 0 return $self->{'radix'} ** $x_negative_at_n{$self->{'digit_order'}};
80 }
81 }
82  
83 # ENHANCE-ME: prove dY range
84 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 {
87 1 my %absdx_minimum = (XYX => 1,
88 XXY => 1,
89 YXX => 0, # dX=0 at N=0
90 XnYX => 2, # dX=-2 at N=0
91 XnXY => 1,
92 YXnX => 0, # dX=0 at N=0
93 );
94 sub absdx_minimum {
95 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=1 SCAR=6.9
Pod documented
96 0 return $absdx_minimum{$self->{'digit_order'}};
97 }
98 }
99 {
100 1 my %absdy_minimum = (XYX => 0, # dY=0 at N=0
101 XXY => 0, # dY=0 at N=0
102 YXX => 1,
103 XnYX => 0, # dY=0 at N=0
104 XnXY => 0, # dY=0 at N=0
105 YXnX => 1,
106 );
107 sub absdy_minimum {
108 0 ▶ my ($self) = @_;
Subroutine absdy_minimum: not called CC=1 SCAR=6.9
Pod documented
109 0 return $absdy_minimum{$self->{'digit_order'}};
110 }
111 }
112  
113 # was this anything?
114 #
115 # sub dir4_minimum {
116 # my ($self) = @_;
117 # if ($self->{'digit_order'} eq 'zzXYX') {
118 # return Math::NumSeq::PlanePathDelta::_delta_func_Dir4
119 # ($self->{'radix'}-1,-2);
120 # } else {
121 # return 0;
122 # }
123 # }
124  
125 {
126 # radix>2 has a straight somewhere
127 # radix=2 only has straight in XXY, XnXY
128 1 my %turn_any_straight = (# XYX => 0,
129 XXY => 1,
130 # YXX => 0,
131 XnXY => 1,
132 # XnYX => 0,
133 # YXnX => 0,
134 );
135 sub turn_any_straight {
136 0 ▶ my ($self) = @_;
Subroutine turn_any_straight: not called CC=2 SCAR=17.9
Pod documented
137 return ($self->{'radix'} > 2
138 0 ▶ || $turn_any_straight{$self->{'digit_order'}});
Condition0%0/3
$self->{'radix'} > 2 || $turn_any_straight{$self->{'digit_order'}}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'radix'} > 2 || $turn_any_straight{$self->{'digit_order'}}
$self->{'radix'} > 2 $turn_any_straight{$self->{'digit_order'}}
Truth table0%0/3
A || B
A= $self->{'radix'} > 2B= $turn_any_straight{$self->{'digit_order'}}
ABresult
1X1
011
000
139 }
140 }
141  
142 1 sub _UNDOCUMENTED__turn_any_left_at_n {
143 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_left_at_n: not called CC=5 SCAR=34.0
144 0 my $digit_order = $self->{'digit_order'};
145 0 my $radix = $self->{'radix'};
146 0 ▶ if ($digit_order eq 'XXY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_order eq "XXY") 0 0 0
147 0 return $radix*$radix - 1;
148 }
149 0 ▶ if ($digit_order eq 'YXX' || $digit_order eq 'XnYX') {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_order eq "YXX" or $digit_order eq "XnYX") 0 0 0
Condition0%0/3
$digit_order eq "YXX" or $digit_order eq "XnYX"
l!l&&r!l&&!r
000
MC/DC0%0/2
$digit_order eq "YXX" or $digit_order eq "XnYX"
$digit_order eq "YXX" $digit_order eq "XnYX"
Truth table0%0/3
A or B
A= $digit_order eq "YXX"B= $digit_order eq "XnYX"
ABresult
1X1
011
000
150 0 return $radix;
151 }
152 0 ▶ if ($digit_order eq 'XnXY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_order eq "XnXY") 0 0 0
153 0 return $radix*$radix ;
154 }
155 0 return $radix - 1;
156 }
157 sub _UNDOCUMENTED__turn_any_right_at_n {
158 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: not called CC=5 SCAR=34.0
159 0 my $digit_order = $self->{'digit_order'};
160 0 my $radix = $self->{'radix'};
161 0 ▶ if ($digit_order eq 'XXY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_order eq "XXY") 0 0 0
162 0 return $radix*$radix;
163 }
164 0 ▶ if ($digit_order eq 'XnXY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_order eq "XnXY") 0 0 0
165 0 return $radix*$radix - 1;
166 }
167 0 ▶ if ($digit_order eq 'YXX' || $digit_order eq 'XnYX') {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_order eq "YXX" or $digit_order eq "XnYX") 0 0 0
Condition0%0/3
$digit_order eq "YXX" or $digit_order eq "XnYX"
l!l&&r!l&&!r
000
MC/DC0%0/2
$digit_order eq "YXX" or $digit_order eq "XnYX"
$digit_order eq "YXX" $digit_order eq "XnYX"
Truth table0%0/3
A or B
A= $digit_order eq "YXX"B= $digit_order eq "XnYX"
ABresult
1X1
011
000
168 0 return $radix - 1;
169 }
170 0 return $radix;
171 }
172  
173  
174 #------------------------------------------------------------------------------
175 1 my %digit_permutation = (XYX => [0,2,1],
176 YXX => [2,0,1],
177 XXY => [0,1,2],
178  
179 XnYX => [1,2,0],
180 YXnX => [2,1,0],
181 XnXY => [1,0,2],
182 );
183  
184 sub new {
185 8 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=5 SCAR=17.1
Pod documented
186  
187 8 my $radix = $self->{'radix'};
188 8 ▶ if (! defined $radix || $radix <= 2) { $radix = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $radix or $radix <= 2) 8 0 8
Condition33%1/3
not defined $radix or $radix <= 2
l!l&&r!l&&!r
800
MC/DC0%0/2
not defined $radix or $radix <= 2
not defined $radix $radix <= 2
Truth table33%1/3
not(A) or B
A= not defined $radixB= $radix <= 2
ABresult
1X1
011
000
189 8 $self->{'radix'} = $radix;
190  
191 8 ▶ my $digit_order = ($self->{'digit_order'} ||= 'XYX');
Condition100%2/2
$self->{"digit_order"} ||= "XYX"
l!l
62
MC/DC100%1/1
$self->{"digit_order"} ||= "XYX"
$self->{"digit_order"}
Truth table100%2/2
A
A= $self->{"digit_order"}
Aresult
11
00
192 8 ▶ $self->{'digit_permutation'} = $digit_permutation{$digit_order}
Condition33%1/3
$digit_permutation{$digit_order} || croak('Unrecognised digit_order: ', $digit_order)
l!l&&r!l&&!r
800
MC/DC0%0/2
$digit_permutation{$digit_order} || croak('Unrecognised digit_order: ', $digit_order)
$digit_permutation{$digit_order} croak('Unrecognised digit_order: ', $digit_order)
Truth table33%1/3
A || B
A= $digit_permutation{$digit_order}B= croak('Unrecognised digit_order: ', $digit_order)
ABresult
1X1
011
000
193 || croak "Unrecognised digit_order: ",$digit_order;
194  
195 8 return $self;
196 }
197  
198 sub n_to_xy {
199 48 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=6 SCAR=19.9
Pod documented
200 ### ImaginaryHalf n_to_xy(): $n
201  
202 48 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 48 48
203 48 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 48 48
204  
205 {
206 48 my $int = int($n);
207 ### $int
208 ### $n
209 48 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 48 48
210 0 my ($x1,$y1) = $self->n_to_xy($int);
211 0 my ($x2,$y2) = $self->n_to_xy($int+1);
212 0 my $frac = $n - $int; # inherit possible BigFloat
213 0 my $dx = $x2-$x1;
214 0 my $dy = $y2-$y1;
215 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
216 }
217 48 $n = $int; # BigFloat int() gives BigInt, use that
218 }
219  
220 48 my $radix = $self->{'radix'};
221 48 my $zero = ($n*0); # inherit bignum 0
222  
223 48 my @xydigits = ([],[0],[]);
224 48 my $digit_permutation = $digit_permutation{$self->{'digit_order'}};
225 48 my @ndigits = digit_split_lowtohigh($n, $radix);
226 48 foreach my $i (0 .. $#ndigits) {
227 102 my $p = $digit_permutation->[$i%3];
228 102 ▶ push @{$xydigits[$p]}, $ndigits[$i], ($p < 2 ? (0) : ());
Branch: 1 branch
BranchTrueFalseTotal
$p < 2 ? : 68 34 102
229 }
230  
231 48 return (digit_join_lowtohigh ($xydigits[0], $radix, $zero)
232 - digit_join_lowtohigh ($xydigits[1], $radix, $zero),
233 digit_join_lowtohigh ($xydigits[2], $radix, $zero));
234 }
235  
236 sub xy_to_n {
237 48 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=7 SCAR=19.8
Pod documented
238 ### ImaginaryHalf xy_to_n(): "$x, $y"
239  
240 48 $y = round_nearest ($y);
241 48 ▶ if (is_infinite($y)) { return $y; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 48 48
242 48 ▶ if ($y < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0) 0 48 48
243  
244 48 $x = round_nearest ($x);
245 48 ▶ if (is_infinite($x)) { return $x; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 48 48
246  
247 48 my $zero = ($x * 0 * $y); # inherit bignum 0
248 48 my $radix = $self->{'radix'};
249 48 my @ydigits = digit_split_lowtohigh($y, $radix);
250 48 my $digit_permutation = $digit_permutation{$self->{'digit_order'}};
251  
252 48 my @ndigits; # digits low to high
253 my @nd;
254 48 ▶ while ($x || @ydigits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x or @ydigits) 42 48 90
Condition100%3/3
$x or @ydigits
l!l&&r!l&&!r
36648
MC/DC100%2/2
$x or @ydigits
$x @ydigits
Truth table100%3/3
A or B
A= $xB= @ydigits
ABresult
1X1
011
000
255 42 $nd[0] = _divrem_mutate ($x, $radix);
256 42 $x = -$x;
257 42 $nd[1] = _divrem_mutate ($x, $radix);
258 42 $x = -$x;
259 42 ▶ $nd[2] = shift @ydigits || 0;
Condition100%2/2
shift @ydigits || 0
l!l
2418
MC/DC100%1/1
shift @ydigits || 0
shift @ydigits
Truth table100%2/2
A
A= shift @ydigits
Aresult
11
00
260  
261 42 push @ndigits,
262 $nd[$digit_permutation->[0]],
263 $nd[$digit_permutation->[1]],
264 $nd[$digit_permutation->[2]];
265 }
266 48 return digit_join_lowtohigh (\@ndigits, $radix, $zero);
267 }
268  
269 # Nlevel=2^level-1
270 # 66666666 55 55 5555 7.[16].7
271 # 66666666 55 55 5555 7.[16].7
272 # 66666666 33 22 4444 7.[16].7
273 # 9 66666666 33 01 4444 7.[16].7
274 # ^ ^ ^ ^ ^ ^ ^
275 # -11 -3 -1 1 2 6 22
276 #
277 # X=1 when level=1
278 # X=1+1=2 when level=4
279 # X=2+4=6 when level=7
280 # X=6+16=22 when level=10
281 #
282 # X=0-2=-2 when level=3
283 # X=-2-8=-10 when level=6
284 # X=-10-32=-42 when level=9
285 #
286 # Y=1 k=0 want level=2
287 # Y=2 k=1 want level=5
288 # Y=4 k=2 want level=8
289 #
290 # X = 1 + 1 + 4 + 16 + 4^k
291 # = 1 + (4^(k+1) - 1) / (4-1)
292 # X*(R2-1) = (R2-1) + R2^(k+1) - 1
293 # X*(R2-1) + 1 - (R2-1) = R2^(k+1)
294 # R2^(k+1) = (X-1)*(R2-1) + 1
295 # k+1 = round down pow (X-1)*(R2-1) + 1
296 # (1-1)*3+1=1 k+1=0 want level=1
297 # (2-1)*3+1=4 k+1=1 want level=4
298 # (6-1)*3+1=16 k+1=2 want level=7
299 # (22-1)*3+1=64 k+1=3 want level=10
300 #
301 # X = 1 + 2 + 8 + 32 + ... 2*4^k
302 # = 1 + 2*(4^(k+1) - 1) / (4-1)
303 # X = 1 + R*(R2^(k+1) - 1) / (R2-1)
304 # R*(R2^(k+1) - 1) / (R2-1) = X-1
305 # R2^(k+1) - 1 = (X-1)*(R2-1)/R
306 # R2^(k+2) - R2 = (X-1)*(R2-1)*R
307 # R2^(k+2) = (X-1)*(R2-1)*R + R2
308 # (1-1)*3*2+4=4 k+2=1 want level=3
309 # (3-1)*3*2+4=16 k+2=2 want level=6
310 # (11-1)*3*2+4=64 k+2=3 want level=9
311  
312 # exact
313 sub rect_to_n_range {
314 96 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=6 SCAR=19.1
Pod documented
315 ### ImaginaryBase rect_to_n_range(): "$x1,$y1 $x2,$y2"
316  
317 96 my $zero = $x1 * 0 * $x2 * $y1 * $y2;
318  
319 96 $y1 = round_nearest($y1);
320 96 $y2 = round_nearest($y2);
321 96 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 96 96
322 96 ▶ if ($y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < 0) 0 96 96
323 ### rectangle all Y negative, no points ...
324 0 return (1, 0);
325 }
326 96 ▶ if (is_infinite($y2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y2)) 0 96 96
327 0 return (0, $y2);
328 }
329 96 ▶ if ($y1 < 0) { $y1 *= 0; } # "*=" to preserve bigint y1
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 96 96
330  
331 96 $x1 = round_nearest($x1);
332 96 $x2 = round_nearest($x2);
333  
334 96 my $radix = $self->{'radix'};
335  
336 96 my ($min_xdigits, $max_xdigits)
337 = _negaradix_range_digits_lowtohigh($x1,$x2, $radix);
338 96 ▶ unless (defined $min_xdigits) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min_xdigits) 0 96 96
339 0 return (0, $max_xdigits); # infinity
340 }
341  
342 96 my @min_ydigits = digit_split_lowtohigh ($y1, $radix);
343 96 my @max_ydigits = digit_split_lowtohigh ($y2, $radix);
344  
345 96 my $digit_permutation = $digit_permutation{$self->{'digit_order'}};
346 my @min_ndigits
347 96 = _digit_permutation_interleave ($digit_permutation,
348 $min_xdigits, \@min_ydigits);
349 my @max_ndigits
350 96 = _digit_permutation_interleave ($digit_permutation,
351 $max_xdigits, \@max_ydigits);
352  
353 96 return (digit_join_lowtohigh (\@min_ndigits, $radix, $zero),
354 digit_join_lowtohigh (\@max_ndigits, $radix, $zero));
355 }
356  
357 sub _digit_permutation_interleave {
358 192 ▶ my ($digit_permutation, $xaref, $yaref) = @_;
Subroutine _digit_permutation_interleave: called CC=5 SCAR=16.1
359 192 my @ret;
360 my @d;
361 192 foreach (0 .. max($#$xaref,2*$#$yaref)) {
362 480 ▶ $d[0] = shift @$xaref || 0;
Condition100%2/2
shift @$xaref || 0
l!l
84396
MC/DC100%1/1
shift @$xaref || 0
shift @$xaref
Truth table100%2/2
A
A= shift @$xaref
Aresult
11
00
363 480 ▶ $d[1] = shift @$xaref || 0;
Condition100%2/2
shift @$xaref || 0
l!l
72408
MC/DC100%1/1
shift @$xaref || 0
shift @$xaref
Truth table100%2/2
A
A= shift @$xaref
Aresult
11
00
364 480 ▶ $d[2] = shift @$yaref || 0;
Condition100%2/2
shift @$yaref || 0
l!l
72408
MC/DC100%1/1
shift @$yaref || 0
shift @$yaref
Truth table100%2/2
A
A= shift @$yaref
Aresult
11
00
365 480 push @ret,
366 $d[$digit_permutation->[0]],
367 $d[$digit_permutation->[1]],
368 $d[$digit_permutation->[2]];
369 }
370 192 return @ret;
371 }
372  
373 #------------------------------------------------------------------------------
374 # levels
375  
376 1 *level_to_n_range = \&Math::PlanePath::ImaginaryBase::level_to_n_range;
377 1 *n_to_level = \&Math::PlanePath::ImaginaryBase::n_to_level;
378  
379 #------------------------------------------------------------------------------
380 1 1;
381 __END__
« PlanePath/DekkingCurve.pm ↑ Summary PlanePath/HIndexing.pm »