John Gamble


Array::Tour::Spiral - Return coordinates to take a spiral path.


  use Array::Tour::Spiral qw(:directions);

  my $spiral = Array::Tour::Spiral->new(
      dimensions => [5, 5],
      counterclock => $counterclock,
      corner_right => $corner_right,
      corner_bottom => $corner_bottom
      inward => $inward);

Creates the object with its attributes. The attributes are:


Set the size of the grid:

        my $spath1 = Array::Tour::Spiral->new(dimensions => [16, 16]);

If the grid is going to be square, a single integer is sufficient:

        my $spath1 = Array::Tour::Spiral->new(dimensions => 16);
counterclock corner_bottom corner_right inward

Default values: 0. All are boolean values that affect the starting point and the direction of the spiral path. By default, the spiral is generated outwards from the center, using the upper left corner (if there is a choice), in a clockwise direction. See the Examples section to see what effects the different combinations produce.


Perl 5.8 or later. This is the version of perl under which this module was developed.


A simple iterator that will return the coordinates of the next cell if one were to tour a matrice's cells in a spiral path.

Spiral Object Methods


$dir = $tour->direction()

Return the direction we just walked.

Overrides Array::Tour's direction() method.


Returns an array reference to the next coordinates to use. Returns undef if there is no next cell to visit.

    my $ctr = 1;
    my $tour = Array::Tour::Spiral->new(dimensions => 64);

    while (my $cref = $tour->next())
        my($x_coord, $y_coord, $z_coord) = @{$cref};
        $grid[$y_coord, $x_coord] = isprime($ctr++);

The above example generates Ulam's Spiral in the array @grid.

Overrides Array::Tour's next() method.


  $larips = $spiral->anti_spiral();

Return a new object that follows the same path as the original object, reversing the inward/outward direction.



  Override Array::Tour's _set() method for one that can handle
  our parameters.



  Set the attributes knowing that the spiral path goes inward.



  Set the attributes knowing that the spiral path goes outward.

Example: A Spiral Tour of the Square

The four by four case demonstrates the different possible spiral arrangements. There are four possible central positions. By default, the spiral will begin in the top left corner, but the options corner_bottom and corner_right can force the starting point to a different corner of the square.

The results below show the results of the four different combinations of ($corner_bottom, $corner_right), traveling clockwise. The characters 'a' .. 'p' were drawn in order to show the path of the spiral:

      (0,0)     (0,1)     (1,1)     (1,0)
      ghij      pefg      mnop      jklm
      fabk      odah      lcde      ibcn
      edcl      ncbi      kbaf      hado
      ponm      mlkj      jihg      gfep

What if the grid is five by five? With both dimensions odd, there is no left/right or top/bottom corner. There are still four possible paths to take though, as shown using the characters 'a' .. 'y':

       (0,0)      (0,1)      (1,1)      (1,0)
       uvwxy      qrstu      mnopq      yjklm
       tghij      pefgv      lcder      xibcn
       sfabk      odahw      kbafs      whado
       redcl      ncbix      jihgt      vgfep
       qponm      mlkjy      yxwvu      utsrq

Even though there is only one center square, the spiral path takes the same starting direction as the spiral on the four by four square does.

Example: The Spiral Tour of the Rectangle

Some of our assumptions go awry if width does not equal height. If the shorter of the two dimensions is even, the starting corner does not always go where one expects. Here are some examples.


John M. Gamble may be found at <>