NAME
Math::PlanePath::SquareSpiral  integer points drawn around a square (or rectangle)
SYNOPSIS
use Math::PlanePath::SquareSpiral;
my $path = Math::PlanePath::SquareSpiral>new;
my ($x, $y) = $path>n_to_xy (123);
DESCRIPTION
This path makes a square spiral,
37363534333231 3
 
38 1716151413 30 2
   
39 18 543 12 29 1
     
40 19 6 12 11 28 ... < y=0
     
41 20 78910 27 52 1
   
42 212223242526 51 2
 
4344454647484950
^
3 2 1 x=0 1 2 3
See examples/squarenumbers.pl in the sources for a simple program printing these numbers.
This path is well known from Stanislaw Ulam finding interesting straight lines when plotting the prime numbers on it. See examples/ulamspiralxpm.pl in the sources for a program generating that, or see mathimage using this SquareSpiral to draw Ulam's pattern and more.
Straight Lines
The perfect squares 1,4,9,16,25 fall on diagonals with the even perfect squares going to the upper left and the odd ones to the lower right. The pronic numbers 2,6,12,20,30,42 etc k^2+k half way between the squares fall on similar diagonals to the upper right and lower left. The decagonal numbers 10,27,52,85 etc 4*k^23*k go horizontally to the right at y=1.
In general straight lines and diagonals are 4*k^2 + b*k + c. b=0 is the even perfect squares up to the left, then b is an eighth turn counterclockwise, or clockwise if negative. So b=1 is horizontally to the left, b=2 diagonally down to the left, b=3 down vertically, etc.
Honaker's primegenerating polynomial 4*k^2 + 4*k + 59 goes down to the right, after the first 30 or so values loop around a bit.
Wider
An optional wider
parameter makes the path wider, becoming a rectangle spiral instead of a square. For example
$path = Math::PlanePath::SquareSpiral>new (wider => 3);
gives
2928272625242322 2
 
30 1110 9 8 7 6 21 1
   
31 12 1 2 3 4 5 20 < y=0
  
32 13141516171819 1

33343536... 2
^
4 3 2 1 x=0 1 2 3
The centre horizontal 1 to 2 is extended by wider
many further places, then the path loops around that shape. The starting point 1 is shifted to the left by wider/2 places (rounded up to an integer) to keep the spiral centred on the origin x=0,y=0.
Widening doesn't change the nature of the straight lines which arise, it just rotates them around. For example in this wider=3 example the perfect squares are still on diagonals, but the even squares go towards the bottom left (instead of top left when wider=0) and the odd squares to the top right (instead of the bottom right).
Each loop is still 8 longer than the previous, as the widening is basically a constant amount in each loop.
Corners
Other spirals can be formed by cutting the corners of the square so as to go around faster. See the following modules,
Corners Cut Class
 
1 HeptSpiralSkewed
2 HexSpiralSkewed
3 PentSpiralSkewed
4 DiamondSpiral
The PyramidSpiral is a reshaped SquareSpiral looping at the same rate.
FUNCTIONS
See "FUNCTIONS" in Math::PlanePath for the behaviour common to all path classes.
$path = Math::PlanePath::SquareSpiral>new ()
$path = Math::PlanePath::SquareSpiral>new (wider => $w)

Create and return a new square spiral object. An optional
wider
parameter widens the spiral path, it defaults to 0 which is no widening. ($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, as 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 N in the path as centred in a square of side 1, so the entire plane is covered.
SEE ALSO
Math::PlanePath, Math::PlanePath::PyramidSpiral
Math::PlanePath::DiamondSpiral, Math::PlanePath::PentSpiralSkewed, Math::PlanePath::HexSpiralSkewed, Math::PlanePath::HeptSpiralSkewed
X11 cursor font "box spiral" cursor which is this style (but going clockwise).
HOME PAGE
http://user42.tuxfamily.org/mathplanepath/index.html
LICENSE
Copyright 2010, 2011 Kevin Ryde
This file is part of MathPlanePath.
MathPlanePath 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.
MathPlanePath 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 MathPlanePath. If not, see <http://www.gnu.org/licenses/>.