PlanePath/BetaOmega.pm

filter: Statementstmt 93.2% 137 / 147 Branchbran 81.2% 52 / 64 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 90.6% 214 / 236 CC 38 SCAR 24.8
CC
38
Cov
90%
rect_to_n_range
21.6
xy_to_n
11.1
n_to_xy
7.1
CRAP 12.0
« PlanePath/FilledRings.pm ↑ Summary NumSeq/PlanePathTurn.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 # math-image --path=BetaOmega --lines --scale=20
20 #
21 # math-image --path=BetaOmega --all --output=numbers_dash
22  
23 # http://www.upb.de/pc2/papers/files/pdfps399main.toappear.ps # gone
24 # http://www.uni-paderborn.de/pc2/papers/files/pdfps399main.toappear.ps
25 # http://wwwcs.upb.de/pc2/papers/files/399.ps # gone
26 #
27 # copy ?
28 # http://www.cs.uleth.ca/~wismath/cccg/papers/27l.ps
29  
30  
31 package Math::PlanePath::BetaOmega;
32 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 $VERSION = 128;
37 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 @ISA = ('Math::PlanePath::Base::NSEW',
40 'Math::PlanePath');
41  
42 use Math::PlanePath::Base::Generic
43 2 'is_infinite',
44 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 use Math::PlanePath::Base::Digits
46 2 'round_down_pow',
47 'bit_split_lowtohigh',
48 'digit_split_lowtohigh',
49 2 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # uncomment this to run the ### lines
52 #use Smart::Comments;
53  
54  
55  
56  
57 2 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use constant y_negative_at_n => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 *xy_is_visited = \&Math::PlanePath::Base::Generic::_xy_is_visited_x_positive;
61 2 ▶use constant 1.02 _UNDOCUMENTED__dxdy_list_at_n => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63  
64 #------------------------------------------------------------------------------
65  
66 # tables generated by tools/beta-omega-table.pl
67 #
68 2 my @next_state = (28, 8,36,88, 8,28,32,76, 4,16,44,64, 16, 4,40,84,
69 12,24,52,72, 24,12,48,92, 20, 0,60,80, 0,20,56,68,
70 68, 4,40,60, 64, 0,60,40, 76,12,48,36, 72, 8,36,48,
71 84,20,56,44, 80,16,44,56, 92,28,32,52, 88,24,52,32,
72 28, 8,36,48, 8,28,32,52, 4,16,44,56, 16, 4,40,60,
73 12,24,52,32, 24,12,48,36, 20, 0,60,40, 0,20,56,44);
74 2 my @digit_to_x = (0,0,1,1, 0,1,1,0, 1,0,0,1, 1,1,0,0,
75 1,1,0,0, 1,0,0,1, 0,1,1,0, 0,0,1,1,
76 1,1,0,0, 0,1,1,0, 1,0,0,1, 0,0,1,1,
77 0,0,1,1, 1,0,0,1, 0,1,1,0, 1,1,0,0,
78 0,0,1,1, 0,1,1,0, 1,0,0,1, 1,1,0,0,
79 1,1,0,0, 1,0,0,1, 0,1,1,0, 0,0,1,1);
80 2 my @digit_to_y = (0,1,1,0, 0,0,1,1, 0,0,1,1, 0,1,1,0,
81 1,0,0,1, 1,1,0,0, 1,1,0,0, 1,0,0,1,
82 0,1,1,0, 1,1,0,0, 1,1,0,0, 0,1,1,0,
83 1,0,0,1, 0,0,1,1, 0,0,1,1, 1,0,0,1,
84 0,1,1,0, 0,0,1,1, 0,0,1,1, 0,1,1,0,
85 1,0,0,1, 1,1,0,0, 1,1,0,0, 1,0,0,1);
86 2 my @xy_to_digit = (0,1,3,2, 0,3,1,2, 1,2,0,3, 3,2,0,1,
87 2,3,1,0, 2,1,3,0, 3,0,2,1, 1,0,2,3,
88 3,2,0,1, 3,0,2,1, 2,1,3,0, 0,1,3,2,
89 1,0,2,3, 1,2,0,3, 0,3,1,2, 2,3,1,0,
90 0,1,3,2, 0,3,1,2, 1,2,0,3, 3,2,0,1,
91 2,3,1,0, 2,1,3,0, 3,0,2,1, 1,0,2,3);
92 2 my @min_digit = (0,0,3,0, 0,2,1,1, 2,undef,undef,undef,
93 0,0,1,0, 0,1,3,2, 2,undef,undef,undef,
94 1,0,0,1, 0,0,2,2, 3,undef,undef,undef,
95 3,0,0,2, 0,0,2,1, 1,undef,undef,undef,
96 2,1,1,2, 0,0,3,0, 0,undef,undef,undef,
97 2,2,3,1, 0,0,1,0, 0,undef,undef,undef,
98 3,2,2,0, 0,1,0,0, 1,undef,undef,undef,
99 1,1,2,0, 0,2,0,0, 3,undef,undef,undef,
100 3,0,0,2, 0,0,2,1, 1,undef,undef,undef,
101 3,2,2,0, 0,1,0,0, 1,undef,undef,undef,
102 2,2,3,1, 0,0,1,0, 0,undef,undef,undef,
103 0,0,3,0, 0,2,1,1, 2,undef,undef,undef,
104 1,1,2,0, 0,2,0,0, 3,undef,undef,undef,
105 1,0,0,1, 0,0,2,2, 3,undef,undef,undef,
106 0,0,1,0, 0,1,3,2, 2,undef,undef,undef,
107 2,1,1,2, 0,0,3,0, 0,undef,undef,undef,
108 0,0,3,0, 0,2,1,1, 2,undef,undef,undef,
109 0,0,1,0, 0,1,3,2, 2,undef,undef,undef,
110 1,0,0,1, 0,0,2,2, 3,undef,undef,undef,
111 3,0,0,2, 0,0,2,1, 1,undef,undef,undef,
112 2,1,1,2, 0,0,3,0, 0,undef,undef,undef,
113 2,2,3,1, 0,0,1,0, 0,undef,undef,undef,
114 3,2,2,0, 0,1,0,0, 1,undef,undef,undef,
115 1,1,2,0, 0,2,0,0, 3);
116 2 my @max_digit = (0,3,3,1, 3,3,1,2, 2,undef,undef,undef,
117 0,1,1,3, 3,2,3,3, 2,undef,undef,undef,
118 1,1,0,2, 3,3,2,3, 3,undef,undef,undef,
119 3,3,0,3, 3,1,2,2, 1,undef,undef,undef,
120 2,2,1,3, 3,1,3,3, 0,undef,undef,undef,
121 2,3,3,2, 3,3,1,1, 0,undef,undef,undef,
122 3,3,2,3, 3,2,0,1, 1,undef,undef,undef,
123 1,2,2,1, 3,3,0,3, 3,undef,undef,undef,
124 3,3,0,3, 3,1,2,2, 1,undef,undef,undef,
125 3,3,2,3, 3,2,0,1, 1,undef,undef,undef,
126 2,3,3,2, 3,3,1,1, 0,undef,undef,undef,
127 0,3,3,1, 3,3,1,2, 2,undef,undef,undef,
128 1,2,2,1, 3,3,0,3, 3,undef,undef,undef,
129 1,1,0,2, 3,3,2,3, 3,undef,undef,undef,
130 0,1,1,3, 3,2,3,3, 2,undef,undef,undef,
131 2,2,1,3, 3,1,3,3, 0,undef,undef,undef,
132 0,3,3,1, 3,3,1,2, 2,undef,undef,undef,
133 0,1,1,3, 3,2,3,3, 2,undef,undef,undef,
134 1,1,0,2, 3,3,2,3, 3,undef,undef,undef,
135 3,3,0,3, 3,1,2,2, 1,undef,undef,undef,
136 2,2,1,3, 3,1,3,3, 0,undef,undef,undef,
137 2,3,3,2, 3,3,1,1, 0,undef,undef,undef,
138 3,3,2,3, 3,2,0,1, 1,undef,undef,undef,
139 1,2,2,1, 3,3,0,3, 3);
140  
141 sub n_to_xy {
142 2185 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=7 SCAR=19.6
Pod documented
143 ### BetaOmega n_to_xy(): $n
144 ### hex: sprintf "%#X", $n
145  
146 2185 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 2185 2185
147 2185 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 2185 2185
148  
149 2185 my $int = int($n);
150 2185 $n -= $int; # remaining fraction, preserve possible BigFloat/BigRat
151  
152 2185 my $zero = $int * 0; # inherit bignum
153 2185 my @ndigits = digit_split_lowtohigh($int,4);
154 ### ndigits: join(', ',@ndigits)." count ".scalar(@ndigits)
155  
156 2185 ▶ my $state = ($#ndigits & 1 ? 28 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$#ndigits & 1 ? : 479 1706 2185
157 2185 ▶ my $dirstate = ($#ndigits & 1 ? 0 : 28); # default if all $ndigit==3
Branch: 1 branch
BranchTrueFalseTotal
$#ndigits & 1 ? : 479 1706 2185
158 2185 my @xbits;
159 my @ybits;
160  
161 2185 foreach my $i (reverse 0 .. $#ndigits) {
162 10208 my $ndigit = $ndigits[$i]; # high to low
163 10208 $state += $ndigit;
164 10208 ▶ if ($ndigit != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigit != 3) 7487 2721 10208
165 7487 $dirstate = $state; # lowest non-3 digit
166 }
167  
168 ### $ndigit
169 ### $state
170 ### $dirstate
171 ### digit_to_x: $digit_to_x[$state]
172 ### digit_to_y: $digit_to_y[$state]
173 ### next_state: $next_state[$state]
174  
175 10208 $xbits[$i] = $digit_to_x[$state];
176 10208 $ybits[$i] = $digit_to_y[$state];
177 10208 $state = $next_state[$state];
178 }
179  
180 ### $dirstate
181 ### frac: $n
182 ### Ymin: - (((4+$zero)**int($#ndigits/2) - 1) * 2 / 3)
183  
184 # with $n fractional part
185 2185 return ($n * ($digit_to_x[$dirstate+1] - $digit_to_x[$dirstate])
186 + digit_join_lowtohigh(\@xbits, 2, $zero),
187  
188 $n * ($digit_to_y[$dirstate+1] - $digit_to_y[$dirstate])
189 + (digit_join_lowtohigh(\@ybits, 2, $zero)
190  
191 # Ymin = - (4^floor(level/2) - 1) * 2 / 3
192 - (((4+$zero)**int(scalar(@ndigits)/2) - 1) * 2 / 3)));
193 }
194  
195  
196 # ($len,$level) rounded down for $y ...
197 sub _y_round_down_len_level {
198 53 ▶ my ($y) = @_;
Subroutine _y_round_down_len_level: called CC=3 SCAR=11.0
199 53 my $pos;
200 53 ▶ if ($pos = ($y >= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos = $y >= 0) { } 26 27 53
201 # eg. 1 becomes 3, or 5 becomes 15, 2^k-1
202 26 $y = 3 * $y;
203 } else {
204 # eg. -2 becomes 7, or -10 becomes 31, 2^k-1
205 27 $y = 1 - 3*$y;
206 }
207 53 my ($len, $level) = round_down_pow($y,2);
208  
209 # Make positive y give even level, and negative y give odd level.
210 # If positive and odd then reduce, or if negative and even then reduce.
211 53 ▶ if (($level & 1) == $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (($level & 1) == $pos) 41 12 53
212 41 $level--;
213 41 $len /= 2;
214 }
215  
216 53 return ($len, $level);
217 }
218  
219 sub xy_to_n {
220 16 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=10 SCAR=24.1
Pod documented
221 ### BetaOmega xy_to_n(): "$x, $y"
222  
223 16 $x = round_nearest ($x);
224 16 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 0 16 16
225 0 return undef;
226 }
227 16 ▶ if (is_infinite($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 16 16
228 0 return $x;
229 }
230 16 my @xbits = bit_split_lowtohigh($x);
231  
232 16 $y = round_nearest ($y);
233 16 my $zero = ($x * 0 * $y);
234 16 my ($len, $level) = _y_round_down_len_level ($y);
235 ### y: "len=$len level=$level"
236  
237 16 ▶ if ($#xbits > $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#xbits > $level) 0 16 16
238 ### increase level to xbits ...
239 0 $level = $#xbits;
240 0 $len = (2+$zero) ** $level;
241 }
242 ### $len
243 ### $level
244  
245 16 ▶ $y += (($level&1 ? 4 : 2) * $len - 2) / 3;
Branch: 1 branch
BranchTrueFalseTotal
$level & 1 ? : 8 8 16
246 ### offset y to: $y
247 16 ▶ if (is_infinite($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 16 16
248 0 return $y;
249 }
250 16 my @ybits = bit_split_lowtohigh($y);
251 16 ▶ my $state = ($level & 1 ? 28 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$level & 1 ? : 8 8 16
252  
253 16 my @ndigits;
254 16 foreach my $i (reverse 0 .. $level) { # high to low
255 ### at: "i=$i state=$state xbit=".($xbits[$i]||0)." ybit=".($ybits[$i]||0)
256  
257 40 ▶ my $ndigit = $xy_to_digit[$state + 2*($xbits[$i]||0) + ($ybits[$i]||0)];
Condition100%4/4
$xbits[$i] || 0
l!l
1228
$ybits[$i] || 0
l!l
2020
MC/DC100%1/1
$xbits[$i] || 0
$xbits[$i]
MC/DC100%1/1
$ybits[$i] || 0
$ybits[$i]
Truth table100%2/2
A
A= $xbits[$i]
Aresult
11
00
Truth table100%2/2
A
A= $ybits[$i]
Aresult
11
00
258 40 $ndigits[$i] = $ndigit;
259 40 $state = $next_state[$state+$ndigit];
260 }
261  
262 16 return digit_join_lowtohigh(\@ndigits, 4, $zero);
263 }
264  
265 # exact
266 sub rect_to_n_range {
267 24 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=21 SCAR=30.7
Pod documented
268 ### BetaOmega rect_to_n_range(): "$x1,$y1, $x2,$y2"
269  
270 24 $x1 = round_nearest ($x1);
271 24 $x2 = round_nearest ($x2);
272 24 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 24 24
273  
274 24 ▶ if ($x2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0) 0 24 24
275 0 return (1, 0);
276 }
277  
278 24 $y1 = round_nearest ($y1);
279 24 $y2 = round_nearest ($y2);
280 24 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 12 12 24
281  
282 24 my ($len, $level) = round_down_pow ($x2, 2);
283 ### x len/level: "$len $level"
284  
285 # If y1/y2 both positive or both negative then only look at the bigger of
286 # the two. If y1 negative and y2 positive then consider both.
287 24 ▶ foreach my $y (($y2 > 0 ? ($y2) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$y2 > 0 ? : 8 16 24
$y1 < 0 ? : 12 12 24
288 ($y1 < 0 ? ($y1) : ())) {
289 20 my ($ylen, $ylevel) = _y_round_down_len_level ($y);
290 ### y len/level: "$ylen $ylevel"
291 20 ▶ if ($ylevel > $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ylevel > $level) 12 8 20
292 12 $level = $ylevel;
293 12 $len = $ylen;
294 }
295 }
296 24 ▶ if (is_infinite($len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($len)) 0 24 24
297 0 return (0, $len);
298 }
299  
300 24 my $n_min = my $n_max = 0;
301 24 my $y_min = my $y_max = - (4**int(($level+1)/2) - 1) * 2 / 3;
302 24 my $x_min = my $x_max = 0;
303 24 ▶ my $min_state = my $max_state = ($level & 1 ? 28 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$level & 1 ? : 12 12 24
304 ### $x_min
305 ### $y_min
306  
307 24 ▶ while ($level >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level >= 0) 52 24 76
308 ### $level
309 ### $len
310 {
311 52 my $x_cmp = $x_min + $len;
312 52 my $y_cmp = $y_min + $len;
313 52 ▶ my $digit = $min_digit[3*$min_state
Branch: 4 branches
BranchTrueFalseTotal
$x1 >= $x_cmp ? : 0 52 52
$x2 >= $x_cmp ? : 22 30 52
$y1 >= $y_cmp ? : 4 48 52
$y2 >= $y_cmp ? : 28 20 48
314 + ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0)
315 + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0)];
316  
317 # my $xr = ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0);
318 # my $yr = ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0);
319 # my $key = 3*$min_state + ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0) + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0);
320 # ### min at: "min_state=$min_state $x_min,$y_min cmp $x_cmp,$y_cmp"
321 # ### min_state: state_string($min_state)
322 # ### $xr
323 # ### $yr
324 # ### $key
325 # ### min digit: $digit
326 # ### min key: $key
327 # ### y offset: $digit_to_y[$max_state+$digit]
328  
329 52 $n_min = 4*$n_min + $digit;
330 52 $min_state += $digit;
331 52 ▶ if ($digit_to_x[$min_state]) { $x_min += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_x[$min_state]) 0 52 52
332 52 $y_min += $len * $digit_to_y[$min_state];
333 52 $min_state = $next_state[$min_state];
334 }
335 {
336 52 my $x_cmp = $x_max + $len;
337 52 my $y_cmp = $y_max + $len;
338 52 ▶ my $digit = $max_digit[3*$max_state
Branch: 4 branches
BranchTrueFalseTotal
$x1 >= $x_cmp ? : 0 52 52
$x2 >= $x_cmp ? : 16 36 52
$y1 >= $y_cmp ? : 14 38 52
$y2 >= $y_cmp ? : 34 4 38
339 + ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0)
340 + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0)];
341  
342 # my $xr = ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0);
343 # my $yr = ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0);
344 # my $key = 3*$min_state + ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0) + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0);
345 # ### max at: "max_state=$max_state $x_max,$y_max cmp $x_cmp,$y_cmp"
346 # ### $x_cmp
347 # ### $y_cmp
348 # ### $xr
349 # ### $yr
350 # ### $key
351 # ### max digit: $digit
352 # ### x offset: $digit_to_x[$max_state+$digit]
353 # ### y offset: $digit_to_y[$max_state+$digit]
354 # ### y digit offset: $digit_to_y[$max_state+$digit]
355 # ### y min shift part: - ($level&1)
356  
357 52 $n_max = 4*$n_max + $digit;
358 52 $max_state += $digit;
359 52 ▶ if ($digit_to_x[$max_state]) { $x_max += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_x[$max_state]) 13 39 52
360 52 $y_max += $len * $digit_to_y[$max_state];
361 52 $max_state = $next_state[$max_state];
362 }
363  
364 52 $len = int($len/2);
365 52 $level--;
366 }
367  
368 24 return ($n_min, $n_max);
369 }
370  
371 #------------------------------------------------------------------------------
372 # levels
373  
374 2 ▶use Math::PlanePath::HilbertCurve;
Subroutine BEGIN: called CC=1 SCAR=0.0
375 2 *level_to_n_range = \&Math::PlanePath::HilbertCurve::level_to_n_range;
376 2 *n_to_level = \&Math::PlanePath::HilbertCurve::n_to_level;
377  
378 #------------------------------------------------------------------------------
379 2 1;
380 __END__
« PlanePath/FilledRings.pm ↑ Summary NumSeq/PlanePathTurn.pm »