The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


Math::PlanePath::SquareArms -- four spiral arms


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


This path follows four spiral arms, each advancing successively,

                ...--33--29                 3
     26--22--18--14--10  25                 2
      |               |   |
     30  11-- 7-- 3   6  21                 1
      |   |           |   |
    ...  15   4   1   2  17  ...        <- Y=0
          |   |   |       |   |
         19   8   5-- 9--13  32            -1
          |   |               |
         23  12--16--20--24--28            -2
         27--31--...                       -3

      ^   ^   ^   ^   ^   ^   ^ 
     -3  -2  -1  X=0  1   2   3 ...

Each arm is quadratic, with each loop 128 longer than the preceding. The perfect squares fall in eight straight lines 4, with the even squares on the X and Y axes and the odd squares on the diagonals X=Y and X=-Y.

Some novel straight lines arise from numbers which are a repdigit in one or more bases (Sloane's A167782). "111" in various bases falls on straight lines. Numbers "[16][16][16]" in bases 17,19,21,etc are a horizontal at Y=3 because they're perfect squares, and "[64][64][64]" in base 65,66,etc go a vertically downwards from X=12,Y=-266 similarly because they're squares.

Each arm is N=4*k+rem for a remainder rem=0,1,2,3, so sequences related to multiples of 4 or with a modulo 4 pattern may fall on particular arms.


See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.

$path = Math::PlanePath::SquareArms->new ()

Create and return a new path 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, as the path starts at 1.

Fractional $n gives a point on the line between $n and $n+4, that $n+4 being the next point on the same spiralling arm. This is probably of limited use, but arises fairly naturally from the calculation.

Descriptive Methods

$arms = $path->arms_count()

Return 4.


Rectangle N Range

Within a square X=-d...+d, and Y=-d...+d the biggest N is the end of the N=5 arm in that square, which is N=9, 25, 49, 81, etc, (2d+1)^2, in successive corners of the square. So for a rectangle find a surrounding d square,

    d = max(abs(x1),abs(y1),abs(x2),abs(y2))

from which

    Nmax = (2*d+1)^2
         = (4*d + 4)*d + 1

This can be used for a minimum too by finding the smallest d covered by the rectangle.

    dlo = max (0,
               min(abs(y1),abs(y2)) if x=0 not covered
               min(abs(x1),abs(x2)) if y=0 not covered

from which the maximum of the preceding dlo-1 square,

    Nlo = /  1 if dlo=0
          \  (2*(dlo-1)+1)^2 +1  if dlo!=0
              = (2*dlo - 1)^2
              = (4*dlo - 4)*dlo + 1

For a tighter maximum, horizontally N increases to the left or right of the diagonal X=Y line (or X=Y+/-1 line), which means one end or the other is the maximum. Similar vertically N increases above or below the off-diagonal X=-Y so the top or bottom is the maximum. This means for a rectangle the biggest N is at one of the four corners,

    Nhi = max (xy_to_n (x1,y1),
               xy_to_n (x1,y2),
               xy_to_n (x2,y1),
               xy_to_n (x2,y2))

The current code uses a dlo for Nlo and the corners for Nhi, which means the high is exact but the low is not.


Math::PlanePath, Math::PlanePath::DiamondArms, Math::PlanePath::HexArms, Math::PlanePath::SquareSpiral



Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 <>.