1Math::PlanePath::HexSpiUrsaelr(3C)ontributed Perl DocumeMnattaht:i:oPnlanePath::HexSpiral(3)
2
3
4
6 Math::PlanePath::HexSpiral -- integer points around a hexagonal spiral
7
9 use Math::PlanePath::HexSpiral;
10 my $path = Math::PlanePath::HexSpiral->new;
11 my ($x, $y) = $path->n_to_xy (123);
12
14 This path makes a hexagonal spiral, with points spread out horizontally
15 to fit on a square grid.
16
17 28 -- 27 -- 26 -- 25 3
18 / \
19 29 13 -- 12 -- 11 24 2
20 / / \ \
21 30 14 4 --- 3 10 23 1
22 / / / \ \ \
23 31 15 5 1 --- 2 9 22 <- Y=0
24 \ \ \ / /
25 32 16 6 --- 7 --- 8 21 -1
26 \ \ /
27 33 17 -- 18 -- 19 -- 20 -2
28 \
29 34 -- 35 ... -3
30
31 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
32 -6 -5 -4 -3 -2 -1 X=0 1 2 3 4 5 6
33
34 Each horizontal gap is 2, so for instance n=1 is at X=0,Y=0 then n=2 is
35 at X=2,Y=0. The diagonals are just 1 across, so n=3 is at X=1,Y=1.
36 Each alternate row is offset from the one above or below. The result
37 is a triangular lattice per "Triangular Lattice" in Math::PlanePath.
38
39 The octagonal numbers 8,21,40,65, etc 3*k^2-2*k fall on a horizontal
40 straight line at Y=-1. In general straight lines are 3*k^2 + b*k + c.
41 A plain 3*k^2 goes diagonally up to the left, then b is a 1/6 turn
42 anti-clockwise, or clockwise if negative. So b=1 goes horizontally to
43 the left, b=2 diagonally down to the left, b=3 diagonally down to the
44 right, etc.
45
46 Wider
47 An optional "wider" parameter makes the path wider, stretched along the
48 top and bottom horizontals. For example
49
50 $path = Math::PlanePath::HexSpiral->new (wider => 2);
51
52 gives
53
54 ... 36----35 3
55 \
56 21----20----19----18----17 34 2
57 / \ \
58 22 8---- 7---- 6---- 5 16 33 1
59 / / \ \ \
60 23 9 1---- 2---- 3---- 4 15 32 <- Y=0
61 \ \ / /
62 24 10----11----12----13----14 31 -1
63 \ /
64 25----26----27----28---29----30 -2
65
66 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
67 -7 -6 -5 -4 -3 -2 -1 X=0 1 2 3 4 5 6 7
68
69 The centre horizontal from N=1 is extended by "wider" many extra
70 places, then the path loops around that shape. The starting point N=1
71 is shifted to the left by wider many places to keep the spiral centred
72 on the origin X=0,Y=0. Each horizontal gap is still 2.
73
74 Each loop is still 6 longer than the previous, since the widening is
75 basically a constant amount added into each loop.
76
77 N Start
78 The default is to number points starting N=1 as shown above. An
79 optional "n_start" can give a different start with the same shape etc.
80 For example to start at 0,
81
82 n_start => 0
83
84 27 26 25 24 3
85 28 12 11 10 23 2
86 29 13 3 2 9 22 1
87 30 14 4 0 1 8 21 <- Y=0
88 31 15 5 6 7 20 ... -1
89 32 16 17 18 19 38 -2
90 33 34 35 36 37 -3
91 ^
92 -6 -5 -4 -3 -2 -1 X=0 1 2 3 4 5 6
93
94 In this numbering the X axis N=0,1,8,21,etc is the octagonal numbers
95 3*X*(X+1).
96
98 See "FUNCTIONS" in Math::PlanePath for behaviour common to all path
99 classes.
100
101 "$path = Math::PlanePath::HexSpiral->new ()"
102 "$path = Math::PlanePath::HexSpiral->new (wider => $w)"
103 Create and return a new hex spiral object. An optional "wider"
104 parameter widens the path, it defaults to 0 which is no widening.
105
106 "($x,$y) = $path->n_to_xy ($n)"
107 Return the X,Y coordinates of point number $n on the path.
108
109 For "$n < 1" the return is an empty list, it being considered the
110 path starts at 1.
111
112 "$n = $path->xy_to_n ($x,$y)"
113 Return the point number for coordinates "$x,$y". $x and $y are
114 each rounded to the nearest integer, which has the effect of
115 treating each $n in the path as a square of side 1.
116
117 Only every second square in the plane has an N, being those where
118 X,Y both odd or both even. If "$x,$y" is a position without an N,
119 ie. one of X,Y odd the other even, then the return is "undef".
120
122 Entries in Sloane's Online Encyclopedia of Integer Sequences related to
123 this path include
124
125 <http://oeis.org/A056105> (etc)
126
127 A328818 X coordinate
128 A307012 Y coordinate
129 A307011 (X-Y)/2
130 A307013 (X+Y)/2
131
132 A056105 N on X axis
133 A056106 N on X=Y diagonal
134 A056107 N on North-West diagonal
135 A056108 N on negative X axis
136 A056109 N on South-West diagonal
137 A003215 N on South-East diagonal
138
139 A063178 total sum N previous row or diagonal
140 A135711 boundary length of N hexagons
141 A135708 grid sticks of N hexagons
142
143 n_start=0
144 A001399 N positions of turns (extra initial 1)
145 A000567 N on X axis, octagonal numbers
146 A049451 N on X negative axis
147 A049450 N on X=Y diagonal north-east
148 A033428 N on north-west diagonal, 3*k^2
149 A045944 N on south-west diagonal, octagonal numbers second kind
150 A063436 N on WSW slope dX=-3,dY=-1
151 A028896 N on south-east diagonal
152
154 Math::PlanePath, Math::PlanePath::HexSpiralSkewed,
155 Math::PlanePath::HexArms, Math::PlanePath::TriangleSpiral,
156 Math::PlanePath::TriangularHypot
157
159 <http://user42.tuxfamily.org/math-planepath/index.html>
160
162 Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
163 2020 Kevin Ryde
164
165 This file is part of Math-PlanePath.
166
167 Math-PlanePath is free software; you can redistribute it and/or modify
168 it under the terms of the GNU General Public License as published by
169 the Free Software Foundation; either version 3, or (at your option) any
170 later version.
171
172 Math-PlanePath is distributed in the hope that it will be useful, but
173 WITHOUT ANY WARRANTY; without even the implied warranty of
174 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
175 General Public License for more details.
176
177 You should have received a copy of the GNU General Public License along
178 with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
179
180
181
182perl v5.38.0 2023-07-20 Math::PlanePath::HexSpiral(3)