PlanePath/QuadricCurve.pm

filter: Statementstmt 87.5% 119 / 136 Branchbran 75.8% 44 / 58 Conditioncond 82.6% 19 / 23 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 94.7% 18 / 19 Podpod 100% 5 / 5 totaltotal 84.1% 212 / 252 CC 40 SCAR 21.7
CC
40
Cov
84%
xy_to_n
15.1
n_to_xy
12.2
n_to_level
12.0
CRAP 8.7
« PlanePath/AlternatePaperMidpoint.pm ↑ Summary PlanePath/PentSpiralSkewed.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 # cf http://mathcurve.com/fractals/minkowski/minkowski.shtml
20  
21  
22 package Math::PlanePath::QuadricCurve;
23 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 $VERSION = 128;
28 2 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 @ISA = ('Math::PlanePath::Base::NSEW',
31 'Math::PlanePath');
32  
33 use Math::PlanePath::Base::Generic
34 2 'is_infinite',
35 2 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 use Math::PlanePath::Base::Digits
37 2 'round_down_pow',
38 'round_up_pow',
39 2 ▶ 'digit_split_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
41  
42 # uncomment this to run the ### lines
43 #use Devel::Comments;
44  
45 2 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use constant y_negative_at_n => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use constant sumxy_minimum => 0; # triangular X>=-Y
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use constant diffxy_minimum => 0; # triangular Y<=X so X-Y>=0
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51  
52 #------------------------------------------------------------------------------
53  
54 # 2---3
55 # | |
56 # 0---1 4 7---8
57 # | |
58 # 5---6
59 #
60 sub n_to_xy {
61 1014 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=12 SCAR=25.0
Pod documented
62 ### QuadricCurve n_to_xy(): $n
63  
64 1014 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 1014 1014
65 1014 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 1014 1014
66  
67 1014 my $x;
68 {
69 1014 my $int = int($n);
70 1014 $x = $n - $int; # frac
71 1014 $n = $int; # BigFloat/BigRat int() gives BigInt, use that
72 }
73 1014 my $y = $x * 0; # inherit bignum 0
74 1014 my $len = $y + 1; # inherit bignum 1
75  
76 1014 foreach my $digit (digit_split_lowtohigh($n,8)) {
77 ### at: "$x,$y digit=$digit"
78  
79 3436 ▶ if ($digit == 0) {
Branch: 8 branches
BranchTrueFalseTotal
if ($digit == 0) { } 308 3128 3436
elsif ($digit == 1) { } 869 2259 3128
elsif ($digit == 2) { } 381 1878 2259
elsif ($digit == 3) { } 381 1497 1878
elsif ($digit == 4) { } 381 1116 1497
elsif ($digit == 5) { } 373 743 1116
elsif ($digit == 6) { } 373 370 743
elsif ($digit == 7) { } 370 0 370
80  
81 } elsif ($digit == 1) {
82 869 ($x,$y) = (-$y + $len, # rotate +90 and offset
83 $x);
84  
85 } elsif ($digit == 2) {
86 381 $x += $len; # offset
87 381 $y += $len;
88  
89 } elsif ($digit == 3) {
90 381 ($x,$y) = ($y + 2*$len, # rotate -90 and offset
91 -$x + $len);
92  
93 } elsif ($digit == 4) {
94 381 ($x,$y) = ($y + 2*$len, # rotate -90 and offset
95 -$x);
96  
97 } elsif ($digit == 5) {
98 373 $x += 2*$len; # offset
99 373 $y -= $len;
100  
101 } elsif ($digit == 6) {
102 373 ($x,$y) = (-$y + 3*$len, # rotate +90 and offset
103 $x - $len);
104  
105 } elsif ($digit == 7) {
106 ### assert: $digit==7
107 370 $x += 3*$len; # offset
108 }
109 3436 $len *= 4;
110 }
111  
112 ### final: "$x,$y"
113 1014 return ($x,$y);
114 }
115  
116  
117 # 8
118 # |
119 # 7---6
120 # |
121 # 3---4---5
122 # |
123 # 2---1
124 # |
125 # 0
126 #
127 # |
128 # * 11--12--13
129 # / \ |
130 # 2---3 10---9
131 # / | | \ |
132 # 0---1 4 7---8
133 # \ | | /
134 # 5---6
135 # \ /
136 # *
137 #
138 sub xy_to_n {
139 5618 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=15 SCAR=27.1
Pod documented
140 ### QuadricCurve xy_to_n(): "$x, $y"
141  
142 5618 $x = round_nearest ($x);
143 5618 $y = round_nearest ($y);
144 5618 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 265 5353 5618
145 ### neg x ...
146 265 return undef;
147 }
148 5353 ▶ my ($len,$level) = round_down_pow (($x+abs($y)) || 1, 4);
Condition100%2/2
$x + abs($y) || 1
l!l
53521
MC/DC100%1/1
$x + abs($y) || 1
$x + abs($y)
Truth table100%2/2
A
A= $x + abs($y)
Aresult
11
00
149 ### $level
150 ### $len
151 5353 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 5353 5353
152 0 return $level;
153 }
154  
155 my $diamond_p = sub {
156 ### diamond_p(): "$x,$y len=$len is ".(($x == 0 && $y == 0) || ($y <= $x && $y > -$x && $y < $len-$x && $y >= $x-$len))
157 51656 ▶ return (($x == 0 && $y == 0)
Subroutine __ANON__: called CC=6 SCAR=17.9
Condition100%15/15
$x == 0 && $y == 0 || $y <= $x && $y > -$x && $y < $len - $x && $y >= $x - $len
l!l&&r!l&&!r
773323147652
$x == 0 and $y == 0
!ll&&!rl&&r
50080803773
$y <= $x and $y > -$x and $y < $len - $x and $y >= $x - $len
!ll&&!rl&&r
4476728853231
$y <= $x and $y > -$x and $y < $len - $x
!ll&&!rl&&r
30778139896116
$y <= $x and $y > -$x
!ll&&!rl&&r
202041057420105
MC/DC100%6/6
$x == 0 && $y == 0 || $y <= $x && $y > -$x && $y < $len - $x && $y >= $x - $len
$x == 0 $y == 0 $y <= $x $y > -$x $y < $len - $x $y >= $x - $len
Truth table72%8/11
A and B || C and D and E and F
A= $x == 0B= $y == 0C= $y <= $xD= $y > -$xE= $y < $len - $xF= $y >= $x - $len
ABCDEFresult
11XXXX1
0X11111
1011111
0X0XXX0
0X10XX0
0X110X0
0X11100
100XXX0
1010XX0
10110X0
1011100
158 || ($y <= $x
159 && $y > -$x
160 && $y < $len-$x
161 && $y >= $x-$len));
162 5353 };
163  
164 5353 my $n = 0;
165 5353 foreach (0 .. $level) {
166 8756 $n *= 8;
167 ### at: "level=$level len=$len x=$x,y=$y n=$n"
168 8756 ▶ if (&$diamond_p()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$diamond_p()) { } 367 8389 8756
169 # digit 0 ...
170 } else {
171 8389 ($x,$y) = ($y, -($x-$len)); # shift and rotate -90
172  
173 8389 ▶ if (&$diamond_p()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$diamond_p()) { } 1295 7094 8389
174 # digit 1 ...
175 1295 $n += 1;
176 } else {
177 7094 ($x,$y) = (-$y, $x-$len); # shift and rotate +90
178  
179 7094 ▶ if (&$diamond_p()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$diamond_p()) { } 1060 6034 7094
180 # digit 2 ...
181 1060 $n += 2;
182 } else {
183 6034 ($x,$y) = (-$y, $x-$len); # shift and rotate +90
184  
185 6034 ▶ if (&$diamond_p()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$diamond_p()) { } 268 5766 6034
186 # digit 3 ...
187 268 $n += 3;
188 } else {
189 5766 $x -= $len;
190  
191 5766 ▶ if (&$diamond_p()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$diamond_p()) { } 360 5406 5766
192 # digit 4 ...
193 360 $n += 4;
194 } else {
195 5406 ($x,$y) = ($y, -($x-$len)); # shift and rotate -90
196  
197 5406 ▶ if (&$diamond_p()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$diamond_p()) { } 148 5258 5406
198 # digit 5 ...
199 148 $n += 5;
200 } else {
201 5258 ($x,$y) = ($y, -($x-$len)); # shift and rotate -90
202  
203 5258 ▶ if (&$diamond_p()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$diamond_p()) { } 305 4953 5258
204 # digit 6 ...
205 305 $n += 6;
206 } else {
207 4953 ($x,$y) = (-$y, $x-$len); # shift and rotate +90
208  
209 4953 ▶ if (&$diamond_p()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$diamond_p()) { } 201 4752 4953
210 # digit 7 ...
211 201 $n += 7;
212  
213 } else {
214 4752 return undef;
215 }
216 }
217 }
218 }
219 }
220 }
221 }
222 }
223 4004 $len /= 4;
224 }
225 ### end at: "x=$x,y=$y n=$n"
226 601 ▶ if ($x != 0 || $y != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x != 0 or $y != 0) 0 601 601
Condition33%1/3
$x != 0 or $y != 0
l!l&&r!l&&!r
00601
MC/DC0%0/2
$x != 0 or $y != 0
$x != 0 $y != 0
Truth table33%1/3
A or B
A= $x != 0B= $y != 0
ABresult
1X1
011
000
227 0 return undef;
228 }
229 601 return $n;
230 }
231  
232 # level extends to x= 4^level
233 # level = log4(x)
234 #
235 # not exact
236 sub rect_to_n_range {
237 1 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=4 SCAR=15.0
Pod documented
238 ### QuadricCurve rect_to_n_range(): "$x1,$y1 $x2,$y2"
239  
240 1 $x1 = round_nearest ($x1);
241 1 $x2 = round_nearest ($x2);
242 1 ▶ if ($x2 < $x1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < $x1) 0 1 1
243 0 $x2 = $x1; # x2 bigger
244 }
245 1 ▶ if ($x2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0) 0 1 1
246 0 return (1,0); # rect all x negative, no points
247 }
248 1 $y1 = abs (round_nearest ($y1));
249 1 $y2 = abs (round_nearest ($y2));
250 1 ▶ if ($y2 < $y1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 < $y1) 0 1 1
251 0 $y2 = $y1; # y2 bigger abs
252 }
253  
254 1 my $p4 = $x2+$y2+1;
255 ### $p4
256 1 return (0, $p4*$p4);
257 }
258  
259 #------------------------------------------------------------------------------
260 # levels
261  
262 sub level_to_n_range {
263 3 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: called CC=1 SCAR=0.0
Pod documented
264 3 return (0, 8**$level);
265 }
266 sub n_to_level {
267 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
268 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
269 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
270 0 $n = round_nearest($n);
271 0 my ($pow, $exp) = round_up_pow ($n, 8);
272 0 return $exp;
273 }
274  
275 #------------------------------------------------------------------------------
276  
277 {
278 # 0 1 2 3 4 5 6 7
279 2 my @_UNDOCUMENTED__n_to_turn_LSR = (undef, 1,-1,-1, 0, 1,1,-1);
280 sub _UNDOCUMENTED__n_to_turn_LSR {
281 998 ▶ my ($self, $n) = @_;
Subroutine _UNDOCUMENTED__n_to_turn_LSR: called CC=5 SCAR=18.4
282 998 ▶ if ($n < 1 || is_infinite($n)) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1 or is_infinite($n)) 0 998 998
Condition33%1/3
$n < 1 or is_infinite($n)
l!l&&r!l&&!r
00998
MC/DC0%0/2
$n < 1 or is_infinite($n)
$n < 1 is_infinite($n)
Truth table33%1/3
A or B
A= $n < 1B= is_infinite($n)
ABresult
1X1
011
000
283 998 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 1138 0 1138
284 1138 ▶ if (my $digit = _divrem_mutate($n,8)) { # lowest non-zero digit
Branch: 1 branch
BranchTrueFalseTotal
if (my $digit = _divrem_mutate($n, 8)) 998 140 1138
285 998 return $_UNDOCUMENTED__n_to_turn_LSR[$digit];
286 }
287 }
288 0 return undef;
289 }
290 }
291  
292  
293 #------------------------------------------------------------------------------
294 2 1;
295 __END__
« PlanePath/AlternatePaperMidpoint.pm ↑ Summary PlanePath/PentSpiralSkewed.pm »