PlanePath/DiagonalRationals.pm

filter: Statementstmt 83.4% 91 / 109 Branchbran 46.6% 14 / 30 Conditioncond 35.2% 6 / 17 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 81.4% 22 / 27 Podpod 100% 9 / 9 totaltotal 71.2% 144 / 202 CC 22 SCAR 16.4
CC
22
Cov
71%
xy_is_visited
20.0
rect_to_n_range
7.7
absdx_minimum
6.0
CRAP 5.2
« PlanePath/KochPeaks.pm ↑ Summary PlanePath/Corner.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 # Maybe:
20 # including_zero=>1 to have 0/1 for A038567
21  
22  
23 package Math::PlanePath::DiagonalRationals;
24 7 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 #use List::Util 'max';
28 7 *max = \&Math::PlanePath::_max;
29  
30 7 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 $VERSION = 128;
32 7 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 @ISA = ('Math::PlanePath');
34 7 *_rect_for_first_quadrant = \&Math::PlanePath::_rect_for_first_quadrant;
35  
36 use Math::PlanePath::Base::Generic
37 7 'is_infinite',
38 7 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 7 ▶use Math::PlanePath::CoprimeColumns;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 *_extend = \&Math::PlanePath::CoprimeColumns::_extend;
42 7 *_coprime = \&Math::PlanePath::CoprimeColumns::_coprime;
43 7 ▶use vars '@_x_to_n';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 7 *_x_to_n = \@Math::PlanePath::CoprimeColumns::_x_to_n;
45  
46 # uncomment this to run the ### lines
47 # use Smart::Comments;
48  
49  
50 7 use constant parameter_info_array =>
51 [ { name => 'direction',
52 share_key => 'direction_downup',
53 display => 'Direction',
54 type => 'enum',
55 default => 'down',
56 choices => ['down','up'],
57 choices_display => ['Down','Up'],
58 description => 'Number points downwards or upwards along the diagonals.',
59 },
60 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
61 7 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 7 ▶use constant default_n_start => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 7 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 7 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 7 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 7 ▶use constant x_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 7 ▶use constant y_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 7 ▶use constant gcdxy_maximum => 1; # no common factor
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 sub absdx_minimum {
72 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=2 SCAR=17.9
Pod documented
73 0 ▶ return ($self->{'direction'} eq 'down' ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
74 }
75 sub absdy_minimum {
76 0 ▶ my ($self) = @_;
Subroutine absdy_minimum: not called CC=2 SCAR=17.9
Pod documented
77 0 ▶ return ($self->{'direction'} eq 'down' ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'direction'} eq 'down' ? : 0 0 0
78 }
79 7 ▶use constant dsumxy_minimum => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 7 ▶use constant dsumxy_maximum => 1; # to next diagonal stripe
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 sub dir_minimum_dxdy {
83 0 ▶ my ($self) = @_;
Subroutine dir_minimum_dxdy: 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 ? (0,1) # North
86 : (1,0)); # East
87 }
88 sub dir_maximum_dxdy {
89 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: 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,-1) # South-East
92 : (2,-1)); # ESE at N=3 down to X axis
93 }
94  
95 #------------------------------------------------------------------------------
96  
97 sub new {
98 2 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=5 SCAR=17.5
Pod documented
99  
100 2 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 1 1 2
101 1 $self->{'n_start'} = $self->default_n_start;
102 }
103 2 ▶ my $direction = ($self->{'direction'} ||= 'down');
Condition50%1/2
$self->{"direction"} ||= "down"
l!l
02
MC/DC0%0/1
$self->{"direction"} ||= "down"
$self->{"direction"}
Truth table50%1/2
A
A= $self->{"direction"}
Aresult
11
00
104 2 ▶ if (! ($direction eq 'up' || $direction eq 'down')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($direction eq "up" or $direction eq "down") 0 2 2
Condition33%1/3
$direction eq "up" or $direction eq "down"
l!l&&r!l&&!r
020
MC/DC0%0/2
$direction eq "up" or $direction eq "down"
$direction eq "up" $direction eq "down"
Truth table33%1/3
A or B
A= $direction eq "up"B= $direction eq "down"
ABresult
1X1
011
000
105 0 croak "Unrecognised direction option: ", $direction;
106 }
107  
108 2 return $self;
109 }
110  
111 sub n_to_xy {
112 21 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=3 SCAR=11.8
Pod documented
113 ### DiagonalRationals n_to_xy(): $n
114  
115 21 ▶ if (2*($n - $self->{'n_start'}) < -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 * ($n - $self->{"n_start"}) < -1) 0 21 21
116 ### before n_start ...
117 0 return;
118 }
119 21 ▶ my ($x,$y) = $self->Math::PlanePath::CoprimeColumns::n_to_xy($n+1)
Branch: 1 branch
BranchTrueFalseTotal
() = $self->Math::PlanePath::CoprimeColumns::n_to_xy($n + 1) or return 0 21 21
120 or return;
121 ### CoprimeColumns returned: "x=$x y=$y"
122  
123 21 $x -= $y;
124 ### shear to: "x=$x y=$y"
125  
126 21 return ($x,$y);
127 }
128  
129 # Note: shared by FactorRationals
130 sub xy_is_visited {
131 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited: not called CC=4 SCAR=30.0
Pod documented
132 0 $x = round_nearest ($x);
133 0 $y = round_nearest ($y);
134 0 ▶ if ($x < 1
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 1 or $y < 1 or not _coprime($x, $y)) 0 0 0
Condition0%0/6
$x < 1 or $y < 1 or not _coprime($x, $y)
l!l&&r!l&&!r
000
$x < 1 or $y < 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$x < 1 or $y < 1 or not _coprime($x, $y)
$x < 1 $y < 1 not _coprime($x, $y)
Truth table0%0/4
A or B or not(C)
A= $x < 1B= $y < 1C= not _coprime($x, $y)
ABCresult
1XX1
01X1
0011
0000
135 || $y < 1
136 || ! _coprime($x,$y)) {
137 0 return 0;
138 }
139 0 return 1;
140 }
141  
142 sub xy_to_n {
143 5646 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=3 SCAR=11.0
Pod documented
144 ### DiagonalRationals xy_to_n(): "$x,$y"
145  
146 5646 my $n = Math::PlanePath::CoprimeColumns::xy_to_n($self,$x+$y,$y);
147  
148 # not the N=0 at Xcol=1,Ycol=1 which is Xdiag=1,Ydiag=0
149 5646 ▶ if (defined $n && $n > $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n and $n > $self->{'n_start'}) { } 2028 3618 5646
Condition100%3/3
defined $n and $n > $self->{"n_start"}
!ll&&!rl&&r
361712028
MC/DC100%2/2
defined $n and $n > $self->{"n_start"}
defined $n $n > $self->{"n_start"}
Truth table100%3/3
A and B
A= defined $nB= $n > $self->{"n_start"}
ABresult
0X0
100
111
150 2028 return $n-1;
151 } else {
152 3618 return undef;
153 }
154 }
155  
156 # not exact
157 sub rect_to_n_range {
158 23 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=7 SCAR=20.4
Pod documented
159 ### DiagonalRationals rect_to_n_range(): "$x1,$y1 $x2,$y2"
160  
161 23 $x1 = round_nearest($x1);
162 23 $y1 = round_nearest($y1);
163 23 $x2 = round_nearest($x2);
164 23 $y2 = round_nearest($y2);
165 23 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 1 22 23
166 23 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 23 23
167  
168 23 ▶ if ($x2 < 1 || $y2 < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 1 or $y2 < 1) 0 23 23
Condition33%1/3
$x2 < 1 or $y2 < 1
l!l&&r!l&&!r
0023
MC/DC0%0/2
$x2 < 1 or $y2 < 1
$x2 < 1 $y2 < 1
Truth table33%1/3
A or B
A= $x2 < 1B= $y2 < 1
ABresult
1X1
011
000
169 ### outside quadrant ...
170 0 return (1, 0);
171 }
172  
173 ### rect: "$x1,$y1 $x2,$y2"
174  
175 23 my $d2 = $x2 + $y2 + 1;
176 23 ▶ if (is_infinite($d2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($d2)) 0 23 23
177 0 return (1, $d2);
178 }
179 23 ▶ while ($#_x_to_n < $d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#Math::PlanePath::DiagonalRationals::_x_to_n < $d2) 192 23 215
180 192 _extend();
181 }
182 23 my $d1 = max (2, $x1 + $y1);
183 ### $d1
184 ### $d2
185  
186 return ($_x_to_n[$d1] - 1 + $self->{'n_start'},
187 23 $_x_to_n[$d2] + $self->{'n_start'});
188 }
189  
190 7 1;
191 __END__
« PlanePath/KochPeaks.pm ↑ Summary PlanePath/Corner.pm »