PlanePath/CellularRule54.pm

filter: Statementstmt 86.2% 107 / 124 Branchbran 69.4% 25 / 36 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 91.3% 21 / 23 Podpod 100% 5 / 5 totaltotal 82.1% 166 / 202 CC 22 SCAR 18.5
CC
22
Cov
82%
_rect_for_V
16.4
xy_to_n
7.0
rect_to_n_range
6.0
CRAP 6.4
« PlanePath/HexSpiralSkewed.pm ↑ Summary PlanePath/CellularRule190.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 # math-image --path=CellularRule54 --all --scale=10
19 # math-image --path=CellularRule54 --all --output=numbers --size=132x50
20  
21 package Math::PlanePath::CellularRule54;
22 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 $VERSION = 128;
27 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 @ISA = ('Math::PlanePath');
29 2 *_divrem = \&Math::PlanePath::_divrem;
30 2 *_sqrtint = \&Math::PlanePath::_sqrtint;
31  
32 use Math::PlanePath::Base::Generic
33 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # uncomment this to run the ### lines
36 #use Smart::Comments;
37  
38  
39 2 use constant parameter_info_array =>
40 [ Math::PlanePath::Base::Generic::parameter_info_nstart1(),
41 2 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 2 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 sub x_negative_at_n {
46 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
47 0 return $self->n_start + 1;
48 }
49 2 ▶use constant sumxy_minimum => 0; # triangular X>=-Y so X+Y>=0
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use constant diffxy_maximum => 0; # triangular X<=Y so X-Y<=0
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use constant dx_maximum => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use constant dy_minimum => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use constant absdx_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use constant dsumxy_maximum => 4; # straight East dX=+4
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use constant ddiffxy_maximum => 4; # straight East dX=+4
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use constant dir_maximum_dxdy => (-1,0); # supremum, West and dY=+1 up
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59  
60 #------------------------------------------------------------------------------
61  
62 sub new {
63 1 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=2 SCAR=7.6
Pod documented
64 1 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 0 1 1
65 0 $self->{'n_start'} = $self->default_n_start;
66 }
67 1 return $self;
68 }
69 # left add
70 # even y=0 0 1
71 # 2 1 2
72 # 4 3 3
73 # 6 6 4
74 # left = y/2*(y/2+1)/2
75 # = y*(y+2)/8 of 4-cell figures
76 # inverse y = -1 + sqrt(2 * $n + -1)
77 #
78 # left add
79 # odd y=1 0 3
80 # 3 3 6
81 # 5 9 9
82 # 7 18 12
83 # left = 3*(y-1)/2*((y-1)/2+1)/2
84 # = 3*(y-1)*(y+1)/8 of 4-cell figures
85 #
86 # nbase y even = y*(y+2)/8 + 3*((y+1)-1)*((y+1)+1)/8
87 # = [ y*(y+2) + 3*y*(y+2) ] / 8
88 # = y*(y+2)/2
89 # y=0 nbase=0
90 # y=2 nbase=4
91 # y=4 nbase=12
92 # y=6 nbase=24
93 #
94 # nbase y odd = 3*(y-1)*(y+1)/8 + (y+1)*(y+3)/8
95 # = (y+1) * (3y-3 + y+3)/8
96 # = (y+1)*4y/8
97 # = y*(y+1)/2
98 # y=1 nbase=1
99 # y=3 nbase=6
100 # y=5 nbase=15
101 # y=7 nbase=28
102 # inverse y = -1/2 + sqrt(2 * $n + -7/4)
103 # = sqrt(2n-7/4) - 1/2
104 # = (2*sqrt(2n-7/4) - 1)/2
105 # = (sqrt(4n-7)-1)/2
106 #
107 # dual
108 # d = [ 0, 1, 2, 3 ]
109 # N = [ 1, 5, 13, 25 ]
110 # N = (2 d^2 + 2 d + 1)
111 # = ((2*$d + 2)*$d + 1)
112 # d = -1/2 + sqrt(1/2 * $n + -1/4)
113 # = sqrt(1/2 * $n + -1/4) - 1/2
114 # = [ 2*sqrt(1/2 * $n + -1/4) - 1 ] / 2
115 # = [ sqrt(4/2 * $n + -4/4) - 1 ] / 2
116 # = [ sqrt(2*$n - 1) - 1 ] / 2
117 #
118  
119 sub n_to_xy {
120 25 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=4 SCAR=13.9
Pod documented
121 ### CellularRule54 n_to_xy(): $n
122  
123 25 $n = $n - $self->{'n_start'}; # to N=0 basis
124 25 my $frac;
125 {
126 25 my $int = int($n);
127 25 $frac = $n - $int;
128 25 $n = $int; # BigFloat int() gives BigInt, use that
129 25 ▶ if (2*$frac >= 1) { # $frac>=0.5 and BigInt friendly
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $frac >= 1) 0 25 25
130 0 $frac -= 1;
131 0 $n += 1;
132 }
133 # -0.5 <= $frac < 0.5
134 ### assert: $frac >= -0.5
135 ### assert: $frac < 0.5
136 }
137  
138 25 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 3 22 25
139 3 return;
140 }
141  
142 # d is the two-row group number, d=2*y, where n belongs
143 # start of the two-row group is nbase = 2 d^2 + 2 d starting from N=0
144 #
145 22 my $d = int((_sqrtint(2*$n+1) - 1) / 2);
146 22 $n -= (2*$d + 2)*$d; # remainder within two-row
147 ### $d
148 ### remainder: $n
149 22 ▶ if ($n <= $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $d) { } 7 15 22
150 # d+1 many points in the Y=0,2,4,6 etc even row, spaced 4*n apart
151 7 $d *= 2; # y=2*d
152 7 return ($frac + 4*$n - $d,
153 $d);
154 } else {
155 # 3*d many points in the Y=1,3,5,7 etc odd row, using 3 in 4 cells
156 15 $n -= $d+1; # remainder 0 upwards into odd row
157 15 $d = 2*$d+1; # y=2*d+1
158 15 my ($q) = _divrem($n,3);
159 15 return ($frac + $n + $q - $d,
160 $d);
161 }
162 }
163  
164 sub xy_to_n {
165 496 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=7 SCAR=19.5
Pod documented
166 496 $x = round_nearest ($x);
167 496 $y = round_nearest ($y);
168 ### CellularRule54 xy_to_n(): "$x,$y"
169  
170 496 ▶ if ($y < 0
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0 or $x < -$y or $x > $y) 240 256 496
Condition83%5/6
$y < 0 or $x < -$y or $x > $y
l!l&&r!l&&!r
120120256
$y < 0 or $x < -$y
l!l&&r!l&&!r
0120376
MC/DC66%2/3
$y < 0 or $x < -$y or $x > $y
$y < 0 $x < -$y $x > $y
Truth table75%3/4
A or B or C
A= $y < 0B= $x < -$yC= $x > $y
ABCresult
1XX1
01X1
0011
0000
171 || $x < -$y
172 || $x > $y) {
173 240 return undef;
174 }
175 256 $x += $y;
176 ### x centred: $x
177 256 ▶ if ($y % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y % 2) { } 136 120 256
178 ### odd row, 3 in 4 ...
179 136 ▶ if (($x % 4) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 4 == 3) 28 108 136
180 28 return undef;
181 }
182 108 return $x - int($x/4) + $y*($y+1)/2 + $self->{'n_start'};
183 } else {
184 ## even row, sparse ...
185 120 ▶ if ($x % 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 4) 84 36 120
186 84 return undef;
187 }
188 36 return $x/4 + $y*($y+2)/2 + $self->{'n_start'};
189 }
190 }
191  
192 # not exact
193 sub rect_to_n_range {
194 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=2 SCAR=17.9
Pod documented
195 ### CellularRule54 rect_to_n_range(): "$x1,$y1, $x2,$y2"
196  
197 0 ▶ ($x1,$y1, $x2,$y2) = _rect_for_V ($x1,$y1, $x2,$y2)
Branch: 1 branch
BranchTrueFalseTotal
() = _rect_for_V($x1, $y1, $x2, $y2) or return 1, 0 0 0 0
198 or return (1,0); # rect outside pyramid
199  
200 0 my $zero = ($x1 * 0 * $y1 * $x2 * $y2); # inherit bignum
201  
202 # nbase y even y*(y+2)/2
203 # nbase y odd y*(y+1)/2
204 # y even end (y+1)*(y+2)/2
205 # y odd end (y+1)*(y+3)/2
206  
207 0 $y2 += 1;
208 return (# even/odd left end
209 $zero + $y1*($y1 + 2-($y1%2))/2 + $self->{'n_start'},
210  
211 # even/odd right end
212 0 $zero + $y2*($y2 + 2-($y2%2))/2 + $self->{'n_start'} - 1);
213 }
214  
215 # Return ($x1,$y1, $x2,$y2) which is the rectangle part chopped to the top
216 # row entirely within the pyramid V and the bottom row partly within.
217 #
218 sub _rect_for_V {
219 222 ▶ my ($x1,$y1, $x2,$y2) = @_;
Subroutine _rect_for_V: called CC=11 SCAR=28.0
220 ### _rect_for_V(): "$x1,$y1, $x2,$y2"
221  
222 222 $y1 = round_nearest ($y1);
223 222 $y2 = round_nearest ($y2);
224 222 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # swap to y1<=y2
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 50 172 222
225  
226 222 ▶ unless ($y2 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y2 >= 0) 0 222 222
227 ### rect all negative, no N ...
228 0 return;
229 }
230 222 ▶ unless ($y1 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y1 >= 0) 0 222 222
231 # increase y1 to zero, including negative infinity discarded
232 0 $y1 = 0;
233 }
234  
235 222 $x1 = round_nearest ($x1);
236 222 $x2 = round_nearest ($x2);
237 222 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); } # swap to x1<=x2
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 111 111 222
238 222 my $neg_y2 = -$y2;
239  
240 # \ /
241 # y2 \ / +-----
242 # \ / |
243 # \ /
244 # \/ x1
245 #
246 # \ /
247 # ----+ \ / y2
248 # | \ /
249 # \ /
250 # x2 \/
251 #
252 222 ▶ if ($x1 > $y2 # off to the right
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $y2 or $x2 < $neg_y2) 0 222 222
Condition33%1/3
$x1 > $y2 or $x2 < $neg_y2
l!l&&r!l&&!r
00222
MC/DC0%0/2
$x1 > $y2 or $x2 < $neg_y2
$x1 > $y2 $x2 < $neg_y2
Truth table33%1/3
A or B
A= $x1 > $y2B= $x2 < $neg_y2
ABresult
1X1
011
000
253 || $x2 < $neg_y2) { # off to the left
254 ### rect all off to the left or right, no N
255 0 return;
256 }
257  
258 # \ / x2
259 # \ +------+ y2
260 # \ | / |
261 # \ +------+
262 # \/
263 #
264 222 ▶ if ($x2 > $y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 > $y2) 0 222 222
265 ### top-right beyond pyramid, reduce ...
266 0 $x2 = $y2;
267 }
268  
269 #
270 # x1 \ /
271 # y2 +--------+ / y2
272 # | \ | /
273 # +--------+/
274 # \/
275 #
276 222 ▶ if ($x1 < $neg_y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < $neg_y2) 0 222 222
277 ### top-left beyond pyramid, increase ...
278 0 $x1 = $neg_y2;
279 }
280  
281 # \ | /
282 # \ |/
283 # \ /| |
284 # y1 \ / +-------+
285 # \/ x1
286 #
287 # \| /
288 # \ /
289 # |\ /
290 # -------+ \ / y1
291 # x2 \/
292 #
293 # in both of the following y1=x2 or y1=-x2 leaves y1<=y2 because have
294 # already established some part of the rectangle is in the V shape
295 #
296 222 ▶ if ($x1 > $y1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x1 > $y1) { } 0 222 222
elsif ((my $neg_x2 = -$x2) > $y1) { } 0 222 222
297 ### x1 off to the right, so y1 row is outside, increase y1 ...
298 0 $y1 = $x1;
299  
300 } elsif ((my $neg_x2 = -$x2) > $y1) {
301 ### x2 off to the left, so y1 row is outside, increase y1 ...
302 0 $y1 = $neg_x2;
303 }
304  
305 # values ordered
306 ### assert: $x1 <= $x2
307 ### assert: $y1 <= $y2
308  
309 # top row x1..x2 entirely within pyramid
310 ### assert: $x1 >= -$y2
311 ### assert: $x2 <= $y2
312  
313 # bottom row x1..x2 some part within pyramid
314 ### assert: $x1 <= $y1
315 ### assert: $x2 >= -$y1
316  
317 222 return ($x1,$y1, $x2,$y2);
318 }
319  
320 2 1;
321 __END__
« PlanePath/HexSpiralSkewed.pm ↑ Summary PlanePath/CellularRule190.pm »