PlanePath/DekkingCurve.pm

filter: Statementstmt 89.7% 149 / 166 Branchbran 60.4% 29 / 48 Conditioncond 62.2% 28 / 45 MC/DCmcdc 48.0% 12 / 25 Subroutinesub 100% 26 / 26 Podpod 100% 10 / 10 totaltotal 79.3% 254 / 320 CC 46 SCAR 22.5
CC
46
Cov
80%
xy_to_n
21.5
n_to_xy
10.5
rect_to_n_range
10.5
CRAP 9.5
« PlanePath/QuintetCurve.pm ↑ Summary PlanePath/ImaginaryHalf.pm »
1 # Copyright 2011, 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::DekkingCurve;
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 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 @ISA = ('Math::PlanePath::Base::NSEW',
30 'Math::PlanePath');
31 1 *_divrem = \&Math::PlanePath::_divrem;
32 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
33  
34 use Math::PlanePath::Base::Generic
35 1 'is_infinite',
36 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 use Math::PlanePath::Base::Digits
38 1 'round_up_pow',
39 'round_down_pow',
40 'digit_split_lowtohigh',
41 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
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_x_negative => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant class_y_negative => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 1 use constant parameter_info_array => [ { name => 'arms',
52 share_key => 'arms_4',
53 display => 'Arms',
54 type => 'integer',
55 minimum => 1,
56 maximum => 4,
57 default => 1,
58 width => 1,
59 description => 'Arms',
60 1 ▶ } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62  
63 #------------------------------------------------------------------------------
64  
65 sub x_negative {
66 4 ▶ my ($self) = @_;
Subroutine x_negative: called CC=1 SCAR=0.0
Pod documented
67 4 return $self->{'arms'} > 1;
68 }
69 {
70 1 my @x_negative_at_n = (undef, undef, 5, 2, 2);
71 sub x_negative_at_n {
72 3 ▶ my ($self) = @_;
Subroutine x_negative_at_n: called CC=1 SCAR=0.0
Pod documented
73 3 return $x_negative_at_n[$self->{'arms'}];
74 }
75 }
76  
77 1 sub y_negative {
78 4 ▶ my ($self) = @_;
Subroutine y_negative: called CC=1 SCAR=0.0
Pod documented
79 4 return $self->{'arms'} > 2;
80 }
81 {
82 1 my @y_negative_at_n = (undef, undef, undef, 8, 3);
83 sub y_negative_at_n {
84 3 ▶ my ($self) = @_;
Subroutine y_negative_at_n: called CC=1 SCAR=0.0
Pod documented
85 3 return $y_negative_at_n[$self->{'arms'}];
86 }
87 }
88  
89 #------------------------------------------------------------------------------
90  
91 1 ▶use Math::PlanePath::DekkingCentres;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 ▶use vars '@_next_state','@_digit_to_x','@_digit_to_y','@_yx_to_digit';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 BEGIN {
94 1 ▶ *_next_state = \@Math::PlanePath::DekkingCentres::_next_state;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 *_digit_to_x = \@Math::PlanePath::DekkingCentres::_digit_to_x;
96 1 *_digit_to_y = \@Math::PlanePath::DekkingCentres::_digit_to_y;
97 1 *_yx_to_digit = \@Math::PlanePath::DekkingCentres::_yx_to_digit;
98 }
99  
100 sub new {
101 12 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
102 12 ▶ $self->{'arms'} ||= 1;
Condition100%2/2
$self->{"arms"} ||= 1
l!l
48
MC/DC100%1/1
$self->{"arms"} ||= 1
$self->{"arms"}
Truth table100%2/2
A
A= $self->{"arms"}
Aresult
11
00
103 12 return $self;
104 }
105  
106 # tables generated by tools/dekking-curve-table.pl
107 #
108 1 my @edge_dx = (0,0,0,1,1, 0,0,1,1,0, 0,0,0,1,0, 0,0,1,0,1, 0,1,0,1,1,
109 1,1,1,1,1, 1,1,1,0,1, 1,1,0,1,0, 0,0,1,0,0, 0,1,1,0,0,
110 1,1,1,0,0, 1,1,0,0,1, 1,1,1,0,1, 1,1,0,1,0, 1,0,1,0,0,
111 0,0,0,0,0, 0,0,0,1,0, 0,0,1,0,1, 1,1,0,1,1, 1,0,0,1,1,
112 1,1,1,1,1, 1,0,0,0,0, 1,1,1,1,1, 0,0,0,0,1, 1,0,0,1,1,
113 1,1,1,0,0, 1,1,1,1,1, 0,0,0,1,1, 0,0,1,0,1, 0,1,0,1,1,
114 0,0,0,0,0, 0,1,1,1,1, 0,0,0,0,0, 1,1,1,1,0, 0,1,1,0,0,
115 0,0,0,1,1, 0,0,0,0,0, 1,1,1,0,0, 1,1,0,1,0, 1,0,1,0,0);
116 1 my @edge_dy = (0,0,0,0,0, 0,0,0,1,0, 0,0,1,0,1, 1,1,0,1,1, 1,0,0,1,1,
117 0,0,0,1,1, 0,0,1,1,0, 0,0,0,1,0, 0,0,1,0,1, 0,1,0,1,1,
118 1,1,1,1,1, 1,1,1,0,1, 1,1,0,1,0, 0,0,1,0,0, 0,1,1,0,0,
119 1,1,1,0,0, 1,1,0,0,1, 1,1,1,0,1, 1,1,0,1,0, 1,0,1,0,0,
120 0,0,0,1,1, 0,0,0,0,0, 1,1,1,0,0, 1,1,0,1,0, 1,0,1,0,0,
121 1,1,1,1,1, 1,0,0,0,0, 1,1,1,1,1, 0,0,0,0,1, 1,0,0,1,1,
122 1,1,1,0,0, 1,1,1,1,1, 0,0,0,1,1, 0,0,1,0,1, 0,1,0,1,1,
123 0,0,0,0,0, 0,1,1,1,1, 0,0,0,0,0, 1,1,1,1,0, 0,1,1,0,0);
124  
125 sub n_to_xy {
126 22674 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=8 SCAR=23.6
Pod documented
127 ### DekkingCurve n_to_xy(): $n
128  
129 22674 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 22674 22674
130 22674 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 22674 22674
131  
132 22674 my $int = int($n);
133 22674 $n -= $int; # fraction part
134  
135 22674 my $arms = $self->{'arms'};
136 22674 my $arm = _divrem_mutate ($int, $arms);
137 22674 ▶ if ($arm) { $int += 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($arm) 0 22674 22674
138 22674 my @digits = digit_split_lowtohigh($int,25);
139 22674 my $state = 0;
140 22674 my @x;
141 my @y;
142 22674 foreach my $i (reverse 0 .. $#digits) {
143 107202 $state += $digits[$i];
144 107202 $x[$i] = $_digit_to_x[$state];
145 107202 $y[$i] = $_digit_to_y[$state];
146 107202 $state = $_next_state[$state];
147 }
148  
149 ### @x
150 ### @y
151 ### $state
152 ### dx: $_digit_to_x[$state+24] - $_digit_to_x[$state]
153 ### dy: $_digit_to_y[$state+24] - $_digit_to_y[$state]
154  
155 22674 my $zero = $int * 0;
156 22674 my $x = ($n * (($_digit_to_x[$state+24] - $_digit_to_x[$state])/4)
157 + digit_join_lowtohigh(\@x, 5, $zero)
158 + $edge_dx[$state]);
159 22674 my $y = ($n * (($_digit_to_y[$state+24] - $_digit_to_y[$state])/4)
160 + digit_join_lowtohigh(\@y, 5, $zero)
161 + $edge_dy[$state]);
162  
163 22674 ▶ if ($arm < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm < 2) 22674 0 22674
164 22674 ▶ if ($arm < 1) { return ($x,$y); } # arm==0
Branch: 1 branch
BranchTrueFalseTotal
if ($arm < 1) 22674 0 22674
165 0 return (-$y,$x); # arm==1 rotate +90
166 }
167 0 ▶ if ($arm < 3) { return (-$x,-$y); } # arm==2
Branch: 1 branch
BranchTrueFalseTotal
if ($arm < 3) 0 0 0
168 0 return ($y,-$x); # arm==3 rotate -90
169 }
170  
171 sub xy_to_n {
172 9405 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=17 SCAR=30.7
Pod documented
173 ### DekkingCurve xy_to_n(): "$x, $y"
174  
175 9405 $x = round_nearest ($x);
176 9405 $y = round_nearest ($y);
177 9405 ▶ if (is_infinite($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 9405 9405
178 0 return $x;
179 }
180 9405 ▶ if (is_infinite($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 9405 9405
181 0 return $y;
182 }
183  
184 9405 my $arms = $self->{'arms'};
185 9405 ▶ if (($arms < 2 && $x < 0) || ($arms < 3 && $y < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arms < 2 and $x < 0 or $arms < 3 and $y < 0) 0 9405 9405
Condition33%3/9
$arms < 2 and $x < 0 or $arms < 3 and $y < 0
l!l&&r!l&&!r
009405
$arms < 2 and $x < 0
!ll&&!rl&&r
094050
$arms < 3 and $y < 0
!ll&&!rl&&r
094050
MC/DC0%0/4
$arms < 2 and $x < 0 or $arms < 3 and $y < 0
$arms < 2 $x < 0 $arms < 3 $y < 0
Truth table14%1/7
A and B or C and D
A= $arms < 2B= $x < 0C= $arms < 3D= $y < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
186 ### X or Y negative, no N value ...
187 0 return undef;
188 }
189  
190 9405 foreach my $arm (0 .. $arms-1) {
191 9405 foreach my $xoffset (0,-1) {
192 13792 foreach my $yoffset (0,-1) {
193  
194 22565 my @x = digit_split_lowtohigh($x+$xoffset,5);
195 22565 my @y = digit_split_lowtohigh($y+$yoffset,5);
196 22565 my $state = 0;
197 22565 my @n;
198 22565 foreach my $i (reverse 0 .. max($#x,$#y)) {
199 106999 ▶ my $digit = $n[$i] = $_yx_to_digit[$state + 5*($y[$i]||0) + ($x[$i]||0)];
Condition100%4/4
$y[$i] || 0
l!l
4762359376
$x[$i] || 0
l!l
5089456105
MC/DC100%1/1
$y[$i] || 0
$y[$i]
MC/DC100%1/1
$x[$i] || 0
$x[$i]
Truth table100%2/2
A
A= $y[$i]
Aresult
11
00
Truth table100%2/2
A
A= $x[$i]
Aresult
11
00
200 106999 $state = $_next_state[$state+$digit];
201 }
202 22565 my $zero = $x*0*$y;
203 22565 my $n = digit_join_lowtohigh(\@n, 25, $zero);
204 22565 $n = $n*$arms;
205 22565 ▶ if (my ($nx,$ny) = $self->n_to_xy($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->n_to_xy($n)) 22565 0 22565
206 22565 ▶ if ($nx == $x && $ny == $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nx == $x and $ny == $y) 5019 17546 22565
Condition100%3/3
$nx == $x and $ny == $y
!ll&&!rl&&r
1178357635019
MC/DC100%2/2
$nx == $x and $ny == $y
$nx == $x $ny == $y
Truth table100%3/3
A and B
A= $nx == $xB= $ny == $y
ABresult
0X0
100
111
207 5019 ▶ return $n + ($arm ? $arm-$arms : $arm);
Branch: 1 branch
BranchTrueFalseTotal
$arm ? : 0 5019 5019
208 }
209 }
210 }
211 }
212 4386 ($x,$y) = ($y,-$x); # rotate -90
213 ### rotate to: "$x, $y"
214 }
215 4386 return undef;
216 }
217  
218 # not exact
219 sub rect_to_n_range {
220 8 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=7 SCAR=23.5
Pod documented
221 ### DekkingCurve rect_to_n_range(): "$x1,$y1, $x2,$y2"
222  
223 8 $x1 = round_nearest ($x1);
224 8 $x2 = round_nearest ($x2);
225 8 $y1 = round_nearest ($y1);
226 8 $y2 = round_nearest ($y2);
227  
228 8 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 8 8
229 8 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 8 8
230  
231 8 my $arms = $self->{'arms'};
232 8 ▶ if (($arms < 2 && $x2 < 0) || ($arms < 3 && $y2 < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arms < 2 and $x2 < 0 or $arms < 3 and $y2 < 0) 0 8 8
Condition33%3/9
$arms < 2 and $x2 < 0 or $arms < 3 and $y2 < 0
l!l&&r!l&&!r
008
$arms < 2 and $x2 < 0
!ll&&!rl&&r
080
$arms < 3 and $y2 < 0
!ll&&!rl&&r
080
MC/DC0%0/4
$arms < 2 and $x2 < 0 or $arms < 3 and $y2 < 0
$arms < 2 $x2 < 0 $arms < 3 $y2 < 0
Truth table14%1/7
A and B or C and D
A= $arms < 2B= $x2 < 0C= $arms < 3D= $y2 < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
233 ### rectangle all negative, no N values ...
234 0 return (1, 0);
235 }
236  
237 8 my ($pow) = round_down_pow (max(abs($x1),abs($y1),$x2,$y2) + 1, 5);
238 ### $pow
239 8 return (0, 25*$pow*$pow*$arms - 1);
240 }
241  
242 #------------------------------------------------------------------------------
243  
244 sub level_to_n_range {
245 2 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
246 2 return (0, 25**$level * $self->{'arms'});
247 }
248 sub n_to_level {
249 8 ▶ my ($self, $n) = @_;
Subroutine n_to_level: called CC=3 SCAR=11.7
Pod documented
250 ### n_to_level(): $n
251 8 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 8 8
252 8 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 8 8
253 8 $n = round_nearest($n);
254 8 $n += $self->{'arms'}-1; # division rounding up
255 8 _divrem_mutate ($n, $self->{'arms'});
256 8 my ($pow, $exp) = round_up_pow ($n, 25);
257 8 return $exp;
258 }
259  
260 #------------------------------------------------------------------------------
261 # Not taking into account multiple arms ...
262  
263 # Return true if X axis segment $x to $x+1 is traversed
264 sub _UNDOCUMENTED__xseg_is_traversed {
265 3126 ▶ my ($self, $x) = @_;
Subroutine _UNDOCUMENTED__xseg_is_traversed: called CC=8 SCAR=21.3
266 3126 ▶ if ($x < 0 || is_infinite($x)) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or is_infinite($x)) 0 3126 3126
Condition33%1/3
$x < 0 or is_infinite($x)
l!l&&r!l&&!r
003126
MC/DC0%0/2
$x < 0 or is_infinite($x)
$x < 0 is_infinite($x)
Truth table33%1/3
A or B
A= $x < 0B= is_infinite($x)
ABresult
1X1
011
000
267 3126 ▶ if ($x == 0) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0) 1 3125 3126
268 3125 my $digit = _divrem_mutate($x, 5);
269 3125 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) 2500 625 3125
270 2500 return ($digit == 1);
271 }
272 # find lowest non-zero
273 625 ▶ while ($x && ! ($digit = _divrem_mutate($x, 5))) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($x and not $digit = _divrem_mutate($x, 5)) 156 625 781
Condition66%2/3
$x and not $digit = _divrem_mutate($x, 5)
!ll&&!rl&&r
0625156
MC/DC50%1/2
$x and not $digit = _divrem_mutate($x, 5)
$x not $digit = _divrem_mutate($x, 5)
Truth table66%2/3
A and not(B)
A= $xB= not $digit = _divrem_mutate($x, 5)
ABresult
0X0
100
111
274 625 ▶ return ($digit == 1 || $digit == 2);
Condition100%3/3
$digit == 1 || $digit == 2
l!l&&r!l&&!r
157156312
MC/DC100%2/2
$digit == 1 || $digit == 2
$digit == 1 $digit == 2
Truth table100%3/3
A || B
A= $digit == 1B= $digit == 2
ABresult
1X1
011
000
275 }
276  
277 # Return true if Y axis segment $y to $y+1 is traversed
278 sub _UNDOCUMENTED__yseg_is_traversed {
279 3126 ▶ my ($self, $y) = @_;
Subroutine _UNDOCUMENTED__yseg_is_traversed: called CC=7 SCAR=19.8
280 3126 ▶ if ($y < 0 || is_infinite($y)) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0 or is_infinite($y)) 0 3126 3126
Condition33%1/3
$y < 0 or is_infinite($y)
l!l&&r!l&&!r
003126
MC/DC0%0/2
$y < 0 or is_infinite($y)
$y < 0 is_infinite($y)
Truth table33%1/3
A or B
A= $y < 0B= is_infinite($y)
ABresult
1X1
011
000
281 3126 my $digit = _divrem_mutate($y, 5);
282 3126 ▶ if ($digit != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit != 4) 2501 625 3126
283 2501 return ($digit == 3);
284 }
285 # find lowest non-4
286 625 ▶ while ($y && ($digit = _divrem_mutate($y, 5)) == 4) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($y and ($digit = _divrem_mutate($y, 5)) == 4) 156 625 781
Condition100%3/3
$y and ($digit = _divrem_mutate($y, 5)) == 4
!ll&&!rl&&r
5620156
MC/DC100%2/2
$y and ($digit = _divrem_mutate($y, 5)) == 4
$y ($digit = _divrem_mutate($y, 5)) == 4
Truth table100%3/3
A and B
A= $yB= ($digit = _divrem_mutate($y, 5)) == 4
ABresult
0X0
100
111
287 625 ▶ return ($digit == 2 || $digit == 3);
Condition100%3/3
$digit == 2 || $digit == 3
l!l&&r!l&&!r
156156313
MC/DC100%2/2
$digit == 2 || $digit == 3
$digit == 2 $digit == 3
Truth table100%3/3
A || B
A= $digit == 2B= $digit == 3
ABresult
1X1
011
000
288 }
289  
290 #------------------------------------------------------------------------------
291 1 1;
292 __END__
« PlanePath/QuintetCurve.pm ↑ Summary PlanePath/ImaginaryHalf.pm »