PlanePath/AlternatePaperMidpoint.pm

filter: Statementstmt 90.7% 147 / 162 Branchbran 76.7% 43 / 56 Conditioncond 58.8% 10 / 17 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 81.8% 18 / 22 Podpod 100% 10 / 10 totaltotal 83.5% 229 / 274 SCAR 37.0
CC
36
Cov
85%
rect_to_n_range
15.2
xy_to_n
14.3
n_to_xy
9.0
CRAP 40.3
« PlanePath/GcdRationals.pm ↑ Summary PlanePath/BetaOmega.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 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 # math-image --path=AlternatePaperMidpoint,arms=8 --all --output=numbers_dash
20 # math-image --path=AlternatePaperMidpoint --lines --scale=20
21  
22  
23 package Math::PlanePath::AlternatePaperMidpoint;
24 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use List::Util 'min'; # 'max'
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 *max = \&Math::PlanePath::_max;
28  
29 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 $VERSION = 127;
31 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 @ISA = ('Math::PlanePath::Base::NSEW',
34 'Math::PlanePath');
35  
36 use Math::PlanePath::Base::Generic
37 1 'is_infinite',
38 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 use Math::PlanePath::Base::Digits
40 1 'round_down_pow',
41 'digit_split_lowtohigh',
42 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
44  
45 1 ▶use Math::PlanePath::AlternatePaper;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # uncomment this to run the ### lines
48 #use Smart::Comments;
49  
50  
51 1 use constant parameter_info_array => [ { name => 'arms',
52 share_key => 'arms_8',
53 display => 'Arms',
54 type => 'integer',
55 minimum => 1,
56 maximum => 8,
57 default => 1,
58 width => 1,
59 description => 'Arms',
60 1 ▶ } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 sub x_negative {
65 6 ▶ my ($self) = @_;
Subroutine x_negative: called CC=1 SCAR=0.0
Pod documented
66 6 return ($self->{'arms'} >= 3);
67 }
68 sub y_negative {
69 6 ▶ my ($self) = @_;
Subroutine y_negative: called CC=1 SCAR=0.0
Pod documented
70 6 return ($self->{'arms'} >= 5);
71 }
72 {
73 1 my @x_negative_at_n = (undef,
74 undef,undef,11,3,
75 3,3,3,3);
76 sub x_negative_at_n {
77 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
78 0 return $x_negative_at_n[$self->{'arms'}];
79 }
80 }
81 {
82 1 my @y_negative_at_n = (undef,
83 undef,undef,undef,undef,
84 24,11,12,7);
85 sub y_negative_at_n {
86 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
87 0 return $y_negative_at_n[$self->{'arms'}];
88 }
89 }
90  
91 1 sub sumxy_minimum {
92 0 ▶ my ($self) = @_;
Subroutine sumxy_minimum: not called CC=2 SCAR=17.9
Pod documented
93 0 ▶ return ($self->arms_count <= 3
Branch: 1 branch
BranchTrueFalseTotal
$self->arms_count <= 3 ? : 0 0 0
94 ? 0 # 1,2,3 arms above X=-Y diagonal
95 : undef);
96 }
97 sub diffxy_minimum {
98 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
99 0 ▶ return ($self->arms_count == 1
Branch: 1 branch
BranchTrueFalseTotal
$self->arms_count == 1 ? : 0 0 0
100 ? 0 # 1 arms right of X=Y diagonal
101 : undef);
102 }
103  
104  
105 #------------------------------------------------------------------------------
106  
107 sub new {
108 25 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
109 25 ▶ $self->{'arms'} = max(1, min(8, $self->{'arms'} || 1));
Condition100%2/2
$self->{'arms'} || 1
l!l
223
MC/DC100%1/1
$self->{'arms'} || 1
$self->{'arms'}
Truth table100%2/2
A
A= $self->{'arms'}
Aresult
11
00
110 25 return $self;
111 }
112  
113 # +-----------+ states
114 # |\ -------/|
115 # | \ \ 4 / |
116 # |^ \ \ / |
117 # || \ v / /||
118 # || \ / / ||
119 # ||8 / * /12||
120 # || / / \ ||
121 # ||/ / ^ \ ||
122 # | / \ \ v|
123 # | / 0 \ \ |
124 # |/ ------ \|
125 # +-----------+
126 #
127 # + state=0 digits
128 # /|\
129 # / | \
130 # / | \
131 # /\ 1|3 /\
132 # / \ | / \
133 # / 0 \|/ 2 \
134 # +------+------+
135  
136 1 my @next_state = (0, 12, 0, 8, # 0 forward
137 4, 8, 4, 12, # 4 forward NW
138 4, 8, 0, 8, # 8 reverse
139 0, 12, 4, 12, # 12 reverse NE
140 );
141 1 my @digit_to_x = (0,0,1,1,
142 1,1,0,0,
143 0,0,0,0,
144 1,1,1,1,
145 );
146 1 my @digit_to_y = (0,0,0,0,
147 1,1,1,1,
148 0,0,1,1,
149 1,1,0,0,
150 );
151  
152 sub n_to_xy {
153 2659 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=9 SCAR=22.0
Pod documented
154 ### AlternatePaperMidpoint n_to_xy(): $n
155  
156 2659 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 2659 2659
157 2659 ▶ if (is_infinite($n)) { return ($n, $n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 2659 2659
158  
159 {
160 2659 my $int = int($n);
161 2659 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 6 2653 2659
162 6 my ($x1,$y1) = $self->n_to_xy($int);
163 6 my ($x2,$y2) = $self->n_to_xy($int+$self->{'arms'});
164 6 my $frac = $n - $int; # inherit possible BigFloat
165 6 my $dx = $x2-$x1;
166 6 my $dy = $y2-$y1;
167 6 return ($frac*$dx + $x1, $frac*$dy + $y1);
168 }
169 2653 $n = $int; # BigFloat int() gives BigInt, use that
170 }
171  
172 2653 my $zero = ($n * 0); # inherit bignum 0
173 2653 my $arm = _divrem_mutate ($n, $self->{'arms'});
174 ### $arm
175 ### $n
176  
177 2653 my @digits = digit_split_lowtohigh($n,4);
178 2653 my $state = my $dirstate = 0;
179  
180 2653 my @x;
181 my @y;
182 2653 foreach my $i (reverse 1 .. scalar(@digits)) {
183 7102 my $digit = $digits[$i-1]; # high to low, all digits
184 7102 $state += $digit;
185 7102 ▶ if ($digit != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit != 3) 5200 1902 7102
186 5200 $dirstate = $state;
187 }
188 7102 $x[$i] = $digit_to_x[$state]; # high to low, leaving one lowest
189 7102 $y[$i] = $digit_to_y[$state];
190 7102 $state = $next_state[$state];
191 }
192  
193 2653 $x[0] = $digit_to_x[$state]; # state=4,12 increment
194 2653 $y[0] = $digit_to_y[$state + 3]; # state=4,8 increment
195  
196 2653 my $x = digit_join_lowtohigh(\@x,2,$zero);
197 2653 my $y = digit_join_lowtohigh(\@y,2,$zero);
198  
199 ### final: "x=$x,y=$y state=$state"
200  
201 2653 ▶ if ($arm & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 1) 1441 1212 2653
202 1441 ($x,$y) = ($y+1,$x+1); # transpose and offset
203 }
204 2653 ▶ if ($arm & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 2) 1310 1343 2653
205 1310 ($x,$y) = (-$y,$x+1); # rotate +90 and offset
206 }
207 2653 ▶ if ($arm & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 4) 1046 1607 2653
208 1046 $x = -1 - $x; # rotate 180 and offset
209 1046 $y = 1 - $y;
210 }
211  
212 # ### rotated return: "$x,$y"
213 2653 return ($x,$y);
214 }
215  
216 # | |
217 # 64-65-66 71-72-73-74 95
218 # | |
219 # 63 98-97-96
220 # | |
221 # 20-21 62 99
222 # | | |
223 # 19 22 61-60-59
224 # | | |
225 # 16-17-18 23 56-57-58
226 # | | |
227 # 15 26-25-24 55 50-49-48-47
228 # | | | | |
229 # 4--5 14 27-28-29 54 51 36-37 46
230 # | | | | | | | | |
231 # 3 6 13-12-11 30 53-52 35 38 45-44-43
232 # | | | | | | |
233 # 0--1--2 7--8--9-10 31-32-33-34 39-40-41-42
234 #
235 # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
236  
237 # 43-35 42-50-58 57-49-41
238 # | | | |
239 # 91-99 51 27 34-26-18 17-25-33
240 # | | | | |
241 # 83-75-67-59 19-11--3 10 9 32-40
242 # | | | |
243 # 84-76-68-60 20-12--4 2 1 24 48 96-88
244 # | | | | | |
245 # 92 52 28 5 6 0--8-16 56-64-72-80
246 # | | | |
247 # 44-36 13 14 7-15-23 63-71-79-87
248 # | | | | |
249 # 37-29-21 22-30-38 31 55 95
250 # | | | |
251 # 45-53-61 62-54-46 39-47
252 # | |
253 # 69 70
254  
255 sub xy_to_n {
256 44 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=14 SCAR=26.6
Pod documented
257 ### AlternatePaperMidpoint xy_to_n(): "$x, $y"
258  
259 44 $x = round_nearest($x);
260 44 $y = round_nearest($y);
261  
262 44 ▶ if (is_infinite($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 44 44
263 0 return $x; # infinity
264 }
265 44 ▶ if (is_infinite($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 44 44
266 0 return $y; # infinity
267 }
268  
269 # arm in various octants, rotate/transpose to first
270 44 my $arm;
271 44 ▶ if ($y >= ($x>=0?0:2)) { # Y>=0 when X positive, Y>=2 when X negative
Branch: 2 branches
BranchTrueFalseTotal
if ($y >= ($x >= 0 ? 0 : 2)) { } 37 7 44
$x >= 0 ? : 28 16 44
272 37 $arm = 0;
273 } else {
274 # lower arms 4,5,6,7 ...
275 7 $arm = 4;
276 7 $x = -1 - $x; # rotate 180, offset
277 7 $y = 1 - $y;
278 }
279 44 ▶ if ($x < ($y>0?1:0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x < ($y > 0 ? 1 : 0)) 11 33 44
$y > 0 ? : 32 12 44
280 ### second quad arms 2,3 ...
281 11 ($x,$y) = ($y-1,-$x); # rotate -90, offset
282 11 $arm += 2;
283 }
284 44 ▶ if ($y > $x-($x%2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $x - $x % 2) 12 32 44
285 ### above diagonal, arm 1 ...
286 12 ($x,$y) = ($y-1,$x-1); # offset and transpose
287 12 $arm++;
288 }
289 ### assert: $x >= 0
290 ### assert: $y >= 0
291 ### assert: $y <= $x - ($x%2)
292  
293 44 ▶ if ($arm >= $self->{'arms'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm >= $self->{"arms"}) 0 44 44
294 0 return undef;
295 }
296  
297 44 my ($len, $level) = round_down_pow ($x, 2);
298 44 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 44 44
299 0 return ($level);
300 }
301  
302 # + state=0 digits
303 # /|\
304 # / | \
305 # / | \
306 # /\ 1|3 /\
307 # / \ | / \
308 # / 0 \|/ 2 \
309 # +------+------+
310  
311 # + state=0 digits
312 # /|\
313 # / | \
314 # / | \
315 # /\ 2|0 /\
316 # / \ | / \
317 # / 3 \|/ 1 \
318 # +------+------+
319  
320 44 my $n = ($x * 0 * $y); # inherit bignum 0
321 44 my $rev = 0;
322  
323 44 $len *= 2;
324 44 ▶ while ($level-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level-- >= 0) 214 44 258
325 ### at: "xy=$x,$y rev=$rev len=$len n=".sprintf('%#x',$n)
326  
327 ### assert: $x >= 0
328 ### assert: $y >= 0
329 ### assert: $y <= $x - ($x%2)
330 ### assert: $x+$y+($x%2) < 2*$len
331  
332 214 my $digit;
333 214 ▶ if ($x < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $len) { } 125 89 214
334 ### diagonal: $x+$y+($x%2), $len
335 125 ▶ if ($x+$y+($x%2) < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x + $y + $x % 2 < $len) { } 67 58 125
336 ### part 0 ...
337 67 $digit = 0;
338 } else {
339 ### part 1 ...
340 58 ($x,$y) = ($y,$len-1-$x); # shift, rotate -90
341 58 $rev ^= 3;
342 58 $digit = 2; # becoming digit=1 with reverse
343 }
344 } else {
345 89 $x -= $len;
346 ### 2,3 ycmp: $y, $x-($x%2)
347 89 ▶ if ($y <= $x-($x%2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $x - $x % 2) { } 51 38 89
348 ### part 2 ...
349 51 $digit = 2;
350 } else {
351 ### part 3 ...
352 38 ($x,$y) = ($len-1-$y,$x); # shift, rotate +90
353 38 $rev ^= 3;
354 38 $digit = 0; # becoming digit=3 with reverse
355 }
356 }
357 ### $digit
358  
359 214 $digit ^= $rev; # $digit = 3-$digit if reverse
360 ### reversed digit: $digit
361  
362 214 $n *= 4;
363 214 $n += $digit;
364 214 $len /= 2;
365 }
366 ### final: "xy=$x,$y rev=$rev"
367  
368 ### assert: $x == 0
369 ### assert: $y == 0
370  
371 44 return $n*$self->{'arms'} + $arm;
372 }
373  
374  
375 # not exact
376 sub rect_to_n_range {
377 40 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=12 SCAR=27.2
Pod documented
378 ### AlternatePaperMidpoint rect_to_n_range(): "$x1,$y1 $x2,$y2 arms=$self->{'arms'}"
379  
380 40 $x1 = round_nearest($x1);
381 40 $x2 = round_nearest($x2);
382 40 $y1 = round_nearest($y1);
383 40 $y2 = round_nearest($y2);
384  
385 40 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 40 40
386 40 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 40 40
387  
388 40 my $arms = $self->{'arms'};
389 40 ▶ if (($arms == 1 && $y1 > $x2) # x2,y1 bottom right corner
Branch: 1 branch
BranchTrueFalseTotal
if ($arms == 1 and $y1 > $x2 or $arms <= 2 and $x2 < 0 or $arms <= 4 and $y2 < 0) 0 40 40
Condition53%8/15
$arms == 1 and $y1 > $x2 or $arms <= 2 and $x2 < 0 or $arms <= 4 and $y2 < 0
l!l&&r!l&&!r
0040
$arms == 1 and $y1 > $x2 or $arms <= 2 and $x2 < 0
l!l&&r!l&&!r
0040
$arms == 1 and $y1 > $x2
!ll&&!rl&&r
3550
$arms <= 2 and $x2 < 0
!ll&&!rl&&r
30100
$arms <= 4 and $y2 < 0
!ll&&!rl&&r
20200
MC/DC0%0/6
$arms == 1 and $y1 > $x2 or $arms <= 2 and $x2 < 0 or $arms <= 4 and $y2 < 0
$arms == 1 $y1 > $x2 $arms <= 2 $x2 < 0 $arms <= 4 $y2 < 0
Truth table20%3/15
A and B or C and D or E and F
A= $arms == 1B= $y1 > $x2C= $arms <= 2D= $x2 < 0E= $arms <= 4F= $y2 < 0
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
390 || ($arms <= 2 && $x2 < 0)
391 || ($arms <= 4 && $y2 < 0)) {
392 ### outside ...
393 0 return (1,0);
394 }
395  
396 40 ▶ my ($len) = round_down_pow (max ($x2,
Branch: 3 branches
BranchTrueFalseTotal
$arms >= 2 ? : 35 5 40
$arms >= 4 ? : 25 15 40
$arms >= 6 ? : 15 25 40
397 ($arms >= 2 ? $y2-1 : ()),
398 ($arms >= 4 ? -1-$x1 : ()),
399 ($arms >= 6 ? -$y1 : ())),
400 2);
401 40 return (0, 2*$arms*$len*$len-1);
402 }
403  
404 #------------------------------------------------------------------------------
405 # levels
406  
407 1 ▶use Math::PlanePath::DragonMidpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
408 1 *level_to_n_range = \&Math::PlanePath::DragonMidpoint::level_to_n_range;
409 1 *n_to_level = \&Math::PlanePath::DragonMidpoint::n_to_level;
410  
411 #------------------------------------------------------------------------------
412 1 1;
413 __END__
« PlanePath/GcdRationals.pm ↑ Summary PlanePath/BetaOmega.pm »