1Math::PlanePath::DiamonUdsSeprirCaoln(t3r)ibuted Perl DoMcautmhe:n:tPaltainoenPath::DiamondSpiral(3)
2
3
4
6 Math::PlanePath::DiamondSpiral -- integer points around a diamond
7 shaped spiral
8
10 use Math::PlanePath::DiamondSpiral;
11 my $path = Math::PlanePath::DiamondSpiral->new;
12 my ($x, $y) = $path->n_to_xy (123);
13
15 This path makes a diamond shaped spiral.
16
17 19 3
18 / \
19 20 9 18 2
20 / / \ \
21 21 10 3 8 17 1
22 / / / \ \ \
23 22 11 4 1---2 7 16 <- Y=0
24 \ \ \ / /
25 23 12 5---6 15 ... -1
26 \ \ / /
27 24 13--14 27 -2
28 \ /
29 25--26 -3
30
31 ^
32 -3 -2 -1 X=0 1 2 3
33
34 This is not simply the "SquareSpiral" rotated, it spirals around
35 faster, with side lengths following a pattern 1,1,1,1, 2,2,2,2,
36 3,3,3,3, etc, if the flat kink at the bottom (like N=13 to N=14) is
37 treated as part of the lower right diagonal.
38
39 Going diagonally on the sides as done here is like cutting the corners
40 of the "SquareSpiral", which is how it gets around in fewer steps than
41 the "SquareSpiral". See "PentSpiralSkewed", "HexSpiralSkewed" and
42 "HeptSpiralSkewed" for similar cutting just 3, 2 or 1 of the corners.
43
44 N=1,5,13,25,etc on the Y negative axis is the "centred square numbers"
45 2*k*(k+1)+1.
46
47 N Start
48 The default is to number points starting N=1 as shown above. An
49 optional "n_start" can give a different start, with the same shape etc.
50 For example to start at 0,
51
52 n_start => 0 18
53 / \
54 19 8 17
55 / / \ \
56 20 9 2 7 16
57 / / / \ \ \
58 21 10 3 0-- 1 6 15
59 \ \ \ / /
60 22 11 4-- 5 14 ...
61 \ \ / /
62 23 12--13 26
63 \ /
64 24--25
65
66 N=0,1,6,15,28,etc on the X axis is the hexagonal numbers k*(2k-1).
67 N=0,3,10,21,36,etc on the negative X axis is the hexagonal numbers of
68 the "second kind" k*(2k-1) for k<0. Combining those two is the
69 triangular numbers 0,1,3,6,10,15,21,etc, k*(k+1)/2, on the X axis
70 alternately positive and negative.
71
72 N=0,2,8,18,etc on the Y axis is 2*squares, 2*Y^2. N=0,4,12,24,etc on
73 the negative Y axis is 2*pronic, 2*Y*(Y+1).
74
76 See "FUNCTIONS" in Math::PlanePath for behaviour common to all path
77 classes.
78
79 "$path = Math::PlanePath::DiamondSpiral->new ()"
80 "$path = Math::PlanePath::DiamondSpiral->new (n_start => $n)"
81 Create and return a new diamond spiral object.
82
83 "($x,$y) = $path->n_to_xy ($n)"
84 Return the X,Y coordinates of point number $n on the path.
85
86 For "$n < 1" the return is an empty list, it being considered the
87 path starts at 1.
88
89 "$n = $path->xy_to_n ($x,$y)"
90 Return the point number for coordinates "$x,$y". $x and $y are
91 each rounded to the nearest integer, which has the effect of
92 treating each point in the path as a square of side 1, so the
93 entire plane is covered.
94
95 "($n_lo, $n_hi) = $path->rect_to_n_range ($x1,$y1, $x2,$y2)"
96 The returned range is exact, meaning $n_lo and $n_hi are the
97 smallest and biggest in the rectangle.
98
100 Rectangle to N Range
101 Within each row N increases as X moves away from the Y axis, and within
102 each column similarly N increases as Y moves away from the X axis. So
103 in a rectangle the maximum N is at one of the four corners.
104
105 |
106 x1,y2 M---|----M x2,y2
107 | | |
108 -------O---------
109 | | |
110 | | |
111 x1,y1 M---|----M x1,y1
112 |
113
114 For any two columns x1 and x2 with x1<x2, the values in column x2 are
115 all bigger if x2>-x1. This is so even when x1 and x2 are on the same
116 side of the origin, ie. both positive or both negative.
117
118 For any two rows y1 and y2, the values in the part of the row with X>0
119 are bigger if y2>=-y1, and in the part of the row with X<=0 it's
120 y2>-y1, or equivalently y2>=-y1+1. So the biggest corner is at
121
122 max_x = (x2 > -x1 ? x2 : x1)
123 max_y = (y2 >= -y1+(max_x<=0) ? y2 : y1)
124
125 The minimum is similar but a little simpler. In any column the minimum
126 is at Y=0, and in any row the minimum is at X=0. So at 0,0 if that's
127 in the rectangle, or the edge on the side nearest the origin when not.
128
129 min_x = / if x2 < 0 then x2
130 | if x1 > 0 then x1
131 \ else 0
132
133 min_y = / if y2 < 0 then y2
134 | if y1 > 0 then y1
135 \ else 0
136
138 Entries in Sloane's Online Encyclopedia of Integer Sequences related to
139 this path include
140
141 <http://oeis.org/A010751> (etc)
142
143 n_start=1
144 A130883 N on X axis, 2*n^2-n+1
145 A058331 N on Y axis, 2*n^2 + 1
146 A001105 N on column X=1, 2*n^2
147 A084849 N on X negative axis, 2*n^2+n+1
148 A001844 N on Y negative axis, centred squares 2*n^2+2n+1
149 A215471 N with >=5 primes among its 8 neighbours
150 A215468 sum 8 neighbours N
151
152 A217015 N permutation points order SquareSpiral rotate -90,
153 value DiamondSpiral N at each
154 A217296 inverse permutation
155
156 n_start=0
157 A010751 X coordinate, runs 1 inc, 2 dec, 3 inc, etc
158 A305258 Y coordinate
159 A053616 abs(Y), runs k to 0 to k
160 A000384 N on X axis, hexagonal numbers
161 A001105 N on Y axis, 2*n^2 (and cf similar A184636)
162 A014105 N on X negative axis, second hexagonals
163 A046092 N on Y negative axis, 2*pronic
164 A003982 delta(abs(X)+abs(Y)), 1 when N on Y negative axis
165 which is where move "outward" to next ring
166
167 n_start=-1
168 A188551 N positions of turns, from N=1 up
169 A188552 and which are primes
170
172 Math::PlanePath, Math::PlanePath::DiamondArms,
173 Math::PlanePath::AztecDiamondRings, Math::PlanePath::SquareSpiral,
174 Math::PlanePath::HexSpiralSkewed, Math::PlanePath::PyramidSides,
175 Math::PlanePath::ToothpickSpiral
176
178 <http://user42.tuxfamily.org/math-planepath/index.html>
179
181 Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
182 2020 Kevin Ryde
183
184 This file is part of Math-PlanePath.
185
186 Math-PlanePath is free software; you can redistribute it and/or modify
187 it under the terms of the GNU General Public License as published by
188 the Free Software Foundation; either version 3, or (at your option) any
189 later version.
190
191 Math-PlanePath is distributed in the hope that it will be useful, but
192 WITHOUT ANY WARRANTY; without even the implied warranty of
193 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
194 General Public License for more details.
195
196 You should have received a copy of the GNU General Public License along
197 with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
198
199
200
201perl v5.36.0 2022-07-22 Math::PlanePath::DiamondSpiral(3)