and 1 contributors

NAME

Math::PlanePath::DiamondSpiral -- integer points around a diamond shaped spiral

SYNOPSIS

use Math::PlanePath::DiamondSpiral;
my \$path = Math::PlanePath::DiamondSpiral->new;
my (\$x, \$y) = \$path->n_to_xy (123);

DESCRIPTION

This path makes a diamond shaped spiral.

19                    3
/    \
20   9  18                2
/    /   \   \
21  10   3   8  17            1
/    /   /   \  \   \
22  11   4   1---2   7  16    <- Y=0
\    \   \       /   /
23  12   5---6  15  ...      -1
\   \        /   /
24  13--14  27           -2
\        /
25--26               -3

^
-3  -2  -1  X=0  1   2   3

This is not simply the SquareSpiral rotated, it spirals around faster, with side lengths following a pattern 1,1,1,1, 2,2,2,2, 3,3,3,3, etc, if the flat kink at the bottom (like N=13 to N=14) is treated as part of the lower right diagonal.

Going diagonally on the sides as done here is like cutting the corners of the SquareSpiral, which is how it gets around in fewer steps than the SquareSpiral. See PentSpiralSkewed, HexSpiralSkewed and HeptSpiralSkewed for similar cutting just 3, 2 or 1 of the corners.

N=1,5,13,25,etc on the Y negative axis is the "centred square numbers" 2*k*(k+1)+1.

N Start

The default is to number points starting N=1 as shown above. An optional n_start can give a different start, with the same shape etc. For example to start at 0,

n_start => 0         18
/   \
19   8  17
/   /   \   \
20   9   2   7  16
/   /   /   \   \   \
21  10   3   0-- 1   6  15
\   \   \       /   /
22  11   4-- 5  14  ...
\   \       /   /
23  12--13  26
\       /
24--25

N=0,1,6,15,28,etc on the X axis is the hexagonal numbers k*(2k-1). N=0,3,10,21,36,etc on the negative X axis is the hexagonal numbers of the "second kind" k*(2k-1) for k<0. Combining those two is the triangular numbers 0,1,3,6,10,15,21,etc, k*(k+1)/2, on the X axis alternately positive and negative.

N=0,2,8,18,etc on the Y axis is 2*squares, 2*Y^2. N=0,4,12,24,etc on the negative Y axis is 2*pronic, 2*Y*(Y+1).

FUNCTIONS

See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.

\$path = Math::PlanePath::DiamondSpiral->new ()
\$path = Math::PlanePath::DiamondSpiral->new (n_start => \$n)

Create and return a new diamond spiral object.

(\$x,\$y) = \$path->n_to_xy (\$n)

Return the X,Y coordinates of point number \$n on the path.

For \$n < 1 the return is an empty list, it being considered the path starts at 1.

\$n = \$path->xy_to_n (\$x,\$y)

Return the point number for coordinates \$x,\$y. \$x and \$y are each rounded to the nearest integer, which has the effect of treating each point in the path as a square of side 1, so the entire plane is covered.

(\$n_lo, \$n_hi) = \$path->rect_to_n_range (\$x1,\$y1, \$x2,\$y2)

The returned range is exact, meaning \$n_lo and \$n_hi are the smallest and biggest in the rectangle.

FORMULAS

Rectangle to N Range

Within each row N increases as X moves away from the Y axis, and within each column similarly N increases as Y moves away from the X axis. So in a rectangle the maximum N is at one of the four corners.

|
x1,y2 M---|----M x2,y2
|   |    |
-------O---------
|   |    |
|   |    |
x1,y1 M---|----M x1,y1
|

For any two columns x1 and x2 with x1<x2, the values in column x2 are all bigger if x2>-x1. This is so even when x1 and x2 are on the same side of the origin, ie. both positive or both negative.

For any two rows y1 and y2, the values in the part of the row with X>0 are bigger if y2>=-y1, and in the part of the row with X<=0 it's y2>-y1, or equivalently y2>=-y1+1. So the biggest corner is at

max_x = (x2 > -x1             ? x2 : x1)
max_y = (y2 >= -y1+(max_x<=0) ? y2 : y1)

The minimum is similar but a little simpler. In any column the minimum is at Y=0, and in any row the minimum is at X=0. So at 0,0 if that's in the rectangle, or the edge on the side nearest the origin when not.

min_x = / if x2 < 0 then x2
| if x1 > 0 then x1
\ else           0

min_y = / if y2 < 0 then y2
| if y1 > 0 then y1
\ else           0

OEIS

Entries in Sloane's Online Encyclopedia of Integer Sequences related to this path include

n_start=1
A130883    N on X axis, 2*n^2-n+1
A058331    N on Y axis, 2*n^2 + 1
A001105    N on column X=1, 2*n^2
A084849    N on X negative axis, 2*n^2+n+1
A001844    N on Y negative axis, centred squares 2*n^2+2n+1
A215471    N with >=5 primes among its 8 neighbours
A215468    sum 8 neighbours N

A217015    N permutation points order SquareSpiral rotate -90,
value DiamondSpiral N at each
A217296    inverse permutation

n_start=0
A010751    X coordinate, runs 1 inc, 2 dec, 3 inc, etc
A053616    abs(Y), runs k to 0 to k
A000384    N on X axis, hexagonal numbers
A001105    N on Y axis, 2*n^2 (and cf similar A184636)
A014105    N on X negative axis, second hexagonals
A046092    N on Y negative axis, 2*pronic
A003982    delta(abs(X)+abs(Y)), 1 when N on Y negative axis
which is where move "outward" to next ring

n_start=-1
A188551    N positions of turns, from N=1 up
A188552      and which are primes

http://user42.tuxfamily.org/math-planepath/index.html