PlanePath/WunderlichMeander.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 230 CC 34 SCAR 59.6
CC
34
Cov
0%
rect_to_n_range
702.0
n_to_xy
30.0
xy_to_n
30.0
CRAP 387.0
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 package Math::PlanePath::WunderlichMeander;
20 use 5.004;
21 use strict;
22 #use List::Util 'max';
23 *max = \&Math::PlanePath::_max;
24  
25 use vars '$VERSION', '@ISA';
26 $VERSION = 128;
27 use Math::PlanePath;
28 use Math::PlanePath::Base::NSEW;
29 @ISA = ('Math::PlanePath::Base::NSEW',
30 'Math::PlanePath');
31  
32 use Math::PlanePath::Base::Generic
33 'is_infinite',
34 'round_nearest';
35 use Math::PlanePath::Base::Digits
36 'round_down_pow',
37 'digit_split_lowtohigh',
38 'digit_join_lowtohigh';
39  
40 # uncomment this to run the ### lines
41 #use Smart::Comments;
42  
43  
44 use constant n_start => 0;
45 use constant class_x_negative => 0;
46 use constant class_y_negative => 0;
47 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1;
48 use constant 1.02 _UNDOCUMENTED__dxdy_list_at_n => 5;
49  
50  
51 #------------------------------------------------------------------------------
52  
53 # tables generated by tools/wunderlich-meander-table.pl
54 #
55 my @next_state = (18,18, 0, 0, 0, 9, 27,27, 0, # 0
56 27,27, 9, 9, 9, 0, 18,18, 9, # 9
57 0, 0,18, 18,18,27, 9, 9,18, # 18
58 9, 9,27, 27,27,18, 0, 0,27); # 27
59 my @digit_to_x = (0,1,2, 2,2,1, 1,0,0, # 0
60 2,1,0, 0,0,1, 1,2,2, # 9
61 0,0,0, 1,2,2, 1,1,2, # 18
62 2,2,2, 1,0,0, 1,1,0); # 27
63 my @digit_to_y = (0,0,0, 1,2,2, 1,1,2, # 0
64 2,2,2, 1,0,0, 1,1,0, # 9
65 0,1,2, 2,2,1, 1,0,0, # 18
66 2,1,0, 0,0,1, 1,2,2); # 27
67 my @xy_to_digit = (0,7,8, 1,6,5, 2,3,4, # 0
68 4,3,2, 5,6,1, 8,7,0, # 9
69 0,1,2, 7,6,3, 8,5,4, # 18
70 4,5,8, 3,6,7, 2,1,0); # 27
71 my @min_digit = (0,0,0,1,2,1, # 0
72 0,0,0,1,2,1,
73 0,0,0,1,2,1,
74 7,5,3,3,3,5,
75 8,5,4,4,4,5,
76 7,6,3,3,3,6,
77 4,4,4,5,8,5, # 36
78 3,3,3,5,7,5,
79 2,1,0,0,0,1,
80 2,1,0,0,0,1,
81 2,1,0,0,0,1,
82 3,3,3,6,7,6,
83 0,0,0,7,8,7, # 72
84 0,0,0,5,5,6,
85 0,0,0,3,4,3,
86 1,1,1,3,4,3,
87 2,2,2,3,4,3,
88 1,1,1,5,5,6,
89 4,3,2,2,2,3, # 108
90 4,3,1,1,1,3,
91 4,3,0,0,0,3,
92 5,5,0,0,0,6,
93 8,7,0,0,0,7,
94 5,5,1,1,1,6);
95 my @max_digit = (0,1,2,2,2,1, # 0
96 7,7,7,6,3,6,
97 8,8,8,6,4,6,
98 8,8,8,6,4,6,
99 8,8,8,5,4,5,
100 7,7,7,6,3,6,
101 4,5,8,8,8,5, # 36
102 4,6,8,8,8,6,
103 4,6,8,8,8,6,
104 3,6,7,7,7,6,
105 2,2,2,1,0,1,
106 3,6,7,7,7,6,
107 0,7,8,8,8,7, # 72
108 1,7,8,8,8,7,
109 2,7,8,8,8,7,
110 2,6,6,6,5,6,
111 2,3,4,4,4,3,
112 1,6,6,6,5,6,
113 4,4,4,3,2,3, # 108
114 5,6,6,6,2,6,
115 8,8,8,7,2,7,
116 8,8,8,7,1,7,
117 8,8,8,7,0,7,
118 5,6,6,6,1,6);
119  
120 sub n_to_xy {
121 my ($self, $n) = @_;
122 ### WunderlichMeander n_to_xy(): $n
123  
124 if ($n < 0) { return; }
125 if (is_infinite($n)) { return ($n,$n); }
126  
127 {
128 # ENHANCE-ME: determine dx/dy direction from last state, not full
129 # calculation of N+1
130 my $int = int($n);
131 if ($n != $int) {
132 my $frac = $n - $int; # inherit possible BigFloat/BigRat
133 my ($x1,$y1) = $self->n_to_xy($int);
134 my ($x2,$y2) = $self->n_to_xy($int+1);
135 my $dx = $x2-$x1;
136 my $dy = $y2-$y1;
137 return ($frac*$dx + $x1, $frac*$dy + $y1);
138 }
139 $n = $int;
140 }
141  
142 my @digits = digit_split_lowtohigh($n,9);
143 my $len = ($n*0 + 3) ** scalar(@digits); # inherit bignum 3
144  
145 ### digits: join(', ',@digits)." count ".scalar(@digits)
146 ### $len
147  
148 my $state = ($#digits & 1 ? 18 : 0);
149 my $x = 0;
150 my $y = 0;
151  
152 while (@digits) {
153 $len /= 3;
154 $state += pop @digits; # high to low
155  
156 ### $len
157 ### $state
158 ### digit_to_x: $digit_to_x[$state]
159 ### digit_to_y: $digit_to_y[$state]
160 ### next_state: $next_state[$state]
161  
162 $x += $len * $digit_to_x[$state];
163 $y += $len * $digit_to_y[$state];
164 $state = $next_state[$state];
165 }
166  
167 ### final: "$x,$y"
168 return ($x, $y);
169 }
170  
171 sub xy_to_n {
172 my ($self, $x, $y) = @_;
173 ### WunderlichMeander xy_to_n(): "$x, $y"
174  
175 $x = round_nearest ($x);
176 $y = round_nearest ($y);
177 if ($x < 0 || $y < 0) {
178 return undef;
179 }
180 if (is_infinite($x)) {
181 return $x;
182 }
183 if (is_infinite($y)) {
184 return $y;
185 }
186  
187 my @xdigits = digit_split_lowtohigh ($x, 3);
188 my @ydigits = digit_split_lowtohigh ($y, 3);
189 my $level = max($#xdigits,$#ydigits);
190 my $state = ($level & 1 ? 18 : 0);
191 my @ndigits;
192 foreach my $i (reverse 0 .. $level) { # high to low
193 my $ndigit = $xy_to_digit[$state
194 + 3*($xdigits[$i]||0)
195 + ($ydigits[$i]||0)];
196 $ndigits[$i] = $ndigit;
197 $state = $next_state[$state+$ndigit];
198 }
199  
200 return digit_join_lowtohigh (\@ndigits, 9,
201 $x * 0 * $y); # bignum zero
202 }
203  
204 # exact
205 sub rect_to_n_range {
206 my ($self, $x1,$y1, $x2,$y2) = @_;
207 ### WunderlichMeander rect_to_n_range(): "$x1,$y1, $x2,$y2"
208  
209 $x1 = round_nearest ($x1);
210 $x2 = round_nearest ($x2);
211 ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
212  
213 $y1 = round_nearest ($y1);
214 $y2 = round_nearest ($y2);
215 ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
216  
217 if ($x2 < 0 || $y2 < 0) {
218 return (1, 0);
219 }
220  
221 my ($len, $level) = round_down_pow (max($x2,$y2), 3);
222 ### $len
223 ### $level
224 if (is_infinite($level)) {
225 return (0, $level);
226 }
227  
228 my $n_min = my $n_max
229 = my $x_min = my $y_min
230 = my $x_max = my $y_max
231 = ($x1 * 0 * $x2 * $y1 * $y2); # inherit bignum 0
232  
233 my $min_state
234 = my $max_state
235 = ($level & 1 ? 18 : 0);
236  
237 # x__ 0
238 # xx_ 1
239 # xxx 2
240 # _xx 3
241 # __x 4
242 # _x_ 5
243 #
244 while ($level >= 0) {
245 my $l2 = 2*$len;
246 {
247 my $x_cmp1 = $x_min + $len;
248 my $y_cmp1 = $y_min + $len;
249 my $x_cmp2 = $x_min + $l2;
250 my $y_cmp2 = $y_min + $l2;
251 my $digit = $min_digit[4*$min_state # 4*9=36 apart
252 + ($x1 >= $x_cmp2 ? 4
253 : $x1 >= $x_cmp1 ? ($x2 < $x_cmp2 ? 5 : 3)
254 : ($x2 < $x_cmp1 ? 0
255 : $x2 < $x_cmp2 ? 1 : 2))
256 + ($y1 >= $y_cmp2 ? 6*4
257 : $y1 >= $y_cmp1 ? ($y2 < $y_cmp2 ? 6*5 : 6*3)
258 : ($y2 < $y_cmp1 ? 6*0
259 : $y2 < $y_cmp2 ? 6*1 : 6*2))];
260  
261 # my $key = 4*$min_state # 4*9=36 apart
262 # + ($x1 >= $x_cmp2 ? 4
263 # : $x1 >= $x_cmp1 ? ($x2 < $x_cmp2 ? 5 : 3)
264 # : ($x2 < $x_cmp1 ? 0
265 # : $x2 < $x_cmp2 ? 1 : 2))
266 # + ($y1 >= $y_cmp2 ? 6*4
267 # : $y1 >= $y_cmp1 ? ($y2 < $y_cmp2 ? 6*5 : 6*3)
268 # : ($y2 < $y_cmp1 ? 6*0
269 # : $y2 < $y_cmp2 ? 6*1 : 6*2));
270 # ### $min_state
271 # ### $len
272 # ### $l2
273 # ### $key
274 # ### $x_cmp1
275 # ### $x_cmp2
276 # ### $digit
277  
278  
279 $n_min = 9*$n_min + $digit;
280 $min_state += $digit;
281 $x_min += $len * $digit_to_x[$min_state];
282 $y_min += $len * $digit_to_y[$min_state];
283 $min_state = $next_state[$min_state];
284 }
285 {
286 my $x_cmp1 = $x_max + $len;
287 my $y_cmp1 = $y_max + $len;
288 my $x_cmp2 = $x_max + $l2;
289 my $y_cmp2 = $y_max + $l2;
290 my $digit = $max_digit[4*$max_state # 4*9=36 apart
291 + ($x1 >= $x_cmp2 ? 4
292 : $x1 >= $x_cmp1 ? ($x2 < $x_cmp2 ? 5 : 3)
293 : ($x2 < $x_cmp1 ? 0
294 : $x2 < $x_cmp2 ? 1 : 2))
295 + ($y1 >= $y_cmp2 ? 6*4
296 : $y1 >= $y_cmp1 ? ($y2 < $y_cmp2 ? 6*5 : 6*3)
297 : ($y2 < $y_cmp1 ? 6*0
298 : $y2 < $y_cmp2 ? 6*1 : 6*2))];
299  
300 # my $key = 4*$max_state # 4*9=36 apart
301 # + ($x1 >= $x_cmp2 ? 4
302 # : $x1 >= $x_cmp1 ? ($x2 < $x_cmp2 ? 5 : 3)
303 # : ($x2 < $x_cmp1 ? 0
304 # : $x2 < $x_cmp2 ? 1 : 2))
305 # + ($y1 >= $y_cmp2 ? 4
306 # : $y1 >= $y_cmp1 ? ($y2 < $y_cmp2 ? 5 : 3)
307 # : ($y2 < $y_cmp1 ? 0
308 # : $y2 < $y_cmp2 ? 1 : 2));
309 # ### $max_state
310 # ### $len
311 # ### $l2
312 # ### $x_key
313 # ### $key
314 # ### $x_max
315 # ### $y_max
316 # ### $x_cmp1
317 # ### $x_cmp2
318 # ### $y_cmp1
319 # ### $y_cmp2
320 # ### $digit
321 # ### max digit: $max_digit[$key]
322  
323 $n_max = 9*$n_max + $digit;
324 $max_state += $digit;
325 $x_max += $len * $digit_to_x[$max_state];
326 $y_max += $len * $digit_to_y[$max_state];
327 $max_state = $next_state[$max_state];
328 }
329  
330 $len = int($len/3);
331 $level--;
332 }
333 return ($n_min, $n_max);
334 }
335  
336 #-----------------------------------------------------------------------------
337 # level_to_n_range()
338  
339 use Math::PlanePath::SquareReplicate;
340 *level_to_n_range = \&Math::PlanePath::SquareReplicate::level_to_n_range;
341 *n_to_level = \&Math::PlanePath::SquareReplicate::n_to_level;
342  
343 #-----------------------------------------------------------------------------
344 1;
345 __END__