PlanePath/AztecDiamondRings.pm

filter: Statementstmt 95.5% 107 / 112 Branchbran 96.6% 29 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.0% 23 / 25 Podpod 100% 6 / 6 totaltotal 95.3% 165 / 173 CC 16 SCAR 12.7
CC
16
Cov
96%
rect_to_n_range
9.0
n_to_xy
5.0
xy_to_n
3.0
CRAP 3.6
« PlanePath/AnvilSpiral.pm ↑ Summary PlanePath/PyramidSpiral.pm »
1 # Copyright 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  
20 # cute groupings
21 # AztecDiamondRings, FibonacciWord fibonacci_word_type plain, 10x10 scale 15
22  
23  
24 package Math::PlanePath::AztecDiamondRings;
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 = 128;
33 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 @ISA = ('Math::PlanePath');
35 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
36  
37 use Math::PlanePath::Base::Generic
38 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # uncomment this to run the ### lines
41 # use Smart::Comments;
42  
43  
44 1 use constant parameter_info_array =>
45 [
46 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
47 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use constant n_frac_discontinuity => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant xy_is_visited => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 sub x_negative_at_n {
53 0 ▶ my ($self) = @_;
Subroutine x_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
54 0 return $self->n_start + 1;
55 }
56 sub y_negative_at_n {
57 0 ▶ my ($self) = @_;
Subroutine y_negative_at_n: not called CC=1 SCAR=6.9
Pod documented
58 0 return $self->n_start + 2;
59 }
60 1 ▶use constant dx_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,0, # E
65 1,1, # NE
66 # not North
67 -1,1, # NW
68 -1,0, # W
69 -1,-1, # SW
70 0,-1, # S
71 1 ▶ 1,-1); # SE;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use constant dsumxy_minimum => -2; # diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use constant dsumxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use constant turn_any_right => 0; # only left or straight
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79  
80 #------------------------------------------------------------------------------
81  
82 sub new {
83 6 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
84 6 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 5 1 6
85 5 $self->{'n_start'} = $self->default_n_start;
86 }
87 6 return $self;
88 }
89  
90 # starting from X axis and n_start=0
91 # d = [ 1, 2, 3, 4, 5 ]
92 # n = [ 0,4,12,24,40 ]
93 # N = (2 d^2 - 2 d)
94 # = (2*$d**2 - 2*$d)
95 # = ((2*$d - 2)*$d)
96 # d = 1/2 + sqrt(1/2 * $n + 1/4)
97 # = (sqrt(2*$n+1) + 1)/2
98 #
99 # X negative axis
100 # d = [ 1, 2, 3, 4,5 ]
101 # n = [ 2, 8, 18, 32, 50 ]
102 # N = (2 d^2)
103  
104 sub n_to_xy {
105 115 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=5 SCAR=16.1
Pod documented
106 #### n_to_xy: $n
107  
108 # adjust to N=0 at origin X=0,Y=0
109 115 $n = $n - $self->{'n_start'};
110 115 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 115 115
111  
112 115 my $d = int( (_sqrtint(2*$n+1) + 1)/2 );
113 115 $n -= 2*$d*$d; # to $n=0 half way around at horiz Y=-1 X<-1
114  
115 115 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) { } 79 36 115
116 79 my $x = -$d-$n-1;
117 79 ▶ if ($n < -$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < -$d) { } 58 21 79
118 # top-right
119 58 return ($x,
120 min($n+2*$d, $d-1));
121 } else {
122 # top-left
123 21 return (max($x, -$d),
124 -1-$n);
125 }
126 } else {
127 36 my $x = $n-$d;
128 36 ▶ if ($n < $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $d) { } 16 20 36
129 # bottom-left
130 16 my $y = -1-$n;
131 16 return ($x,
132 max($y, -$d));
133 } else {
134 # bottom-right
135 20 return (min($x, $d-1),
136 $n-2*$d);
137 }
138 }
139 }
140  
141 sub xy_to_n {
142 52 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=3 SCAR=11.0
Pod documented
143 ### AztecDiamondRings xy_to_n(): "$x, $y"
144  
145 52 $x = round_nearest ($x);
146 52 $y = round_nearest ($y);
147  
148 52 ▶ if ($x >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 0) 31 21 52
149 31 my $d = $x + abs($y);
150 31 return (2*$d + 2)*$d + $y + $self->{'n_start'};
151 }
152 21 ▶ if ($y >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= 0) { } 14 7 21
153 14 my $d = $y - $x;
154 14 return 2*$d*$d - 1 - $y + $self->{'n_start'};
155 } else {
156 7 my $d = $y + $x;
157 7 return (2*$d + 4)*$d + 1 - $y + $self->{'n_start'};
158 }
159 }
160  
161  
162 # | | x2>=-x1 |
163 # M---+ | M-------M | +---M
164 # | | | | | | | | |
165 # +---m | +----m--+ | m---+
166 # | | |
167 # -----+------ -------+------- -----+--------
168 # | | |
169 #
170 # | | |
171 # M---+ | M-------M y2>=-y1 | +---M
172 # | | | | | | | | |
173 # | m | | | | | m |
174 # -------+------ -------m------- -----+--------
175 # | | | | | | | | |
176 # M---+ | M-------M | +---M
177 # | | |
178 #
179 # | | |
180 # -----+------ -------+------- -----+--------
181 # | | |
182 # +---m | +--m----+ | m---+
183 # | | | | | | | | |
184 # M---+ | M-------M | +---M
185 # | | |
186  
187 # exact
188 sub rect_to_n_range {
189 22 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=9 SCAR=22.0
Pod documented
190 ### AztecDiamondRings rect_to_n_range(): "$x1,$y1, $x2,$y2"
191  
192 22 $x1 = round_nearest ($x1);
193 22 $y1 = round_nearest ($y1);
194 22 $x2 = round_nearest ($x2);
195 22 $y2 = round_nearest ($y2);
196  
197 22 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 5 17 22
198 22 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 2 20 22
199  
200 22 my $min_x = 0;
201 22 ▶ my $min_y = ($y2 < 0 ? ($min_x = -1, $y2)
Branch: 2 branches
BranchTrueFalseTotal
$y2 < 0 ? : 5 17 22
$y1 > 0 ? : 3 14 17
202 : $y1 > 0 ? $y1
203 : 0);
204 22 ▶ if ($x2 < $min_x) { $min_x = $x2 } # right edge if 0/-1 not covered
Branch: 2 branches
BranchTrueFalseTotal
if ($x2 < $min_x) { } 4 18 22
elsif ($x1 > $min_x) { } 3 15 18
205 3 elsif ($x1 > $min_x) { $min_x = $x1 } # left edge if 0/-1 not covered
206  
207 22 ▶ my $max_y = ($y2 >= -$y1 ? $y2 : $y1);
Branch: 1 branch
BranchTrueFalseTotal
$y2 >= -$y1 ? : 15 7 22
208 22 ▶ my $max_x = ($x2 >= -$x1-($max_y<0) ? $x2 : $x1);
Branch: 1 branch
BranchTrueFalseTotal
$x2 >= -$x1 - ($max_y < 0) ? : 13 9 22
209  
210 ### min at: "$min_x, $min_y"
211 ### max at: "$max_x, $max_y"
212 22 return ($self->xy_to_n($min_x,$min_y),
213 $self->xy_to_n($max_x,$max_y));
214 }
215  
216 1 1;
217 __END__
« PlanePath/AnvilSpiral.pm ↑ Summary PlanePath/PyramidSpiral.pm »