PlanePath/Corner.pm

filter: Statementstmt 87.1% 102 / 117 Branchbran 70.5% 24 / 34 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 83.3% 20 / 24 Podpod 100% 6 / 6 totaltotal 80.9% 157 / 194 SCAR 31.6
CC
21
Cov
82%
rect_to_n_range
12.3
_UNDOCUMENTED__turn_any_left_at_n
6.0
xy_to_n
4.2
CRAP 23.7
« PlanePath/CellularRule190.pm ↑ Summary PlanePath/KochPeaks.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 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 # cf Matthew Szudzik ElegantPairing.pdf going inwardly.
20 #
21 # 5 | 25--...
22 # |
23 # 4 | 16--17--18--19 24
24 # | |
25 # 3 | 9--10--11 15 23
26 # | | |
27 # 2 | 4-- 5 8 14 22
28 # | | | |
29 # 1 | 1 4 7 13 21
30 # | | | | |
31 # Y=0 | 0 2 6 12 20
32 # +---------------------
33 # X=0 1 2 3 4
34 #
35 # cf A185728 where diagonal is last in each gnomon
36 # A185725 gnomon sides alternately starting from ends
37 # A185726 gnomon sides alternately starting from diagonal
38 #
39 # corner going alternately up and down
40 # A081344,A194280 by diagonals
41 # A081345 X axis, A081346 Y axis
42 #
43 # corner alternately up and down, starting with 3-wide
44 # A080335 N on diagonal
45 # A081347 N on axis
46 # A081348 N on axis
47 #
48 # cf A004120 ??
49 #
50  
51 package Math::PlanePath::Corner;
52 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 $VERSION = 127;
58 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 *_sqrtint = \&Math::PlanePath::_sqrtint;
60 2 @ISA = ('Math::PlanePath');
61  
62 use Math::PlanePath::Base::Generic
63 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 # uncomment this to run the ### lines
66 # use Smart::Comments;
67  
68  
69 2 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 2 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1;
73  
74 2 ▶use Math::PlanePath::SquareSpiral;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 2 *parameter_info_array = \&Math::PlanePath::SquareSpiral::parameter_info_array;
76  
77 2 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 # dSum east dX=1,dY=0 for dSum=+1
81 # south dX=0,dY=-1 for dSum=-1
82 # gnomon up to start of next gnomon is
83 # X=wider+k,Y=0 to X=0,Y=k+1
84 # dSum = 0-(wider+k) + (k+1)-0
85 # = -wider-k + k + 1
86 # = 1-wider
87 sub dsumxy_minimum {
88 0 ▶ my ($self) = @_;
Subroutine dsumxy_minimum: not called CC=1 SCAR=6.9
Pod documented
89 0 return min(-1, 1-$self->{'wider'});
90 }
91 2 ▶use constant dsumxy_maximum => 1; # East along top
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 # dDiffXY east dX=1,dY=0 for dDiffXY=1-0 = 1
94 # south dX=0,dY=-1 for dDiffXY=0-(-1) = 1
95 # gnomon up to start of next gnomon is
96 # X=wider+k,Y=0 to X=0,Y=k+1
97 # dDiffXY = 0-(wider+k) - ((k+1)-0)
98 # = -wider - 2*k - 1 unbounded negative
99 2 ▶use constant ddiffxy_maximum => 1; # East along top
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 # use constant dir_minimum_dxdy => (1,0); # East at N=2
102 2 ▶use constant dir_maximum_dxdy => (0,-1); # South at N=3
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 sub turn_any_left {
105 0 ▶ my ($self) = @_;
Subroutine turn_any_left: not called CC=1 SCAR=6.9
Pod documented
106 0 return ($self->{'wider'} != 0); # wider=0 no left, right or straight always
107 }
108 sub _UNDOCUMENTED__turn_any_left_at_n {
109 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_left_at_n: not called CC=2 SCAR=17.9
110 0 ▶ return ($self->{'wider'} ? $self->n_start + $self->{'wider'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'wider'} ? : 0 0 0
111 : undef); # wider=0 no left
112 }
113 sub _UNDOCUMENTED__turn_any_right_at_n {
114 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: not called CC=1 SCAR=6.9
115 0 return $self->n_start + $self->{'wider'} + 1;
116 }
117  
118  
119 #------------------------------------------------------------------------------
120  
121 # same as PyramidSides, just 45 degress around
122  
123 sub new {
124 57 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=3 SCAR=11.0
Pod documented
125 57 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 57 0 57
126 57 $self->{'n_start'} = $self->default_n_start;
127 }
128 57 ▶ $self->{'wider'} ||= 0; # default
Condition100%2/2
$self->{"wider"} ||= 0
l!l
525
MC/DC100%1/1
$self->{"wider"} ||= 0
$self->{"wider"}
Truth table100%2/2
A
A= $self->{"wider"}
Aresult
11
00
129 57 return $self;
130 }
131  
132 sub n_to_xy {
133 101 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=3 SCAR=11.0
Pod documented
134 ### Corner n_to_xy: $n
135  
136 # adjust to N=1 at origin X=0,Y=0
137 101 $n = $n - $self->{'n_start'} + 1;
138  
139 # comparing $n<0.5, but done in integers for the benefit of Math::BigInt
140 101 ▶ if (2*$n < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $n < 1) 1 100 101
141 1 return;
142 }
143  
144 100 my $wider = $self->{'wider'};
145  
146 # wider==0
147 # vertical at X=0 has N=1, 2, 5, 10, 17, 26
148 # but start 0.5 back so at X=-0.5 have N=0.5, 1.5, 4.5, 9.5, 16.5, 25.5
149 # N = (Y^2 + 1/2)
150 # Y = floor sqrt(N - 1/2)
151 # = floor sqrt(4*N - 2)/2 staying in integers for the sqrt()
152 #
153 # wider==1
154 # 0.5 back so at X=-0.5 have N=0.5, 2.5, 6.5, 12.5
155 # N = (Y^2 + Y + 1/2)
156 # Y = floor -1/2 + sqrt(N - 1/4)
157 # = floor (-1 + sqrt(4*N - 1))/2
158 #
159 # wider==2
160 # 0.5 back so at X=-0.5 have N=0.5, 3.5, 8.5, 15.5
161 # N = (Y^2 + 2 Y + 1/2)
162 # Y = floor -1 + sqrt(N + 1/2)
163 # = floor (-2 + sqrt(4*N + 2))/2
164 #
165 # wider==3
166 # 0.5 back so at X=-0.5 have N=0.5, 4.5, 10.5, 18.5
167 # N = (Y^2 + 3 Y + 1/2)
168 # Y = floor -3/2 + sqrt(N + 7/4)
169 # = floor (-3 + sqrt(4*N + 7))/2
170 #
171 # 0,1,4,9
172 # my $y = int((sqrt(4*$n + -1) - $wider) / 2);
173 # ### y frac: (sqrt(4*$n + -1) - $wider) / 2
174  
175 100 my $y = int((_sqrtint(4*$n + $wider*$wider - 2) - $wider) / 2);
176 ### y frac: (sqrt(int(4*$n) + $wider*$wider - 2) - $wider) / 2
177 ### $y
178  
179 # diagonal at X=Y has N=1, 3, 7, 13, 21
180 # N = ((Y + 1)*Y + (Y+1)*wider + 1)
181 # = ((Y + 1 + wider)*Y + wider + 1)
182 # so subtract that leaving N negative on the horizontal part, or positive
183 # for the downward vertical part
184 #
185 100 $n -= $y*($y+1+$wider) + $wider + 1;
186 ### corner n: $y*($y+1+$wider) + $wider + 1
187 ### rem: $n
188 ### assert: $n!=$n || $n >= -($y+$wider+0.5)
189 # ### assert: $n <= ($y+0.5)
190  
191 100 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) { } 68 32 100
192 # top horizontal
193 68 return ($n + $y+$wider,
194 $y);
195 } else {
196 # right vertical
197 32 return ($y+$wider,
198 -$n + $y);
199 }
200 }
201  
202 sub xy_to_n {
203 392 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=4 SCAR=14.4
Pod documented
204 ### Corner xy_to_n(): "$x,$y"
205  
206 392 $x = round_nearest ($x);
207 392 $y = round_nearest ($y);
208 392 ▶ if ($x < 0 || $y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 392 392
Condition33%1/3
$x < 0 or $y < 0
l!l&&r!l&&!r
00392
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
209 0 return undef;
210 }
211  
212 392 my $wider = $self->{'wider'};
213 392 my $xw = $x - $wider;
214 392 ▶ if ($y >= $xw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= $xw) { } 313 79 392
215 ### top edge, N left is: $y*$y + $wider*$y + 1
216 return ($y*$y + $wider*$y # Y axis N value
217 + $x # plus X offset across
218 313 + $self->{'n_start'});
219 } else {
220 ### right vertical, N diag is: $xw*$xw + $xw*$wider
221 ### $xw
222 # Ndiag = Nleft + Y+w
223 # N = xw*xw + w*xw + 1 + xw+w + (xw - y)
224 # = xw*xw + w*xw + 1 + xw+w + xw - y
225 # = xw*xw + xw*(w+2) + 1 + w - y
226 # = xw*(xw + w+2) + w+1 - y
227 return ($xw*($xw+$wider+2) + $wider
228 - $y
229 79 + $self->{'n_start'});
230 }
231 }
232  
233 # exact
234 sub rect_to_n_range {
235 146 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=10 SCAR=25.1
Pod documented
236 ### Corner rect_to_n_range(): "$x1,$y1, $x2,$y2"
237  
238 146 $x1 = round_nearest ($x1);
239 146 $y1 = round_nearest ($y1);
240 146 $x2 = round_nearest ($x2);
241 146 $y2 = round_nearest ($y2);
242 146 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 146 146
243 146 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 146 146
244  
245 146 ▶ if ($y2 < 0 || $x2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < 0 or $x2 < 0) 0 146 146
Condition33%1/3
$y2 < 0 or $x2 < 0
l!l&&r!l&&!r
00146
MC/DC0%0/2
$y2 < 0 or $x2 < 0
$y2 < 0 $x2 < 0
Truth table33%1/3
A or B
A= $y2 < 0B= $x2 < 0
ABresult
1X1
011
000
246 0 return (1, 0); # rect all negative, no N
247 }
248  
249 146 ▶ if ($x1 < 0) { $x1 *= 0; } # "*=" to preserve bigint x1 or y1
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 0 146 146
250 146 ▶ if ($y1 < 0) { $y1 *= 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 146 146
251  
252 146 my $wider = $self->{'wider'};
253 146 my $ylo = $y1;
254 146 my $xw = $x1 - $wider;
255  
256 146 ▶ if ($y1 <= $xw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 <= $xw) 47 99 146
257 # left column is partly or wholly below X=Y diagonal
258 #
259 # make x1,y1 the min pos
260 47 ▶ $y1 = ($y2 < $xw
Branch: 1 branch
BranchTrueFalseTotal
$y2 < $xw ? : 20 27 47
261  
262 # wholly below diag, min "*" is at top y2 of the x1 column
263 #
264 # | /
265 # | /
266 # | / *------+ y2
267 # | / | |
268 # | / +------+ y1
269 # | / x1 x2
270 # +------------------
271 # ^.....^
272 # wider xw
273 #
274 ? $y2
275  
276 # only partly below diag, min "*" is the X=Y+wider diagonal at x1
277 #
278 # /
279 # | +------+ y2
280 # | | / |
281 # | |/ |
282 # | * |
283 # | /| |
284 # | / +------+ y1
285 # | / x1 x2
286 # +------------------
287 # ^...^xw
288 # wider
289 #
290 : $xw);
291 }
292  
293 146 ▶ if ($y2 <= $x2 - $wider) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 <= $x2 - $wider) 54 92 146
294 # right column entirely at or below X=Y+wider diagonal so max is at the
295 # ylo bottom end of the column
296 #
297 # | /
298 # | --/---+ y2
299 # | | / |
300 # | |/ |
301 # | / |
302 # | /| |
303 # | / +------+ ylo
304 # | / x2
305 # +------------------
306 # ^
307 # wider
308 #
309 54 $y2 = $ylo; # x2,y2 now the max pos
310 }
311  
312 ### min xy: "$x1,$y1"
313 ### max xy: "$x2,$y2"
314 146 return ($self->xy_to_n ($x1,$y1),
315 $self->xy_to_n ($x2,$y2));
316 }
317  
318 #------------------------------------------------------------------------------
319  
320 sub _NOTDOCUMENTED_n_to_figure_boundary {
321 36 ▶ my ($self, $n) = @_;
Subroutine _NOTDOCUMENTED_n_to_figure_boundary: called CC=4 SCAR=13.9
322 ### _NOTDOCUMENTED_n_to_figure_boundary(): $n
323  
324 # adjust to N=1 at origin X=0,Y=0
325 36 $n = $n - $self->{'n_start'} + 1;
326  
327 36 ▶ if ($n < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 36 36
328 0 return undef;
329 }
330  
331 36 my $wider = $self->{'wider'};
332 36 ▶ if ($n <= $wider) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $wider) 4 32 36
333 # single block row
334 # +---+-----+----+
335 # | 1 | ... | $n | boundary = 2*N + 2
336 # +---+-----+----+
337 4 return 2*$n + 2;
338 }
339  
340 32 my $d = int((_sqrtint(4*$n + $wider*$wider - 2) - $wider) / 2);
341 ### $d
342 ### $wider
343  
344 32 ▶ if ($n > $d*($d+1+$wider) + $wider) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $d * ($d + 1 + $wider) + $wider) 17 15 32
345 17 $wider++;
346 ### increment for +2 after turn ...
347 }
348 32 return 4*$d + 2*$wider + 2;
349 }
350  
351 #------------------------------------------------------------------------------
352 2 1;
353 __END__
« PlanePath/CellularRule190.pm ↑ Summary PlanePath/KochPeaks.pm »