PlanePath/HypotOctant.pm

filter: Statementstmt 69.0% 105 / 152 Branchbran 43.4% 20 / 46 Conditioncond 14.2% 2 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 77.7% 14 / 18 Podpod 100% 7 / 7 totaltotal 60.8% 149 / 245 CC 29 SCAR 33.9
CC
29
Cov
60%
xy_to_n
110.0
rect_to_n_range
30.0
n_to_xy
6.7
CRAP 29.8
« PlanePath/Hypot.pm ↑ Summary PlanePath/ZOrderCurve.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 # Circle drop splash rings from
20 # math-image --path=HypotOctant --values=DigitProductSteps,values_type=count
21 # math-image --path=Hypot --values=DigitProduct
22 # math-image --path=Hypot --values=DigitCount
23 # math-image --path=Hypot --values=Modulo,modulus=1000
24 # http://stefan.guninski.com/oeisposter/
25 #
26 # pi*r^2 - pi*(r-1)^2 = pi*(2r-1)
27 # octant is 1/8 of that pi*(2x-1)/8
28 # pi*(2x-1)/8=100k
29 # 2x-1 = 100k*8/pi
30 # x = 100*4/pi*k
31 #
32 # A000328 Number of points of norm <= n^2 in square lattice.
33 # 1, 5, 13, 29, 49, 81, 113, 149, 197, 253, 317, 377, 441, 529, 613, 709, 797
34 # a(n) = 1 + 4 * sum(j=0, n^2 / 4, n^2 / (4*j+1) - n^2 / (4*j+3) )
35 #
36 # A057655 num points norm <= n in square lattice.
37 #
38 # A036702 num points |z=a+bi| <= n with 0<=a, 0<=b<=a, so octant
39 # A036703 num points n-1 < z <= n, first diffs?
40  
41  
42  
43 package Math::PlanePath::HypotOctant;
44 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 $VERSION = 128;
50 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 @ISA = ('Math::PlanePath');
52  
53 use Math::PlanePath::Base::Generic
54 1 'is_infinite',
55 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 # uncomment this to run the ### lines
58 #use Smart::Comments;
59  
60  
61 1 use constant parameter_info_array =>
62 [ { name => 'points',
63 share_key => 'points_aeo',
64 display => 'Points',
65 type => 'enum',
66 default => 'all',
67 choices => ['all','even','odd'],
68 choices_display => ['All','Even','Odd'],
69 description => 'Which X,Y points visit, either all of them or just X+Y even or X+Y odd.',
70 },
71 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 sub x_minimum {
77 6 ▶ my ($self) = @_;
Subroutine x_minimum: called CC=2 SCAR=6.9
Pod documented
78 6 ▶ return ($self->{'points'} eq 'odd'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'points'} eq 'odd' ? : 3 3 6
79 ? 1 # odd, line X=Y not included
80 : 0); # octant Y<=X so X-Y>=0
81 }
82 # points=odd X=1,Y=0
83 # otherwise X=0,Y=0
84 1 *sumabsxy_minimum = \&x_minimum;
85 1 *diffxy_minimum = \&x_minimum; # X>=Y so X-Y>=0
86 1 *absdiffxy_minimum = \&x_minimum;
87 1 *rsquared_minimum = \&x_minimum;
88  
89 sub absdy_minimum {
90 0 ▶ my ($self) = @_;
Subroutine absdy_minimum: not called CC=2 SCAR=17.9
Pod documented
91 0 ▶ return ($self->{'points'} eq 'all'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'points'} eq 'all' ? : 0 0 0
92 ? 0
93 : 1); # never same Y
94 }
95  
96 sub dir_minimum_dxdy {
97 0 ▶ my ($self) = @_;
Subroutine dir_minimum_dxdy: not called CC=2 SCAR=17.9
Pod documented
98 0 ▶ return ($self->{'points'} eq 'all'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'points'} eq 'all' ? : 0 0 0
99 ? (1,0) # all i=1 to X=1,Y=0
100 : (1,1)); # odd,even always at least NE
101 }
102 # max direction SE diagonal as anything else is at most tangent to the
103 # eighth of a circle
104 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106  
107 #------------------------------------------------------------------------------
108  
109 # my @n_to_x = (undef, 0);
110 # my @n_to_y = (undef, 0);
111 # my @hypot_to_n = (1);
112 # my @y_next_x = (1, 1);
113 # my @y_next_hypot = (1, 2);
114  
115 sub new {
116 6 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=5 SCAR=16.1
Pod documented
117  
118 6 ▶ my $points = ($self->{'points'} ||= 'all');
Condition100%2/2
$self->{"points"} ||= "all"
l!l
42
MC/DC100%1/1
$self->{"points"} ||= "all"
$self->{"points"}
Truth table100%2/2
A
A= $self->{"points"}
Aresult
11
00
119 6 ▶ if ($points eq 'all') {
Branch: 3 branches
BranchTrueFalseTotal
if ($points eq 'all') { } 3 3 6
elsif ($points eq 'even') { } 1 2 3
elsif ($points eq 'odd') { } 2 0 2
120 3 $self->{'n_to_x'} = [undef];
121 3 $self->{'n_to_y'} = [undef];
122 3 $self->{'hypot_to_n'} = [];
123 3 $self->{'y_next_x'} = [0];
124 3 $self->{'y_next_hypot'} = [0];
125 3 $self->{'x_inc'} = 1;
126 3 $self->{'x_inc_factor'} = 2;
127 3 $self->{'x_inc_squared'} = 1;
128 3 $self->{'opposite_parity'} = -1;
129  
130 } elsif ($points eq 'even') {
131 1 $self->{'n_to_x'} = [undef, 0];
132 1 $self->{'n_to_y'} = [undef, 0];
133 1 $self->{'hypot_to_n'} = [1];
134 1 $self->{'y_next_x'} = [2, 1];
135 1 $self->{'y_next_hypot'} = [4, 2];
136 1 $self->{'x_inc'} = 2;
137 1 $self->{'x_inc_factor'} = 4;
138 1 $self->{'x_inc_squared'} = 4;
139 1 $self->{'opposite_parity'} = 1;
140  
141 } elsif ($points eq 'odd') {
142 2 $self->{'n_to_x'} = [undef];
143 2 $self->{'n_to_y'} = [undef];
144 2 $self->{'hypot_to_n'} = [undef];
145 2 $self->{'y_next_x'} = [1];
146 2 $self->{'y_next_hypot'} = [1];
147 2 $self->{'x_inc'} = 2;
148 2 $self->{'x_inc_factor'} = 4;
149 2 $self->{'x_inc_squared'} = 4;
150 2 $self->{'opposite_parity'} = 0;
151  
152 } else {
153 0 croak "Unrecognised points option: ", $points;
154 }
155 6 return $self;
156 }
157  
158  
159 # at h=x^2+y^2
160 # step to (x+k)^2+y^2
161 # is add 2*x*k+k*k
162  
163 sub _extend {
164 2173 ▶ my ($self) = @_;
Subroutine _extend: called CC=5 SCAR=16.1
165 ### _extend() n: scalar(@{$self->{'n_to_x'}})
166  
167 2173 my $n_to_x = $self->{'n_to_x'};
168 2173 my $n_to_y = $self->{'n_to_y'};
169 2173 my $hypot_to_n = $self->{'hypot_to_n'};
170 2173 my $y_next_x = $self->{'y_next_x'};
171 2173 my $y_next_hypot = $self->{'y_next_hypot'};
172  
173 2173 my @y = (0);
174 2173 my $hypot = $y_next_hypot->[0];
175 2173 ▶ for (my $i = 1; $i < @$y_next_x; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$y_next_x) 63482 2173 65655
176 63482 ▶ if ($hypot == $y_next_hypot->[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hypot == $y_next_hypot->[$i]) { } 1157 62325 63482
elsif ($hypot > $y_next_hypot->[$i]) { } 4158 58167 62325
177 1157 push @y, $i;
178 } elsif ($hypot > $y_next_hypot->[$i]) {
179 4158 @y = ($i);
180 4158 $hypot = $y_next_hypot->[$i];
181 }
182 }
183  
184 2173 ▶ if ($y[-1] == $#$y_next_x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y[-1] == $y_next_x->$#*) 134 2039 2173
185 134 my $y = scalar(@$y_next_x);
186 134 my $x = $y + ($self->{'points'} eq 'odd');
187 134 $y_next_x->[$y] = $x;
188 134 $y_next_hypot->[$y] = $x*$x+$y*$y;
189 ### assert: $y_next_hypot->[$y] == $y**2 + $y_next_x->[$y]**2
190 }
191  
192 ### store: join(' ',map{"$n_to_x->[$_],$n_to_y->[$_]"} 0 .. $#$n_to_x)
193 ### at n: scalar(@$n_to_x)
194 ### hypot_to_n: "h=$hypot n=".scalar(@$n_to_x)
195  
196 2173 $hypot_to_n->[$hypot] = scalar(@$n_to_x);
197 2173 push @$n_to_y, @y;
198 push @$n_to_x,
199 map {
200 2173 my $x = $y_next_x->[$_];
201 2999 $y_next_x->[$_] += $self->{'x_inc'};
202 $y_next_hypot->[$_]
203 2999 += $self->{'x_inc_factor'} * $x + $self->{'x_inc_squared'};
204 ### assert: $y_next_hypot->[$_] == $_**2 + $y_next_x->[$_]**2
205 2999 $x
206 } @y;
207  
208 # ### hypot_to_n now: join(' ',map {defined($hypot_to_n->[$_]) && "h=$_,n=$hypot_to_n->[$_]"} 0 .. $#$hypot_to_n)
209 }
210  
211 sub n_to_xy {
212 3000 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=5 SCAR=19.0
Pod documented
213 ### Hypot n_to_xy(): $n
214  
215 3000 ▶ if ($n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 3000 3000
216 3000 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 3000 3000
217  
218 {
219 3000 my $int = int($n);
220 3000 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 3000 3000
221 0 my $frac = $n - $int; # inherit possible BigFloat/BigRat
222 0 my ($x1,$y1) = $self->n_to_xy($int);
223 0 my ($x2,$y2) = $self->n_to_xy($int+1);
224 0 my $dx = $x2-$x1;
225 0 my $dy = $y2-$y1;
226 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
227 }
228 }
229  
230 3000 my $n_to_x = $self->{'n_to_x'};
231 3000 my $n_to_y = $self->{'n_to_y'};
232  
233 3000 ▶ while ($n > $#$n_to_x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $n_to_x->$#*) 2173 3000 5173
234 2173 _extend($self);
235 }
236  
237 3000 return ($n_to_x->[$n], $n_to_y->[$n]);
238 }
239  
240 sub xy_to_n {
241 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: not called CC=10 SCAR=47.0
Pod documented
242 ### Hypot xy_to_n(): "$x, $y"
243 ### hypot_to_n last: $#{$self->{'hypot_to_n'}}
244  
245 0 $x = round_nearest ($x);
246 0 $y = round_nearest ($y);
247  
248 0 ▶ if ((($x%2) ^ ($y%2)) == $self->{'opposite_parity'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x % 2 ^ $y % 2) == $self->{"opposite_parity"}) 0 0 0
249 0 return undef;
250 }
251  
252 0 my $hypot = $x*$x + $y*$y;
253 0 ▶ if (is_infinite($hypot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($hypot)) 0 0 0
254 0 return $hypot;
255 }
256  
257 0 ▶ if ($x < 0 || $y < 0 || $y > $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0 or $y > $x) 0 0 0
Condition0%0/6
$x < 0 or $y < 0 or $y > $x
l!l&&r!l&&!r
000
$x < 0 or $y < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$x < 0 or $y < 0 or $y > $x
$x < 0 $y < 0 $y > $x
Truth table0%0/4
A or B or C
A= $x < 0B= $y < 0C= $y > $x
ABCresult
1XX1
01X1
0011
0000
258 ### outside first octant ...
259 0 return undef;
260 }
261  
262 0 my $hypot_to_n = $self->{'hypot_to_n'};
263 0 ▶ while ($hypot > $#$hypot_to_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hypot > $hypot_to_n->$#*) 0 0 0
264 0 _extend($self);
265 }
266  
267 0 my $n_to_x = $self->{'n_to_x'};
268 0 my $n_to_y = $self->{'n_to_y'};
269  
270 0 my $n = $hypot_to_n->[$hypot];
271 0 for (;;) {
272 0 ▶ if ($x == $n_to_x->[$n] && $y == $n_to_y->[$n]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $n_to_x->[$n] and $y == $n_to_y->[$n]) 0 0 0
Condition0%0/3
$x == $n_to_x->[$n] and $y == $n_to_y->[$n]
!ll&&!rl&&r
000
MC/DC0%0/2
$x == $n_to_x->[$n] and $y == $n_to_y->[$n]
$x == $n_to_x->[$n] $y == $n_to_y->[$n]
Truth table0%0/3
A and B
A= $x == $n_to_x->[$n]B= $y == $n_to_y->[$n]
ABresult
0X0
100
111
273 0 return $n;
274 }
275 0 $n += 1;
276  
277 0 ▶ if ($n_to_x->[$n]**2 + $n_to_y->[$n]**2 != $hypot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_to_x->[$n] ** 2 + $n_to_y->[$n] ** 2 != $hypot) 0 0 0
278 ### oops, hypot_to_n no good ...
279 0 return undef;
280 }
281 }
282 }
283  
284 # not exact
285 sub rect_to_n_range {
286 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=5 SCAR=34.0
Pod documented
287  
288 0 $x1 = round_nearest ($x1);
289 0 $y1 = round_nearest ($y1);
290 0 $x2 = round_nearest ($x2);
291 0 $y2 = round_nearest ($y2);
292 0 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 0 0
293 0 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 0 0
294  
295 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
296 0 return (1, 0);
297 }
298  
299 # circle area pi*r^2, with r^2 = $x2**2 + $y2**2
300 0 return (1, 1 + int (3.2/8 * (($x2+1)**2 + ($y2+1)**2)));
301 }
302  
303 1 1;
304 __END__
« PlanePath/Hypot.pm ↑ Summary PlanePath/ZOrderCurve.pm »