PlanePath/LTiling.pm

filter: Statementstmt 50.7% 71 / 140 Branchbran 21.4% 12 / 56 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 73.6% 14 / 19 Podpod 100% 8 / 8 totaltotal 45.4% 106 / 233 CC 32 SCAR 43.9
CC
32
Cov
42%
n_to_xy
240.0
rect_to_n_range
42.0
n_to_level
12.0
CRAP 80.3
« PlanePath/ChanTree.pm ↑ Summary PlanePath/CretanLabyrinth.pm »
1 # Copyright 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 package Math::PlanePath::LTiling;
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 'round_down_pow',
37 'round_up_pow',
38 1 ▶ 'digit_split_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40  
41 # uncomment this to run the ### lines
42 #use Smart::Comments;
43  
44  
45 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 use constant parameter_info_array =>
50 [ { name => 'L_fill',
51 display => 'L Fill',
52 type => 'enum',
53 default => 'middle',
54 choices => ['middle','left','upper','ends','all'],
55 choices_display => ['Middle','Left','Upper','Ends','All'],
56 description => 'Which points to number with each "L".',
57 },
58 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 1 my %sumxy_minimum = (middle => 0, # X=0,Y=0
61 left => 1, # X=1,Y=0
62 upper => 1, # X=0,Y=1
63 ends => 1, # X=1,Y=0 and X=0,Y=1
64 all => 0, # X=0,Y=0
65 );
66 sub sumxy_minimum {
67 0 ▶ my ($self) = @_;
Subroutine sumxy_minimum: not called CC=1 SCAR=6.9
Pod documented
68 0 return $sumxy_minimum{$self->{'L_fill'}};
69 }
70 1 *sumabsxy_minimum = \&sumxy_minimum;
71 1 *absdiffxy_minimum = \&sumxy_minimum;
72 1 *rsquared_minimum = \&sumxy_minimum;
73  
74 {
75 1 my %turn_any_straight = (# middle => 0,
76 left => 1,
77 # upper => 0,
78 ends => 1,
79 all => 1,
80 );
81 sub turn_any_straight {
82 0 ▶ my ($self) = @_;
Subroutine turn_any_straight: not called CC=1 SCAR=6.9
Pod documented
83 0 return $turn_any_straight{$self->{'L_fill'}};
84 }
85 }
86  
87 #------------------------------------------------------------------------------
88  
89 1 sub new {
90 6 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=3 SCAR=11.2
Pod documented
91 6 my $L_fill = $self->{'L_fill'};
92 6 ▶ if (! defined $L_fill) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $L_fill) { } 4 2 6
elsif (not exists $sumxy_minimum{$L_fill}) { } 0 2 2
93 4 $self->{'L_fill'} = 'middle';
94 } elsif (! exists $sumxy_minimum{$L_fill}) {
95 0 croak "Unrecognised L_fill option: ",$L_fill;
96 }
97 6 return $self;
98 }
99  
100 sub n_to_xy {
101 0 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: not called CC=15 SCAR=54.8
Pod documented
102 ### LTiling n_to_xy(): $n
103  
104 0 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
105 0 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
106  
107 {
108 0 my $int = int($n);
109 ### $int
110 ### $n
111 0 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 0 0
112 0 my ($x1,$y1) = $self->n_to_xy($int);
113 0 my ($x2,$y2) = $self->n_to_xy($int+1);
114 0 my $frac = $n - $int; # inherit possible BigFloat
115 0 my $dx = $x2-$x1;
116 0 my $dy = $y2-$y1;
117 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
118 }
119 0 $n = $int; # BigFloat int() gives BigInt, use that
120 }
121  
122 0 my $x = my $y = ($n * 0); # inherit bignum 0
123 0 my $len = $x + 1; # inherit bignum 1
124  
125 0 my $L_fill = $self->{'L_fill'};
126 0 ▶ if ($L_fill eq 'left') {
Branch: 4 branches
BranchTrueFalseTotal
if ($L_fill eq 'left') { } 0 0 0
elsif ($L_fill eq 'upper') { } 0 0 0
elsif ($L_fill eq 'ends') { } 0 0 0
elsif ($L_fill eq 'all') { } 0 0 0
127 0 $x += 1;
128 } elsif ($L_fill eq 'upper') {
129 0 $y += 1;
130 } elsif ($L_fill eq 'ends') {
131 0 my $rem = _divrem_mutate ($n, 2);
132 0 ▶ if ($rem) { # low digit==1
Branch: 1 branch
BranchTrueFalseTotal
if ($rem) { } 0 0 0
133 0 $y = $len; # 1
134 } else { # low digit==0
135 0 $x = $len; # 1
136 }
137 } elsif ($L_fill eq 'all') {
138 0 my $rem = _divrem_mutate ($n, 3);
139 0 ▶ if ($rem == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rem == 1) { } 0 0 0
elsif ($rem == 2) { } 0 0 0
140 0 $x = $len; # 1
141 } elsif ($rem == 2) {
142 0 $y = $len; # 1
143 }
144 }
145  
146 0 foreach my $digit (digit_split_lowtohigh($n,4)) {
147 ### at: "$x,$y digit=$digit"
148  
149 0 ▶ if ($digit == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($digit == 1) { } 0 0 0
elsif ($digit == 2) { } 0 0 0
elsif ($digit == 3) { } 0 0 0
150 0 ($x,$y) = (4*$len-1-$y,$x);
151 } elsif ($digit == 2) {
152 0 $x += $len;
153 0 $y += $len;
154 } elsif ($digit == 3) {
155 0 ($x,$y) = ($y,4*$len-1-$x);
156 }
157 0 $len *= 2;
158 }
159  
160 ### final: "$x,$y"
161 0 return ($x,$y);
162 }
163  
164 1 my @yx_to_digit = ([0,0,1,1],
165 [0,2,2,1],
166 [3,2],
167 [3,3]);
168 1 my %fill_factor = (middle => 1,
169 left => 1,
170 upper => 1,
171 ends => 2,
172 all => 3);
173 1 my %yx_to_fill = (middle => [[0]],
174 left => [[undef,0]],
175 upper => [[],
176 [0]],
177 ends => [[undef,0],
178 [1]],
179 all => [[0,1],
180 [2]]);
181 sub xy_to_n {
182 51 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=9 SCAR=24.3
Pod documented
183 ### LTiling xy_to_n(): "$x, $y"
184  
185 51 $x = round_nearest ($x);
186 51 $y = round_nearest ($y);
187 51 ▶ if ($x < 0 || $y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 51 51
Condition33%1/3
$x < 0 or $y < 0
l!l&&r!l&&!r
0051
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
188 0 return undef;
189 }
190  
191 51 my ($len, $level) = round_down_pow (max($x,$y),
192 2);
193 51 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 51 51
194 0 return $level;
195 }
196  
197 51 my $n = ($x * 0 * $y); # inherit bignum 0
198  
199 51 ▶ while ($level-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level-- >= 0) 244 51 295
200 ### assert: $x >= 0
201 ### assert: $y >= 0
202 ### assert: ($y < 2*$len && $x < 4*$len) || ($x < 2*$len && $y < 4*$len)
203  
204 ### $len
205 ### x: int($x/$len)
206 ### y: int($y/$len)
207  
208 244 my $digit = $yx_to_digit[int($y/$len)]->[int($x/$len)];
209 244 ▶ if ($digit == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($digit == 1) { } 0 244 244
elsif ($digit == 2) { } 136 108 244
elsif ($digit == 3) { } 0 108 108
210 0 ($x,$y) = ($y,4*$len-1-$x);
211 } elsif ($digit == 2) {
212 136 $x -= $len;
213 136 $y -= $len;
214 } elsif ($digit == 3) {
215 0 ($x,$y) = (4*$len-1-$y,$x);
216 }
217  
218 ### to: "digit=$digit xy=$x,$y"
219  
220 244 $n = $n*4 + $digit;
221 244 $len /= 2;
222 }
223  
224 ### assert: ($x==0 && $y== 0) || ($x==1 && $y== 0) || ($x==0 && $y== 1)
225  
226 51 my $fill = $self->{'L_fill'};
227 51 ▶ if (defined (my $digit = $yx_to_fill{$fill}->[$y]->[$x])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $digit = $yx_to_fill{$fill}[$y][$x])) 51 0 51
228 51 return $n*$fill_factor{$fill} + $digit;
229 }
230 0 return undef;
231 }
232  
233 1 my %range_factor = (middle => 3,
234 left => 3,
235 upper => 3,
236 ends => 6,
237 all => 8);
238 # not exact
239 sub rect_to_n_range {
240 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=6 SCAR=37.4
Pod documented
241 ### LTiling rect_to_n_range(): "$x1,$y1 $x2,$y2"
242  
243 0 $x1 = round_nearest ($x1);
244 0 $y1 = round_nearest ($y1);
245 0 $x2 = round_nearest ($x2);
246 0 $y2 = round_nearest ($y2);
247 0 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 0 0
248 0 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 0 0
249 ### rect: "X = $x1 to $x2, Y = $y1 to $y2"
250  
251 0 ▶ if ($x2 < 0 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0) 0 0 0
Condition0%0/3
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x2 < 0 or $y2 < 0
$x2 < 0 $y2 < 0
Truth table0%0/3
A or B
A= $x2 < 0B= $y2 < 0
ABresult
1X1
011
000
252 ### rectangle outside first quadrant ...
253 0 return (1, 0);
254 }
255  
256 0 my ($len, $level) = round_down_pow (max($x2,$y2), 2);
257 ### $len
258 ### $level
259 0 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 0 0
260 0 return (0,$level);
261 }
262  
263 return (0,
264 0 $len*$len * $range_factor{$self->{'L_fill'}});
265 }
266  
267  
268 #------------------------------------------------------------------------------
269 # levels
270  
271 sub level_to_n_range {
272 7 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
273 7 return (0, 4**$level * $fill_factor{$self->{'L_fill'}} - 1);
274 }
275 sub n_to_level {
276 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
277 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
278 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
279 0 $n = round_nearest($n);
280 0 _divrem_mutate ($n, $fill_factor{$self->{'L_fill'}});
281 0 my ($pow, $exp) = round_up_pow ($n+1, 4);
282 0 return $exp;
283 }
284  
285 #------------------------------------------------------------------------------
286 1 1;
287 __END__
« PlanePath/ChanTree.pm ↑ Summary PlanePath/CretanLabyrinth.pm »