PlanePath/CubicBase.pm

filter: Statementstmt 82.2% 125 / 152 Branchbran 77.0% 37 / 48 Conditioncond 73.3% 11 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 73.6% 14 / 19 Podpod 100% 7 / 7 totaltotal 79.6% 200 / 251 CC 31 SCAR 24.4
CC
31
Cov
80%
xy_to_n
20.3
n_to_xy
13.7
new
3.0
CRAP 11.4
« PlanePath/KochCurve.pm ↑ Summary PlanePath/HilbertSides.pm »
1 # Copyright 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=CubicBase --all --output=numbers --size=60x20
20 # math-image --path=CubicBase --values=Multiples,multiples=27 --output=numbers --size=60x20
21  
22 # math-image --path=CubicBase --expression='i<128?i:0' --output=numbers --size=132x20
23 #
24  
25 package Math::PlanePath::CubicBase;
26 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 #use List::Util 'max';
29 1 *max = \&Math::PlanePath::_max;
30  
31 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 $VERSION = 128;
33 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 @ISA = ('Math::PlanePath');
35  
36 use Math::PlanePath::Base::Generic
37 1 'is_infinite',
38 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 use Math::PlanePath::Base::Digits
40 1 'parameter_info_array',
41 'digit_split_lowtohigh',
42 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # uncomment this to run the ### lines
45 #use Smart::Comments;
46  
47  
48 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_even;
50  
51 # use constant parameter_info_array =>
52 # [ Math::PlanePath::Base::Digits::parameter_info_radix2(),
53 #
54 # # Experimental ...
55 # # { name => 'skewed',
56 # # type => 'boolean',
57 # # default => 0,
58 # # },
59 # ];
60  
61 sub x_negative_at_n {
62 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
63 0 return $self->{'radix'};
64 }
65 sub y_negative_at_n {
66 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
67 0 return $self->{'radix'}**2;
68 }
69 1 ▶use constant absdx_minimum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use constant dir_maximum_dxdy => (-1, -3); # supremum
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 sub turn_any_straight {
73 0 ▶ my ($self) = @_;
Subroutine turn_any_straight: not called CC=1 SCAR=6.9
Pod documented
74 0 return $self->{'radix'} > 2; # never straight in radix=2
75 }
76 sub _UNDOCUMENTED__turn_any_left_at_n {
77 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_left_at_n: not called CC=1 SCAR=6.9
78 0 return $self->{'radix'} - 1;
79 }
80 sub _UNDOCUMENTED__turn_any_right_at_n {
81 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: not called CC=1 SCAR=6.9
82 0 return $self->{'radix'};
83 }
84  
85  
86 #------------------------------------------------------------------------------
87 sub new {
88 6 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=3 SCAR=11.0
Pod documented
89  
90 6 my $radix = $self->{'radix'};
91 6 ▶ if (! defined $radix || $radix <= 2) { $radix = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $radix or $radix <= 2) 3 3 6
Condition66%2/3
not defined $radix or $radix <= 2
l!l&&r!l&&!r
303
MC/DC50%1/2
not defined $radix or $radix <= 2
not defined $radix $radix <= 2
Truth table66%2/3
not(A) or B
A= not defined $radixB= $radix <= 2
ABresult
1X1
011
000
92 6 $self->{'radix'} = $radix;
93  
94 6 return $self;
95 }
96  
97 sub n_to_xy {
98 33 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=12 SCAR=26.2
Pod documented
99 ### CubicBase n_to_xy(): "$n"
100  
101 33 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 33 33
102 33 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 33 33
103  
104 # is this sort of midpoint worthwhile? not documented yet
105 {
106 33 my $int = int($n);
107 ### $int
108 ### $n
109 33 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 33 33
110 0 my ($x1,$y1) = $self->n_to_xy($int);
111 0 my ($x2,$y2) = $self->n_to_xy($int+1);
112 0 my $frac = $n - $int; # inherit possible BigFloat
113 0 my $dx = $x2-$x1;
114 0 my $dy = $y2-$y1;
115 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
116 }
117 33 $n = $int; # BigFloat int() gives BigInt, use that
118 }
119  
120 33 my $x = 0;
121 33 my $y = 0;
122  
123 33 my $radix = $self->{'radix'};
124 33 ▶ if (my @digits = digit_split_lowtohigh($n,$radix)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@digits) = digit_split_lowtohigh($n, $radix)) 31 2 33
125 31 my $len = ($n * 0) + 1; # inherit bignum 1
126 31 my $ext = 1;
127 31 for (;;) {
128 { # 0 degrees
129 37 $x += (2*(shift @digits)) * $len; # low to high
130 }
131 37 ▶ @digits || last;
Branch: 1 branch
BranchTrueFalseTotal
@digits or last 8 29 37
132  
133 29 ▶ if ($ext ^= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext ^= 1) 4 25 29
134 4 $len *= $radix;
135 }
136  
137 { # +120 degrees
138 29 my $dlen = (shift @digits) * $len; # low to high
139 29 $x -= $dlen;
140 29 $y += $dlen;
141 }
142 29 ▶ @digits || last;
Branch: 1 branch
BranchTrueFalseTotal
@digits or last 19 10 29
143  
144 10 ▶ if ($ext ^= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext ^= 1) 8 2 10
145 8 $len *= $radix;
146 }
147  
148 { # +240 degrees
149 10 my $dlen = (shift @digits) * $len; # low to high
150 10 $x -= $dlen;
151 10 $y -= $dlen;
152 }
153 10 ▶ @digits || last;
Branch: 1 branch
BranchTrueFalseTotal
@digits or last 4 6 10
154  
155 6 ▶ if ($ext ^= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext ^= 1) 0 6 6
156 0 $len *= $radix;
157 }
158 }
159  
160 31 ▶ if ($self->{'skewed'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"skewed"}) 0 31 31
161 0 $x = ($x + $y) / 2;
162 }
163 }
164  
165 ### result: "$x,$y"
166 33 return ($x,$y);
167 }
168  
169 sub xy_to_n {
170 33 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=18 SCAR=30.1
Pod documented
171 ### CubicBase xy_to_n(): "$x, $y"
172  
173 33 $x = round_nearest ($x);
174 33 $y = round_nearest ($y);
175 33 ▶ if (is_infinite($x)) { return ($x); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 33 33
176 33 ▶ if (is_infinite($y)) { return ($y); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 33 33
177  
178 33 ▶ if ($self->{'skewed'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'skewed'}) { } 0 33 33
179 0 $x = 2*$x - $y;
180 } else {
181 33 ▶ if (($x + $y) % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x + $y) % 2) 0 33 33
182 # nothing on odd squares, only A2 even squares
183 0 return undef;
184 }
185 }
186 # $x = ($x-$y)/2; # into i,j coordinates
187  
188 33 foreach my $overflow ($x+$y, $x-$y) {
189 66 ▶ if (is_infinite($overflow)) { return $overflow; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($overflow)) 0 66 66
190 }
191  
192 33 my $radix = $self->{'radix'};
193 33 my $zero = ($x * 0 * $y); # inherit bignum 0
194 33 my @n; # digits low to high
195  
196 33 ▶ if ($x || $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x or $y) 31 2 33
Condition100%3/3
$x or $y
l!l&&r!l&&!r
2922
MC/DC100%2/2
$x or $y
$x $y
Truth table100%3/3
A or B
A= $xB= $y
ABresult
1X1
011
000
197 31 my $ext = 1;
198  
199 31 for (;;) {
200 ### at: "x=$x y=$y"
201  
202 {
203 37 my $digit = (($x+$y)/2) % $radix;
204 37 push @n, $digit;
205 37 $x -= 2*$digit;
206  
207 ### 0deg digit: $digit
208 ### subtract to: "x=$x y=$y"
209 }
210  
211 37 ▶ last unless $x || $y;
Branch: 1 branch
BranchTrueFalseTotal
unless $x or $y 8 29 37
Condition66%2/3
$x or $y
l!l&&r!l&&!r
2908
MC/DC50%1/2
$x or $y
$x $y
Truth table66%2/3
A or B
A= $xB= $y
ABresult
1X1
011
000
212 29 ▶ if ($ext ^= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext ^= 1) 4 25 29
213 ### assert: ($x % $radix) == 0
214 ### assert: ($y % $radix) == 0
215 4 $x = int($x/$radix);
216 4 $y = int($y/$radix);
217 ### divide out to: "x=$x y=$y"
218 }
219  
220 {
221 29 my $digit = (($y-$x)/2) % $radix;
222 29 push @n, $digit;
223 29 $x += $digit;
224 29 $y -= $digit;
225  
226 ### 120deg digit: $digit
227 ### subtract to: "x=$x y=$y"
228 }
229  
230 29 ▶ last unless $x || $y;
Branch: 1 branch
BranchTrueFalseTotal
unless $x or $y 19 10 29
Condition66%2/3
$x or $y
l!l&&r!l&&!r
10019
MC/DC50%1/2
$x or $y
$x $y
Truth table66%2/3
A or B
A= $xB= $y
ABresult
1X1
011
000
231 10 ▶ if ($ext ^= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext ^= 1) 8 2 10
232 ### assert: ($x % $radix) == 0
233 ### assert: ($y % $radix) == 0
234 8 $x = int($x/$radix);
235 8 $y = int($y/$radix);
236 ### divide out to: "x=$x y=$y"
237 }
238  
239 {
240 10 my $digit = (-$y) % $radix;
241 10 push @n, $digit;
242 10 $x += $digit;
243 10 $y += $digit;
244  
245 ### 240deg digit: $digit
246 ### subtract to: "x=$x y=$y"
247 }
248  
249 10 ▶ last unless $x || $y;
Branch: 1 branch
BranchTrueFalseTotal
unless $x or $y 4 6 10
Condition66%2/3
$x or $y
l!l&&r!l&&!r
604
MC/DC50%1/2
$x or $y
$x $y
Truth table66%2/3
A or B
A= $xB= $y
ABresult
1X1
011
000
250 6 ▶ if ($ext ^= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext ^= 1) 0 6 6
251 ### assert: ($x % $radix) == 0
252 ### assert: ($y % $radix) == 0
253 0 $x = int($x/$radix);
254 0 $y = int($y/$radix);
255 ### divide out to: "x=$x y=$y"
256 }
257 }
258 }
259  
260 33 return digit_join_lowtohigh (\@n, $radix, $zero);
261 }
262  
263 # ENHANCE-ME: Can probably do better by measuring extents in 3 directions
264 # for a hexagonal boundary.
265 #
266 # not exact
267 sub rect_to_n_range {
268 33 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=1 SCAR=0.0
Pod documented
269 ### CubicBase rect_to_n_range(): "$x1,$y1 $x2,$y2"
270  
271 33 $x1 = round_nearest ($x1);
272 33 $y1 = round_nearest ($y1);
273 33 $x2 = round_nearest ($x2);
274 33 $y2 = round_nearest ($y2);
275  
276 33 my $radix = $self->{'radix'};
277 33 my $xm = max(abs($x1),abs($x2)) * $radix*$radix*$radix;
278 33 my $ym = max(abs($y1),abs($y2)) * $radix*$radix*$radix;
279  
280 33 return (0,
281 $xm*$xm+$ym*$ym);
282 }
283  
284 #------------------------------------------------------------------------------
285 # levels
286  
287 1 ▶use Math::PlanePath::ImaginaryBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
288 1 *level_to_n_range = \&Math::PlanePath::ImaginaryBase::level_to_n_range;
289 1 *n_to_level = \&Math::PlanePath::ImaginaryBase::n_to_level;
290  
291  
292 #------------------------------------------------------------------------------
293 1 1;
294 __END__
« PlanePath/KochCurve.pm ↑ Summary PlanePath/HilbertSides.pm »