PlanePath/Diagonals.pm

filter: Statementstmt 65.1% 71 / 109 Branchbran 21.4% 9 / 42 Conditioncond 46.6% 7 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 51.7% 15 / 29 Podpod 100% 17 / 17 totaltotal 54.3% 120 / 221 CC 28 SCAR 19.8
CC
28
Cov
52%
rect_to_n_range
30.0
rsquared_minimum
12.0
new
8.0
CRAP 7.2
« PlanePath/AlternateTerdragon.pm ↑ Summary PlanePath/SacksSpiral.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 # Leading diagonal 2,8,18 = 2*d^2
20 # cf A185787 lists numerous seqs for rows,columns,diagonals
21  
22  
23 package Math::PlanePath::Diagonals;
24 3 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 #use List::Util 'max';
28 3 *max = \&Math::PlanePath::_max;
29  
30 3 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 $VERSION = 128;
32 3 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 @ISA = ('Math::PlanePath');
34  
35 use Math::PlanePath::Base::Generic
36 3 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 *_sqrtint = \&Math::PlanePath::_sqrtint;
38  
39 # uncomment this to run the ### lines
40 # use Smart::Comments;
41  
42 3 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 3 use constant parameter_info_array =>
47 [ { name => 'direction',
48 share_key => 'direction_downup',
49 display => 'Direction',
50 type => 'enum',
51 default => 'down',
52 choices => ['down','up'],
53 choices_display => ['Down','Up'],
54 description => 'Number points downwards or upwards along the diagonals.',
55 },
56 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
57 { name => 'x_start',
58 display => 'X start',
59 type => 'integer',
60 default => 0,
61 width => 3,
62 description => 'Starting X coordinate.',
63 },
64 { name => 'y_start',
65 display => 'Y start',
66 type => 'integer',
67 default => 0,
68 width => 3,
69 description => 'Starting Y coordinate.',
70 },
71 3 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 sub x_minimum {
74 0 ▶ my ($self) = @_;
Subroutine x_minimum: not called CC=1 SCAR=6.9
Pod documented
75 0 return $self->{'x_start'};
76 }
77 sub y_minimum {
78 0 ▶ my ($self) = @_;
Subroutine y_minimum: not called CC=1 SCAR=6.9
Pod documented
79 0 return $self->{'y_start'};
80 }
81  
82 sub dx_minimum {
83 0 ▶ my ($self) = @_;
Subroutine dx_minimum: not called CC=2 SCAR=17.9
Pod documented
84 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
85 ? undef # down jumps back unlimited at bottom
86 : -1); # up at most -1 across
87 }
88 sub dx_maximum {
89 0 ▶ my ($self) = @_;
Subroutine dx_maximum: not called CC=2 SCAR=17.9
Pod documented
90 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
91 ? 1 # down at most +1 across
92 : undef); # up jumps back across unlimited at top
93 }
94  
95 sub dy_minimum {
96 0 ▶ my ($self) = @_;
Subroutine dy_minimum: not called CC=2 SCAR=17.9
Pod documented
97 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
98 ? -1 # down at most -1
99 : undef); # up jumps down unlimited at top
100 }
101 sub dy_maximum {
102 0 ▶ my ($self) = @_;
Subroutine dy_maximum: not called CC=2 SCAR=17.9
Pod documented
103 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
104 ? undef # down jumps up unlimited at bottom
105 : 1); # up at most +1
106 }
107  
108 sub absdx_minimum {
109 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=2 SCAR=17.9
Pod documented
110 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
111 ? 0 # N=1 dX=0,dY=1
112 : 1); # otherwise always changes
113 }
114 sub absdy_minimum {
115 0 ▶ my ($self) = @_;
Subroutine absdy_minimum: not called CC=2 SCAR=17.9
Pod documented
116 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
117 ? 1 # otherwise always changes
118 : 0); # N=1 dX=1,dY=0
119 }
120  
121 # within diagonal X+Y=k is dSum=0
122 # end of diagonal X=Xstart+k Y=Ystart
123 # to X=Xstart Y=Ystart+k+1
124 # is (Xstart + Ystart+k+1) - (Xstart+k + Ystart) = 1 always, to next diagonal
125 #
126 3 ▶use constant dsumxy_minimum => 0; # advancing diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
127 3 ▶use constant dsumxy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 sub ddiffxy_minimum {
130 0 ▶ my ($self) = @_;
Subroutine ddiffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
131 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
132 ? undef # "down" jumps back unlimited at bottom
133 : -2); # NW diagonal
134 }
135 sub ddiffxy_maximum {
136 0 ▶ my ($self) = @_;
Subroutine ddiffxy_maximum: not called CC=2 SCAR=17.9
Pod documented
137 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
138 ? 2 # SE diagonal
139 : undef); # "up" jumps down unlimited at top
140 }
141  
142 sub dir_minimum_dxdy {
143 0 ▶ my ($self) = @_;
Subroutine dir_minimum_dxdy: not called CC=2 SCAR=17.9
Pod documented
144 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
145 ? (0,1) # North, vertical at N=1
146 : (1,0)); # East, horiz at N=1
147 }
148 sub dir_maximum_dxdy {
149 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=2 SCAR=17.9
Pod documented
150 0 ▶ return ($self->{'direction'} eq 'down'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
151 ? (1,-1) # South-East at N=2
152 : (2,-1)); # ESE at N=3
153 }
154  
155 # If Xstart>0 or Ystart>0 then the origin is not reached.
156 sub rsquared_minimum {
157 0 ▶ my ($self) = @_;
Subroutine rsquared_minimum: not called CC=3 SCAR=24.8
Pod documented
158 return (( $self->{'x_start'} > 0 ? $self->{'x_start'}**2 : 0)
159 0 ▶ + ($self->{'y_start'} > 0 ? $self->{'y_start'}**2 : 0));
Branch: 2 branches
BranchTrueFalseTotal
$self->{'x_start'} > 0 ? : 0 0 0
$self->{'y_start'} > 0 ? : 0 0 0
160 }
161  
162  
163  
164 #------------------------------------------------------------------------------
165  
166 sub new {
167 6 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=7 SCAR=20.8
Pod documented
168 6 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 6 0 6
169 6 $self->{'n_start'} = $self->default_n_start;
170 }
171  
172 6 ▶ my $direction = ($self->{'direction'} ||= 'down');
Condition100%2/2
$self->{"direction"} ||= "down"
l!l
15
MC/DC100%1/1
$self->{"direction"} ||= "down"
$self->{"direction"}
Truth table100%2/2
A
A= $self->{"direction"}
Aresult
11
00
173 6 ▶ if (! ($direction eq 'up' || $direction eq 'down')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($direction eq "up" or $direction eq "down") 0 6 6
Condition66%2/3
$direction eq "up" or $direction eq "down"
l!l&&r!l&&!r
150
MC/DC0%0/2
$direction eq "up" or $direction eq "down"
$direction eq "up" $direction eq "down"
Truth table66%2/3
A or B
A= $direction eq "up"B= $direction eq "down"
ABresult
1X1
011
000
174 0 croak "Unrecognised direction option: ", $direction;
175 }
176  
177 6 ▶ $self->{'x_start'} ||= 0;
Condition50%1/2
$self->{"x_start"} ||= 0
l!l
06
MC/DC0%0/1
$self->{"x_start"} ||= 0
$self->{"x_start"}
Truth table50%1/2
A
A= $self->{"x_start"}
Aresult
11
00
178 6 ▶ $self->{'y_start'} ||= 0;
Condition50%1/2
$self->{"y_start"} ||= 0
l!l
06
MC/DC0%0/1
$self->{"y_start"} ||= 0
$self->{"y_start"}
Truth table50%1/2
A
A= $self->{"y_start"}
Aresult
11
00
179 6 return $self;
180 }
181  
182 # start each diagonal at 0.5 earlier than the integer point
183 # d = [ 0, 1, 2, 3, 4 ]
184 # n = [ -0.5, 0.5, 2.5, 5.5, 9.5 ]
185 # +1 +2 +3 +4
186 # 1 1 1
187 # N = (1/2 d^2 + 1/2 d - 1/2)
188 # = (1/2*$d**2 + 1/2*$d - 1/2)
189 # = ((1/2*$d + 1/2)*$d - 1/2)
190 # d = -1/2 + sqrt(2 * $n + 5/4)
191 # = (sqrt(8*$n + 5) -1)/2
192  
193 sub n_to_xy {
194 37 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=3 SCAR=11.0
Pod documented
195 ### Diagonals n_to_xy(): "$n ".(ref $n || '')
196  
197 # adjust to N=0 at origin X=0,Y=0
198 37 $n = $n - $self->{'n_start'};
199  
200 37 my $d;
201 {
202 37 my $r = 8*$n + 5;
203 37 ▶ if ($r < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < 1) 2 35 37
204 ### which is N < -0.5 ...
205 2 return;
206 }
207 ### sqrt of: "$r"
208 ### sqrt is: sqrt(int($r)).""
209  
210 35 $d = int((_sqrtint($r) - 1) / 2);
211 ### assert: $d >= 0
212 ### d: "$d"
213 ### $d
214 }
215  
216 # subtract for offset into diagonal, range -0.5 <= $n < $d+0.5
217 35 $n -= $d*($d+1)/2;
218 ### subtract to n: "$n"
219  
220 35 my $y = -$n + $d; # $n first so BigFloat not BigInt from $d
221 # and X=$n
222  
223 35 ▶ if ($self->{'direction'} eq 'up') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"direction"} eq "up") 14 21 35
224 14 ($n,$y) = ($y,$n);
225 }
226 return ($n + $self->{'x_start'},
227 35 $y + $self->{'y_start'});
228 }
229  
230 # round y on an 0.5 downwards so that x=-0.5,y=0.5 gives n=1 which is the
231 # inverse of n_to_xy() ... or is that inconsistent with other classes doing
232 # floor() always?
233 #
234 # d(d+1)/2+1
235 # = (d^2 + d + 2) / 2
236 #
237 sub xy_to_n {
238 34 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=4 SCAR=14.3
Pod documented
239 ### xy_to_n(): $x, $y
240 34 $x = $x - $self->{'x_start'}; # "-" operator to provoke warning if x==undef
241 34 $y = $y - $self->{'y_start'};
242 34 ▶ if ($self->{'direction'} eq 'up') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"direction"} eq "up") 14 20 34
243 14 ($x,$y) = ($y,$x);
244 }
245 34 $x = round_nearest ($x);
246 34 $y = round_nearest (- $y);
247 ### rounded
248 ### $x
249 ### $y
250 34 ▶ if ($x < 0 || $y > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y > 0) 0 34 34
Condition33%1/3
$x < 0 or $y > 0
l!l&&r!l&&!r
0034
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
251 0 return undef; # outside
252 }
253  
254 34 my $d = $x - $y;
255 ### $d
256 34 return $d*($d+1)/2 + $x + $self->{'n_start'};
257 }
258  
259 # bottom-left to top-right, used by DiagonalsAlternating too
260 # exact
261 sub rect_to_n_range {
262 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=5 SCAR=34.0
Pod documented
263  
264 0 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 0 0
265 0 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 0 0
266 0 ▶ if ($x2 < $self->{'x_start'} || $y2 < $self->{'y_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < $self->{"x_start"} or $y2 < $self->{"y_start"}) 0 0 0
Condition0%0/3
$x2 < $self->{"x_start"} or $y2 < $self->{"y_start"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$x2 < $self->{"x_start"} or $y2 < $self->{"y_start"}
$x2 < $self->{"x_start"} $y2 < $self->{"y_start"}
Truth table0%0/3
A or B
A= $x2 < $self->{"x_start"}B= $y2 < $self->{"y_start"}
ABresult
1X1
011
000
267 0 return (1, 0); # rect all negative, no N
268 }
269  
270 0 $x1 = max ($x1, $self->{'x_start'});
271 0 $y1 = max ($y1, $self->{'y_start'});
272  
273 # exact range bottom left to top right
274 0 return ($self->xy_to_n ($x1,$y1),
275 $self->xy_to_n ($x2,$y2));
276 }
277  
278 3 1;
279 __END__
« PlanePath/AlternateTerdragon.pm ↑ Summary PlanePath/SacksSpiral.pm »