PlanePath/TriangleSpiral.pm

filter: Statementstmt 64.9% 63 / 97 Branchbran 8.3% 1 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 76.0% 19 / 25 Podpod 100% 6 / 6 totaltotal 56.3% 89 / 158 CC 13 SCAR 25.3
CC
13
Cov
53%
rect_to_n_range
42.0
xy_to_n
30.0
n_to_xy
12.0
CRAP 12.6
« PlanePath/GreekKeySpiral.pm ↑ Summary PlanePath/RationalsTree.pm »
1 # Copyright 2010, 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 package Math::PlanePath::TriangleSpiral;
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 #use List::Util 'max';
23 1 *max = \&Math::PlanePath::_max;
24  
25 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 $VERSION = 128;
27 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 @ISA = ('Math::PlanePath');
29  
30 use Math::PlanePath::Base::Generic
31 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
33  
34 # uncomment this to run the ### lines
35 #use Smart::Comments;
36  
37  
38 1 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_even;
39 1 use constant parameter_info_array =>
40 1 ▶ [ Math::PlanePath::Base::Generic::parameter_info_nstart1() ];
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 sub x_negative_at_n {
43 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
44 0 return $self->n_start + 4;
45 }
46 sub y_negative_at_n {
47 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
48 0 return $self->n_start + 6;
49 }
50 sub _UNDOCUMENTED__dxdy_list_at_n {
51 0 ▶ my ($self) = @_;
Subroutine _UNDOCUMENTED__dxdy_list_at_n: not called CC=1 SCAR=6.9
52 0 return $self->n_start + 3;
53 }
54 1 ▶use constant dx_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant dx_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 use constant 1.02 _UNDOCUMENTED__dxdy_list => (2,0, # E
59 -1,1, # NW
60 1 ▶ -1,-1); # SW
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use constant absdx_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use constant dsumxy_minimum => -2; # SW diagonal
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use constant dsumxy_maximum => 2; # dX=+2 horiz
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use constant ddiffxy_minimum => -2; # NW diagonal
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use constant ddiffxy_maximum => 2; # dX=+2 horiz
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use constant dir_maximum_dxdy => (-1,-1); # at most South-West diagonal
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 1 ▶use constant turn_any_right => 0; # only left or straight
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70  
71 #------------------------------------------------------------------------------
72  
73 sub new {
74 1 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=2 SCAR=7.0
Pod documented
75 1 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 1 0 1
76 1 $self->{'n_start'} = $self->default_n_start;
77 }
78 1 return $self;
79 }
80  
81 # base at bottom right corner
82 # d = [ 1, 2, 3 ]
83 # n = [ 2, 11, 29 ]
84 # $d = 1/2 + sqrt(2/9 * $n + -7/36)
85 # = 1/2 + sqrt(8/36 * $n + -7/36)
86 # = 0.5 + sqrt(8*$n + -7)/6
87 # = (1 + 2*sqrt(8*$n + -7)/6) / 2
88 # = (1 + sqrt(8*$n + -7)/3) / 2
89 # = (3 + sqrt(8*$n - 7)) / 6
90 #
91 # $n = (9/2*$d**2 + -9/2*$d + 2)
92 # = (4.5*$d - 4.5)*$d + 2
93 #
94 # top of pyramid
95 # d = [ 1, 2, 3 ]
96 # n = [ 4, 16, 37 ]
97 # $n = (9/2*$d**2 + -3/2*$d + 1)
98 # so remainder from there
99 # rem = $n - (9/2*$d**2 + -3/2*$d + 1)
100 # = $n - (4.5*$d*$d - 1.5*$d + 1)
101 # = $n - ((4.5*$d - 1.5)*$d + 1)
102 #
103 #
104 sub n_to_xy {
105 0 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: not called CC=3 SCAR=24.8
Pod documented
106 #### TriangleSpiral n_to_xy: $n
107  
108 0 $n = $n - $self->{'n_start'}; # starting $n==0, warn if $n==undef
109 0 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
110  
111 0 my $d = int ((3 + _sqrtint(8*$n+1)) / 6);
112 #### $d
113  
114 0 $n -= (9*$d - 3)*$d/2;
115 #### remainder: $n
116  
117 0 ▶ if ($n <= 3*$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 3 * $d) { } 0 0 0
118 ### sides, remainder pos/neg from top
119 0 return (-$n,
120 2*$d - abs($n));
121 } else {
122 ### rightwards from bottom left
123 ### remainder: $n - 3*$d
124 # corner is x=-3*$d
125 # so -3*$d + 2*($n - 3*$d)
126 # = -3*$d + 2*$n - 6*$d
127 # = -9*$d + 2*$n
128 # = 2*$n - 9*$d
129 0 return (2*$n - 9*$d,
130 -$d);
131 }
132 }
133  
134 sub xy_to_n {
135 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: not called CC=5 SCAR=34.0
Pod documented
136 0 $x = round_nearest ($x);
137 0 $y = round_nearest ($y);
138 ### xy_to_n(): "$x,$y"
139  
140 0 ▶ if (($x ^ $y) & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x ^ $y) & 1) 0 0 0
141 0 return undef; # nothing on odd points
142 }
143  
144 0 ▶ if ($y < 0 && 3*$y <= $x && $x <= -3*$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0 and 3 * $y <= $x and $x <= -3 * $y) { } 0 0 0
Condition0%0/6
$y < 0 and 3 * $y <= $x and $x <= -3 * $y
!ll&&!rl&&r
000
$y < 0 and 3 * $y <= $x
!ll&&!rl&&r
000
MC/DC0%0/3
$y < 0 and 3 * $y <= $x and $x <= -3 * $y
$y < 0 3 * $y <= $x $x <= -3 * $y
Truth table0%0/4
A and B and C
A= $y < 0B= 3 * $y <= $xC= $x <= -3 * $y
ABCresult
0XX0
10X0
1100
1111
145 ### bottom horizontal
146 # negative y, at vertical x=0
147 # [ -1, -2, -3, -4, -5, -6 ]
148 # [ 8.5, 25, 50.5, 85, 128.5, 181 ]
149 # $n = (9/2*$y**2 + -3*$y + 1)
150 # = (4.5*$y*$y + -3*$y + 1)
151 # = ((4.5*$y -3)*$y + 1)
152 # from which $x/2
153 #
154 0 return ((9*$y - 6)*$y/2) + $x/2 + $self->{'n_start'};
155  
156 } else {
157 ### sides diagonal
158 #
159 # positive y, x=0 centres
160 # [ 2, 4, 6, 8 ]
161 # [ 4, 16, 37, 67 ]
162 # n = (9/8*$d**2 + -3/4*$d + 1)
163 # = (9/8*$d + -3/4)*$d + 1
164 # = (9*$d + - 6)*$d/8 + 1
165 # from which -$x offset
166 #
167 0 my $d = abs($x) + $y;
168 0 return ((9*$d - 6)*$d/8) - $x + $self->{'n_start'};
169 }
170 }
171  
172 # not exact
173 sub rect_to_n_range {
174 0 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: not called CC=6 SCAR=37.4
Pod documented
175  
176 0 $x1 = round_nearest ($x1);
177 0 $y1 = round_nearest ($y1);
178 0 $x2 = round_nearest ($x2);
179 0 $y2 = round_nearest ($y2);
180  
181 0 my $d = 0;
182 0 foreach my $x ($x1, $x2) {
183 0 foreach my $y ($y1, $y2) {
184 0 ▶ $d = max ($d,
Branch: 1 branch
BranchTrueFalseTotal
$y < 0 && 3 * $y <= $x && $x <= -3 * $y ? : 0 0 0
Condition0%0/6
$y < 0 and 3 * $y <= $x and $x <= -3 * $y
!ll&&!rl&&r
000
$y < 0 and 3 * $y <= $x
!ll&&!rl&&r
000
MC/DC0%0/3
$y < 0 and 3 * $y <= $x and $x <= -3 * $y
$y < 0 3 * $y <= $x $x <= -3 * $y
Truth table0%0/4
A and B and C
A= $y < 0B= 3 * $y <= $xC= $x <= -3 * $y
ABCresult
0XX0
10X0
1100
1111
185 1 + ($y < 0 && 3*$y <= $x && $x <= -3*$y
186 ? -$y # bottom horizontal
187 : int ((abs($x) + $y) / 2))); # sides
188 }
189 }
190 return ($self->{'n_start'},
191 0 (9*$d - 9)*$d/2 + $self->{'n_start'});
192 }
193  
194 1 1;
195 __END__
« PlanePath/GreekKeySpiral.pm ↑ Summary PlanePath/RationalsTree.pm »