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