Math::PlanePath::KnightSpiral -- integer points drawn around a square
use Math::PlanePath::KnightSpiral; my $path = Math::PlanePath::KnightSpiral->new; my ($x, $y) = $path->n_to_xy (123);
This path traverses the plane with an infinite "knight's tour" in the form of a square spiral.
... 21 4 9 14 19 2 10 15 20 3 8 28 1 5 22 1 18 13 <- y=0 16 11 24 7 2 27 1 23 6 17 12 25 2 26 ^ -2 -1 x=0 1 2 3
Each step is a chess knight's move 1 across and 2 along, or vice versa. The pattern makes 4 cycles on a 2-wide path around a square before stepping outwards to do the same again to a then bigger square. The above sample shows the first 4-cycle around the central 1 then stepping out at 26 and beginning to go around the outside of the now 5x5 square.
A traced out picture of the path can be seen at the following page (quarter way down under "Open Knight's Tour"),
http://www.borderschess.org/KTart.htm
The math-image program can draw the path lines too. And see examples/knights-sloane.pl expressing the knight's tour by the numbering of the SquareSpiral (sequence A068608 of Sloane's On-Line Encyclopedia of Integer Sequences).
math-image
$path = Math::PlanePath::KnightSpiral->new ()
Create and return a new square spiral object.
($x,$y) = $path->n_to_xy ($n)
Return the x,y coordinates of point number $n on the path.
$n
For $n < 1 the return is an empty list, it being considered the path starts at 1.
$n < 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.
$x,$y
$x
$y
Math::PlanePath, Math::PlanePath::SquareSpiral
http://user42.tuxfamily.org/math-planepath/index.html
Math-PlanePath is Copyright 2010 Kevin Ryde
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/>.
To install Math::PlanePath, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::PlanePath
CPAN shell
perl -MCPAN -e shell install Math::PlanePath
For more information on module installation, please visit the detailed CPAN module installation guide.