PlanePath/Staircase.pm

filter: Statementstmt 76.2% 74 / 97 Branchbran 22.7% 5 / 22 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 94.7% 18 / 19 Podpod 100% 4 / 4 totaltotal 62.9% 102 / 162 CC 16 SCAR 37.8
CC
16
Cov
61%
rect_to_n_range
132.0
xy_to_n
3.3
n_to_xy
3.0
CRAP 43.8
« PlanePath/DivisibleColumns.pm ↑ Summary PlanePath/R5DragonCurve.pm »
1 # Copyright 2010, 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 package Math::PlanePath::Staircase;
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 $VERSION = 128;
25 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 @ISA = ('Math::PlanePath');
27 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
28 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
29  
30 use Math::PlanePath::Base::Generic
31 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # uncomment this to run the ### lines
34 #use Smart::Comments;
35  
36  
37 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1;
41  
42 1 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use constant dsumxy_minimum => -1; # straight S
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use constant dsumxy_maximum => 2; # next row
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant ddiffxy_maximum => 1; # straight S,E
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant dir_maximum_dxdy => (0,-1); # South
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 use constant parameter_info_array =>
50 [
51 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
52 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54  
55 #------------------------------------------------------------------------------
56  
57 sub new {
58 3 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=7.0
Pod documented
59 3 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 3 0 3
60 3 $self->{'n_start'} = $self->default_n_start;
61 }
62 3 return $self;
63 }
64  
65 # start from 0.5 back
66 # d = [ 0, 1, 2, 3 ]
67 # n = [ 1.5, 6.5, 15.5 ]
68 # n = ((2*$d - 1)*$d + 0.5)
69 # d = 1/4 + sqrt(1/2 * $n + -3/16)
70 #
71 # start from integer vertical
72 # d = [ 0, 1, 2, 3, 4 ]
73 # n = [ 1, 2, 7, 16, 29 ]
74 # n = ((2*$d - 1)*$d + 1)
75 # d = 1/4 + sqrt(1/2 * $n + -7/16)
76 # = [1 + sqrt(8*$n-7) ] / 4
77 #
78 sub n_to_xy {
79 25 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=3 SCAR=11.0
Pod documented
80 #### Staircase n_to_xy: $n
81  
82 # adjust to N=1 start
83 25 $n = $n - $self->{'n_start'} + 1;
84  
85 25 my $d;
86 {
87 25 my $r = 8*$n - 3;
88 25 ▶ if ($r < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < 1) 0 25 25
89 0 return; # N < 0.5, so before start of path
90 }
91 25 $d = int( (_sqrtint($r) + 1)/4 );
92 }
93 ### $d
94 ### base: ((2*$d - 1)*$d + 0.5)
95  
96 25 $n -= (2*$d - 1)*$d;
97 ### fractional: $n
98  
99 25 my $int = int($n);
100 25 $n -= $int;
101  
102 25 my $rem = _divrem_mutate ($int, 2);
103 25 ▶ if ($rem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rem) { } 14 11 25
104 ### down ...
105 14 return ($int,
106 -$n + 2*$d - $int);
107 } else {
108 ### across ...
109 11 return ($n + $int-1,
110 2*$d - $int);
111 }
112 }
113  
114 # d = [ 1 2, 3, 4 ]
115 # N = [ 2, 7, 16, 29 ]
116 # N = (2 d^2 - d + 1)
117 # and add 2*$d
118 # base = 2*d^2 - d + 1 + 2*d
119 # = 2*d^2 + d + 1
120 # = (2*$d + 1)*$d + 1
121 #
122 sub xy_to_n {
123 15 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=3 SCAR=12.0
Pod documented
124  
125 15 $x = round_nearest ($x);
126 15 $y = round_nearest ($y);
127 15 ▶ if ($x < 0 || $y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 15 15
Condition33%1/3
$x < 0 or $y < 0
l!l&&r!l&&!r
0015
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
128 0 return undef;
129 }
130 15 my $d = int(($x + $y + 1) / 2);
131 15 return (2*$d + 1)*$d - $y + $x + $self->{'n_start'};
132 }
133  
134 # exact
135 sub rect_to_n_range {
136 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=11 SCAR=48.8
Pod documented
137 ### Staircase rect_to_n_range(): "$x1,$y1 $x2,$y2"
138  
139 0 $x1 = round_nearest ($x1);
140 0 $y1 = round_nearest ($y1);
141 0 $x2 = round_nearest ($x2);
142 0 $y2 = round_nearest ($y2);
143  
144 0 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); } # x2 > x1
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 0 0
145 0 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # y2 > y1
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 0 0
146 0 ▶ if ($x2 < 0 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0) 0 0 0
Condition0%0/3
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x2 < 0 or $y2 < 0
$x2 < 0 $y2 < 0
Truth table0%0/3
A or B
A= $x2 < 0B= $y2 < 0
ABresult
1X1
011
000
147 0 return (1, 0); # nothing outside first quadrant
148 }
149  
150 0 ▶ if ($x1 < 0) { $x1 *= 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 0 0 0
151 0 ▶ if ($y1 < 0) { $y1 *= 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 0 0
152 0 my $y_min = $y1;
153  
154 0 ▶ if ((($x1 ^ $y1) & 1) && $y1 < $y2) { # y2==y_max
Branch: 1 branch
BranchTrueFalseTotal
if (($x1 ^ $y1) & 1 and $y1 < $y2) 0 0 0
Condition0%0/3
($x1 ^ $y1) & 1 and $y1 < $y2
!ll&&!rl&&r
000
MC/DC0%0/2
($x1 ^ $y1) & 1 and $y1 < $y2
($x1 ^ $y1) & 1 $y1 < $y2
Truth table0%0/3
A and B
A= ($x1 ^ $y1) & 1B= $y1 < $y2
ABresult
0X0
100
111
155 0 $y1 += 1;
156 ### y1 inc: $y1
157 }
158 0 ▶ if (! (($x2 ^ $y2) & 1) && $y2 > $y_min) {
Branch: 1 branch
BranchTrueFalseTotal
if (not +($x2 ^ $y2) & 1 and $y2 > $y_min) 0 0 0
Condition0%0/3
not +($x2 ^ $y2) & 1 and $y2 > $y_min
!ll&&!rl&&r
000
MC/DC0%0/2
not +($x2 ^ $y2) & 1 and $y2 > $y_min
not +($x2 ^ $y2) & 1 $y2 > $y_min
Truth table0%0/3
not(A) and B
A= not +($x2 ^ $y2) & 1B= $y2 > $y_min
ABresult
0X0
100
111
159 0 $y2 -= 1;
160 ### y2 dec: $y2
161 }
162 0 return ($self->xy_to_n($x1,$y1),
163 $self->xy_to_n($x2,$y2));
164 }
165  
166 1 1;
167 __END__
« PlanePath/DivisibleColumns.pm ↑ Summary PlanePath/R5DragonCurve.pm »