PlanePath/SierpinskiCurve.pm

filter: Statementstmt 69.9% 179 / 256 Branchbran 58.1% 64 / 110 Conditioncond 85.3% 35 / 41 MC/DCmcdc 84.6% 22 / 26 Subroutinesub 60.5% 23 / 38 Podpod 100% 18 / 18 totaltotal 69.7% 341 / 489 CC 73 SCAR 32.7
CC
73
Cov
68%
n_to_dxdy
132.0
_UNDOCUMENTED__dxdy_list
72.0
xy_to_n
22.0
CRAP 26.4
« PlanePath/GrayCode.pm ↑ Summary PlanePath/FractionsTree.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 # Sierpinski, "Sur Une Nouvelle Courbe Qui Remplit Toute Une Aire Plaine",
20 # Bull. Acad. Sci. Cracovie Series A, 1912, pages 462-478.
21  
22  
23 package Math::PlanePath::SierpinskiCurve;
24 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use List::Util 'sum','first';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 #use List::Util 'min','max';
28 2 *min = \&Math::PlanePath::_min;
29 2 *max = \&Math::PlanePath::_max;
30  
31 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 $VERSION = 128;
33 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 @ISA = ('Math::PlanePath');
35 2 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
36  
37 use Math::PlanePath::Base::Generic
38 2 'is_infinite',
39 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 use Math::PlanePath::Base::Digits
41 2 'round_up_pow',
42 'round_down_pow',
43 2 ▶ 'digit_split_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 # uncomment this to run the ### lines
46 # use Smart::Comments;
47  
48  
49 2 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 sub x_negative {
52 8 ▶ my ($self) = @_;
Subroutine x_negative: called CC=1 SCAR=0.0
Pod documented
53 8 return ($self->{'arms'} >= 3);
54 }
55 sub y_negative {
56 8 ▶ my ($self) = @_;
Subroutine y_negative: called CC=1 SCAR=0.0
Pod documented
57 8 return ($self->{'arms'} >= 5);
58 }
59  
60 2 use constant parameter_info_array =>
61 [
62 { name => 'arms',
63 share_key => 'arms_8',
64 display => 'Arms',
65 type => 'integer',
66 minimum => 1,
67 maximum => 8,
68 default => 1,
69 width => 1,
70 description => 'Arms',
71 },
72  
73 { name => 'straight_spacing',
74 display => 'Straight Spacing',
75 type => 'integer',
76 minimum => 1,
77 default => 1,
78 width => 1,
79 description => 'Spacing of the straight line points.',
80 },
81 { name => 'diagonal_spacing',
82 display => 'Diagonal Spacing',
83 type => 'integer',
84 minimum => 1,
85 default => 1,
86 width => 1,
87 description => 'Spacing of the diagonal points.',
88 },
89 2 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 # Ntop = (4^level)/2 - 1
92 # Xtop = 3*2^(level-1) - 1
93 # fill = Ntop / (Xtop*(Xtop-1)/2)
94 # -> 2 * ((4^level)/2 - 1) / (3*2^(level-1) - 1)^2
95 # -> 2 * ((4^level)/2) / (3*2^(level-1))^2
96 # = 4^level / (9*4^(level-1)
97 # = 4/9 = 0.444
98  
99 sub x_negative_at_n {
100 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=2 SCAR=17.9
Pod documented
101 0 ▶ return $self->arms_count >= 3 ? 2 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->arms_count >= 3 ? : 0 0 0
102 }
103 sub y_negative_at_n {
104 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=2 SCAR=17.9
Pod documented
105 0 ▶ return $self->arms_count >= 5 ? 4 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->arms_count >= 5 ? : 0 0 0
106 }
107  
108 {
109 # Note: shared by Math::PlanePath::SierpinskiCurveStair
110 2 my @x_minimum = (undef,
111 1, # 1 arm
112 0, # 2 arms
113 ); # more than 2 arm, X goes negative
114 sub x_minimum {
115 0 ▶ my ($self) = @_;
Subroutine x_minimum: not called CC=1 SCAR=6.9
Pod documented
116 0 return $x_minimum[$self->arms_count];
117 }
118 }
119 {
120 # Note: shared by Math::PlanePath::SierpinskiCurveStair
121 2 my @sumxy_minimum = (undef,
122 1, # 1 arm, octant and X>=1 so X+Y>=1
123 1, # 2 arms, X>=1 or Y>=1 so X+Y>=1
124 0, # 3 arms, Y>=1 and X>=Y, so X+Y>=0
125 ); # more than 3 arm, Sum goes negative so undef
126 sub sumxy_minimum {
127 0 ▶ my ($self) = @_;
Subroutine sumxy_minimum: not called CC=1 SCAR=6.9
Pod documented
128 0 return $sumxy_minimum[$self->arms_count];
129 }
130 }
131 2 ▶use constant sumabsxy_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 # Note: shared by Math::PlanePath::SierpinskiCurveStair
134 # Math::PlanePath::AlternatePaper
135 # Math::PlanePath::AlternatePaperMidpoint
136 sub diffxy_minimum {
137 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
138 0 ▶ return ($self->arms_count == 1
Branch: 1 branch
BranchTrueFalseTotal
$self->arms_count == 1 ? : 0 0 0
139 ? 1 # octant Y<=X-1 so X-Y>=1
140 : undef); # more than 1 arm, DiffXY goes negative
141 }
142 2 ▶use constant absdiffxy_minimum => 1; # X=Y never occurs
Subroutine BEGIN: called CC=1 SCAR=0.0
143 2 ▶use constant rsquared_minimum => 1; # minimum X=1,Y=0
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 sub dx_minimum {
146 0 ▶ my ($self) = @_;
Subroutine dx_minimum: not called CC=1 SCAR=6.9
Pod documented
147 return - max($self->{'straight_spacing'},
148 0 $self->{'diagonal_spacing'});
149 }
150 2 *dy_minimum = \&dx_minimum;
151  
152 sub dx_maximum {
153 0 ▶ my ($self) = @_;
Subroutine dx_maximum: not called CC=1 SCAR=6.9
Pod documented
154 return max($self->{'straight_spacing'},
155 0 $self->{'diagonal_spacing'});
156 }
157 2 *dy_maximum = \&dx_maximum;
158  
159 sub _UNDOCUMENTED__dxdy_list {
160 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list: not called CC=8 SCAR=42.8
161 0 my $s = $self->{'straight_spacing'};
162 0 my $d = $self->{'diagonal_spacing'};
163 0 ▶ return ($s,0, # E eight scaled
Branch: 7 branches
BranchTrueFalseTotal
$d ? : 0 0 0
$s ? : 0 0 0
$d ? : 0 0 0
$s ? : 0 0 0
$d ? : 0 0 0
$s ? : 0 0 0
$d ? : 0 0 0
164 ($d ? ( $d, $d) : ()), # NE except s=0
165 ($s ? ( 0, $s) : ()), # N or d=0 skips
166 ($d ? (-$d, $d) : ()), # NW
167 ($s ? (-$s, 0) : ()), # W
168 ($d ? (-$d,-$d) : ()), # SW
169 ($s ? ( 0,-$s) : ()), # S
170 ($d ? ( $d,-$d) : ())); # SE
171  
172 }
173 {
174 2 my @_UNDOCUMENTED__dxdy_list_at_n = (undef,
175 21, 20, 27, 36,
176 29, 12, 12, 13);
177 sub _UNDOCUMENTED__dxdy_list_at_n {
178 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
179 0 return $_UNDOCUMENTED__dxdy_list_at_n[$self->{'arms'}];
180 }
181 }
182 2 sub dsumxy_minimum {
183 0 ▶ my ($self) = @_;
Subroutine dsumxy_minimum: not called CC=1 SCAR=6.9
Pod documented
184 return - max($self->{'straight_spacing'},
185 0 2*$self->{'diagonal_spacing'});
186 }
187 sub dsumxy_maximum {
188 0 ▶ my ($self) = @_;
Subroutine dsumxy_maximum: not called CC=1 SCAR=6.9
Pod documented
189 return max($self->{'straight_spacing'},
190 0 2*$self->{'diagonal_spacing'});
191 }
192 2 *ddiffxy_minimum = \&dsumxy_minimum;
193 2 *ddiffxy_maximum = \&dsumxy_maximum;
194  
195 2 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
196 2 ▶use constant turn_any_straight => 0; # never straight
Subroutine BEGIN: called CC=1 SCAR=0.0
197  
198  
199 #------------------------------------------------------------------------------
200  
201 sub new {
202 256 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=4 SCAR=13.9
Pod documented
203  
204 256 ▶ $self->{'arms'} = max(1, min(8, $self->{'arms'} || 1));
Condition100%2/2
$self->{'arms'} || 1
l!l
2515
MC/DC100%1/1
$self->{'arms'} || 1
$self->{'arms'}
Truth table100%2/2
A
A= $self->{'arms'}
Aresult
11
00
205 256 ▶ $self->{'straight_spacing'} ||= 1;
Condition100%2/2
$self->{"straight_spacing"} ||= 1
l!l
16096
MC/DC100%1/1
$self->{"straight_spacing"} ||= 1
$self->{"straight_spacing"}
Truth table100%2/2
A
A= $self->{"straight_spacing"}
Aresult
11
00
206 256 ▶ $self->{'diagonal_spacing'} ||= 1;
Condition100%2/2
$self->{"diagonal_spacing"} ||= 1
l!l
16096
MC/DC100%1/1
$self->{"diagonal_spacing"} ||= 1
$self->{"diagonal_spacing"}
Truth table100%2/2
A
A= $self->{"diagonal_spacing"}
Aresult
11
00
207 256 return $self;
208 }
209  
210 sub n_to_xy {
211 7496 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=10 SCAR=23.1
Pod documented
212 ### SierpinskiCurve n_to_xy(): $n
213  
214 7496 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 7496 7496
215 0 return;
216 }
217 7496 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 7496 7496
218 0 return ($n,$n);
219 }
220  
221 7496 my $int = int($n); # BigFloat int() gives BigInt, use that
222 7496 $n -= $int; # preserve possible BigFloat
223 ### $int
224 ### $n
225  
226 7496 my $arm = _divrem_mutate ($int, $self->{'arms'});
227  
228 7496 my $s = $self->{'straight_spacing'};
229 7496 my $d = $self->{'diagonal_spacing'};
230 7496 my $base = 2*$d+$s;
231 7496 my $x = my $y = ($int * 0); # inherit big 0
232 7496 my $len = $x + $base; # inherit big
233  
234 7496 foreach my $digit (digit_split_lowtohigh($int,4)) {
235 ### at: "$x,$y digit=$digit"
236  
237 13859 ▶ if ($digit == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($digit == 0) { } 1829 12030 13859
elsif ($digit == 1) { } 5747 6283 12030
elsif ($digit == 2) { } 3652 2631 6283
238 1829 $x = $n*$d + $x;
239 1829 $y = $n*$d + $y;
240 1829 $n = 0;
241  
242 } elsif ($digit == 1) {
243 5747 ($x,$y) = ($n*$s - $y + $len-$d-$s, # rotate +90
244 $x + $d);
245 5747 $n = 0;
246  
247 } elsif ($digit == 2) {
248 # rotate -90
249 3652 ($x,$y) = ($n*$d + $y + $len-$d,
250 -$n*$d - $x + $len-$d-$s);
251 3652 $n = 0;
252  
253 } else { # digit==3
254 2631 $x += $len;
255 }
256 13859 $len *= 2;
257 }
258  
259 # n=0 or n=33..33
260 7496 $x = $n*$d + $x;
261 7496 $y = $n*$d + $y;
262  
263 7496 $x += 1;
264 7496 ▶ if ($arm & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 1) 3156 4340 7496
265 3156 ($x,$y) = ($y,$x); # mirror 45
266 }
267 7496 ▶ if ($arm & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 2) 2788 4708 7496
268 2788 ($x,$y) = (-1-$y,$x); # rotate +90
269 }
270 7496 ▶ if ($arm & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 4) 1826 5670 7496
271 1826 $x = -1-$x; # rotate 180
272 1826 $y = -1-$y;
273 }
274  
275 # use POSIX 'floor';
276 # $x += floor($x/3);
277 # $y += floor($y/3);
278  
279 # $x += floor(($x-1)/3) + floor(($x-2)/3);
280 # $y += floor(($y-1)/3) + floor(($y-2)/3);
281  
282  
283 ### final: "$x,$y"
284 7496 return ($x,$y);
285 }
286  
287 2 my @digit_to_dir = (0, -2, 2, 0);
288 2 my @dir8_to_dx = (1, 1, 0,-1, -1, -1, 0, 1);
289 2 my @dir8_to_dy = (0, 1, 1, 1, 0, -1, -1,-1);
290 2 my @digit_to_nextturn = (-1, # after digit=0
291 2, # digit=1
292 -1); # digit=2
293 sub n_to_dxdy {
294 0 ▶ my ($self, $n) = @_;
Subroutine n_to_dxdy: not called CC=11 SCAR=48.8
Pod documented
295 ### n_to_dxdy(): $n
296  
297 0 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
298 0 return; # first direction at N=0
299 }
300  
301 0 my $int = int($n);
302 0 $n -= $int;
303  
304 0 my $arm = _divrem_mutate($int,$self->{'arms'});
305 0 my $lowbit = _divrem_mutate($int,2);
306 ### $lowbit
307 ### $int
308  
309 0 ▶ if (is_infinite($int)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($int)) 0 0 0
310 0 return ($int,$int);
311 }
312 0 my @ndigits = digit_split_lowtohigh($int,4);
313 ### @ndigits
314  
315 0 my $dir8 = sum(0, map {$digit_to_dir[$_]} @ndigits);
316 0 ▶ if ($arm & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 1) 0 0 0
317 0 $dir8 = - $dir8; # mirrored on second,fourth,etc arm
318 }
319 0 $dir8 += ($arm|1); # NE,NW,SW, or SE
320  
321 0 my $turn;
322 0 ▶ if ($n || $lowbit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n or $lowbit) 0 0 0
Condition0%0/3
$n or $lowbit
l!l&&r!l&&!r
000
MC/DC0%0/2
$n or $lowbit
$n $lowbit
Truth table0%0/3
A or B
A= $nB= $lowbit
ABresult
1X1
011
000
323 # next turn
324  
325 # lowest non-3 digit, or zero if all 3s (implicit 0 above high digit)
326 0 ▶ $turn = $digit_to_nextturn[ first {$_!=3} @ndigits, 0 ];
Subroutine __ANON__: not called CC=1 SCAR=6.9
327 0 ▶ if ($arm & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm & 1) 0 0 0
328 0 $turn = - $turn; # mirrored on second,fourth,etc arm
329 }
330 }
331  
332 0 ▶ if ($lowbit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowbit) 0 0 0
333 0 $dir8 += $turn;
334 }
335  
336 0 my $s = $self->{'straight_spacing'};
337 0 my $d = $self->{'diagonal_spacing'};
338  
339 0 $dir8 &= 7;
340 0 ▶ my $spacing = ($dir8 & 1 ? $d : $s);
Branch: 1 branch
BranchTrueFalseTotal
$dir8 & 1 ? : 0 0 0
341 0 my $dx = $spacing * $dir8_to_dx[$dir8];
342 0 my $dy = $spacing * $dir8_to_dy[$dir8];
343  
344 0 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
345 0 $dir8 += $turn;
346 0 $dir8 &= 7;
347 0 ▶ $spacing = ($dir8 & 1 ? $d : $s);
Branch: 1 branch
BranchTrueFalseTotal
$dir8 & 1 ? : 0 0 0
348 0 $dx += $n*($spacing * $dir8_to_dx[$dir8]
349 - $dx);
350 0 $dy += $n*($spacing * $dir8_to_dy[$dir8]
351 - $dy);
352 }
353  
354 0 return ($dx, $dy);
355 }
356  
357 # 2| . 3 .
358 # 1| 1 . 2
359 # 0| . 0 .
360 # +------
361 # 0 1 2
362 #
363 # 4| . . . 3 . # diagonal_spacing == 3
364 # 3| . . . . 2 4 # mod=2*3+1=7
365 # 2| . . . . . . .
366 # 1| 1 . . . . . . .
367 # 0| . 0 . . . . . . 6
368 # +------------------
369 # 0 1 2 3 4 5 6 7 8
370 #
371 sub _NOTWORKING__xy_is_visited {
372 0 ▶ my ($self, $x, $y) = @_;
Subroutine _NOTWORKING__xy_is_visited: not called CC=2 SCAR=17.9
373 0 $x = round_nearest($x);
374 0 $y = round_nearest($y);
375 0 my $mod = 2*$self->{'diagonal_spacing'} + $self->{'straight_spacing'};
376 0 ▶ return (_rect_within_arms($x,$y, $x,$y, $self->{'arms'})
Condition0%0/3
_rect_within_arms($x, $y, $x, $y, $self->{'arms'}) && $x % $mod + $y % $mod & 1
!ll&&!rl&&r
000
MC/DC0%0/2
_rect_within_arms($x, $y, $x, $y, $self->{'arms'}) && $x % $mod + $y % $mod & 1
_rect_within_arms($x, $y, $x, $y, $self->{'arms'}) $x % $mod + $y % $mod & 1
Truth table0%0/3
A && B
A= _rect_within_arms($x, $y, $x, $y, $self->{'arms'})B= $x % $mod + $y % $mod & 1
ABresult
0X0
100
111
377 && ((($x%$mod)+($y%$mod)) & 1));
378 }
379  
380 # x1 * x2 *
381 # +-----*-+y2*
382 # | *| *
383 # | * *
384 # | |* *
385 # | | **
386 # +-------+y1*
387 # ----------------
388 #
389 # arms=5 x1,y2 after X=Y-1 line, so x1 > y2-1, x1 >= y2
390 # ************
391 # x1 * x2
392 # +---*----+y2
393 # | * |
394 # | * |
395 # |* |
396 # * |
397 # *+--------+y1
398 # *
399 #
400 # arms=7 x1,y1 after X=-2-Y line, so x1 > -2-y1
401 # ************
402 # ** +------+
403 # * *| |
404 # * * |
405 # * |* |
406 # * | * |
407 # *y1+--*---+
408 # * x1 *
409 #
410 # _rect_within_arms() returns true if rectangle x1,y1,x2,y2 has some part
411 # within the extent of the $arms set of octants.
412 #
413 sub _rect_within_arms {
414 363 ▶ my ($x1,$y1, $x2,$y2, $arms) = @_;
Subroutine _rect_within_arms: called CC=10 SCAR=23.0
415 363 ▶ return ($arms <= 4
Branch: 5 branches
BranchTrueFalseTotal
$arms <= 4 ? : 122 241 363
$arms <= 2 ? : 49 72 121
$arms == 1 ? : 25 24 49
$arms <= 6 ? : 32 63 95
$arms == 5 ? : 14 18 32
Condition100%12/12
$y2 >= 0 and $arms <= 2 ? $arms == 1 ? $x2 > $y1 : $x2 >= 0 : $arms == 4 || $x2 >= -$y2
!ll&&!rl&&r
14117
$arms == 4 or $x2 >= -$y2
l!l&&r!l&&!r
39312
$y2 >= 0 or $arms <= 6 ? $arms == 5 ? $x1 < $y2 : $x1 < 0 : $arms == 8 || $x1 <= -2 - $y1
l!l&&r!l&&!r
146914
$arms == 8 or $x1 <= -2 - $y1
l!l&&r!l&&!r
36261
MC/DC100%2/2
$y2 >= 0 and $arms <= 2 ? $arms == 1 ? $x2 > $y1 : $x2 >= 0 : $arms == 4 || $x2 >= -$y2
$y2 >= 0 $arms <= 2 ? $arms == 1 ? $x2 > $y1 : $x2 >= 0 : $arms == 4 || $x2 >= -$y2
MC/DC100%2/2
$arms == 4 or $x2 >= -$y2
$arms == 4 $x2 >= -$y2
MC/DC100%2/2
$y2 >= 0 or $arms <= 6 ? $arms == 5 ? $x1 < $y2 : $x1 < 0 : $arms == 8 || $x1 <= -2 - $y1
$y2 >= 0 $arms <= 6 ? $arms == 5 ? $x1 < $y2 : $x1 < 0 : $arms == 8 || $x1 <= -2 - $y1
MC/DC100%2/2
$arms == 8 or $x1 <= -2 - $y1
$arms == 8 $x1 <= -2 - $y1
Truth table100%3/3
A and B
A= $y2 >= 0B= $arms <= 2 ? $arms == 1 ? $x2 > $y1 : $x2 >= 0 : $arms == 4 || $x2 >= -$y2
ABresult
0X0
100
111
Truth table100%3/3
A or B
A= $arms == 4B= $x2 >= -$y2
ABresult
1X1
011
000
Truth table100%3/3
A or B
A= $y2 >= 0B= $arms <= 6 ? $arms == 5 ? $x1 < $y2 : $x1 < 0 : $arms == 8 || $x1 <= -2 - $y1
ABresult
1X1
011
000
Truth table100%3/3
A or B
A= $arms == 8B= $x1 <= -2 - $y1
ABresult
1X1
011
000
416 ? ($y2 >= 0 # y2 top edge must be positive
417 && ($arms <= 2
418 ? ($arms == 1 ? $x2 > $y1 # arms==1 bottom right
419 : $x2 >= 0) # arms==2 right edge
420 : ($arms == 4 # arms==4 anything
421 || $x2 >= -$y2))) # arms==3 top right
422  
423 # arms >= 5
424 : ($y2 >= 0 # y2 top edge positive is good, otherwise check
425 || ($arms <= 6
426 ? ($arms == 5 ? $x1 < $y2 # arms==5 top left
427 : $x1 < 0) # arms==6 left edge
428 : ($arms == 8 # arms==8 anything
429 || $x1 <= -2-$y1)))); # arms==7 bottom left
430 }
431  
432 sub xy_to_n {
433 57896 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=22 SCAR=30.9
Pod documented
434 ### SierpinskiCurve xy_to_n(): "$x, $y"
435  
436 57896 $x = round_nearest($x);
437 57896 $y = round_nearest($y);
438  
439 57896 my $arm = 0;
440 57896 ▶ if ($y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0) 27206 30690 57896
441 27206 $arm = 4;
442 27206 $x = -1-$x; # rotate -180
443 27206 $y = -1-$y;
444 }
445 57896 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 28811 29085 57896
446 28811 $arm += 2;
447 28811 ($x,$y) = ($y, -1-$x); # rotate -90
448 }
449 57896 ▶ if ($y > $x) { # second octant
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $x) 25613 32283 57896
450 25613 $arm++;
451 25613 ($x,$y) = ($y,$x); # mirror 45
452 }
453  
454 57896 my $arms = $self->{'arms'};
455 57896 ▶ if ($arm >= $arms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arm >= $arms) 24000 33896 57896
456 24000 return undef;
457 }
458  
459 33896 $x -= 1;
460 33896 ▶ if ($x < 0 || $x < $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $x < $y) 4200 29696 33896
Condition100%3/3
$x < 0 or $x < $y
l!l&&r!l&&!r
500370029696
MC/DC100%2/2
$x < 0 or $x < $y
$x < 0 $x < $y
Truth table100%3/3
A or B
A= $x < 0B= $x < $y
ABresult
1X1
011
000
461 4200 return undef;
462 }
463 ### x adjust to zero: "$x,$y"
464 ### assert: $x >= 0
465 ### assert: $y >= 0
466  
467 29696 my $s = $self->{'straight_spacing'};
468 29696 my $d = $self->{'diagonal_spacing'};
469 29696 my $base = (2*$d+$s);
470 29696 ▶ my ($len,$level) = round_down_pow (($x+$y)/$base || 1, 2);
Condition100%2/2
($x + $y) / $base || 1
l!l
28783913
MC/DC100%1/1
($x + $y) / $base || 1
($x + $y) / $base
Truth table100%2/2
A
A= ($x + $y) / $base
Aresult
11
00
471 ### $level
472 ### $len
473 29696 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 29696 29696
474 0 return $level;
475 }
476  
477 # Xtop = 3*2^(level-1)-1
478 #
479 29696 $len *= 2*$base;
480 ### initial len: $len
481  
482 29696 my $n = 0;
483 29696 foreach (0 .. $level) {
484 35801 $n *= 4;
485 ### at: "loop=$_ len=$len x=$x,y=$y n=$n"
486 ### assert: $x >= 0
487 ### assert: $y >= 0
488  
489 35801 my $len_sub_d = $len - $d;
490 35801 ▶ if ($x < $len_sub_d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $len_sub_d) { } 33048 2753 35801
491 ### digit 0 or 1...
492 33048 ▶ if ($x+$y+$s < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x + $y + $s < $len) { } 26450 6598 33048
493 ### digit 0 ...
494 } else {
495 ### digit 1 ...
496 6598 ($x,$y) = ($y-$d, $len-$s-$d-$x); # shift then rotate -90
497 6598 $n += 1;
498 }
499 } else {
500 2753 $x -= $len_sub_d;
501 ### digit 2 or 3 to: "x=$x y=$y"
502 2753 ▶ if ($x < $y) { # before diagonal
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $y) { } 1860 893 2753
503 ### digit 2...
504 1860 ($x,$y) = ($len-$d-$s-$y, $x); # shift y-len then rotate +90
505 1860 $n += 2;
506 } else {
507 #### digit 3...
508 893 $x -= $d;
509 893 $n += 3;
510 }
511 2753 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 376 2377 2753
512 376 return undef;
513 }
514 }
515 35425 $len /= 2;
516 }
517  
518 ### end at: "x=$x,y=$y n=$n"
519 ### assert: $x >= 0
520 ### assert: $y >= 0
521  
522 29320 $n *= 4;
523 29320 ▶ if ($y == 0 && $x == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($y == 0 and $x == 0) { } 1797 27523 29320
elsif ($x == $d and $y == $d) { } 1762 25761 27523
elsif ($x == $d + $s and $y == $d) { } 1611 24150 25761
elsif ($x == $base and $y == 0) { } 1276 22874 24150
Condition100%3/3
$y == 0 and $x == 0
!ll&&!rl&&r
1949580281797
MC/DC100%2/2
$y == 0 and $x == 0
$y == 0 $x == 0
Truth table100%3/3
A and B
A= $y == 0B= $x == 0
ABresult
0X0
100
111
524 ### final digit 0 ...
525 ▶ } elsif ($x == $d && $y == $d) {
Condition100%3/3
$x == $d and $y == $d
!ll&&!rl&&r
2265531061762
MC/DC100%2/2
$x == $d and $y == $d
$x == $d $y == $d
Truth table100%3/3
A and B
A= $x == $dB= $y == $d
ABresult
0X0
100
111
526 ### final digit 1 ...
527 1762 $n += 1;
528 ▶ } elsif ($x == $d+$s && $y == $d) {
Condition100%3/3
$x == $d + $s and $y == $d
!ll&&!rl&&r
2028038701611
MC/DC100%2/2
$x == $d + $s and $y == $d
$x == $d + $s $y == $d
Truth table100%3/3
A and B
A= $x == $d + $sB= $y == $d
ABresult
0X0
100
111
529 ### final digit 2 ...
530 1611 $n += 2;
531 ▶ } elsif ($x == $base && $y == 0) {
Condition100%3/3
$x == $base and $y == 0
!ll&&!rl&&r
2178810861276
MC/DC100%2/2
$x == $base and $y == 0
$x == $base $y == 0
Truth table100%3/3
A and B
A= $x == $baseB= $y == 0
ABresult
0X0
100
111
532 ### final digit 3 ...
533 1276 $n += 3;
534 } else {
535 22874 return undef;
536 }
537  
538 6446 return $n*$arms + $arm;
539 }
540  
541 # not exact
542 sub rect_to_n_range {
543 363 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=7 SCAR=19.5
Pod documented
544 ### SierpinskiCurve rect_to_n_range(): "$x1,$y1 $x2,$y2"
545  
546 363 $x1 = round_nearest ($x1);
547 363 $x2 = round_nearest ($x2);
548 363 $y1 = round_nearest ($y1);
549 363 $y2 = round_nearest ($y2);
550 363 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 363 363
551 363 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 1 362 363
552  
553 363 my $arms = $self->{'arms'};
554 363 ▶ unless (_rect_within_arms($x1,$y1, $x2,$y2, $arms)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_rect_within_arms($x1, $y1, $x2, $y2, $arms)) 9 354 363
555 ### rect outside octants, for arms: $arms
556 9 return (1,0);
557 }
558  
559 354 my $max = ($x2 + $y2);
560 354 ▶ if ($arms >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arms >= 3) 307 47 354
561 307 _apply_max ($max, -1-$x1 + $y2);
562  
563 307 ▶ if ($arms >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arms >= 5) 237 70 307
564 237 _apply_max ($max, -1-$x1 - $y1-1);
565  
566 237 ▶ if ($arms >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arms >= 7) 135 102 237
567 135 _apply_max ($max, $x2 - $y1-1);
568 }
569 }
570 }
571  
572 # base=2d+s
573 # level begins at
574 # base*(2^level-1)-s = X+Y ... maybe
575 # base*2^level = X+base
576 # 2^level = (X+base)/base
577 # level = log2((X+base)/base)
578 # then
579 # Nlevel = 4^level-1
580  
581 354 my $base = 2 * $self->{'diagonal_spacing'} + $self->{'straight_spacing'};
582 354 my ($power) = round_down_pow (int(($max+$base-2)/$base),
583 2);
584 354 return (0, 4*$power*$power * $arms - 1);
585 }
586  
587 sub _apply_max {
588 ### _apply_max(): "$_[0] cf $_[1]"
589 679 ▶ unless ($_[0] > $_[1]) {
Subroutine _apply_max: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0] > $_[1]) 275 404 679
590 275 $_[0] = $_[1];
591 }
592 }
593  
594 #------------------------------------------------------------------------------
595  
596 sub level_to_n_range {
597 9 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
598 9 return (0, 4**$level * $self->{'arms'} - 1);
599 }
600 sub n_to_level {
601 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
602 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
603 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
604 0 $n = round_nearest($n);
605 0 _divrem_mutate ($n, $self->{'arms'});
606 0 my ($pow, $exp) = round_up_pow ($n+1, 4);
607 0 return $exp;
608 }
609  
610 #------------------------------------------------------------------------------
611 2 1;
612 __END__
« PlanePath/GrayCode.pm ↑ Summary PlanePath/FractionsTree.pm »