++ed by:

2 PAUSE users

Kevin Ryde
and 1 contributors


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


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


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 


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.


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.


Math::PlanePath, Math::PlanePath::DiamonArms, Math::PlanePath::SquareSpiral, Math::PlanePath::HexSpiralSkewed, Math::PlanePath::PyramidSides




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/>.