PlanePath/DragonRounded.pm

filter: Statementstmt 91.2% 157 / 172 Branchbran 79.4% 27 / 34 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 90.0% 27 / 30 Podpod 100% 8 / 8 totaltotal 89.8% 222 / 247 CC 19 SCAR 17.4
CC
19
Cov
89%
n_to_xy
14.0
n_to_level
12.0
xy_to_n
3.0
CRAP 5.7
« PlanePath/CfracDigits.pm ↑ Summary PlanePath/GosperSide.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 # math-image --path=DragonRounded --lines --scale=10
20 # math-image --path=DragonRounded,arms=4 --all --output=numbers_dash --size=132x60
21 #
22  
23  
24 package Math::PlanePath::DragonRounded;
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 #use List::Util 'min','max';
28 1 *min = \&Math::PlanePath::_min;
29 1 *max = \&Math::PlanePath::_max;
30  
31 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 $VERSION = 129;
33 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 @ISA = ('Math::PlanePath');
35 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
36  
37 use Math::PlanePath::Base::Generic
38 1 ▶ 'is_infinite',
Subroutine BEGIN: called CC=1 SCAR=0.0
39 'round_nearest',
40 1 'floor';
41 use Math::PlanePath::Base::Digits
42 1 ▶ 'round_up_pow';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Math::PlanePath::DragonMidpoint;
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 n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant parameter_info_array => [ { name => 'arms',
Subroutine BEGIN: called CC=1 SCAR=0.0
51 share_key => 'arms_4',
52 display => 'Arms',
53 type => 'integer',
54 minimum => 1,
55 maximum => 4,
56 default => 1,
57 width => 1,
58 description => 'Arms',
59 1 } ];
60  
61 {
62 1 my @x_negative_at_n = (undef, 8,5,2,2);
63 0 ▶ sub x_negative_at_n {
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
64 0 my ($self) = @_;
65 0 return $x_negative_at_n[$self->{'arms'}];
66 }
67 }
68 {
69 1 my @y_negative_at_n = (undef, 26,17,8,3);
70 0 ▶ sub y_negative_at_n {
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
71 0 my ($self) = @_;
72 0 return $y_negative_at_n[$self->{'arms'}];
73 }
74 }
75 1 ▶use constant sumabsxy_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use constant absdiffxy_minimum => 1; # X=Y doesn't occur
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use constant rsquared_minimum => 1; # minimum X=1,Y=0
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 1 ▶use constant dx_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 *_UNDOCUMENTED__dxdy_list = \&Math::PlanePath::_UNDOCUMENTED__dxdy_list_eight;
84 1 ▶use constant dsumxy_minimum => -2; # diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use constant dsumxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 ▶use constant turn_any_straight => 0; # never straight
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91  
92 #------------------------------------------------------------------------------
93  
94 17 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
95 17 my $self = shift->SUPER::new(@_);
96 17 ▶ $self->{'arms'} = max(1, min(4, $self->{'arms'} || 1));
Condition100%2/2
$self->{'arms'} || 1
l!l
134
MC/DC100%1/1
$self->{'arms'} || 1
$self->{'arms'}
Truth table100%2/2
A
A= $self->{'arms'}
Aresult
11
00
97 17 return $self;
98 }
99  
100 241 ▶sub n_to_xy {
Subroutine n_to_xy: called CC=14 SCAR=26.4
Pod documented
101 241 my ($self, $n) = @_;
102 ### DragonRounded n_to_xy(): $n
103  
104 0 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 241 241
105 0 ▶ if (is_infinite($n)) { return ($n, $n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 241 241
106  
107 241 my $frac;
108 {
109 241 my $int = int($n);
110 241 $frac = $n - $int;
111 241 $n = $int; # BigFloat int() gives BigInt, use that
112 }
113 ### $frac
114  
115 241 my $zero = ($n * 0); # inherit bignum 0
116  
117 # arm as initial rotation
118 241 my $rot = _divrem_mutate ($n, $self->{'arms'});
119  
120 # two points per edge
121 241 my $x_offset = _divrem_mutate ($n, 2);
122  
123 # ENHANCE-ME: sx,sy just from len=3*2**level
124 241 my @digits;
125 my @sx;
126 241 my @sy;
127 {
128 241 my $sx = $zero + 3;
129 241 my $sy = $zero;
130 241 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 1677 241 1918
131 1677 push @digits, ($n % 2);
132 1677 push @sx, $sx;
133 1677 push @sy, $sy;
134 1677 $n = int($n/2);
135  
136 # (sx,sy) + rot+90(sx,sy)
137 1677 ($sx,$sy) = ($sx - $sy,
138 $sy + $sx);
139 }
140 }
141  
142 ### @digits
143 241 my $rev = 0;
144 241 my $x = $zero;
145 241 my $y = $zero;
146 241 my $above_low_zero = 0;
147  
148 241 ▶ for (my $i = $#digits; $i >= 0; $i--) { # high to low
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 1677 241 1918
149 1677 my $digit = $digits[$i];
150 1677 my $sx = $sx[$i];
151 1677 my $sy = $sy[$i];
152 ### at: "$x,$y $digit side $sx,$sy"
153 ### $rot
154  
155 1677 ▶ if ($rot & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 2) 729 948 1677
156 729 ($sx,$sy) = (-$sx,-$sy);
157 }
158 1677 ▶ if ($rot & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 1) 832 845 1677
159 832 ($sx,$sy) = (-$sy,$sx);
160 }
161 ### rotated side: "$sx,$sy"
162  
163 1677 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) { } 835 842 1677
164 835 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 409 426 835
165 409 $x += -$sy;
166 409 $y += $sx;
167 ### rev add to: "$x,$y next is still rev"
168 } else {
169 426 $above_low_zero = $digits[$i+1];
170 426 $rot ++;
171 426 $rev = 0;
172 ### rev rot, next is no rev ...
173 }
174 } else {
175 842 ▶ if ($digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit) { } 534 308 842
176 534 $rot ++;
177 534 $x += $sx;
178 534 $y += $sy;
179 534 $rev = 1;
180 ### plain add to: "$x,$y next is rev"
181 } else {
182 308 $above_low_zero = $digits[$i+1];
183 }
184 }
185 }
186  
187 # Digit above the low zero is the direction of the next turn, 0 for left,
188 # 1 for right, and that determines the y_offset to apply to go across
189 # towards the next edge. When original input $n is odd, which means
190 # $x_offset 0 at this point, there's no y_offset as going along the edge
191 # not across the vertex.
192 #
193 241 ▶ my $y_offset = ($x_offset ? ($above_low_zero ? -$frac : $frac)
Branch: 2 branches
BranchTrueFalseTotal
$x_offset ? : 127 114 241
$above_low_zero ? : 62 65 127
194 : 0);
195 241 $x_offset = $frac + 1 + $x_offset;
196  
197 ### final: "$x,$y rot=$rot above_low_zero=$above_low_zero offset=$x_offset,$y_offset"
198 241 ▶ if ($rot & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 2) 99 142 241
199 99 ($x_offset,$y_offset) = (-$x_offset,-$y_offset); # rotate 180
200 }
201 241 ▶ if ($rot & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot & 1) 121 120 241
202 121 ($x_offset,$y_offset) = (-$y_offset,$x_offset); # rotate +90
203 }
204 241 $x = $x_offset + $x;
205 241 $y = $y_offset + $y;
206 ### rotated offset: "$x_offset,$y_offset return $x,$y"
207 241 return ($x,$y);
208 }
209  
210 1 my @yx_rtom_dx = ([undef, 1, 1, undef, 1, 1],
211 [ 0, undef, undef, 1, undef, undef],
212 [ 0, undef, undef, 1, undef, undef],
213 [undef, 1, 1, undef, 1, 1],
214 [ 1, undef, undef, 0, undef, undef],
215 [ 1, undef, undef, 0, undef, undef]);
216  
217 1 my @yx_rtom_dy = ([undef, 0, 0, undef, -1, -1],
218 [ 0, undef, undef, 0, undef, undef],
219 [ 0, undef, undef, 0, undef, undef],
220 [undef, -1, -1, undef, 0, 0],
221 [ 0, undef, undef, 0, undef, undef],
222 [ 0, undef, undef, 0, undef, undef]);
223  
224 279 ▶sub xy_to_n {
Subroutine xy_to_n: called CC=3 SCAR=11.0
Pod documented
225 279 my ($self, $x, $y) = @_;
226 ### DragonRounded xy_to_n(): "$x, $y"
227  
228 279 $x = round_nearest($x);
229 279 $y = round_nearest($y);
230  
231 279 my $x6 = $x % 6;
232 279 my $y6 = $y % 6;
233 279 ▶ my $dx = $yx_rtom_dx[$y6][$x6]; defined $dx or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $dx or return undef 199 80 279
234 199 ▶ my $dy = $yx_rtom_dy[$y6][$x6]; defined $dy or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $dy or return undef 199 0 199
235  
236 # my $n = $self->Math::PlanePath::DragonMidpoint::xy_to_n
237 # ($x - floor($x/3) - $dx,
238 # $y - floor($y/3) - $dy);
239 # ### dxy: "$dx, $dy"
240 # ### to: ($x - floor($x/3) - $dx).", ".($y - floor($y/3) - $dy)
241 # ### $n
242  
243 199 return $self->Math::PlanePath::DragonMidpoint::xy_to_n
244 ($x - floor($x/3) - $dx,
245 $y - floor($y/3) - $dy);
246 }
247  
248 # level 21 n=1048576 .. 2097152
249 # min 1052677 0b100000001000000000101 at -1026,1 factor 1.99610706057474
250 # n=2^20 min r^2=2^20 plus a bit
251 # maybe ...
252 #
253 # not exact
254 147 ▶sub rect_to_n_range {
Subroutine rect_to_n_range: called CC=1 SCAR=0.0
Pod documented
255 147 my ($self, $x1,$y1, $x2,$y2) = @_;
256 ### DragonRounded rect_to_n_range(): "$x1,$y1 $x2,$y2 arms=$self->{'arms'}"
257  
258 147 $x1 = abs($x1);
259 147 $x2 = abs($x2);
260 147 $y1 = abs($y1);
261 147 $y2 = abs($y2);
262 147 my $xmax = int(max($x1,$x2) / 3);
263 147 my $ymax = int(max($y1,$y2) / 3);
264 return (0,
265 147 ($xmax*$xmax + $ymax*$ymax + 1) * $self->{'arms'} * 16);
266 }
267  
268 #------------------------------------------------------------------------------
269  
270 # each 2 points is a line segment, so 2*DragonMidpoint
271 # level 0 0--1
272 # level 1 0--1 2--3
273 # level 2 0--1 2--3 4--5 6--7
274 #
275 # arms=4
276 # level 0 0--3 / 1--4 / 2--5 / 3--7
277 # level 1
278 #
279 # 2^level segments
280 # 2*2^level rounded points
281 # arms*2^level when multi-arm
282 # numbered starting 0
283 #
284 5 ▶sub level_to_n_range {
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
285 5 my ($self, $level) = @_;
286 5 return (0, 2**($level+1) * $self->{'arms'} - 1);
287 }
288  
289 0 ▶sub n_to_level {
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
290 0 my ($self, $n) = @_;
291 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
292 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
293 0 $n = round_nearest($n);
294 0 _divrem_mutate ($n, 2*$self->{'arms'});
295 0 my ($pow, $exp) = round_up_pow ($n+1, 2);
296 0 return $exp;
297 }
298  
299 #------------------------------------------------------------------------------
300 1 1;
301 __END__
« PlanePath/CfracDigits.pm ↑ Summary PlanePath/GosperSide.pm »