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