PlanePath/SierpinskiArrowhead.pm

filter: Statementstmt 73.9% 142 / 192 Branchbran 56.5% 43 / 76 Conditioncond 85.7% 12 / 14 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 64.7% 22 / 34 Podpod 100% 17 / 17 totaltotal 70.9% 242 / 341 CC 44 SCAR 24.2
CC
44
Cov
70%
xy_to_n
26.7
n_to_xy
14.0
n_to_level
12.0
CRAP 11.3
« PlanePath/HilbertSides.pm ↑ Summary PlanePath/SierpinskiArrowheadCentres.pm »
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::SierpinskiArrowhead;
20 5 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 #use List::Util 'max';
25 5 *max = \&Math::PlanePath::_max;
26  
27 5 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 $VERSION = 128;
29 5 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 @ISA = ('Math::PlanePath');
31  
32 use Math::PlanePath::Base::Generic
33 5 'is_infinite',
34 5 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 use Math::PlanePath::Base::Digits
36 5 'round_down_pow',
37 'round_up_pow',
38 5 ▶ 'digit_split_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # uncomment this to run the ### lines
41 #use Smart::Comments;
42  
43  
44 # Note: shared by SierpinskiArrowheadCentres
45 5 use constant parameter_info_array =>
46 [ { name => 'align',
47 share_key => 'align_trld',
48 display => 'Align',
49 type => 'enum',
50 default => 'triangular',
51 choices => ['triangular','right','left','diagonal'],
52 choices_display => ['Triangular','Right','Left','Diagonal'],
53 },
54 5 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 5 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 5 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 5 my %x_negative = (triangular => 1,
60 left => 1,
61 right => 0,
62 diagonal => 0);
63 # Note: shared by SierpinskiArrowheadCentres
64 sub x_negative {
65 2 ▶ my ($self) = @_;
Subroutine x_negative: called CC=1 SCAR=0.0
Pod documented
66 2 return $x_negative{$self->{'align'}};
67 }
68 {
69 5 my %x_negative_at_n = (triangular => 3,
70 # right => undef,
71 left => 2,
72 # diagonal => undef,
73 );
74 sub x_negative_at_n {
75 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
76 0 return $x_negative_at_n{$self->{'align'}};
77 }
78 }
79  
80 5 ▶use constant sumxy_minimum => 0; # triangular X>=-Y
Subroutine BEGIN: called CC=1 SCAR=0.0
81 5 ▶use Math::PlanePath::SierpinskiTriangle;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 5 *x_maximum = \&Math::PlanePath::SierpinskiTriangle::x_maximum;
83 5 *diffxy_maximum = \&Math::PlanePath::SierpinskiTriangle::diffxy_maximum;
84  
85 sub dx_minimum {
86 0 ▶ my ($self) = @_;
Subroutine dx_minimum: not called CC=2 SCAR=17.9
Pod documented
87 0 ▶ return ($self->{'align'} eq 'triangular' ? -2 : -1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'triangular' ? : 0 0 0
88 }
89 sub dx_maximum {
90 0 ▶ my ($self) = @_;
Subroutine dx_maximum: not called CC=2 SCAR=17.9
Pod documented
91 0 ▶ return ($self->{'align'} eq 'triangular' ? 2 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'triangular' ? : 0 0 0
92 }
93 5 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 5 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 {
97 5 my %_UNDOCUMENTED__dxdy_list
98 = (triangular => [ 2,0, # E N=4 six directions
99 1,1, # NE N=0
100 -1,1, # NW N=2
101 -2,0, # W N=1
102 -1,-1, # SW N=15
103 1,-1, # SE N=6
104 ],
105 right => [ 1,0, # E N=4
106 1,1, # NE N=0
107 0,1, # N N=2
108 -1,0, # W N=1
109 -1,-1, # SW N=15
110 0,-1, # S N=6
111 ],
112 left => [ 1,0, # E N=4
113 0,1, # N N=0
114 -1,1, # NW N=2
115 -1,0, # W N=1
116 0,-1, # S N=15
117 1,-1, # SE N=6
118 ],
119 diagonal => [ 1,0, # E N=0
120 0,1, # N N=2
121 -1,1, # NW N=1
122 -1,0, # W N=15
123 0,-1, # S N=6
124 1,-1, # SE N=4
125 ],
126 );
127 sub _UNDOCUMENTED__dxdy_list {
128 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list: not called CC=1 SCAR=6.9
129 0 return @{$_UNDOCUMENTED__dxdy_list{$self->{'align'}}};
130 }
131 }
132 5 ▶use constant 1.02 _UNDOCUMENTED__dxdy_list_at_n => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 sub absdx_minimum {
135 0 ▶ my ($self) = @_;
Subroutine absdx_minimum: not called CC=2 SCAR=17.9
Pod documented
136 0 ▶ return ($self->{'align'} eq 'triangular' ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'triangular' ? : 0 0 0
137 }
138 sub absdx_maximum {
139 0 ▶ my ($self) = @_;
Subroutine absdx_maximum: not called CC=2 SCAR=17.9
Pod documented
140 0 ▶ return ($self->{'align'} eq 'triangular' ? 2 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'triangular' ? : 0 0 0
141 }
142  
143 {
144 5 my %dsumxy_minimum = (triangular => -2,
145 left => -1,
146 right => -2,
147 diagonal => -1,
148 );
149 sub dsumxy_minimum {
150 0 ▶ my ($self) = @_;
Subroutine dsumxy_minimum: not called CC=1 SCAR=6.9
Pod documented
151 0 return $dsumxy_minimum{$self->{'align'}};
152 }
153 }
154 {
155 5 my %dsumxy_maximum = (triangular => 2,
156 left => 1,
157 right => 2,
158 diagonal => 1,
159 );
160 sub dsumxy_maximum {
161 0 ▶ my ($self) = @_;
Subroutine dsumxy_maximum: not called CC=1 SCAR=6.9
Pod documented
162 0 return $dsumxy_maximum{$self->{'align'}};
163 }
164 }
165  
166 {
167 5 my %ddiffxy_minimum = (triangular => -2,
168 left => -2,
169 right => -1,
170 diagonal => -2,
171 );
172 sub ddiffxy_minimum {
173 0 ▶ my ($self) = @_;
Subroutine ddiffxy_minimum: not called CC=1 SCAR=6.9
Pod documented
174 0 return $ddiffxy_minimum{$self->{'align'}};
175 }
176 }
177 {
178 5 my %ddiffxy_maximum = (triangular => 2,
179 left => 2,
180 right => 1,
181 diagonal => 2,
182 );
183 sub ddiffxy_maximum {
184 0 ▶ my ($self) = @_;
Subroutine ddiffxy_maximum: not called CC=1 SCAR=6.9
Pod documented
185 0 return $ddiffxy_maximum{$self->{'align'}};
186 }
187 }
188  
189 5 sub dir_maximum_dxdy {
190 0 ▶ my ($self) = @_;
Subroutine dir_maximum_dxdy: not called CC=2 SCAR=17.9
Pod documented
191 0 ▶ return ($self->{'align'} eq 'right'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'right' ? : 0 0 0
192 ? (0,-1) # South
193 : (1,-1)); # South-East
194 }
195  
196 5 ▶use constant turn_any_straight => 0; # never straight
Subroutine BEGIN: called CC=1 SCAR=0.0
197  
198  
199 #------------------------------------------------------------------------------
200  
201 # Note: shared by SierpinskiArrowheadCentres
202 sub new {
203 17 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=3 SCAR=11.3
Pod documented
204 17 ▶ my $align = ($self->{'align'} ||= 'triangular');
Condition100%2/2
$self->{"align"} ||= "triangular"
l!l
314
MC/DC100%1/1
$self->{"align"} ||= "triangular"
$self->{"align"}
Truth table100%2/2
A
A= $self->{"align"}
Aresult
11
00
205 17 ▶ if (! exists $x_negative{$align}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $x_negative{$align}) 0 17 17
206 0 croak "Unrecognised align option: ", $align;
207 }
208 17 return $self;
209 }
210  
211 sub n_to_xy {
212 6189 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=13 SCAR=26.4
Pod documented
213 ### SierpinskiArrowhead n_to_xy(): $n
214 6189 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 6189 6189
215 0 return;
216 }
217 6189 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 6189 6189
218 0 return ($n,$n);
219 }
220  
221 6189 my $x = int($n);
222 6189 my $y = $n - $x; # fraction part
223 6189 $n = $x;
224 6189 $x = $y;
225  
226 6189 ▶ if (my @digits = digit_split_lowtohigh($n,3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@digits) = digit_split_lowtohigh($n, 3)) 6185 4 6189
227 6185 my $len = 1;
228 6185 for (;;) {
229 18875 my $digit = shift @digits; # low to high
230  
231 ### odd right: "$x,$y len=$len"
232 ### $digit
233 18875 ▶ if ($digit == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($digit == 0) { } 5564 13311 18875
elsif ($digit == 1) { } 7232 6079 13311
234  
235 } elsif ($digit == 1) {
236 7232 $x = $len - $x; # mirror and offset
237 7232 $y += $len;
238  
239 } else {
240 6079 ($x,$y) = (($x+3*$y)/-2, # rotate +120
241 ($x-$y)/2 + 2*$len);
242 }
243  
244 18875 ▶ @digits || last;
Branch: 1 branch
BranchTrueFalseTotal
@digits or last 2636 16239 18875
245 16239 $len *= 2;
246 16239 $digit = shift @digits; # low to high
247  
248 ### odd left: "$x,$y len=$len"
249 ### $digit
250 16239 ▶ if ($digit == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($digit == 0) { } 4922 11317 16239
elsif ($digit == 1) { } 6218 5099 11317
251  
252 } elsif ($digit == 1) {
253 6218 $x = - $x - $len; # mirror and offset
254 6218 $y += $len;
255  
256 } else {
257 5099 ($x,$y) = ((3*$y-$x)/2, # rotate -120
258 ($x+$y)/-2 + 2*$len)
259 }
260  
261 16239 ▶ @digits || last;
Branch: 1 branch
BranchTrueFalseTotal
@digits or last 3549 12690 16239
262 12690 $len *= 2;
263 }
264 }
265  
266 ### final: "$x,$y"
267 6189 ▶ if ($self->{'align'} eq 'right') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'align'} eq 'right') { } 0 6189 6189
elsif ($self->{'align'} eq 'left') { } 0 6189 6189
elsif ($self->{'align'} eq 'diagonal') { } 0 6189 6189
268 0 return (($x+$y)/2, $y);
269 } elsif ($self->{'align'} eq 'left') {
270 0 return (($x-$y)/2, $y);
271 } elsif ($self->{'align'} eq 'diagonal') {
272 0 return (($x+$y)/2, ($y-$x)/2);
273 } else { # triangular
274 6189 return ($x,$y);
275 }
276 }
277  
278 sub xy_to_n {
279 565 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=20 SCAR=32.8
Pod documented
280 565 $x = round_nearest ($x);
281 565 $y = round_nearest ($y);
282 ### SierpinskiArrowhead xy_to_n(): "$x, $y"
283  
284 565 ▶ if ($y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0) 0 565 565
285 0 return undef;
286 }
287  
288 565 ▶ if ($self->{'align'} eq 'left') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'align'} eq 'left') { } 0 565 565
elsif ($self->{'align'} eq 'triangular') { } 565 0 565
289 0 ▶ if ($x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 0) 0 0 0
290 0 return undef;
291 }
292 0 $x = 2*$x + $y; # adjust to triangular style
293  
294 } elsif ($self->{'align'} eq 'triangular') {
295 565 ▶ if (($x%2) != ($y%2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 2 != $y % 2) 280 285 565
296 280 return undef;
297 }
298  
299 } else {
300 # right or diagonal
301 0 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 0 0 0
302 0 return undef;
303 }
304 0 ▶ if ($self->{'align'} eq 'right') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'align'} eq 'right') { } 0 0 0
305 0 $x = 2*$x - $y;
306 } else { # diagonal
307 0 ($x,$y) = ($x-$y, $x+$y);
308 }
309 }
310 ### adjusted xy: "$x,$y"
311  
312 # On row Y=2^k the points belong to belong in the level below except for
313 # the endmost X=Y or X=-Y. For example Y=4 has N=6 which is in the level
314 # below, but at the end has N=9 belongs to the level above. So $y-1 puts
315 # Y=2^k into the level below and +($y==abs($x)) pushes the end back up to
316 # the next.
317 #
318 285 my ($len, $level) = round_down_pow ($y-1 + ($y==abs($x)),
319 2);
320 ### pow2 round down: $y-1+($y==abs($x))
321 ### $len
322 ### $level
323  
324 285 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 285 285
325 0 return $level;
326 }
327  
328 285 my $n = 0;
329 285 ▶ while ($level-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level-- >= 0) 636 127 763
330 ### at: "$x,$y level=$level len=$len"
331 636 $n *= 3;
332  
333 636 ▶ if ($y < 0 || $x < -$y || $x > $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0 or $x < -$y or $x > $y) 158 478 636
Condition83%5/6
$y < 0 or $x < -$y or $x > $y
l!l&&r!l&&!r
7979478
$y < 0 or $x < -$y
l!l&&r!l&&!r
079557
MC/DC66%2/3
$y < 0 or $x < -$y or $x > $y
$y < 0 $x < -$y $x > $y
Truth table75%3/4
A or B or C
A= $y < 0B= $x < -$yC= $x > $y
ABCresult
1XX1
01X1
0011
0000
334 ### out of range
335 158 return undef;
336 }
337 478 ▶ if ($y < $len + !($x==$y||$x==-$y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $len + !($x == $y || $x == -$y)) { } 91 387 478
Condition100%3/3
$x == $y || $x == -$y
l!l&&r!l&&!r
14087251
MC/DC100%2/2
$x == $y || $x == -$y
$x == $y $x == -$y
Truth table100%3/3
A || B
A= $x == $yB= $x == -$y
ABresult
1X1
011
000
338 ### digit 0, first triangle, no change
339  
340 } else {
341 387 ▶ if ($level & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level & 1) { } 191 196 387
342 ### odd level
343 191 ▶ if ($x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 0) { } 87 104 191
344 ### digit 1, right triangle
345 87 $n += 1;
346 87 $y -= $len;
347 87 $x = - ($x-$len);
348 ### shift right and mirror to: "$x,$y"
349 } else {
350 ### digit 2, left triangle
351 104 $n += 2;
352 104 $y -= 2*$len;
353 ### shift down to: "$x,$y"
354 104 ($x,$y) = ((3*$y-$x)/2, # rotate -120
355 ($x+$y)/-2);
356 ### rotate to: "$x,$y"
357 }
358 } else {
359 ### even level
360 196 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) { } 95 101 196
361 ### digit 1, left triangle
362 95 $n += 1;
363 95 $y -= $len;
364 95 $x = - ($x+$len);
365 ### shift right and mirror to: "$x,$y"
366 } else {
367 ### digit 2, right triangle
368 101 $n += 2;
369 101 $y -= 2*$len;
370 ### shift down to: "$x,$y"
371 101 ($x,$y) = (($x+3*$y)/-2, # rotate +120
372 ($x-$y)/2);
373 ### now: "$x,$y"
374 }
375 }
376 }
377  
378 478 $len /= 2;
379 }
380  
381 127 ▶ if ($x == 0 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) { } 86 41 127
Condition66%2/3
$x == 0 and $y == 0
!ll&&!rl&&r
41086
MC/DC50%1/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table66%2/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
382 86 return $n;
383 } else {
384 41 return undef;
385 }
386 }
387  
388 # not exact
389 sub rect_to_n_range {
390 8 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=4 SCAR=15.5
Pod documented
391 ### SierpinskiArrowhead rect_to_n_range() ...
392  
393 8 $y1 = round_nearest ($y1);
394 8 $y2 = round_nearest ($y2);
395 8 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # swap to y1<=y2
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 8 8
396  
397 8 ▶ if ($self->{'align'} eq 'diagonal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"align"} eq "diagonal") 0 8 8
398 0 $y2 += max (round_nearest ($x1),
399 round_nearest ($x2));
400 }
401  
402 8 ▶ unless ($y2 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y2 >= 0) 0 8 8
403 ### rect all negative, no N ...
404 0 return (1, 0);
405 }
406  
407 8 my ($pow,$exp) = round_down_pow ($y2-1, 2);
408 ### $y2
409 ### $level
410 8 return (0, 3**($exp+1));
411 }
412  
413 #-----------------------------------------------------------------------------
414 # level_to_n_range()
415  
416 sub level_to_n_range {
417 4 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
418 4 return (0, 3**$level);
419 }
420 sub n_to_level {
421 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
422 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
423 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
424 0 $n = round_nearest($n);
425 0 my ($pow, $exp) = round_up_pow ($n, 3);
426 0 return $exp;
427 }
428  
429 #-----------------------------------------------------------------------------
430 5 1;
431 __END__
« PlanePath/HilbertSides.pm ↑ Summary PlanePath/SierpinskiArrowheadCentres.pm »