PlanePath/GosperReplicate.pm

filter: Statementstmt 82.9% 122 / 147 Branchbran 72.2% 26 / 36 Conditioncond 100% 8 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 92.0% 23 / 25 Podpod 100% 6 / 6 totaltotal 83.1% 188 / 226 CC 25 SCAR 17.6
CC
25
Cov
82%
n_to_xy
13.0
n_to_level
12.0
xy_to_n
7.2
CRAP 5.8
« PlanePath/CellularRule190.pm ↑ Summary PlanePath/CfracDigits.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=GosperReplicate --lines --scale=10
20 # math-image --path=GosperReplicate --all --output=numbers_dash
21 # math-image --path=GosperReplicate,numbering_type=rotate --all --output=numbers_dash
22 #
23  
24 package Math::PlanePath::GosperReplicate;
25 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use POSIX 'ceil';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Math::Libm 'hypot';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Math::PlanePath::SacksSpiral;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 $VERSION = 128;
34 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 @ISA = ('Math::PlanePath');
36  
37 use Math::PlanePath::Base::Generic
38 1 'is_infinite',
39 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 use Math::PlanePath::Base::Digits
41 1 'round_up_pow',
42 'digit_split_lowtohigh',
43 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 # uncomment this to run the ### lines
46 #use Smart::Comments;
47  
48  
49 1 use constant parameter_info_array =>
50 [ { name => 'numbering_type',
51 display => 'Numbering',
52 share_key => 'numbering_type_rotate',
53 type => 'enum',
54 default => 'fixed',
55 choices => ['fixed','rotate'],
56 choices_display => ['Fixed','Rotate'],
57 description => 'Fixed or rotating sub-part numbering.',
58 },
59 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 1 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_even;
63  
64 1 ▶use constant x_negative_at_n => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use constant y_negative_at_n => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use constant absdx_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use constant dir_maximum_dxdy => (3,-1);
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 #------------------------------------------------------------------------------
70 sub new {
71 6 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
72 6 ▶ $self->{'numbering_type'} ||= 'fixed'; # default
Condition100%2/2
$self->{"numbering_type"} ||= "fixed"
l!l
42
MC/DC100%1/1
$self->{"numbering_type"} ||= "fixed"
$self->{"numbering_type"}
Truth table100%2/2
A
A= $self->{"numbering_type"}
Aresult
11
00
73 6 return $self;
74 }
75  
76 sub _digits_rotate_lowtohigh {
77 7626 ▶ my ($aref) = @_;
Subroutine _digits_rotate_lowtohigh: called CC=3 SCAR=11.0
78 7626 my $rot = 0;
79 7626 foreach my $digit (reverse @$aref) {
80 25123 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) 22617 2506 25123
81 22617 $rot += $digit-1;
82 22617 $digit = ($rot % 6) + 1; # mutate $aref
83 }
84 }
85 }
86 sub _digits_unrotate_lowtohigh {
87 16009 ▶ my ($aref) = @_;
Subroutine _digits_unrotate_lowtohigh: called CC=3 SCAR=11.0
88 16009 my $rot = 0;
89 16009 foreach my $digit (reverse @$aref) {
90 58866 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) 52553 6313 58866
91 52553 $digit = ($digit-1-$rot) % 6; # mutate $aref
92 52553 $rot += $digit;
93 52553 $digit++;
94 }
95 }
96 }
97  
98 sub n_to_xy {
99 10690 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=12 SCAR=25.7
Pod documented
100 ### GosperReplicate n_to_xy(): $n
101  
102 10690 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 10690 10690
103 0 return;
104 }
105 10690 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 10690 10690
106 0 return ($n,$n);
107 }
108  
109 {
110 10690 my $int = int($n);
111 ### $int
112 ### $n
113 10690 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 10690 10690
114 0 my ($x1,$y1) = $self->n_to_xy($int);
115 0 my ($x2,$y2) = $self->n_to_xy($int+1);
116 0 my $frac = $n - $int; # inherit possible BigFloat
117 0 my $dx = $x2-$x1;
118 0 my $dy = $y2-$y1;
119 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
120 }
121 10690 $n = $int; # BigFloat int() gives BigInt, use that
122 }
123  
124 10690 my $x = my $y = $n*0; # inherit bigint from $n
125 10690 my $sx = $x + 2; # 2
126 10690 my $sy = $x; # 0
127  
128 # digit
129 # 3 2
130 # \ /
131 # 4---0---1
132 # / \
133 # 5 6
134  
135 10690 my @digits = digit_split_lowtohigh($n,7);
136 10690 ▶ if ($self->{'numbering_type'} eq 'rotate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numbering_type"} eq "rotate") 7260 3430 10690
137 7260 _digits_rotate_lowtohigh(\@digits);
138 }
139  
140 10690 foreach my $digit (@digits) {
141 ### digit: "$digit $x,$y side $sx,$sy"
142  
143 33828 ▶ if ($digit == 1) {
Branch: 6 branches
BranchTrueFalseTotal
if ($digit == 1) { } 5258 28570 33828
elsif ($digit == 2) { } 5258 23312 28570
elsif ($digit == 3) { } 5258 18054 23312
elsif ($digit == 4) { } 4915 13139 18054
elsif ($digit == 5) { } 4915 8224 13139
elsif ($digit == 6) { } 4915 3309 8224
144 ### right ...
145 # $x = -$x; # rotate 180
146 # $y = -$y;
147 5258 $x += $sx;
148 5258 $y += $sy;
149 } elsif ($digit == 2) {
150 ### up right ...
151 # ($x,$y) = ((3*$y-$x)/2, # rotate -120
152 # ($x+$y)/-2);
153 5258 $x += ($sx - 3*$sy)/2; # at +60
154 5258 $y += ($sx + $sy)/2;
155  
156 } elsif ($digit == 3) {
157 ### up left ...
158 # ($x,$y) = (($x+3*$y)/2, # -60
159 # ($y-$x)/2);
160 5258 $x += ($sx + 3*$sy)/-2; # at +120
161 5258 $y += ($sx - $sy)/2;
162  
163 } elsif ($digit == 4) {
164 ### left
165 4915 $x -= $sx; # at -180
166 4915 $y -= $sy;
167  
168 } elsif ($digit == 5) {
169 ### down left
170 # ($x,$y) = (($x-3*$y)/2, # rotate +60
171 # ($x+$y)/2);
172 4915 $x += (3*$sy - $sx)/2; # at -120
173 4915 $y += ($sx + $sy)/-2;
174  
175 } elsif ($digit == 6) {
176 ### down right
177 # ($x,$y) = (($x+3*$y)/-2, # rotate +120
178 # ($x-$y)/2);
179 4915 $x += ($sx + 3*$sy)/2; # at -60
180 4915 $y += ($sy - $sx)/2;
181 }
182  
183 # 2*(sx,sy) + rot+60(sx,sy)
184 33828 ($sx,$sy) = ((5*$sx - 3*$sy) / 2,
185 ($sx + 5*$sy) / 2);
186 }
187 10690 return ($x,$y);
188 }
189  
190 # modulus
191 # 1 3
192 # \ /
193 # 5---0---2
194 # / \
195 # 4 6
196 # 0 1 2 3 4 5 6
197 1 my @modulus_to_x = (0,-1, 2, 1,-1,-2, 1);
198 1 my @modulus_to_y = (0, 1, 0, 1,-1, 0,-1);
199 1 my @modulus_to_digit = (0, 3, 1, 2, 5, 4, 6);
200  
201 sub xy_to_n {
202 15643 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=7 SCAR=19.7
Pod documented
203 ### GosperReplicate xy_to_n(): "$x, $y"
204  
205 15643 $x = round_nearest($x);
206 15643 $y = round_nearest($y);
207 15643 ▶ if (($x + $y) % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x + $y) % 2) 0 15643 15643
208 0 return undef;
209 }
210  
211 15643 my $level = _xy_to_level_ceil($x,$y);
212 15643 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 15643 15643
213 0 return $level;
214 }
215  
216 15643 my $zero = ($x * 0 * $y); # inherit bignum 0
217 15643 my @n; # digits low to high
218  
219 15643 ▶ while ($level-- >= 0 && ($x || $y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level-- >= 0 and $x || $y) 57851 15643 73494
Condition100%6/6
$level-- >= 0 and $x || $y
!ll&&!rl&&r
457678015236
$x or $y
l!l&&r!l&&!r
55884196715186
MC/DC66%2/3
$level-- >= 0 and $x || $y
$level-- >= 0 $x $y
Truth table75%3/4
A and B or C
A= $level-- >= 0B= $xC= $y
ABCresult
0XX0
1000
11X1
1011
220 ### at: "$x,$y m=".(($x + 2*$y) % 7)
221  
222 57851 my $m = ($x + 2*$y) % 7;
223 57851 push @n, $modulus_to_digit[$m];
224 57851 $x -= $modulus_to_x[$m];
225 57851 $y -= $modulus_to_y[$m];
226  
227 ### digit: "to $x,$y"
228 ### assert: (3 * $y + 5 * $x) % 14 == 0
229 ### assert: (5 * $y - $x) % 14 == 0
230  
231 # shrink
232 57851 ($x,$y) = ((3*$y + 5*$x) / 14,
233 (5*$y - $x) / 14);
234 }
235  
236 15643 ▶ if ($self->{'numbering_type'} eq 'rotate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numbering_type"} eq "rotate") 15643 0 15643
237 15643 _digits_unrotate_lowtohigh(\@n);
238 }
239 15643 return digit_join_lowtohigh (\@n, 7, $zero);
240 }
241  
242  
243 # not exact
244 sub rect_to_n_range {
245 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=1 SCAR=6.9
Pod documented
246 0 $y1 *= sqrt(3);
247 0 $y2 *= sqrt(3);
248 0 my ($r_lo, $r_hi) = Math::PlanePath::SacksSpiral::_rect_to_radius_range
249 ($x1,$y1, $x2,$y2);
250 0 $r_hi *= 2;
251 0 my $level_plus_1 = ceil( log(max(1,$r_hi/4)) / log(sqrt(7)) ) + 2;
252 0 return (0, 7**$level_plus_1 - 1);
253 }
254  
255 sub _xy_to_level_ceil {
256 15643 ▶ my ($x,$y) = @_;
Subroutine _xy_to_level_ceil: called CC=1 SCAR=0.0
257 15643 my $r = hypot($x,$y);
258 15643 $r *= 2;
259 15643 return ceil( log(max(1,$r/4)) / log(sqrt(7)) ) + 1;
260 }
261  
262 #------------------------------------------------------------------------------
263 # levels
264  
265 sub level_to_n_range {
266 5 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
267 5 return (0, 7**$level - 1);
268 }
269 sub n_to_level {
270 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
271 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
272 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
273 0 $n = round_nearest($n);
274 0 my ($pow, $exp) = round_up_pow ($n+1, 7);
275 0 return $exp;
276 }
277  
278  
279 #------------------------------------------------------------------------------
280 1 1;
281 __END__
« PlanePath/CellularRule190.pm ↑ Summary PlanePath/CfracDigits.pm »