The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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.

                            73                               6
                        74  51  72                           5
                    75  52  33  50  71                       4
                76  53  34  19  32  49  70                   3
            77  54  35  20   9  18  31  48  69               2
        78  55  36  21  10   3   8  17  30  47  68           1
    79  56  37  22  11   4   1   2   7  16  29  46  67   <- Y=0
        80  57  38  23  12   5   6  15  28  45  66          -1
            81  58  39  24  13  14  27  44  65  ...         -2
                82  59  40  25  26  43  64  89              -3
                    83  60  41  42  63  88                  -4
                        84  61  62  87                      -5
                            85  86                          -6

                             ^
    -6  -5  -4  -3  -2  -1  X=0  1   2   3   4   5   6

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.

The hexagonal numbers 6,15,28,45,66,etc, k*(2k-1) from k=2 up, are the horizontal line at Y=-1 going to the right. The hexagonal numbers of the "second kind" 3,10,21,36,55,78, etc k*(2k+1), are the horizontal line at Y=1 going to the left. Combining those two is the triangular numbers 3,6,10,15,21,etc, k*(k+1)/2, alternately on one line and the other.

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.

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.

SEE ALSO

Math::PlanePath, Math::PlanePath::DiamondArms, Math::PlanePath::SquareSpiral, Math::PlanePath::HexSpiralSkewed, Math::PlanePath::PyramidSides

HOME PAGE

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

LICENSE

Copyright 2010, 2011 Kevin Ryde

This file is part of Math-PlanePath.

Math-PlanePath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Math-PlanePath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.