PlanePath/QuadricIslands.pm

filter: Statementstmt 63.4% 80 / 126 Branchbran 23.0% 6 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 84.6% 22 / 26 Podpod 100% 6 / 6 totaltotal 57.8% 114 / 197 CC 17 SCAR 33.5
CC
17
Cov
53%
xy_to_n_list
110.0
n_to_level
12.0
n_to_xy
6.9
CRAP 28.6
« PlanePath/ZOrderCurve.pm ↑ Summary PlanePath/GrayCode.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=QuadricIslands --lines --scale=10
20 # math-image --path=QuadricIslands --all --output=numbers_dash --size=132x50
21  
22  
23 package Math::PlanePath::QuadricIslands;
24 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 #use List::Util 'max';
27 1 *max = \&Math::PlanePath::_max;
28  
29 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 $VERSION = 128;
31 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 @ISA = ('Math::PlanePath');
33  
34 use Math::PlanePath::Base::Generic
35 1 'is_infinite',
36 'round_nearest',
37 1 ▶ 'floor';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 use Math::PlanePath::Base::Digits
39 1 ▶ 'round_down_pow';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 ▶use Math::PlanePath::QuadricCurve;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # uncomment this to run the ### lines
44 #use Smart::Comments;
45  
46  
47 1 ▶use constant n_frac_discontinuity => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant x_negative_at_n => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant y_negative_at_n => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant sumabsxy_minimum => 1; # minimum X=1/2,Y=1/2
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant rsquared_minimum => 0.5; # minimum X=1/2,Y=1/2
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant dsumxy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use constant ddiffxy_minimum => -1; # dDiffXY=+1 or -1
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use constant ddiffxy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use constant dir_maximum_dxdy => (0,-1); # South
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 # N=1,2,3,4 gcd(1/2,1/2) = 1/2
61 1 ▶use constant gcdxy_minimum => 1/2;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 #------------------------------------------------------------------------------
64  
65 # N=1 to 4 4 of, level=0
66 # N=5 to 36 12 of, level=1
67 # N=37 to .. 48 of, level=3
68 #
69 # each loop = 4*8^level
70 #
71 # n_base = 1 + 4*8^0 + 4*8^1 + ... + 4*8^(level-1)
72 # = 1 + 4*[ 8^0 + 8^1 + ... + 8^(level-1) ]
73 # = 1 + 4*[ (8^level - 1)/7 ]
74 # = 1 + 4*(8^level - 1)/7
75 # = (4*8^level - 4 + 7)/7
76 # = (4*8^level + 3)/7
77 #
78 # n >= (4*8^level + 3)/7
79 # 7*n = 4*8^level + 3
80 # (7*n - 3)/4 = 8^level
81 #
82 # nbase(k+1)-nbase(k)
83 # = (4*8^(k+1)+3 - (4*8^k+3)) / 7
84 # = (4*8*8^k - 4*8^k) / 7
85 # = (4*8-4) * 8^k / 7
86 # = 28 * 8^k / 7
87 # = 4 * 8^k
88 #
89 # nbase(0) = (4*8^0 + 3)/7 = (4+3)/7 = 1
90 # nbase(1) = (4*8^1 + 3)/7 = (4*8+3)/7 = (32+3)/7 = 35/7 = 5
91 # nbase(2) = (4*8^2 + 3)/7 = (4*64+3)/7 = (256+3)/7 = 259/7 = 37
92 #
93 ### loop 1: 4* 8**1
94 ### loop 2: 4* 8**2
95 ### loop 3: 4* 8**3
96  
97 # sub _level_to_base {
98 # my ($level) = @_;
99 # return (4*8**$level + 3) / 7;
100 # }
101 # ### level_to_base(1): _level_to_base(1)
102 # ### level_to_base(2): _level_to_base(2)
103 # ### level_to_base(3): _level_to_base(3)
104  
105 # base = (4 * 8**$level + 3)/7
106 # = (4 * 8**($level+1) / 8 + 3)/7
107 # = (8**($level+1) / 2 + 3)/7
108 sub _n_to_base_and_level {
109 14 ▶ my ($n) = @_;
Subroutine _n_to_base_and_level: called CC=1 SCAR=0.0
110 14 my ($base,$level) = round_down_pow ((7*$n - 3)*2, 8);
111 14 return (($base/2 + 3)/7,
112 $level - 1);
113 }
114  
115 sub n_to_xy {
116 14 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=6 SCAR=19.4
Pod documented
117 ### QuadricIslands n_to_xy(): "$n"
118 14 ▶ if ($n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 14 14
119 14 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 14 14
120  
121 14 my ($base, $level) = _n_to_base_and_level($n);
122 ### $level
123 ### $base
124 ### level: "$level"
125 ### next base would be: (4 * 8**($level+1) + 3)/7
126  
127 14 my $rem = $n - $base;
128 ### $rem
129  
130 ### assert: $n >= $base
131 ### assert: $n < 8**($level+1)
132 ### assert: $rem>=0
133 ### assert: $rem < 4 * 8 ** $level
134  
135 14 my $sidelen = 8**$level;
136 14 my $side = int($rem / $sidelen);
137 ### $sidelen
138 ### $side
139 ### $rem
140 14 $rem -= $side*$sidelen;
141 ### assert: $side >= 0 && $side < 4
142 14 my ($x, $y) = Math::PlanePath::QuadricCurve::n_to_xy ($self, $rem);
143  
144 14 my $pos = 4**$level / 2;
145 ### side calc: "$x,$y for pos $pos"
146 ### $x
147 ### $y
148  
149 14 ▶ if ($side < 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($side < 1) { } 7 7 14
elsif ($side < 2) { } 0 7 7
elsif ($side < 3) { } 0 7 7
150 ### horizontal rightwards
151 7 return ($x - $pos,
152 $y - $pos);
153 } elsif ($side < 2) {
154 ### right vertical upwards
155 0 return (-$y + $pos, # rotate +90, offset
156 $x - $pos);
157 } elsif ($side < 3) {
158 ### horizontal leftwards
159 0 return (-$x + $pos, # rotate 180, offset
160 -$y + $pos);
161 } else {
162 ### left vertical downwards
163 7 return ($y - $pos, # rotate -90, offset
164 -$x + $pos);
165 }
166 }
167  
168 # +-------+-------+-------+
169 # |31 | 24 0,1| 23|
170 # | | | |
171 # | +-------+-------+ |
172 # | |4 | |3 | | |
173 # | | | | | | |
174 # +---|--- ---|--- ---|---+ Y=0.5
175 # |32 | | | | | 16|
176 # | | | | | | |
177 # | +=======+=======+ | Y=0
178 # | |1 | |2 | | |
179 # | | | | | | |
180 # +---|--- ---|--- ---|---+ Y=-0.5
181 # | | | | | | |
182 # | | | | | | |
183 # | +-------+-------+ | Y=-1
184 # | | | |
185 # |7 |8 | 15|
186 # +-------+-------+-------+
187 #
188 # -2 <= 2*x < 2, round to -2,-1,0,1
189 # then 4*yround -8,-4,0,4
190 # total -10 to 5 inclusive
191  
192 1 my @inner_n_list = ([1,7], [1,8], [2,8], [2,15], # Y=-1
193 [1,32], [1], [2], [2,16], # Y=-0.5
194 [4,32], [4], [3], [3,16], # Y=0
195 [4,31],[4,24],[3,24],[3,23]); # Y=0.5
196  
197 sub xy_to_n {
198 0 ▶ return scalar((shift->xy_to_n_list(@_))[0]);
Subroutine xy_to_n: not called CC=1 SCAR=6.9
Pod documented
199 }
200 sub xy_to_n_list {
201 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n_list: not called CC=10 SCAR=47.0
Pod documented
202 ### QuadricIslands xy_to_n(): "$x, $y"
203  
204 0 ▶ if ($x >= -1 && $x < 1
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= -1 and $x < 1 and $y >= -1 and $y < 1) 0 0 0
Condition0%0/9
$x >= -1 and $x < 1 and $y >= -1 and $y < 1
!ll&&!rl&&r
000
$x >= -1 and $x < 1 and $y >= -1
!ll&&!rl&&r
000
$x >= -1 and $x < 1
!ll&&!rl&&r
000
MC/DC0%0/4
$x >= -1 and $x < 1 and $y >= -1 and $y < 1
$x >= -1 $x < 1 $y >= -1 $y < 1
Truth table0%0/5
A and B and C and D
A= $x >= -1B= $x < 1C= $y >= -1D= $y < 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
205 && $y >= -1 && $y < 1) {
206  
207 ### round 2x: floor(2*$x)
208 ### round 2y: floor(2*$y)
209 ### index: floor(2*$x) + 4*floor(2*$y) + 10
210 ### assert: floor(2*$x) + 4*floor(2*$y) + 10 >= 0
211 ### assert: floor(2*$x) + 4*floor(2*$y) + 10 <= 15
212  
213 0 return @{$inner_n_list[ floor(2*$x)
214 + 4*floor(2*$y)
215 + 10 ]};
216 }
217  
218 0 $x = round_nearest($x);
219 0 $y = round_nearest($y);
220  
221 0 my $high;
222 0 ▶ if ($x >= $y + ($y>0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $y + ($y > 0)) { } 0 0 0
223 # +($y>0) to exclude the downward bump of the top side
224 ### below leading diagonal ...
225 0 ▶ if ($x < -$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < -$y) { } 0 0 0
226 ### bottom quarter ...
227 0 $high = 0;
228 } else {
229 ### right quarter ...
230 0 $high = 1;
231 0 ($x,$y) = ($y, -$x); # rotate -90
232 }
233 } else {
234 ### above leading diagonal
235 0 ▶ if ($y > -$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > -$x) { } 0 0 0
236 ### top quarter ...
237 0 $high = 2;
238 0 $x = -$x; # rotate 180
239 0 $y = -$y;
240 } else {
241 ### right quarter ...
242 0 $high = 3;
243 0 ($x,$y) = (-$y, $x); # rotate +90
244 }
245 }
246 ### rotate to: "$x,$y high=$high"
247  
248 # ymax = (10*4^(l-1)-1)/3
249 # ymax < (10*4^(l-1)-1)/3+1
250 # (10*4^(l-1)-1)/3+1 > ymax
251 # (10*4^(l-1)-1)/3 > ymax-1
252 # 10*4^(l-1)-1 > 3*(ymax-1)
253 # 10*4^(l-1) > 3*(ymax-1)+1
254 # 10*4^(l-1) > 3*(ymax-1)+1
255 # 10*4^(l-1) > 3*ymax-3+1
256 # 10*4^(l-1) > 3*ymax-2
257 # 4^(l-1) > (3*ymax-2)/10
258 #
259 # (2*4^(l-1) + 1)/3 = ymin
260 # 2*4^(l-1) + 1 = 3*y
261 # 2*4^(l-1) = 3*y-1
262 # 4^(l-1) = (3*y-1)/2
263 #
264 # ypos = 4^l/2 = 2*4^(l-1)
265  
266  
267 # z = -2*y+x
268 # (2*4**($level-1) + 1)/3 = z
269 # 2*4**($level-1) + 1 = 3*z
270 # 2*4**($level-1) = 3*z - 1
271 # 4**($level-1) = (3*z - 1)/2
272 # = (3*(-2y+x)-1)/2
273 # = (-6y+3x - 1)/2
274 # = -3*y + (3x-1)/2
275  
276 # 2*4**($level-1) = -2*y-x
277 # 4**($level-1) = -y-x/2
278 # 4**$level = -4y-2x
279 #
280 # line slope y/x = 1/2 as an index
281 0 my $z = -$y-$x/2;
282 0 my ($len,$level) = round_down_pow ($z, 4);
283 ### $z
284 ### amin: 2*4**($level-1)
285 ### $level
286 ### $len
287 0 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 0 0
288 0 return $level;
289 }
290  
291 0 $len *= 2;
292 0 $x += $len;
293 0 $y += $len;
294 ### shift to: "$x,$y"
295 0 my $n = Math::PlanePath::QuadricCurve::xy_to_n($self, $x, $y);
296  
297 # Nmin = (4*8^l+3)/7
298 # Nmin+high = (4*8^l+3)/7 + h*8^l
299 # = (4*8^l + 3 + 7h*8^l)/7 +
300 # = ((4+7h)*8^l + 3)/7
301 #
302 ### plain curve on: ($x+2*$len).",".($y+2*$len)." give n=".(defined $n && $n)
303 ### $high
304 ### high: (8**$level)*$high
305 ### base: (4 * 8**($level+1) + 3)/7
306 ### base with high: ((4+7*$high) * 8**($level+1) + 3)/7
307  
308 0 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) { } 0 0 0
309 0 return ((4+7*$high) * 8**($level+1) + 3)/7 + $n;
310 } else {
311 0 return;
312 }
313 }
314  
315 # level width extends
316 # side = 4^level
317 # ypos = 4^l / 2
318 # width = 1 + 4 + ... + 4^(l-1)
319 # = (4^l - 1)/3
320 # ymin = ypos(l) - 4^(l-1) - width(l-1)
321 # = 4^l / 2 - 4^(l-1) - (4^(l-1) - 1)/3
322 # = 4^(l-1) * (2 - 1 - 1/3) + 1/3
323 # = (2*4^(l-1) + 1) / 3
324 #
325 # (2*4^(l-1) + 1) / 3 = y
326 # 2*4^(l-1) + 1 = 3*y
327 # 2*4^(l-1) = 3*y-1
328 # 4^(l-1) = (3*y-1)/2
329 #
330 # ENHANCE-ME: slope Y=X/2+1 or thereabouts for sides
331 #
332 # not exact
333 sub rect_to_n_range {
334 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=1 SCAR=6.9
Pod documented
335 ### QuadricIslands rect_to_n_range(): "$x1,$y1 $x2,$y2"
336  
337 # $x1 = round_nearest ($x1);
338 # $y1 = round_nearest ($y1);
339 # $x2 = round_nearest ($x2);
340 # $y2 = round_nearest ($y2);
341  
342 0 my $m = max(abs($x1), abs($x2),
343 abs($y1), abs($y2));
344  
345 0 my ($len,$level) = round_down_pow (6*$m-2, 4);
346 ### $len
347 ### $level
348 0 return (1,
349 (32*8**$level - 4)/7);
350 }
351  
352 # ymax = ypos(l) + 4^(l-1) + width(l-1)
353 # = 4^l / 2 + 4^(l-1) + (4^(l-1) - 1)/3
354 # = 4^(l-1) * (4/2 + 1 + 1/3) - 1/3
355 # = 4^(l-1) * (2 + 1 + 1/3) - 1/3
356 # = 4^(l-1) * 10/3 - 1/3
357 # = (10*4^(l-1) - 1) / 3
358 #
359 # (10*4^(l-1) - 1) / 3 = y
360 # 10*4^(l-1) - 1 = 3*y
361 # 10*4^(l-1) = 3*y+1
362 # 4^(l-1) = (3*y+1)/10
363 #
364 # based on max ??? ...
365 #
366 # my ($power,$level) = round_down_pow ((3*$m+1-3)/10, 4);
367 # ### $power
368 # ### $level
369 # return (1,
370 # (4*8**($level+3) + 3)/7 - 1);
371  
372 #------------------------------------------------------------------------------
373 # Nstart(k) = (4*8^k + 3)/7
374 # Nend(k) = Nstart(k+1) - 1
375 # = (4*8*8^k + 3)/7 - 1
376 # = (4*8*8^k + 8*3 - 8*3 + 3)/7 - 1
377 # = (4*8*8^k + 8*3)/7 + (-8*3 + 3)/7 - 1
378 # = 8*Nstart(k) + (-8*3 + 3)/7 - 1
379 # = 8*Nstart(k) - 4
380  
381 sub level_to_n_range {
382 10 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
383 10 my $n_lo = (4 * 8**$level + 3)/7;
384 10 return ($n_lo, 8*$n_lo - 4);
385 }
386 sub n_to_level {
387 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
388 0 ▶ if ($n < 1) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 0 0
389 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
390 0 $n = round_nearest($n);
391 0 my ($base,$level) = _n_to_base_and_level($n);
392 0 return $level;
393 }
394  
395 #------------------------------------------------------------------------------
396 1 1;
397 __END__
« PlanePath/ZOrderCurve.pm ↑ Summary PlanePath/GrayCode.pm »