PlanePath/SierpinskiArrowheadCentres.pm

filter: Statementstmt 84.8% 145 / 171 Branchbran 67.2% 39 / 58 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 81.8% 18 / 22 Podpod 100% 8 / 8 totaltotal 78.0% 213 / 273 CC 34 SCAR 24.2
CC
34
Cov
79%
xy_to_n
23.4
n_to_level
12.0
n_to_xy
9.1
CRAP 11.3
« PlanePath/SierpinskiArrowhead.pm ↑ Summary PlanePath/TerdragonRounded.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 it
6 # under the terms of the GNU General Public License as published by the Free
7 # 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  
20 # math-image --path=SierpinskiArrowheadCentres --lines --scale=10
21 #
22 # math-image --path=SierpinskiArrowheadCentres --all --output=numbers_dash
23 # math-image --path=SierpinskiArrowheadCentres --all --text --size=80
24  
25  
26 package Math::PlanePath::SierpinskiArrowheadCentres;
27 5 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #use List::Util 'max';
31 5 *max = \&Math::PlanePath::_max;
32  
33 5 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 $VERSION = 128;
35 5 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 @ISA = ('Math::PlanePath');
37  
38 use Math::PlanePath::Base::Generic
39 5 'is_infinite',
40 5 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 use Math::PlanePath::Base::Digits
42 5 'round_down_pow',
43 'round_up_pow',
44 'digit_split_lowtohigh',
45 5 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # uncomment this to run the ### lines
48 #use Smart::Comments;
49  
50  
51 5 ▶use Math::PlanePath::SierpinskiArrowhead;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 5 *parameter_info_array # align parameter
53 = \&Math::PlanePath::SierpinskiArrowhead::parameter_info_array;
54 5 *new = \&Math::PlanePath::SierpinskiArrowhead::new;
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 5 *x_negative = \&Math::PlanePath::SierpinskiArrowhead::x_negative;
59 {
60 5 my %x_negative_at_n = (triangular => 2,
61 # right => undef,
62 left => 2,
63 # diagonal => undef,
64 );
65 sub x_negative_at_n {
66 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
67 0 return $x_negative_at_n{$self->{'align'}};
68 }
69 }
70 5 *x_maximum = \&Math::PlanePath::SierpinskiArrowhead::x_maximum;
71 5 ▶use constant sumxy_minimum => 0; # triangular X>=-Y
Subroutine BEGIN: called CC=1 SCAR=0.0
72 5 ▶use Math::PlanePath::SierpinskiTriangle;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 5 *diffxy_maximum = \&Math::PlanePath::SierpinskiTriangle::diffxy_maximum;
74  
75 5 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 5 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 5 *dx_minimum = \&Math::PlanePath::SierpinskiArrowhead::dx_minimum;
78 5 *dx_maximum = \&Math::PlanePath::SierpinskiArrowhead::dx_maximum;
79  
80 5 *_UNDOCUMENTED__dxdy_list = \&Math::PlanePath::SierpinskiArrowhead::_UNDOCUMENTED__dxdy_list; # same
81 5 ▶use constant 1.02 _UNDOCUMENTED__dxdy_list_at_n => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 5 *absdx_minimum = \&Math::PlanePath::SierpinskiArrowhead::absdx_minimum;
84 5 *absdx_maximum = \&Math::PlanePath::SierpinskiArrowhead::absdx_maximum;
85 5 *dsumxy_minimum = \&Math::PlanePath::SierpinskiArrowhead::dsumxy_minimum;
86 5 *dsumxy_maximum = \&Math::PlanePath::SierpinskiArrowhead::dsumxy_maximum;
87 sub ddiffxy_minimum {
88 0 ▶ my ($self) = @_;
Subroutine ddiffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
89 0 ▶ return ($self->{'align'} eq 'right' ? -1 : -2);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'right' ? : 0 0 0
90 }
91 sub ddiffxy_maximum {
92 0 ▶ my ($self) = @_;
Subroutine ddiffxy_maximum: not called CC=2 SCAR=17.9
Pod documented
93 0 ▶ return ($self->{'align'} eq 'right' ? 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'align'} eq 'right' ? : 0 0 0
94 }
95 5 *dir_maximum_dxdy = \&Math::PlanePath::SierpinskiArrowhead::dir_maximum_dxdy;
96  
97 #------------------------------------------------------------------------------
98  
99 # States as multiples of 3 so that state+digit is the lookup for next state
100 # and x,y bit.
101 #
102 # 0 3 6 9 12 15
103 #
104 # 8 0 4 4 0 8
105 # | | |\ |\ \ \
106 # 7-6 1-2 3 5 5 3 2-1 6-7
107 # \ \ | \ | \ | |
108 # 1 5 7 3 2 6 6 2 3 7 5 1
109 # |\ \ |\ \ \ | \ | | |\ | |\
110 # 0 2-3-4 8 6-5-4 0-1 7-8 8-7 1-0 4-5-6 8 4-3-2 0
111  
112 # 15 6 3
113 # 6 0 0 12 12 6
114  
115 # 0,1 0,2 1,2
116  
117 5 my @next_state = (6,0,15, 12,3,9, # 3,6
118 0,6,12, 15,9,3, # 6,9
119 3,12,6, 9,15,0); # 12,15
120 5 my @state_to_xbit = (0,1,0, 0,1,0,
121 0,0,1, 1,0,0,
122 0,0,1, 1,0,0); # 12,15
123 5 my @state_to_ybit = (0,0,1, 1,0,0,
124 0,1,0, 0,1,0,
125 1,0,0, 0,0,1); # 12,15
126  
127 # dx,dy for digit==0 and digit==1 in each stage
128 5 my @state_to_dx;
129 my @state_to_dy;
130 5 foreach my $state (0,1, 3,4, 6,7, 9,10, 12,13, 15,16) {
131 60 $state_to_dx[$state] = $state_to_xbit[$state+1] - $state_to_xbit[$state];
132 60 $state_to_dy[$state] = $state_to_ybit[$state+1] - $state_to_ybit[$state];
133 }
134  
135 sub n_to_xy {
136 145 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=9 SCAR=22.1
Pod documented
137 ### SierpinskiArrowheadCentres n_to_xy(): $n
138 145 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 145 145
139 0 return;
140 }
141 145 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 145 145
142 0 return ($n,$n);
143 }
144  
145 145 my $int = int($n);
146 145 $n -= $int; # fraction part
147  
148 145 my @digits = digit_split_lowtohigh($int,3);
149 145 ▶ my $state = ($#digits & 1 ? 6 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$#digits & 1 ? : 89 56 145
150 ### @digits
151 ### $state
152  
153 145 my (@x,@y); # bits low to high
154 145 my $dirstate = $state ^ 6; # if all digits==2
155  
156 145 foreach my $i (reverse 0 .. $#digits) {
157 ### at: "x=".join(',',@x[($i+1)..$#digits])." y=".join(',',@y[($i+1)..$#digits])." apply i=$i state=$state digit=$digits[$i]"
158  
159 696 my $digit = $digits[$i]; # high to low
160 696 $state += $digit;
161 696 $x[$i] = $state_to_xbit[$state];
162 696 $y[$i] = $state_to_ybit[$state];
163 696 ▶ if ($digit != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit != 2) 460 236 696
164 460 $dirstate = $state; # lowest non-2 digit
165 }
166 696 $state = $next_state[$state];
167 }
168  
169 145 my $zero = $int * 0;
170 145 my $x = $n*$state_to_dx[$dirstate] + digit_join_lowtohigh(\@x,2,$zero);
171 145 my $y = $n*$state_to_dy[$dirstate] + digit_join_lowtohigh(\@y,2,$zero);
172  
173 145 ▶ if ($self->{'align'} eq 'right') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'align'} eq 'right') { } 9 136 145
elsif ($self->{'align'} eq 'left') { } 9 127 136
elsif ($self->{'align'} eq 'triangular') { } 118 9 127
174 9 $y += $x;
175 } elsif ($self->{'align'} eq 'left') {
176 9 ($x,$y) = (-$y,$x+$y);
177 } elsif ($self->{'align'} eq 'triangular') {
178 118 ($x,$y) = ($x-$y,$x+$y);
179 }
180 145 return ($x,$y);
181 }
182  
183 sub xy_to_n {
184 36 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=19 SCAR=31.5
Pod documented
185 36 $x = round_nearest ($x);
186 36 $y = round_nearest ($y);
187 ### SierpinskiArrowheadCentres xy_to_n(): "$x, $y"
188  
189 36 ▶ if ($y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0) 0 36 36
190 0 return undef;
191 }
192  
193 36 ▶ if ($self->{'align'} eq 'left') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'align'} eq 'left') { } 9 27 36
elsif ($self->{'align'} eq 'triangular') { } 9 18 27
194 9 ▶ if ($x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 0) 0 9 9
195 0 return undef;
196 }
197 9 $x = 2*$x + $y; # adjust to triangular style
198  
199 } elsif ($self->{'align'} eq 'triangular') {
200 9 ▶ if (($x%2) != ($y%2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 2 != $y % 2) 0 9 9
201 0 return undef;
202 }
203  
204 } else {
205 # right or diagonal
206 18 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 0 18 18
207 0 return undef;
208 }
209 18 ▶ if ($self->{'align'} eq 'right') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'align'} eq 'right') { } 9 9 18
210 9 $x = 2*$x - $y;
211 } else { # diagonal
212 9 ($x,$y) = ($x-$y, $x+$y);
213 }
214 }
215 ### adjusted xy: "$x,$y"
216  
217  
218 36 my ($len, $level) = round_down_pow ($y, 2);
219 ### pow2 round up: ($y + ($y==$x || $y==-$x))
220 ### $len
221 ### $level
222 36 $level += 1;
223  
224 36 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 36 36
225 0 return $level;
226 }
227  
228 36 my $n = 0;
229 36 ▶ while ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 60 36 96
230 60 $n *= 3;
231 ### at: "$x,$y level=$level len=$len"
232  
233 60 ▶ if ($y < 0 || $x < -$y || $x > $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0 or $x < -$y or $x > $y) 0 60 60
Condition33%2/6
$y < 0 or $x < -$y or $x > $y
l!l&&r!l&&!r
0060
$y < 0 or $x < -$y
l!l&&r!l&&!r
0060
MC/DC0%0/3
$y < 0 or $x < -$y or $x > $y
$y < 0 $x < -$y $x > $y
Truth table25%1/4
A or B or C
A= $y < 0B= $x < -$yC= $x > $y
ABCresult
1XX1
01X1
0011
0000
234 ### out of range ...
235 0 return undef;
236 }
237  
238 60 ▶ if ($y < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $len) { } 12 48 60
239 ### digit 0, first triangle, no change ...
240  
241 } else {
242 48 ▶ if ($level & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level & 1) { } 24 24 48
243 ### odd level ...
244 24 ▶ if ($x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 0) { } 12 12 24
245 ### digit 1, right triangle ...
246 12 $n += 1;
247 12 $y -= $len;
248 12 $x = - ($x-$len);
249 ### shift right and mirror to: "$x,$y"
250 } else {
251 ### digit 2, left triangle ...
252 12 $n += 2;
253 12 $x += 1;
254 12 $y -= 2*$len-1;
255 ### shift down to: "$x,$y"
256 12 ($x,$y) = ((3*$y-$x)/2, # rotate -120
257 ($x+$y)/-2);
258 ### rotate to: "$x,$y"
259 }
260 } else {
261 ### even level ...
262 24 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) { } 12 12 24
263 ### digit 1, left triangle ...
264 12 $n += 1;
265 12 $y -= $len;
266 12 $x = - ($x+$len);
267 ### shift right and mirror to: "$x,$y"
268 } else {
269 ### digit 2, right triangle ...
270 12 $n += 2;
271 12 $x -= 1;
272 12 $y -= 2*$len-1;
273 ### shift down to: "$x,$y"
274 12 ($x,$y) = (($x+3*$y)/-2, # rotate +120
275 ($x-$y)/2);
276 ### now: "$x,$y"
277 }
278 }
279 }
280  
281 60 $level--;
282 60 $len /= 2;
283 }
284  
285 ### final: "$x,$y with n=$n"
286 36 ▶ if ($x == 0 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) { } 36 0 36
Condition33%1/3
$x == 0 and $y == 0
!ll&&!rl&&r
0036
MC/DC0%0/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table33%1/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
287 36 return $n;
288 } else {
289 0 return undef;
290 }
291 }
292  
293 # not exact
294 sub rect_to_n_range {
295 45 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=4 SCAR=14.4
Pod documented
296 ### SierpinskiArrowheadCentres rect_to_n_range(): "$x1,$y1, $x2,$y2"
297  
298 45 $y1 = round_nearest ($y1);
299 45 $y2 = round_nearest ($y2);
300 45 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # swap to y1<=y2
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 45 45
301  
302 45 ▶ if ($self->{'align'} eq 'diagonal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"align"} eq "diagonal") 9 36 45
303 9 $y2 += max (round_nearest ($x1),
304 round_nearest ($x2));
305 }
306  
307 45 ▶ unless ($y2 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y2 >= 0) 0 45 45
308 ### rect all negative, no N ...
309 0 return (1, 0);
310 }
311  
312 45 my ($len,$level) = round_down_pow ($y2, 2);
313 ### $y2
314 ### $level
315 45 return (0, 3**($level+1) - 1);
316 }
317  
318 #-----------------------------------------------------------------------------
319 # level_to_n_range()
320  
321 # shared by SierpinskiTriangle
322 sub level_to_n_range {
323 8 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
324 8 my $n_start = $self->n_start;
325 8 return ($n_start, $n_start + 3**$level - 1);
326 }
327 sub n_to_level {
328 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
329 0 $n = $n - $self->n_start;
330 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
331 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
332 0 $n = round_nearest($n);
333 0 my ($pow, $exp) = round_up_pow ($n+1, 3);
334 0 return $exp;
335 }
336  
337 #-----------------------------------------------------------------------------
338 5 1;
339 __END__
« PlanePath/SierpinskiArrowhead.pm ↑ Summary PlanePath/TerdragonRounded.pm »