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 ..
20  9 18 ..
21 10  3  8 17 ..
22 11  4  1  2  7 16 ..  <- Y=0
23 12  5  6 15 ..
24 13 14 ..
25 26

^
X=0``````

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 13 to 14) is treated as part of the lower right diagonal.

The triangular number 3,6,10,15,21,etc fall on the horizontal alternately to the left at Y=1 and the right at Y=-1 (one term to the left then one term to the right).

Going diagonally on the sides as done here is like cutting the corners of the SquareSpiral, and that's how it gets around in fewer steps than the SquareSpiral. See the HexSpiralSkewed for similar cutting just two of the four corners.

# FUNCTIONS

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

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

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.

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