PlanePath/QuadricCurve.pm

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