PlanePath/StaircaseAlternating.pm

filter: Statementstmt 87.3% 131 / 150 Branchbran 79.6% 51 / 64 Conditioncond 92.0% 46 / 50 MC/DCmcdc 85.7% 24 / 28 Subroutinesub 80.9% 17 / 21 Podpod 100% 7 / 7 totaltotal 86.2% 276 / 320 CC 50 SCAR 28.8
CC
50
Cov
86%
rect_to_n_range
33.1
xy_to_n
9.2
n_to_xy
8.6
CRAP 17.9
« PlanePath/TriangleSpiralSkewed.pm ↑ Summary NumSeq/PlanePathCoord.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  
20 # math-image --path=StaircaseAlternating --all --output=numbers_dash --size=70x30
21 # math-image --path=StaircaseAlternating,end_type=square --all --output=numbers_dash --size=70x30
22  
23 package Math::PlanePath::StaircaseAlternating;
24 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 $VERSION = 128;
29 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 @ISA = ('Math::PlanePath');
31  
32 use Math::PlanePath::Base::Generic
33 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
36  
37 # uncomment this to run the ### lines
38 #use Smart::Comments;
39  
40  
41 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 my %n_frac_discontinuity = (jump => .5);
45 sub n_frac_discontinuity {
46 0 ▶ my ($self) = @_;
Subroutine n_frac_discontinuity: not called CC=1 SCAR=6.9
Pod documented
47 0 return $n_frac_discontinuity{$self->{'end_type'}};
48 }
49  
50 1 use constant parameter_info_array =>
51 [ { name => 'end_type',
52 share_key => 'end_type_jumpsquare',
53 display => 'Type',
54 type => 'enum',
55 default => 'jump',
56 choices => ['jump','square'],
57 choices_display => ['Jump','Wquare'],
58 },
59 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
60 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62  
63 #------------------------------------------------------------------------------
64 1 ▶use constant dx_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 {
66 1 my %dx_maximum = (jump => 2,
67 square => 1);
68 sub dx_maximum {
69 0 ▶ my ($self) = @_;
Subroutine dx_maximum: not called CC=1 SCAR=6.9
Pod documented
70 0 return $dx_maximum{$self->{'end_type'}};
71 }
72 }
73 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 *dy_maximum = \&dx_maximum;
75 sub _UNDOCUMENTED__dxdy_list {
76 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list: not called CC=2 SCAR=17.9
77 0 ▶ return ($self->{'end_type'} eq 'jump'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'end_type'} eq 'jump' ? : 0 0 0
78 ? (1,0, # E
79 2,0, # E by 2
80 0,1, # N
81 0,2, # N by 2
82 -1,0, # W
83 0,-1) # S
84 : Math::PlanePath::Base::NSEW->_UNDOCUMENTED__dxdy_list);
85 }
86  
87 1 ▶use constant dsumxy_minimum => -1; # straight S
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 *dsumxy_maximum = \&dx_maximum;
89  
90 {
91 1 my %dDiffXY_max = (jump => -2,
92 square => -1);
93 sub ddiffxy_minimum {
94 0 ▶ my ($self) = @_;
Subroutine ddiffxy_minimum: not called CC=1 SCAR=6.9
Pod documented
95 0 return $dDiffXY_max{$self->{'end_type'}};
96 }
97 }
98 1 *ddiffxy_maximum = \&dx_maximum;
99  
100 1 ▶use constant dir_maximum_dxdy => (0,-1); # South
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102  
103 #------------------------------------------------------------------------------
104 sub new {
105 6 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=3 SCAR=11.0
Pod documented
106 6 ▶ $self->{'end_type'} ||= 'jump';
Condition100%2/2
$self->{"end_type"} ||= "jump"
l!l
42
MC/DC100%1/1
$self->{"end_type"} ||= "jump"
$self->{"end_type"}
Truth table100%2/2
A
A= $self->{"end_type"}
Aresult
11
00
107 6 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 6 0 6
108 6 $self->{'n_start'} = $self->default_n_start;
109 }
110 6 return $self;
111 }
112  
113 # --16
114 # |
115 # 17--18
116 # |
117 # --15 19--20
118 # | |
119 # 14--13 21--22
120 # | |
121 # --2 12--11 23--24 34--33
122 # | | | |
123 # 3-- 4 10-- 9 25--26 32--31
124 # | | | |
125 # --1 5-- 6 8-- 7 27--28 30--29
126  
127 # 17
128 # |\
129 # 16 18
130 # | |
131 # 15 19-20
132 # | |
133 # 14-13 21-22
134 # | |
135 # 3 12-11 23-24 34-33
136 # |\ | | |
137 # 2 4 10--9 25-26 32-31
138 # | | \ | \
139 # 1 5--6--7--8 27-28-29-30
140  
141 # .
142 #
143 # 42-43
144 # | |
145 # 41 44-45
146 # | |
147 # 40-39 46-47
148 # | |
149 # . 38-37 48-
150 # |
151 # 14-15 35-36
152 # | | |
153 # 13 16-17 34-33
154 # | | |
155 # 12-11 18-19 32-31
156 # | | |
157 # . 10--9 20-21 30-29
158 # | | |
159 # 2--3 8--7 22-23 28-27
160 # | | | | |
161 # 1 4--5--6 . 24-25-26 .
162 #
163 # start from integer vertical
164 # d = [ 2, 3, 4, 5 ]
165 # N = [ 5, 13, 25, 41 ]
166 # N = (2 d^2 - 2 d + 1)
167 # = ((2*$d - 2)*$d + 1)
168 # d = 1/2 + sqrt(1/2 * $n + -1/4)
169 # = (1 + sqrt(2*$n - 1)) / 2
170 #
171  
172 sub n_to_xy {
173 6 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=8 SCAR=21.6
Pod documented
174 #### StaircaseAlternating n_to_xy: $n
175  
176 # adjust to N=1 at origin X=0,Y=0
177 6 $n = $n - $self->{'n_start'} + 1;
178  
179 6 my $d;
180 6 ▶ if ($self->{'end_type'} eq 'square') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'end_type'} eq 'square') { } 6 0 6
181 6 ▶ if ($n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 6 6
182  
183 6 $d = int( (1 + _sqrtint(2*$n-1)) / 2 );
184 6 $n -= (2*$d - 2)*$d;
185 ### $d
186 ### remainder n: $n
187  
188 6 ▶ if ($n < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 2) 2 4 6
189 2 ▶ if ($d % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d % 2) { } 1 1 2
190 1 return (0, $n+2*$d-3);
191 } else {
192 1 return ($n+2*$d-3, 0);
193 }
194 }
195  
196 } else {
197 0 ▶ if (2*$n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $n < 1) 0 0 0
198  
199 0 $d = int ((1 + _sqrtint(8*$n-3)) / 4);
200 0 $n -= (2*$d - 1)*$d;
201 ### rem: $n
202 }
203  
204 4 my $int = int($n);
205 4 my $frac = $n - $int;
206 4 my $r = int($int/2);
207  
208 4 my ($x,$y);
209 4 ▶ if ($int % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int % 2) { } 1 3 4
210 ### down ...
211 1 $x = $r;
212 1 $y = -$frac + 2*$d - $r;
213 } else {
214 ### across ...
215 3 $x = $frac + $r-1;
216 3 $y = 2*$d - $r;
217 }
218  
219 4 ▶ if ($d % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d % 2) { } 3 1 4
220 3 return ($x,$y);
221 } else {
222 1 return ($y,$x);
223 }
224 }
225  
226 sub xy_to_n {
227 179988 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=9 SCAR=22.2
Pod documented
228 ### StaircaseAlternating xy_to_n(): "$x,$y"
229  
230 179988 $x = round_nearest ($x);
231 179988 $y = round_nearest ($y);
232 179988 ▶ if ($x < 0 || $y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 179988 179988
Condition33%1/3
$x < 0 or $y < 0
l!l&&r!l&&!r
00179988
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
233 0 return undef;
234 }
235  
236 179988 my $jump = ($self->{'end_type'} ne 'square');
237 179988 ▶ unless ($jump) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jump) 89988 90000 179988
238 # square omitted endpoints
239 89988 ▶ if ($x == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x == 0) { } 1698 88290 89988
elsif ($y == 0 and $x % 4 == 0) { } 322 174596 174918
240 1698 ▶ if (($y % 4) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y % 4 == 2) 335 1363 1698
241 335 return undef;
242 }
243 ▶ } elsif ($y == 0 && ($x % 4) == 0) {
Condition100%3/3
$y == 0 and $x % 4 == 0
!ll&&!rl&&r
866281340322
MC/DC100%2/2
$y == 0 and $x % 4 == 0
$y == 0 $x % 4 == 0
Truth table100%3/3
A and B
A= $y == 0B= $x % 4 == 0
ABresult
0X0
100
111
244 322 return undef;
245 }
246 }
247  
248 179331 my $d = int(($x + $y + 1) / 2);
249 return ((2*$d + $jump) * $d
250 + ($d % 2
251 ? $x - $y
252 : $y - $x)
253 179331 ▶ + $self->{'n_start'});
Branch: 1 branch
BranchTrueFalseTotal
$d % 2 ? : 89676 89655 179331
254 }
255  
256 # 12--11 18--19 14--13 21--22
257 # | | | |
258 # . 10-- 9 20 2 12--11 23
259 # | | |
260 # 2-- 3 8-- 7 3-- 4 10-- 9
261 # | | | | |
262 # 1 4-- 5-- 6 1 5-- 6 8
263 #
264 1 my @yx_to_min_dx = (0, 0, 0, -1,
265 0, 0, 1, 0,
266 0, 1, 0, 0,
267 1, 0, 0, 0);
268 1 my @yx_to_min_dy = (0, 1, 0, 0,
269 -1, 0, 0, 0,
270 0, 0, 0, 1,
271 0, 0, 1, 0);
272  
273 1 my @yx_to_max_dx = (1, 0, 0, 0,
274 0, 0, 0, 1,
275 0, 0, 1, 0,
276 0, 1, 0, 0);
277 1 my @yx_to_max_dy = (0, 0, 1, 0,
278 0, 1, 0, 0,
279 1, 0, 0, 0,
280 0, 0, 0, 1);
281  
282 # exact
283 sub rect_to_n_range {
284 20000 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=32 SCAR=35.0
Pod documented
285 ### StaircaseAlternating rect_to_n_range(): "$x1,$y1 $x2,$y2"
286  
287 20000 $x1 = round_nearest ($x1);
288 20000 $y1 = round_nearest ($y1);
289 20000 $x2 = round_nearest ($x2);
290 20000 $y2 = round_nearest ($y2);
291 20000 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); } # x2 > x1
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 20000 20000
292 20000 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # y2 > y1
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 20000 20000
293  
294 20000 ▶ if ($x2 < 0 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0) 0 20000 20000
Condition33%1/3
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
0020000
MC/DC0%0/2
$x2 < 0 or $y2 < 0
$x2 < 0 $y2 < 0
Truth table33%1/3
A or B
A= $x2 < 0B= $y2 < 0
ABresult
1X1
011
000
295 ### entirely outside first quadrant ...
296 0 return (1, 0);
297 }
298  
299 # not less than 0,0
300 20000 ▶ if ($x1 < 0) { $x1 *= 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 0 20000 20000
301 20000 ▶ if ($y1 < 0) { $y1 *= 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 20000 20000
302  
303 20000 my $corner_x1 = $x1;
304 20000 my $corner_y1 = $y1;
305 20000 my $corner_x2 = $x2;
306 20000 my $corner_y2 = $y2;
307 {
308 20000 my $key = 4*($y2 % 4) + ($x2 % 4);
309 20000 ▶ if ($x2 > $x1 && $yx_to_max_dx[$key]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x2 > $x1 and $yx_to_max_dx[$key]) { } 3752 16248 20000
elsif ($y2 > 0 and $y2 > $y1) { } 12184 4228 16412
Condition100%3/3
$x2 > $x1 and $yx_to_max_dx[$key]
!ll&&!rl&&r
5000112483752
MC/DC100%2/2
$x2 > $x1 and $yx_to_max_dx[$key]
$x2 > $x1 $yx_to_max_dx[$key]
Truth table100%3/3
A and B
A= $x2 > $x1B= $yx_to_max_dx[$key]
ABresult
0X0
100
111
310 3752 $corner_x2 -= 1;
311 ▶ } elsif ($y2 > 0 && $y2 > $y1) {
Condition100%3/3
$y2 > 0 and $y2 > $y1
!ll&&!rl&&r
164390012184
MC/DC100%2/2
$y2 > 0 and $y2 > $y1
$y2 > 0 $y2 > $y1
Truth table100%3/3
A and B
A= $y2 > 0B= $y2 > $y1
ABresult
0X0
100
111
312 12184 $corner_y2 -= $yx_to_max_dy[$key];
313 }
314 }
315  
316 20000 my $square = ($self->{'end_type'} eq 'square');
317 20000 ▶ if ($square && $x1 == 0 && ($y1 % 4) == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($square and $x1 == 0 and $y1 % 4 == 2) { } 96 19904 20000
elsif ($square and $y1 == 0 and $x1 > 0 and $x1 % 4 == 0) { } 96 39328 39424
Condition100%6/6
$square and $x1 == 0 and $y1 % 4 == 2
!ll&&!rl&&r
1960030496
$square and $x1 == 0
!ll&&!rl&&r
100009600400
MC/DC100%3/3
$square and $x1 == 0 and $y1 % 4 == 2
$square $x1 == 0 $y1 % 4 == 2
Truth table100%4/4
A and B and C
A= $squareB= $x1 == 0C= $y1 % 4 == 2
ABCresult
0XX0
10X0
1100
1111
318 ### x1,y1 is an omitted Y axis point ...
319 96 ▶ if ($corner_x1 < $x2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($corner_x1 < $x2) { } 72 24 96
elsif ($corner_y1 < $y2) { } 18 6 24
320 72 $corner_x1 += 1;
321 } elsif ($corner_y1 < $y2) {
322 18 $corner_y1 += 1;
323 } else {
324 ### only this point ...
325 6 return (1, 0);
326 }
327  
328 ▶ } elsif ($square && $y1 == 0 && $x1 > 0 && ($x1 % 4) == 0) {
Condition100%9/9
$square and $y1 == 0 and $x1 > 0 and $x1 % 4 == 0
!ll&&!rl&&r
1952028896
$square and $y1 == 0 and $x1 > 0
!ll&&!rl&&r
1950416384
$square and $y1 == 0
!ll&&!rl&&r
100009504400
MC/DC100%4/4
$square and $y1 == 0 and $x1 > 0 and $x1 % 4 == 0
$square $y1 == 0 $x1 > 0 $x1 % 4 == 0
Truth table100%5/5
A and B and C and D
A= $squareB= $y1 == 0C= $x1 > 0D= $x1 % 4 == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
329 96 ▶ if ($corner_y1 < $y2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($corner_y1 < $y2) { } 72 24 96
elsif ($corner_x1 < $x2) { } 18 6 24
330 72 $corner_y1 += 1;
331 } elsif ($corner_x1 < $x2) {
332 18 $corner_x1 += 1;
333 } else {
334 ### only an omitted X axis point ...
335 6 return (1, 0);
336 }
337  
338 }
339 {
340 19988 my $key = 4*($corner_y1 % 4) + ($corner_x1 % 4);
341 ### min key: $key
342 19988 ▶ if ($corner_x1 < $x2 && (my $dx = $yx_to_min_dx[$key])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($corner_x1 < $x2 and my $dx = $yx_to_min_dx[$key]) { } 3792 16196 19988
elsif ($corner_y1 < $y2 and my $dy = $yx_to_min_dy[$key]) { } 3792 16474 20266
Condition100%3/3
$corner_x1 < $x2 and my $dx = $yx_to_min_dx[$key]
!ll&&!rl&&r
5018111783792
MC/DC100%2/2
$corner_x1 < $x2 and my $dx = $yx_to_min_dx[$key]
$corner_x1 < $x2 my $dx = $yx_to_min_dx[$key]
Truth table100%3/3
A and B
A= $corner_x1 < $x2B= my $dx = $yx_to_min_dx[$key]
ABresult
0X0
100
111
343 ### x1 incr ...
344 3792 ▶ unless ($square && $dx < 0 && $corner_y1 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($square and $dx < 0 and $corner_y1 == 0) 1848 72 1920
Condition100%6/6
$square and $dx < 0 and $corner_y1 == 0
!ll&&!rl&&r
328843272
$square and $dx < 0
!ll&&!rl&&r
18721416504
MC/DC100%3/3
$square and $dx < 0 and $corner_y1 == 0
$square $dx < 0 $corner_y1 == 0
Truth table100%4/4
A and B and C
A= $squareB= $dx < 0C= $corner_y1 == 0
ABCresult
0XX0
10X0
1100
1111
345 3720 $corner_x1 += 1;
346 }
347 ▶ } elsif ($corner_y1 < $y2 && (my $dy = $yx_to_min_dy[$key])) {
Condition100%3/3
$corner_y1 < $y2 and my $dy = $yx_to_min_dy[$key]
!ll&&!rl&&r
407083343792
MC/DC100%2/2
$corner_y1 < $y2 and my $dy = $yx_to_min_dy[$key]
$corner_y1 < $y2 my $dy = $yx_to_min_dy[$key]
Truth table100%3/3
A and B
A= $corner_y1 < $y2B= my $dy = $yx_to_min_dy[$key]
ABresult
0X0
100
111
348 ### y1 incr ...
349 3792 ▶ unless ($square && $dy < 0 && $corner_x1 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($square and $dy < 0 and $corner_x1 == 0) 1848 72 1920
Condition100%6/6
$square and $dy < 0 and $corner_x1 == 0
!ll&&!rl&&r
324048072
$square and $dy < 0
!ll&&!rl&&r
18721368552
MC/DC100%3/3
$square and $dy < 0 and $corner_x1 == 0
$square $dy < 0 $corner_x1 == 0
Truth table100%4/4
A and B and C
A= $squareB= $dy < 0C= $corner_x1 == 0
ABCresult
0XX0
10X0
1100
1111
350 3720 $corner_y1 += 1;
351 }
352 }
353 }
354  
355 ### corners: "$x1,$y1 $x2,$y2"
356  
357 19988 return ($self->xy_to_n($corner_x1,$corner_y1),
358 $self->xy_to_n($corner_x2,$corner_y2));
359 }
360  
361 # inexact but easier ...
362 #
363 # if ($self->{'end_type'} eq 'square') {
364 # $x2 += $y2 + 1;
365 # $x2 = int($x2/2);
366 # return (1,
367 # (2*$x2+2)*$x2 + 1);
368 # } else {
369 # $x2 += $y2 + 2;
370 # return (1,
371 # $x2*($x2+1)/2);
372 # }
373  
374 1 1;
375 __END__
« PlanePath/TriangleSpiralSkewed.pm ↑ Summary NumSeq/PlanePathCoord.pm »