PlanePath/Columns.pm

filter: Statementstmt 64.7% 77 / 119 Branchbran 40.4% 17 / 42 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 48.4% 16 / 33 Podpod 100% 18 / 18 totaltotal 57.9% 131 / 226 CC 25 SCAR 14.6
CC
25
Cov
57%
rect_to_n_range
8.9
diffxy_minimum
6.0
dx_minimum
6.0
CRAP 4.3
« PlanePath/Rows.pm ↑ Summary PlanePath/PeanoDiagonals.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::Columns;
20 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 $VERSION = 128;
25 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 @ISA = ('Math::PlanePath');
27  
28 use Math::PlanePath::Base::Generic
29 2 'round_nearest',
30 2 ▶ 'floor';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # uncomment this to run the ### lines
33 #use Devel::Comments;
34  
35 2 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 2 use constant parameter_info_array =>
40 2 ▶ [ Math::PlanePath::Base::Generic::parameter_info_nstart1() ];
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 sub y_maximum {
43 0 ▶ my ($self) = @_;
Subroutine y_maximum: not called CC=1 SCAR=6.9
Pod documented
44 0 return $self->{'height'} - 1;
45 }
46  
47 sub diffxy_minimum {
48 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
49 0 ▶ if ($self->{'height'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'height'} == 0) { } 0 0 0
50 0 return 0; # at X=0,Y=0
51 } else {
52 0 return 1 - $self->{'height'}; # at X=0,Y=height-1
53 }
54 }
55  
56 sub dx_minimum {
57 0 ▶ my ($self) = @_;
Subroutine dx_minimum: not called CC=2 SCAR=17.9
Pod documented
58 0 ▶ return ($self->{'height'} <= 1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} <= 1 ? : 0 0 0
59 ? 1 # single row only
60 : 0);
61 }
62 2 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 sub _UNDOCUMENTED__dxdy_list {
64 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list: not called CC=2 SCAR=17.9
65 return (($self->{'height'} >= 2 ? (0,1) # N too
66 : ()),
67 0 ▶ 1, 1-$self->{'height'});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} >= 2 ? : 0 0 0
68 }
69 sub _UNDOCUMENTED__dxdy_list_at_n {
70 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
71 0 return $self->n_start + $self->{'height'} - 1;
72 }
73  
74 sub dy_minimum {
75 0 ▶ my ($self) = @_;
Subroutine dy_minimum: not called CC=1 SCAR=6.9
Pod documented
76 0 return - ($self->{'height'}-1);
77 }
78 sub dy_maximum {
79 0 ▶ my ($self) = @_;
Subroutine dy_maximum: not called CC=2 SCAR=17.9
Pod documented
80 0 ▶ return ($self->{'height'} <= 1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} <= 1 ? : 0 0 0
81 ? 0 # single row only
82 : 1);
83 }
84  
85 sub absdx_minimum {
86 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=2 SCAR=17.9
Pod documented
87 0 ▶ return ($self->{'height'} <= 1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} <= 1 ? : 0 0 0
88 ? 1 # single row only
89 : 0);
90 }
91 sub absdy_minimum {
92 0 ▶ my ($self) = @_;
Subroutine absdy_minimum: not called CC=2 SCAR=17.9
Pod documented
93 0 ▶ return ($self->{'height'} <= 1 ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} <= 1 ? : 0 0 0
94 }
95  
96 sub dsumxy_minimum {
97 0 ▶ my ($self) = @_;
Subroutine dsumxy_minimum: not called CC=1 SCAR=6.9
Pod documented
98 0 return 2 - $self->{'height'}; # dX=+1 dY=-(height-1)
99 }
100 2 ▶use constant dsumxy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 sub ddiffxy_minimum {
103 0 ▶ my ($self) = @_;
Subroutine ddiffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
104 0 ▶ return ($self->{'height'} == 1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} == 1 ? : 0 0 0
105 ? 1 # constant dX=1,dY=0
106 : -1); # straight N
107 }
108 sub ddiffxy_maximum {
109 0 ▶ my ($self) = @_;
Subroutine ddiffxy_maximum: not called CC=1 SCAR=6.9
Pod documented
110 0 return $self->{'height'}; # dX=+1 dY=-(height-1)
111 }
112  
113 sub dir_minimum_dxdy {
114 0 ▶ my ($self) = @_;
Subroutine dir_minimum_dxdy: not called CC=2 SCAR=17.9
Pod documented
115 0 ▶ return ($self->{'height'} == 1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} == 1 ? : 0 0 0
116 ? (1,0) # height=1 East only
117 : (0,1)); # height>1 North
118 }
119 sub dir_maximum_dxdy {
120 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=1 SCAR=6.9
Pod documented
121 0 return (1, $self->dy_minimum);
122 }
123  
124 sub turn_any_left {
125 0 ▶ my ($self) = @_;
Subroutine turn_any_left: not called CC=1 SCAR=6.9
Pod documented
126 0 return ($self->{'height'} != 1); # height=1 only straight ahead
127 }
128 sub _UNDOCUMENTED__turn_any_left_at_n {
129 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_left_at_n: not called CC=2 SCAR=17.9
130 return ($self->{'height'} == 1 ? undef
131 0 ▶ : $self->n_start + $self->{'height'});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} == 1 ? : 0 0 0
132 }
133  
134 2 *turn_any_right = \&turn_any_left;
135 sub _UNDOCUMENTED__turn_any_right_at_n {
136 1 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__turn_any_right_at_n: called CC=2 SCAR=7.2
137 return ($self->{'height'} == 1 ? undef
138 1 ▶ : $self->n_start + $self->{'height'} - 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'height'} == 1 ? : 0 1 1
139 }
140  
141 sub turn_any_straight {
142 0 ▶ my ($self) = @_;
Subroutine turn_any_straight: not called CC=1 SCAR=6.9
Pod documented
143 0 return ($self->{'height'} != 2); # height=2 never straight
144 }
145  
146  
147 #------------------------------------------------------------------------------
148  
149 sub new {
150 10 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=3 SCAR=11.0
Pod documented
151 10 ▶ if (! exists $self->{'height'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"height"}) 1 9 10
152 1 $self->{'height'} = 1;
153 }
154 10 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 10 0 10
155 10 $self->{'n_start'} = $self->default_n_start;
156 }
157 10 return $self;
158 }
159  
160 sub n_to_xy {
161 8 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=4 SCAR=14.3
Pod documented
162  
163 # no division by zero, and negatives not meaningful for now
164 8 my $height;
165 8 ▶ if (($height = $self->{'height'}) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($height = $self->{"height"}) <= 0) 0 8 8
166 ### no points for height<=0
167 0 return;
168 }
169  
170 8 $n = $n - $self->{'n_start'}; # zero based
171  
172 8 my $int = int($n); # BigFloat int() gives BigInt, use that
173 8 $n -= $int; # fraction part, preserve any BigFloat
174  
175 8 ▶ if (2*$n >= 1) { # if $n >= 0.5, but BigInt friendly
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $n >= 1) 2 6 8
176 2 $n -= 1;
177 2 $int += 1;
178 }
179 ### $n
180 ### $int
181 ### assert: $n >= -0.5
182 ### assert: $n < 0.5
183  
184 8 my $x = int ($int / $height);
185 8 $int -= $x*$height;
186 8 ▶ if ($int < 0) { # ensure round down when $int negative
Branch: 1 branch
BranchTrueFalseTotal
if ($int < 0) 0 8 8
187 0 $int += $height;
188 0 $x -= 1;
189 }
190 ### floor x: $x
191 ### remainder: $int
192  
193 8 return ($x,
194 $n + $int);
195 }
196  
197 sub xy_to_n {
198 6 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=3 SCAR=12.3
Pod documented
199  
200 6 $y = round_nearest ($y);
201 6 ▶ if ($y < 0 || $y >= $self->{'height'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0 or $y >= $self->{"height"}) 0 6 6
Condition33%1/3
$y < 0 or $y >= $self->{"height"}
l!l&&r!l&&!r
006
MC/DC0%0/2
$y < 0 or $y >= $self->{"height"}
$y < 0 $y >= $self->{"height"}
Truth table33%1/3
A or B
A= $y < 0B= $y >= $self->{"height"}
ABresult
1X1
011
000
202 0 return undef; # outside the oblong
203 }
204 6 $x = round_nearest ($x);
205 6 return $x * $self->{'height'} + $y + $self->{'n_start'};
206 }
207  
208 # exact
209 sub rect_to_n_range {
210 6 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=8 SCAR=21.9
Pod documented
211 6 my $height = $self->{'height'};
212  
213 6 $y1 = round_nearest ($y1);
214 6 $y2 = round_nearest ($y2);
215 6 ▶ if ($y2 < $y1) { ($y1,$y2) = ($y2,$y1) } # swap to y1<y2
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < $y1) 1 5 6
216 ### assert: $y1<=$y2
217  
218 6 ▶ if ($height<=0 || $y1 >= $height || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height <= 0 or $y1 >= $height or $y2 < 0) 0 6 6
Condition33%2/6
$height <= 0 or $y1 >= $height or $y2 < 0
l!l&&r!l&&!r
006
$height <= 0 or $y1 >= $height
l!l&&r!l&&!r
006
MC/DC0%0/3
$height <= 0 or $y1 >= $height or $y2 < 0
$height <= 0 $y1 >= $height $y2 < 0
Truth table25%1/4
A or B or C
A= $height <= 0B= $y1 >= $heightC= $y2 < 0
ABCresult
1XX1
01X1
0011
0000
219 ### completely outside 0 to height-1, or height<=0 ...
220 0 return (1,0);
221 }
222  
223 6 $x1 = round_nearest ($x1);
224 6 $x2 = round_nearest ($x2);
225 6 ▶ if ($x2 < $x1) { ($x1,$x2) = ($x2,$x1) } # swap to x1<x2
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < $x1) 0 6 6
226 ### assert: $x1<=$x2
227  
228 6 ▶ if ($y1 < 0) { $y1 *= 0; } # preserve bignum
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 1 5 6
229 6 ▶ if ($y2 >= $height) { $y2 = ($y2*0) + $height-1; } # preserve bignum
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 >= $height) 1 5 6
230  
231 # exact range bottom left to top right
232 return ($x1*$height + $y1 + $self->{'n_start'},
233 6 $x2*$height + $y2 + $self->{'n_start'});
234 }
235  
236 2 1;
237 __END__
« PlanePath/Rows.pm ↑ Summary PlanePath/PeanoDiagonals.pm »