PlanePath/PixelRings.pm

filter: Statementstmt 76.9% 147 / 191 Branchbran 55.1% 32 / 58 Conditioncond 27.2% 3 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 96.3% 26 / 27 Podpod 100% 4 / 4 totaltotal 71.4% 213 / 298 CC 34 SCAR 27.4
CC
34
Cov
70%
rect_to_n_range
56.0
n_to_xy
24.5
xy_to_n
14.6
CRAP 15.5
« PlanePath/TriangularHypot.pm ↑ Summary PlanePath/PyramidSides.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 # ENHANCE-ME: What formula for the cumulative pixel count, and its inverse?
20 # Not floor(k*4*sqrt(2)).
21  
22 # ENHANCE-ME: Maybe n_start
23  
24  
25 package Math::PlanePath::PixelRings;
26 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Math::Libm 'hypot';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 #use List::Util 'min','max';
30 1 *min = \&Math::PlanePath::_min;
31 1 *max = \&Math::PlanePath::_max;
32  
33 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 $VERSION = 128;
35 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 @ISA = ('Math::PlanePath');
37  
38 use Math::PlanePath::Base::Generic
39 1 'is_infinite',
40 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 # uncomment this to run the ### lines
43 #use Smart::Comments;
44  
45  
46 # use constant parameter_info_array =>
47 # [
48 # {
49 # name => 'offset',
50 # share_key => 'offset_05',
51 # type => 'float',
52 # description => 'Radial offset for the centre of each ring.',
53 # default => 0,
54 # minimum => -0.5,
55 # maximum => 0.5,
56 # page_increment => 0.05,
57 # step_increment => 0.005,
58 # width => 7,
59 # decimals => 4,
60 # },
61 # ];
62 1 ▶use constant n_frac_discontinuity => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 1 ▶use constant x_negative_at_n => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use constant y_negative_at_n => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use constant dx_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use constant dx_maximum => 2; # jump N=5 to N=6
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 # eight plus ENE
72 1 ▶use constant 1.02;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 use constant _UNDOCUMENTED__dxdy_list => (1,0, # E N=1
74 2,1, # ENE N=5 <-- extra
75 1,1, # NE N=16
76 0,1, # N N=6
77 -1,1, # NW N=2
78 -1,0, # W N=8
79 -1,-1, # SW N=3
80 0,-1, # S N=11
81 1,-1, # SE N=4
82 1 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use constant _UNDOCUMENTED__dxdy_list_at_n => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 1 ▶use constant dsumxy_minimum => -2; # diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use constant dsumxy_maximum => 3; # dx=2,dy=1 at jump N=5 to N=6
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 1 ▶use constant _UNDOCUMENTED__turn_any_right_at_n => 81;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93  
94 #------------------------------------------------------------------------------
95  
96 sub new {
97 3 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=7.0
Pod documented
98  
99 3 ▶ $self->{'offset'} ||= 0;
Condition50%1/2
$self->{"offset"} ||= 0
l!l
03
MC/DC0%0/1
$self->{"offset"} ||= 0
$self->{"offset"}
Truth table50%1/2
A
A= $self->{"offset"}
Aresult
11
00
100 3 $self->{'cumul'} = [ 1, 2 ];
101 3 $self->{'cumul_x'} = 0;
102 3 $self->{'cumul_y'} = 0;
103 3 $self->{'cumul_add'} = 0;
104  
105 3 return $self;
106 }
107  
108 sub _cumul_extend {
109 4241 ▶ my ($self) = @_;
Subroutine _cumul_extend: called CC=4 SCAR=13.9
110 ### _cumul_extend(): "length of r=".($#{$self->{'cumul'}})
111  
112 4241 my $cumul = $self->{'cumul'};
113 4241 my $r = $#$cumul;
114 4241 $self->{'cumul_add'} += 4;
115 4241 ▶ if ($self->{'cumul_x'} == $self->{'cumul_y'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cumul_x'} == $self->{'cumul_y'}) { } 2116 2125 4241
116 ### at: "$self->{'cumul_x'},$self->{'cumul_y'}"
117 ### step across and maybe up
118 2116 $self->{'cumul_x'}++;
119  
120 ### xy hypot: ($self->{'cumul_x'}+.5)**2 + ($self->{'cumul_y'})**2
121 ### r squared: $r*$r
122 ### E: ($self->{'cumul_x'}+.5)**2 + $self->{'cumul_y'}**2 - ($r+$self->{'offset'})**2
123  
124 2116 ▶ if (($self->{'cumul_x'}+.5)**2 + $self->{'cumul_y'}**2 < ($r+$self->{'offset'})**2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"cumul_x"} + "0.5") ** 2 + $self->{"cumul_y"} ** 2 < ($r + $self->{"offset"}) ** 2) 874 1242 2116
125 ### midpoint of x,y inside, increment to x,y+1
126 874 $self->{'cumul_y'}++;
127 874 $self->{'cumul_add'} += 4;
128 }
129  
130 } else {
131 ### at: "$self->{'cumul_x'},$self->{'cumul_y'}"
132 ### try y+1 with x or x+1 is: ($self->{'cumul_x'}+.5).",".($self->{'cumul_y'}+1)
133 2125 $self->{'cumul_y'}++;
134  
135 ### xy hypot: ($self->{'cumul_x'}+.5)**2 + ($self->{'cumul_y'})**2
136 ### r squared: $r*$r
137 ### E: ($self->{'cumul_x'}+.5)**2 + $self->{'cumul_y'}**2 - ($r+$self->{'offset'})**2
138  
139 2125 ▶ if (($self->{'cumul_x'}+.5)**2 + $self->{'cumul_y'}**2 < ($r+$self->{'offset'})**2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"cumul_x"} + "0.5") ** 2 + $self->{"cumul_y"} ** 2 < ($r + $self->{"offset"}) ** 2) 883 1242 2125
140 ### midpoint inside, increment x too
141 883 $self->{'cumul_x'}++;
142 883 $self->{'cumul_add'} += 4;
143 }
144 }
145 ### to: "$self->{'cumul_x'},$self->{'cumul_y'}"
146 ### cumul extend: scalar(@$cumul).' = '.($cumul->[-1] + $self->{'cumul_add'})
147 ### cumul_add: $self->{'cumul_add'}
148 4241 push @$cumul, $cumul->[-1] + $self->{'cumul_add'};
149 }
150  
151 sub n_to_xy {
152 2117 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=13 SCAR=32.0
Pod documented
153 ### PixelRings n_to_xy(): $n
154  
155 2117 ▶ if ($n < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 2) 1 2116 2117
156 1 ▶ if ($n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 1 1
157 1 return ($n-1, 0);
158 }
159 2116 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 2116 2116
160 0 return ($n,$n);
161 }
162  
163  
164 {
165 # ENHANCE-ME: direction of N+1 from the cumulative lookup
166 2116 my $int = int($n);
167 2116 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 2116 2116
168 0 my $frac = $n - $int;
169 0 my ($x1,$y1) = $self->n_to_xy($int);
170 0 my ($x2,$y2) = $self->n_to_xy($int+1);
171 0 ▶ if ($y2 == 0 && $x2 > 0) { $x2 -= 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 == 0 and $x2 > 0) 0 0 0
Condition0%0/3
$y2 == 0 and $x2 > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$y2 == 0 and $x2 > 0
$y2 == 0 $x2 > 0
Truth table0%0/3
A and B
A= $y2 == 0B= $x2 > 0
ABresult
0X0
100
111
172 0 my $dx = $x2-$x1;
173 0 my $dy = $y2-$y1;
174 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
175 }
176 2116 $n = $int;
177 }
178  
179 ### search cumul for n: $n
180 2116 my $cumul = $self->{'cumul'};
181 2116 my $r = 1;
182 2116 for (;;) {
183 4499191 ▶ if ($r >= @$cumul) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r >= @$cumul) 0 4499191 4499191
184 0 _cumul_extend ($self);
185 }
186 4499191 ▶ if ($cumul->[$r] > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cumul->[$r] > $n) 2116 4497075 4499191
187 2116 last;
188 }
189 4497075 $r++;
190 }
191 2116 $r--;
192  
193 2116 $n -= $cumul->[$r];
194 2116 my $len = $cumul->[$r+1] - $cumul->[$r];
195 ### cumul: "$cumul->[$r] to $cumul->[$r+1]"
196 ### $len
197 ### n rem: $n
198 2116 $len /= 4;
199 2116 my $quadrant = $n / $len;
200 2116 $n %= $len;
201 ### len of quadrant: $len
202 ### $quadrant
203 ### n into quadrant: $n
204  
205 2116 my $rev;
206 2116 ▶ if ($rev = ($n > $len/2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev = $n > $len / 2) 0 2116 2116
207 0 $n = $len - $n;
208 }
209 ### $rev
210 ### $n
211 2116 my $y = $n;
212 2116 my $x = int (sqrt (max (0, ($r+$self->{'offset'})**2 - $y*$y)) + .5);
213 2116 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) 0 2116 2116
214 0 ($x,$y) = ($y,$x);
215 }
216  
217 2116 ▶ if ($quadrant & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quadrant & 2) 0 2116 2116
218 0 $x = -$x;
219 0 $y = -$y;
220 }
221 2116 ▶ if ($quadrant & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quadrant & 1) 0 2116 2116
222 0 ($x,$y) = (-$y, $x);
223 }
224 ### return: "$x, $y"
225 2116 return ($x, $y);
226 }
227  
228 sub xy_to_n {
229 3000 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=12 SCAR=26.8
Pod documented
230 ### PixelRings xy_to_n(): "$x, $y"
231  
232 3000 $x = round_nearest ($x);
233 3000 $y = round_nearest ($y);
234  
235 3000 ▶ if ($x == 0 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) 1 2999 3000
Condition66%2/3
$x == 0 and $y == 0
!ll&&!rl&&r
299901
MC/DC50%1/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table66%2/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
236 1 return 1;
237 }
238  
239 2999 my $r;
240 {
241 2999 my $xa = abs($x);
242 2999 my $ya = abs($y);
243 2999 ▶ if ($xa < $ya) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xa < $ya) 0 2999 2999
244 0 ($xa,$ya) = ($ya,$xa);
245 }
246 2999 $r = int (hypot ($xa+.5,$ya));
247 ### r frac: hypot ($xa+.5,$ya)
248 ### $r
249 ### r < inside frac: hypot ($xa-.5,$ya)
250 2999 ▶ if ($r < hypot ($xa-.5,$ya)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < &hypot($xa - "0.5", $ya)) 879 2120 2999
251 ### pixel not crossed
252 879 return undef;
253 }
254 2120 ▶ if ($xa == $ya) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xa == $ya) 2120 0 2120
255 ### and pixel below for diagonal
256 ### r < below frac: $r . " < " . hypot ($xa+.5,$ya-1)
257 2120 ▶ if ($r < hypot ($xa+.5,$ya-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < &hypot($xa + "0.5", $ya - 1)) 4 2116 2120
258 ### same loop, no sharp corner
259 4 return undef;
260 }
261 }
262 }
263 2116 ▶ if (is_infinite($r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($r)) 0 2116 2116
264 0 return undef;
265 }
266  
267 2116 my $cumul = $self->{'cumul'};
268 2116 ▶ while ($#$cumul <= $r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cumul->$#* <= $r) 4241 2116 6357
269 ### extend cumul for r: $r
270 4241 _cumul_extend ($self);
271 }
272  
273 2116 my $n = $cumul->[$r];
274 2116 my $len = $cumul->[$r+1] - $n;
275 ### $r
276 ### n base: $n
277 ### $len
278 ### len/4: $len/4
279 2116 ▶ if ($y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0) 0 2116 2116
280 ### y neg, rotate 180
281 0 $y = -$y;
282 0 $x = -$x;
283 0 $n += $len/2;
284 }
285 2116 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 0 2116 2116
286 0 $n += $len/4;
287 0 ($x,$y) = ($y,-$x);
288 ### neg x, rotate 90
289 ### n base now: $n + $len/4
290 ### transpose: "$x,$y"
291 }
292 ### assert: $x >= 0
293 ### assert: $y >= 0
294 2116 ▶ if ($y > $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $x) 0 2116 2116
295 ### top octant, reverse: "x=$x len/4=".($len/4)." gives ".($len/4 - $x)
296 0 $y = $len/4 - $x;
297 }
298 ### n return: $n + $y
299 2116 return $n + $y;
300 }
301  
302 # not exact
303 sub rect_to_n_range {
304 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=7 SCAR=40.3
Pod documented
305 ### PixelRings rect_to_n_range(): "$x1,$y1 $x2,$y2"
306  
307 # ENHANCE-ME: use an estimate from rings no bigger than sqrt(2), so can
308 # get a range for big x,y
309  
310 0 $x1 = round_nearest ($x1);
311 0 $y1 = round_nearest ($y1);
312 0 $x2 = round_nearest ($x2);
313 0 $y2 = round_nearest ($y2);
314  
315 0 ▶ my $r_min
Branch: 1 branch
BranchTrueFalseTotal
$x1 < 0 ^ $x2 < 0 || $y1 < 0 ^ $y2 < 0 ? : 0 0 0
Condition0%0/3
$x1 < 0 ^ $x2 < 0 or $y1 < 0 ^ $y2 < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x1 < 0 ^ $x2 < 0 or $y1 < 0 ^ $y2 < 0
$x1 < 0 ^ $x2 < 0 $y1 < 0 ^ $y2 < 0
Truth table0%0/3
A or B
A= $x1 < 0 ^ $x2 < 0B= $y1 < 0 ^ $y2 < 0
ABresult
1X1
011
000
316 = ((($x1<0) ^ ($x2<0)) || (($y1<0) ^ ($y2<0))
317 ? 0
318 : max (0,
319 int (hypot (min(abs($x1),abs($x2)), min(abs($y1),abs($y2))))
320 - 1));
321 0 my $r_max = 2 + int (hypot (max(abs($x1),abs($x2)), max(abs($y1),abs($y2))));
322 ### $r_min
323 ### $r_max
324  
325 0 ▶ if (is_infinite($r_min)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($r_min)) 0 0 0
326 0 return ($r_min, $r_min);
327 }
328  
329 0 my ($n_max, $r_target);
330 0 ▶ if (is_infinite($r_max)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($r_max)) { } 0 0 0
331 0 $n_max = $r_max; # infinity
332 0 $r_target = $r_min;
333 } else {
334 0 $r_target = $r_max;
335 }
336  
337 0 my $cumul = $self->{'cumul'};
338 0 ▶ while ($#$cumul < $r_target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cumul->$#* < $r_target) 0 0 0
339 ### extend cumul for r: $r_target
340 0 _cumul_extend ($self);
341 }
342  
343 0 ▶ if (! defined $n_max) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n_max) 0 0 0
344 0 $n_max = $cumul->[$r_max];
345 }
346 0 return ($cumul->[$r_min], $n_max);
347 }
348  
349 1 1;
350 __END__
« PlanePath/TriangularHypot.pm ↑ Summary PlanePath/PyramidSides.pm »