PlanePath/HexArms.pm

filter: Statementstmt 98.3% 119 / 121 Branchbran 90.9% 20 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 23 / 23 Podpod 100% 3 / 3 totaltotal 97.7% 170 / 174 CC 13 SCAR 11.4
CC
13
Cov
97%
xy_to_n
7.0
n_to_xy
6.0
_rect_to_hex_radius
2.0
CRAP 3.1
« PlanePath/HexSpiral.pm ↑ Summary PlanePath/Base/Generic.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 it
6 # under the terms of the GNU General Public License as published by the Free
7 # 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=HexArms --lines --scale=10
20 # math-image --path=HexArms --all --output=numbers_dash
21 # math-image --path=HexArms --values=Polygonal,polygonal=8
22  
23 # Abundant: A005101
24 # octagonal numbers ...
25 # 26-gonal near vertical x2
26 # 152 near horizontal
27 #
28 # 2
29 # 164 +162
30 # 542 +378 +216
31 # 1136 +594 +216
32 #
33  
34 package Math::PlanePath::HexArms;
35 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 #use List::Util 'max';
38 1 *max = \&Math::PlanePath::_max;
39  
40 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 $VERSION = 128;
42 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 @ISA = ('Math::PlanePath');
44 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
45 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
46  
47 use Math::PlanePath::Base::Generic
48 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # uncomment this to run the ### lines
51 #use Devel::Comments '###';
52  
53  
54 1 ▶use constant arms_count => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_even;
56  
57 1 ▶use constant x_negative_at_n => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use constant y_negative_at_n => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use constant dx_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use constant dx_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 *_UNDOCUMENTED__dxdy_list = \&Math::PlanePath::_UNDOCUMENTED__dxdy_list_six;
64 1 ▶use constant absdx_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use constant dsumxy_minimum => -2; # diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use constant dsumxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use constant turn_any_right => 0; # only left or straight
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72  
73 #------------------------------------------------------------------------------
74  
75 # [ 0, 1, 2, 3,],
76 # [ 0, 1, 3, 6 ],
77 # N = (1/2 d^2 + 1/2 d)
78 # d = -1/2 + sqrt(2 * $n + 1/4)
79 # = (-1 + 2*sqrt(2 * $n + 1/4)) / 2
80 # = (-1 + sqrt(8 * $n + 1)) / 2
81  
82 sub n_to_xy {
83 24 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=6 SCAR=17.9
Pod documented
84 #### HexArms n_to_xy: $n
85  
86 24 ▶ if ($n < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 2) 2 22 24
87 2 ▶ if ($n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 2 2
88 ### centre
89 2 $n--;
90 2 return ($n, -$n); # from n=1 towards n=7 at x=1,y=-1
91 }
92 22 $n -= 2;
93  
94 22 my $frac;
95 22 { my $int = int($n);
96 22 $frac = $n - $int;
97 22 $n = $int; # BigFloat int() gives BigInt, use that
98 }
99  
100 # arm as initial rotation
101 22 my $rot = _divrem_mutate($n,6);
102 ### $n
103  
104 22 my $d = int ((-1 + _sqrtint(8 * $n + 1)) / 2);
105 ### d frac: ((-1 + _sqrtint(8 * $n + 1)) / 2)
106 ### $d
107 ### base: $d*($d+1)/2
108  
109 22 $n -= $d*($d+1)/2;
110 ### remainder: $n
111 ### assert: $n <= $d
112  
113 22 $rot += ($d % 6);
114 22 my $x = $frac + 2 + $d + $n;
115 22 my $y = $frac - $d + $n;
116  
117 22 $rot %= 6;
118 22 ▶ if ($rot >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rot >= 3) 11 11 22
119 11 $rot -= 3;
120 11 $x = -$x; # rotate 180
121 11 $y = -$y;
122 }
123 22 ▶ if ($rot == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rot == 0) { } 8 14 22
elsif ($rot == 1) { } 6 8 14
124 8 return ($x,$y);
125 } elsif ($rot == 1) {
126 6 return (($x-3*$y)/2, # rotate +60
127 ($x+$y)/2);
128 } else {
129 8 return (($x+3*$y)/-2, # rotate +120
130 ($x-$y)/2);
131 }
132 }
133  
134 sub xy_to_n {
135 19 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=7 SCAR=19.5
Pod documented
136  
137 19 $x = round_nearest ($x);
138 19 $y = round_nearest ($y);
139 ### HexArms xy_to_n: "x=$x, y=$y"
140 19 ▶ if (($x ^ $y) & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x ^ $y) & 1) 0 19 19
141 0 return undef; # nothing on odd points
142 }
143 19 ▶ if ($x == 0 && $y == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0) 1 18 19
Condition100%3/3
$x == 0 and $y == 0
!ll&&!rl&&r
1621
MC/DC100%2/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table100%3/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
144 1 return 1;
145 }
146  
147 18 my $rot = 0;
148 # eg. y=2 have (0<=>$y)-$y == -1-2 == -3
149 18 ▶ if ($x < (0 <=> $y) - $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < (0 <=> $y) - $y) 9 9 18
150 ### left diagonal half ...
151 9 $rot = 3;
152 9 $x = -$x; # rotate 180
153 9 $y = -$y;
154 }
155 18 ▶ if ($x < $y) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x < $y) { } 6 12 18
elsif ($y > 0) { } 6 6 12
156 ### upper mid sixth, rot 2 ...
157 6 $rot += 2;
158 6 ($x,$y) = ((3*$y-$x)/2, # rotate -120
159 ($x+$y)/-2);
160 } elsif ($y > 0) {
161 ### first sixth, rot 1 ...
162 6 $rot++;
163 6 ($x,$y) = (($x+3*$y)/2, # rotate -60
164 ($y-$x)/2);
165 } else {
166 ### last sixth, rot 0 ...
167 }
168 ### assert: ($x+$y) % 2 == 0
169  
170 # diagonal down from N=2
171 # d=0 n=2
172 # d=6 n=128
173 # d=12 n=470
174 # N = (3 d^2 + 3 d + 2)
175 # = ((3*$d + 3)*$d + 2)
176 # xoffset = 3*($x+$y-2)
177 # N + xoffset = ((3*$d + 3)*$d + 2) + 3*($x+$y-2)
178 # = (3*$d + 3)*$d + 2 + 3*($x+$y) - 6
179 # = (3*$d + 3)*$d + 3*($x+$y) - 4
180 #
181 18 my $d = ($x-$y-2)/2;
182 ### xy: "$x,$y"
183 ### $rot
184 ### x offset: $x+$y-2
185 ### x offset sixes: 3*($x+$y-2)
186 ### quadratic: "d=$d q=".((3*$d + 3)*$d + 2)
187 ### d mod: $d % 6
188 ### rot d mod: (($rot-$d) % 6)
189 18 return ((3*$d + 3)*$d) + 3*($x+$y) - 4 + (($rot-$d) % 6);
190 }
191  
192 # not exact
193 sub rect_to_n_range {
194 24 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=1 SCAR=0.0
Pod documented
195  
196 # d = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
197 # Nmax = [ 7, 19, 37, 61, 91, 127, 169, 217, 271 ]
198 # being the N=7 arm one spot before the corner of each run
199 # N = (3 d^2 + 3 d + 1)
200 # = ((3*$d + 3)*$d + 1)
201 #
202 24 my $d = _rect_to_hex_radius ($x1,$y1, $x2,$y2);
203 24 return (1,
204 ((3*$d + 3)*$d + 1));
205 }
206  
207 # hexagonal distance
208 sub _rect_to_hex_radius {
209 24 ▶ my ($x1,$y1, $x2,$y2) = @_;
Subroutine _rect_to_hex_radius: called CC=2 SCAR=6.9
210  
211 24 $x1 = abs (round_nearest ($x1));
212 24 $y1 = abs (round_nearest ($y1));
213 24 $x2 = abs (round_nearest ($x2));
214 24 $y2 = abs (round_nearest ($y2));
215  
216 # radial symmetric in +/-y
217 24 my $y = max (abs($y1), abs($y2));
218  
219 # radial symmetric in +/-x
220 24 my $x = max (abs($x1), abs($x2));
221  
222 24 ▶ return ($y >= $x
Branch: 1 branch
BranchTrueFalseTotal
$y >= $x ? : 13 11 24
223 ? $y # middle
224 : int(($x + $y + 1)/2)); # end, round up
225 }
226  
227 1 1;
228 __END__
« PlanePath/HexSpiral.pm ↑ Summary PlanePath/Base/Generic.pm »