Math::...Spiral(3pm)

๐Ÿ“ฆ
version 129 ยท 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

๐Ÿ’Ž NAME ๐Ÿ’Ž

ok so bestie, this page comes to you from the User Contributed Perl Documentation collection, and the man header up top just says Math::...Spiral(3pm) (yes it's abbreviating, we love a mysterious queen ๐Ÿ’…). The actual module is Math::PlanePath::DiamondSpiral โ€” and what it does is lay out integer points around a diamond shaped spiral. That's it, that's the whole gig. Iconic.


๐ŸŒ€ SYNOPSIS ๐ŸŒ€

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

๐ŸŒธ DESCRIPTION ๐ŸŒธ

So this path literally makes a diamond shaped spiral, look at her:

                       19                    3
                     /    \
                   20   9  18                2
                 /    /   \   \
               21  10   3   8  17            1
             /    /   /   \  \   \
           22  11   4   1---2   7  16    <- Y=0
             \    \   \       /   /
               23  12   5---6  15  ...      -1
                 \   \        /   /
                   24  13--14  27           -2
                     \        /
                       25--26               -3

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

ngl, this is not just the SquareSpiral rotated. She spirals around faster โ€” the side lengths follow the pattern 1,1,1,1, 2,2,2,2, 3,3,3,3, etc, if you treat that flat kink at the bottom (like N=13 to N=14) as part of the lower right diagonal.

Going diagonally on the sides like this is basically the same energy as cutting the corners of the SquareSpiral, which is exactly how she gets around in fewer steps than the SquareSpiral. If you want similar corner-cutting but only 3, 2 or 1 corners, go look at PentSpiralSkewed, HexSpiralSkewed and HeptSpiralSkewed.

N=1,5,13,25,etc on the Y negative axis is giving the "centred square numbers", 2*k*(k+1)+1. math girlies stay winning โœจ

๐Ÿ”ข N Start

The default is to number points starting N=1, just like the picture above. An optional n_start can give a different start, with the same shape etc. Like, say you wanna start at 0:

   n_start => 0         18
                      /   \
                    19   8  17
                  /   /   \   \
                20   9   2   7  16
              /   /   /   \   \   \
            21  10   3   0-- 1   6  15
              \   \   \       /   /
                22  11   4-- 5  14  ...
                  \   \       /   /
                    23  12--13  26
                      \       /
                        24--25
  • N=0,1,6,15,28,etc on the X axis are the hexagonal numbers k*(2k-1).
  • N=0,3,10,21,36,etc on the negative X axis are the hexagonal numbers of the "second kind", k*(2k-1) for k<0.
  • Combine those two and you get the triangular numbers 0,1,3,6,10,15,21,etc, k*(k+1)/2, sitting on the X axis alternately positive and negative. cute!
  • N=0,2,8,18,etc on the Y axis is 2*squares, 2*Y^2.
  • N=0,4,12,24,etc on the negative Y axis is 2*pronic, 2*Y*(Y+1).

๐Ÿ› ๏ธ FUNCTIONS ๐Ÿ› ๏ธ

For the behaviour common to all path classes, go read "FUNCTIONS" in Math::PlanePath. Here's what's specific to this one:

  • $path = Math::PlanePath::DiamondSpiral->new ()
  • $path = Math::PlanePath::DiamondSpiral->new (n_start => $n) โ€” creates and returns a brand new diamond spiral object. your very own ๐Ÿ’–
  • ($x,$y) = $path->n_to_xy ($n) โ€” returns the X,Y coordinates of point number $n on the path.

Heads up: for $n < 1 the return is an empty list, because the path is considered to start at 1. no exceptions bestie.

  • $n = $path->xy_to_n ($x,$y) โ€” returns the point number for coordinates $x,$y. Both $x and $y get rounded to the nearest integer, which has the effect of treating each point in the path as a square of side 1, so the entire plane is covered. she's everywhere fr.
  • ($n_lo, $n_hi) = $path->rect_to_n_range ($x1,$y1, $x2,$y2) โ€” the returned range is exact, meaning $n_lo and $n_hi are literally the smallest and biggest in the rectangle. no approximating here, she's precise ๐Ÿ’…

๐Ÿงฎ FORMULAS ๐Ÿงฎ

๐Ÿ“ Rectangle to N Range

Within each row, N increases as X moves away from the Y axis. Within each column, same deal โ€” N increases as Y moves away from the X axis. So inside a rectangle, the maximum N is chillin' at one of the four corners:

             |
   x1,y2 M---|----M x2,y2
         |   |    |
      -------O---------
         |   |    |
         |   |    |
   x1,y1 M---|----M x1,y1
             |

For any two columns x1 and x2 with x1<x2, the values in column x2 are all bigger if x2>-x1. And yes, this holds even when x1 and x2 are on the same side of the origin, ie. both positive or both negative.

For any two rows y1 and y2, the values in the part of the row with X>0 are bigger if y2>=-y1, and in the part of the row with X<=0 it's y2>-y1, or equivalently y2>=-y1+1. So the biggest corner is at

   max_x = (x2 > -x1             ? x2 : x1)
   max_y = (y2 >= -y1+(max_x<=0) ? y2 : y1)

The minimum is similar but a little simpler. In any column the minimum is at Y=0, and in any row the minimum is at X=0. So it's at 0,0 if that's inside the rectangle, or else it's on the edge on the side nearest the origin when it's not.

   min_x = / if x2 < 0 then x2
           | if x1 > 0 then x1
           \ else           0

   min_y = / if y2 < 0 then y2
           | if y1 > 0 then y1
           \ else           0

๐Ÿ” OEIS ๐Ÿ”

Entries in Sloane's Online Encyclopedia of Integer Sequences related to this path include

   <http://oeis.org/A010751> (etc)

   n_start=1
     A130883    N on X axis, 2*n^2-n+1
     A058331    N on Y axis, 2*n^2 + 1
     A001105    N on column X=1, 2*n^2
     A084849    N on X negative axis, 2*n^2+n+1
     A001844    N on Y negative axis, centred squares 2*n^2+2n+1
     A215471    N with >=5 primes among its 8 neighbours
     A215468    sum 8 neighbours N

     A217015    N permutation points order SquareSpiral rotate -90,
                  value DiamondSpiral N at each
     A217296    inverse permutation

   n_start=0
     A010751    X coordinate, runs 1 inc, 2 dec, 3 inc, etc
     A305258    Y coordinate
     A053616    abs(Y), runs k to 0 to k
     A000384    N on X axis, hexagonal numbers
     A001105    N on Y axis, 2*n^2 (and cf similar A184636)
     A014105    N on X negative axis, second hexagonals
     A046092    N on Y negative axis, 2*pronic
     A003982    delta(abs(X)+abs(Y)), 1 when N on Y negative axis
                  which is where move "outward" to next ring

   n_start=-1
     A188551    N positions of turns, from N=1 up
     A188552      and which are primes

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

  • Math::PlanePath
  • Math::PlanePath::DiamondArms
  • Math::PlanePath::AztecDiamondRings
  • Math::PlanePath::SquareSpiral
  • Math::PlanePath::HexSpiralSkewed
  • Math::PlanePath::PyramidSides
  • Math::PlanePath::ToothpickSpiral

๐Ÿ  HOME PAGE ๐Ÿ 

<http://user42.tuxfamily.org/math-planepath/index.html>


๐Ÿ“œ LICENSE ๐Ÿ“œ

Copyright 2010, 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 <http://www.gnu.org/licenses/>.

๐Ÿ’€ not the all-caps warranty disclaimer. she said NO WARRANTY and she MEANT it. respect the license bestie, it's giving legally binding ๐Ÿซถ

and that's the whole page!! this one's from perl v5.32.0, dated 2021-01-23 โ€” so if you're reading this, hi from the past, the diamond spiral still slays ๐Ÿ’–โœจ๐ŸŒˆ