PlanePath/DragonMidpoint.pm

filter: Statementstmt 79.4% 139 / 175 Branchbran 72.5% 29 / 40 Conditioncond 53.8% 14 / 26 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 68.1% 15 / 22 Podpod 100% 9 / 9 totaltotal 74.7% 213 / 285 CC 31 SCAR 24.7
CC
31
Cov
76%
_xy_to_arm
42.0
n_to_xy
15.0
n_to_level
12.0
CRAP 11.8
« NumSeq/PlanePathTurn.pm ↑ Summary PlanePath/KochCurve.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=DragonMidpoint --lines --scale=20
20 # math-image --path=DragonMidpoint --all --output=numbers_dash
21  
22 # A006466 contfrac 2*sum( 1/2^(2^n)), 1 and 2 only
23 # a(5n) recurrence ...
24 # 1,1,1,1, 2,
25 # 1,1,1,1,1,1,1, 2,
26 # 1,1,1,1, 2,
27 # 1,1,1,1, 2,
28 # 1, 2,
29 # 1,1,1,1, 2,
30 # 1,1,1,1,1,1,1, 2,
31 # 1,1,1,1, 2,
32 # 1, 2,
33 # 1,1,1,1,1,1,1, 2,
34 # 1,1,1,1, 2,
35 # 1, 2,
36 # 1,1,1,1, 2,
37 # 1,1,1,1, 2,
38 # 1,1,1,1,1,1,1, 2,
39 # 1,1,1,1, 2,
40 # 1, 2,
41 # 1,1,1,1,1,1,1, 2,
42 # 1,1,1,1, 2,
43 # 1,1,1,1, 2,
44 # 1, 2
45 # A076214 in decimal
46 #
47 # A073097 number of 4s - 6s - 2s - 1 is -1,0,1
48 # A081769 positions of 2s
49 # A073088 cumulative total multiples of 4 roughly, hence (4n-3-cum)/2
50 #
51 # A088435 (contfrac+1)/2 of sum(k>=1,1/3^(2^k)).
52 # A007404 in decimal
53 #
54  
55  
56 package Math::PlanePath::DragonMidpoint;
57 5 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 5 ▶use List::Util 'min'; # 'max'
Subroutine BEGIN: called CC=1 SCAR=0.0
60 5 *max = \&Math::PlanePath::_max;
61  
62 5 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 5 $VERSION = 128;
64 5 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 5 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 5 @ISA = ('Math::PlanePath::Base::NSEW',
67 'Math::PlanePath');
68  
69 use Math::PlanePath::Base::Generic
70 5 'is_infinite',
71 5 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 use Math::PlanePath::Base::Digits
73 5 'bit_split_lowtohigh',
74 'digit_join_lowtohigh',
75 5 ▶ 'round_up_pow';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 5 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
77  
78 # uncomment this to run the ### lines
79 # use Smart::Comments;
80  
81  
82 # whole plane when arms==4
83 5 ▶use Math::PlanePath::DragonCurve;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85  
86 5 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 5 use constant parameter_info_array => [ { name => 'arms',
88 share_key => 'arms_4',
89 display => 'Arms',
90 type => 'integer',
91 minimum => 1,
92 maximum => 4,
93 default => 1,
94 width => 1,
95 description => 'Arms',
96 5 ▶ } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 {
99 5 my @x_negative_at_n = (undef, 6,5,2,2);
100 sub x_negative_at_n {
101 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
102 0 return $x_negative_at_n[$self->{'arms'}];
103 }
104 }
105 {
106 5 my @y_negative_at_n = (undef, 27,19,11,7);
107 sub y_negative_at_n {
108 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
109 0 return $y_negative_at_n[$self->{'arms'}];
110 }
111 }
112 {
113 5 my @_UNDOCUMENTED__dxdy_list_at_n = (undef, 9, 9, 5, 3);
114 sub _UNDOCUMENTED__dxdy_list_at_n {
115 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
116 0 return $_UNDOCUMENTED__dxdy_list_at_n[$self->{'arms'}];
117 }
118 }
119  
120  
121 #------------------------------------------------------------------------------
122  
123 5 sub new {
124 15 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
125 15 ▶ $self->{'arms'} = max(1, min(4, $self->{'arms'} || 1));
Condition100%2/2
$self->{'arms'} || 1
l!l
123
MC/DC100%1/1
$self->{'arms'} || 1
$self->{'arms'}
Truth table100%2/2
A
A= $self->{'arms'}
Aresult
11
00
126 15 return $self;
127 }
128  
129 # sub n_to_xy {
130 # my ($self, $n) = @_;
131 # ### DragonMidpoint n_to_xy(): $n
132 #
133 # if ($n < 0) { return; }
134 # if (is_infinite($n)) { return ($n, $n); }
135 #
136 # {
137 # my $int = int($n);
138 # if ($n != $int) {
139 # my ($x1,$y1) = $self->n_to_xy($int);
140 # my ($x2,$y2) = $self->n_to_xy($int+$self->{'arms'});
141 # my $frac = $n - $int; # inherit possible BigFloat
142 # my $dx = $x2-$x1;
143 # my $dy = $y2-$y1;
144 # return ($frac*$dx + $x1, $frac*$dy + $y1);
145 # }
146 # $n = $int; # BigFloat int() gives BigInt, use that
147 # }
148 #
149 # my ($x1,$y1) = Math::PlanePath::DragonCurve->n_to_xy($n);
150 # my ($x2,$y2) = Math::PlanePath::DragonCurve->n_to_xy($n+1);
151 #
152 # my $dx = $x2-$x1;
153 # my $dy = $y2-$y1;
154 # return ($x1+$y1 + ($dx+$dy-1)/2,
155 # $y1-$x1 + ($dy-$dx+1)/2);
156 # }
157  
158 sub n_to_xy {
159 264 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=15 SCAR=27.1
Pod documented
160 ### DragonMidpoint n_to_xy(): $n
161  
162 264 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 264 264
163 264 ▶ if (is_infinite($n)) { return ($n, $n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 264 264
164  
165 264 my $frac;
166 {
167 264 my $int = int($n);
168 264 $frac = $n - $int; # inherit possible BigFloat
169 264 $n = $int; # BigFloat int() gives BigInt, use that
170 }
171 264 my $zero = ($n * 0); # inherit bignum 0
172  
173 # arm as initial rotation
174 264 my $rot = _divrem_mutate ($n, $self->{'arms'});
175  
176 ### $arms
177 ### rot from arm: $rot
178 ### $n
179  
180 # ENHANCE-ME: sx,sy just from len,len
181 264 my @digits = bit_split_lowtohigh($n);
182 264 my @sx;
183 my @sy;
184  
185 {
186 264 my $sx = $zero + 1;
187 264 my $sy = -$sx;
188 264 foreach (@digits) {
189 2009 push @sx, $sx;
190 2009 push @sy, $sy;
191  
192 # (sx,sy) + rot+90(sx,sy)
193 2009 ($sx,$sy) = ($sx - $sy,
194 $sy + $sx);
195 }
196 }
197  
198 ### @digits
199 264 my $rev = 0;
200 264 my $x = $zero;
201 264 my $y = $zero;
202 264 my $above_low_zero = 0;
203  
204 264 ▶ for (my $i = $#digits; $i >= 0; $i--) { # high to low
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 2009 264 2273
205 2009 my $digit = $digits[$i];
206 2009 my $sx = $sx[$i];
207 2009 my $sy = $sy[$i];
208 ### at: "$x,$y $digit side $sx,$sy"
209 ### $rot
210  
211 2009 ▶ if ($rot & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 2) 955 1054 2009
212 955 $sx = -$sx;
213 955 $sy = -$sy;
214 }
215 2009 ▶ if ($rot & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 1) 1000 1009 2009
216 1000 ($sx,$sy) = (-$sy,$sx);
217 }
218 ### rotated side: "$sx,$sy"
219  
220 2009 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) { } 980 1029 2009
221 980 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 485 495 980
222 485 $x -= $sy;
223 485 $y += $sx;
224 ### rev add to: "$x,$y next is still rev"
225 } else {
226 495 $above_low_zero = $digits[$i+1];
227 495 $rot ++;
228 495 $rev = 0;
229 ### rev rot, next is no rev ...
230 }
231 } else {
232 1029 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 622 407 1029
233 622 $rot ++;
234 622 $x += $sx;
235 622 $y += $sy;
236 622 $rev = 1;
237 ### plain add to: "$x,$y next is rev"
238 } else {
239 407 $above_low_zero = $digits[$i+1];
240 }
241 }
242 }
243  
244 # Digit above the low zero is the direction of the next turn, 0 for left,
245 # 1 for right.
246 #
247 ### final: "$x,$y rot=$rot above_low_zero=".($above_low_zero||0)
248  
249 264 ▶ if ($rot & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 2) 115 149 264
250 115 $frac = -$frac; # rotate 180
251 115 $x -= 1;
252 }
253 264 ▶ if (($rot+1) & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot + 1 & 2) 154 110 264
254 # rot 1 or 2
255 154 $y += 1;
256 }
257 264 ▶ if (!($rot & 1) && $above_low_zero) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rot & 1 and $above_low_zero) 64 200 264
Condition100%3/3
not $rot & 1 and $above_low_zero
!ll&&!rl&&r
1297164
MC/DC100%2/2
not $rot & 1 and $above_low_zero
not $rot & 1 $above_low_zero
Truth table100%3/3
not(A) and B
A= not $rot & 1B= $above_low_zero
ABresult
0X0
100
111
258 64 $frac = -$frac;
259 }
260 264 $above_low_zero ^= ($rot & 1);
261 264 ▶ if ($above_low_zero) {
Branch: 1 branch
BranchTrueFalseTotal
if ($above_low_zero) { } 139 125 264
262 139 $y = $frac + $y;
263 } else {
264 125 $x = $frac + $x;
265 }
266  
267 ### rotated return: "$x,$y"
268 264 return ($x,$y);
269 }
270  
271 # or tables arithmetically,
272 #
273 # my $ax = ((($x+1) ^ ($y+1)) >> 1) & 1;
274 # my $ay = (($x^$y) >> 1) & 1;
275 # ### assert: $ax == - $yx_adj_x[$y%4]->[$x%4]
276 # ### assert: $ay == - $yx_adj_y[$y%4]->[$x%4]
277 #
278 5 my @yx_adj_x = ([0,1,1,0],
279 [1,0,0,1],
280 [1,0,0,1],
281 [0,1,1,0]);
282  
283 5 my @yx_adj_y = ([0,0,1,1],
284 [0,0,1,1],
285 [1,1,0,0],
286 [1,1,0,0]);
287  
288 # arm $x $y 2 | 1 Y=1
289 # 0 0 0 3 | 0 Y=0
290 # 1 0 1 ----+----
291 # 2 -1 1 X=-1 X=0
292 # 3 -1 0
293 5 my @xy_to_arm = ([0, # x=0,y=0
294 1], # x=0,y=1
295 [3, # x=-1,y=0
296 2]); # x=-1,y=1
297  
298 sub xy_to_n {
299 510 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=8 SCAR=20.8
Pod documented
300 ### DragonMidpoint xy_to_n(): "$x, $y"
301  
302 510 $x = round_nearest($x);
303 510 $y = round_nearest($y);
304  
305 510 { my $overflow = abs($x)+abs($y)+2;
306 510 ▶ if (is_infinite($overflow)) { return $overflow; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($overflow)) 0 510 510
307 }
308 510 my $zero = ($x * 0 * $y);
309 510 my @nbits; # low to high
310  
311 510 ▶ while ($x < -1 || $x > 0 || $y < 0 || $y > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < -1 or $x > 0 or $y < 0 or $y > 1) 547 510 1057
Condition100%9/9
$x < -1 or $x > 0 or $y < 0 or $y > 1
l!l&&r!l&&!r
3301263510
$x < -1 or $x > 0 or $y < 0
l!l&&r!l&&!r
3017284773
$x < -1 or $x > 0
l!l&&r!l&&!r
131717001057
MC/DC100%4/4
$x < -1 or $x > 0 or $y < 0 or $y > 1
$x < -1 $x > 0 $y < 0 $y > 1
Truth table100%5/5
A or B or C or D
A= $x < -1B= $x > 0C= $y < 0D= $y > 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
312 3564 my $y4 = $y % 4;
313 3564 my $x4 = $x % 4;
314 3564 my $ax = $yx_adj_x[$y4]->[$x4];
315 3564 my $ay = $yx_adj_y[$y4]->[$x4];
316  
317 ### at: "$x,$y n=$n axy=$ax,$ay bit=".($ax^$ay)
318  
319 3564 push @nbits, $ax^$ay;
320  
321 3564 $x -= $ax;
322 3564 $y -= $ay;
323 ### assert: ($x+$y)%2 == 0
324 3564 ($x,$y) = (($x+$y)/2, # rotate -45 and divide sqrt(2)
325 ($y-$x)/2);
326 }
327  
328 ### final: "xy=$x,$y"
329  
330 510 my $arm = $xy_to_arm[$x]->[$y];
331 ### $arm
332 510 my $arms_count = $self->arms_count;
333 510 ▶ if ($arm >= $arms_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm >= $arms_count) 110 400 510
334 110 return undef;
335 }
336  
337 400 ▶ if ($arm & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 1) 117 283 400
338 ### flip ...
339 117 @nbits = map {$_^1} @nbits;
340 }
341  
342 400 return digit_join_lowtohigh(\@nbits, 2, $zero) * $arms_count + $arm;
343 }
344  
345 #------------------------------------------------------------------------------
346 # xy_is_visited()
347  
348 sub xy_is_visited {
349 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited: not called CC=2 SCAR=17.9
Pod documented
350 return ($self->{'arms'} >= 4
351 0 ▶ || _xy_to_arm($x,$y) < $self->{'arms'});
Condition0%0/3
$self->{'arms'} >= 4 || _xy_to_arm($x, $y) < $self->{'arms'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'arms'} >= 4 || _xy_to_arm($x, $y) < $self->{'arms'}
$self->{'arms'} >= 4 _xy_to_arm($x, $y) < $self->{'arms'}
Truth table0%0/3
A || B
A= $self->{'arms'} >= 4B= _xy_to_arm($x, $y) < $self->{'arms'}
ABresult
1X1
011
000
352 }
353  
354 # return arm number 0,1,2,3
355 sub _xy_to_arm {
356 0 ▶ my ($x, $y) = @_;
Subroutine _xy_to_arm: not called CC=6 SCAR=37.4
357 ### DragonMidpoint _xy_to_arm(): "$x, $y"
358  
359 0 $x = round_nearest($x);
360 0 $y = round_nearest($y);
361  
362 0 { my $overflow = abs($x)+abs($y)+2;
363 0 ▶ if (is_infinite($overflow)) { return $overflow; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($overflow)) 0 0 0
364 }
365  
366 0 ▶ while ($x < -1 || $x > 0 || $y < 0 || $y > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < -1 or $x > 0 or $y < 0 or $y > 1) 0 0 0
Condition0%0/9
$x < -1 or $x > 0 or $y < 0 or $y > 1
l!l&&r!l&&!r
000
$x < -1 or $x > 0 or $y < 0
l!l&&r!l&&!r
000
$x < -1 or $x > 0
l!l&&r!l&&!r
000
MC/DC0%0/4
$x < -1 or $x > 0 or $y < 0 or $y > 1
$x < -1 $x > 0 $y < 0 $y > 1
Truth table0%0/5
A or B or C or D
A= $x < -1B= $x > 0C= $y < 0D= $y > 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
367 0 my $y4 = $y % 4;
368 0 my $x4 = $x % 4;
369 0 $x -= $yx_adj_x[$y4]->[$x4];
370 0 $y -= $yx_adj_y[$y4]->[$x4];
371  
372 ### assert: ($x+$y)%2 == 0
373 0 ($x,$y) = (($x+$y)/2, # rotate -45 and divide sqrt(2)
374 ($y-$x)/2);
375 }
376 0 return $xy_to_arm[$x]->[$y];
377 }
378  
379 #------------------------------------------------------------------------------
380  
381 # not exact
382 sub rect_to_n_range {
383 94 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=1 SCAR=0.0
Pod documented
384 ### DragonMidpoint rect_to_n_range(): "$x1,$y1 $x2,$y2 arms=$self->{'arms'}"
385 94 $x1 = abs($x1);
386 94 $x2 = abs($x2);
387 94 $y1 = abs($y1);
388 94 $y2 = abs($y2);
389 94 my $xmax = int(max($x1,$x2));
390 94 my $ymax = int(max($y1,$y2));
391 return (0,
392 94 ($xmax*$xmax + $ymax*$ymax + 1) * $self->{'arms'} * 5);
393 }
394  
395 # sub rect_to_n_range {
396 # my ($self, $x1,$y1, $x2,$y2) = @_;
397 # ### DragonMidpoint rect_to_n_range(): "$x1,$y1 $x2,$y2"
398 #
399 # return Math::PlanePath::DragonCurve->rect_to_n_range
400 # (sqrt(2)*$x1, sqrt(2)*$y1, sqrt(2)*$x2, sqrt(2)*$y2);
401 # }
402  
403 #------------------------------------------------------------------------------
404  
405 sub level_to_n_range {
406 0 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: not called CC=1 SCAR=6.9
Pod documented
407 0 return (0, 2**$level * $self->{'arms'} - 1);
408 }
409 sub n_to_level {
410 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
411 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
412 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
413 0 $n = round_nearest($n);
414 0 _divrem_mutate ($n, $self->{'arms'});
415 0 my ($pow, $exp) = round_up_pow ($n+1, 2);
416 0 return $exp;
417 }
418  
419 #------------------------------------------------------------------------------
420 5 1;
421 __END__
« NumSeq/PlanePathTurn.pm ↑ Summary PlanePath/KochCurve.pm »