Math::PlanePath::KnightSpiral3pm
Usage
use Math::PlanePath::KnightSpiral;my $path = Math::PlanePath::KnightSpiral->new;my ($x, $y) = $path->n_to_xy (123);
Overview
This path traverses the plane by an infinite "knight's tour" in the form of a square spiral.
...21 4 9 14 19 210 15 20 3 8 28 15 22 1 18 13 <- Y=016 11 24 7 2 27 123 6 17 12 25 226^-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 now 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 5x5 square.
An attractive traced out picture of the path appeared in the past at "www.borderschess.org",
<http://web.archive.org/web/1id_/http://www.borderschess.org/Infinite.gif>
<http://web.archive.org/web/1id_/http://www.borderschess.org/KTinfinity.gif>
<http://web.archive.org/web/20161028114643id_/http://www.borderschess.org/KTart.htm>
(HTML colours might might make the text invisible. Try deleting, or browser option to ignore page colours, or a text browser.)
See math-image to draw the path lines too.
Functions
See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.
- "$path = Math::PlanePath::KnightSpiral->new ()"
-
Create and return a new knight spiral object.
- "($x,$y) = $path->n_to_xy ($n)"
-
Return the X,Y coordinates of point number
$non 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".$xand$yare 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.
Oeis
This Knight's tour is in Sloane's OEIS following the Knight spiral and giving the resulting X,Y location by the "SquareSpiral" numbering. There's eight forms for 4 rotations and the two spirals same or opposite directions.
<http://oeis.org/A068608> (etc)
permutationsA068608 same knight and square spiral directionsA068609 rotate 90 degreesA068610 rotate 180 degreesA068611 rotate 270 degreesA068612 rotate 180 degrees, spiral opp dir (X negate)A068613 rotate 270 degrees, spiral opp dirA068614 spiral opposite direction (Y negate)A068615 rotate 90 degrees, spiral opp dir (X,Y transpose)
See examples/knights-oeis.pl for a sample program printing the values of A068608.
Home page
<http://user42.tuxfamily.org/math-planepath/index.html>
Releases
6 releases since Debian 8 (Jessie); the text changed 3 times.