Math::...Spiral(3pm)

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

๐Ÿ’ซ Math::PlanePath::SacksSpiral โ€” the circular spiral that squares itself every revolution ๐ŸŒ€

okay bestie so this is Math::PlanePath::SacksSpiral, part of the User Contributed Perl Documentation crew, and it is literally the cutest little number spiral you've ever seen, no cap ๐Ÿ’–


โœจ NAME โœจ

Math::PlanePath::SacksSpiral โ€” circular spiral squaring each revolution ๐ŸŒ€๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

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

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

The Sacks spiral, invented by Robert Sacks, is an Archimedean spiral โ€” and if you wanna read more about it, the vibe is over at

<http://www.numberspiral.com>

An Archimedean spiral means the radial distance is a constant factor of the angle, so every single loop sits a constant distance further out than the loop before it. In this case? 1 unit out, periodt. The polar coordinates go like this:

R = sqrt(N)
theta = sqrt(N) * 2pi

and the whole thing comes out roughly like this gorgeous lil layout:

                    18
          19   11        10  17
                     5

    20  12  6   2
                   0  1   4   9  16  25

                   3
      21   13   7        8
                             15   24
                    14
               22        23

The X,Y positions you get back are fractional, except for the perfect squares, which land right on the positive X axis at X=0,1,2,3,etc ๐ŸŽ€. The perfect squares are the closest points, all cozy at 1 unit apart. Everything else? A little further apart, lowkey.

The arms heading off to the right like N=5,10,17,etc or N=8,15,24,etc are constant offsets from the perfect squares, ie. d^2 + c for positive or negative integer c. Going to the left, the central arm N=2,6,12,20,etc is the pronic numbers d^2 + d = d*(d+1), sitting exactly half way between the successive perfect squares. Other arms going left are just offsets from that, ie. d*(d+1) + c for integer c.

Euler's quadratic d^2+d+41 is one such arm going left ๐Ÿ‘‘. Its low values loop around a few times before straightening out at about y=-127. This quadratic has relatively many primes, and if you plot primes on the spiral it literally stands out from its surrounds, main character energy fr fr.

Plotting various quadratic sequences of points can form attractive patterns โœจ. For example the triangular numbers k*(k+1)/2 come out as spiral arcs going clockwise and anti-clockwise. Obsessed.

See examples/sacks-xpm.pl for a complete program plotting the spiral points to an XPM image ๐Ÿ–ผ๏ธ


๐Ÿ’… FUNCTIONS ๐Ÿ’…

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

  • $path = Math::PlanePath::SacksSpiral->new () โ€” creates and returns a brand new path object, slay โœจ
  • ($x,$y) = $path->n_to_xy ($n) โ€” returns the X,Y coordinates of point number $n on the path.

$n can be any value $n >= 0, and fractions give you positions on the spiral in between the integer points ๐Ÿ’ซ

For $n < 0 the return is an empty list โ€” because there are considered to be no negative points in the spiral, so don't even try it bestie ๐Ÿ™ˆ

  • $rsquared = $path->n_to_rsquared ($n) โ€” returns the radial distance R^2 of point $n, or undef if there's no point $n. This is simply $n itself, since R=sqrt(N). Iconic.
  • $n = $path->xy_to_n ($x,$y) โ€” returns an integer point number for coordinates $x,$y. Each integer N is considered the centre of a circle of diameter 1, and an $x,$y within that circle returns N ๐ŸŽฏ

The unit spacing of the spiral means those circles don't overlap โ€” but plot twist, they also don't cover the plane ๐Ÿ˜ญ and if $x,$y isn't within one, the return is undef.

๐ŸŽ€ Descriptive Methods ๐ŸŽ€

  • $dx = $path->dx_minimum()
  • $dx = $path->dx_maximum()
  • $dy = $path->dy_minimum()
  • $dy = $path->dy_maximum()

dX and dY have minimum -pi=-3.14159 and maximum pi=3.14159. The loop beginning at N=2^k is approximately a polygon of 2k+1 many sides and radius R=k. Each side is therefore

side = sin(2pi/(2k+1)) * k
    -> 2pi/(2k+1) * k
    -> pi
  • $str = $path->figure () โ€” returns "circle". That's it, that's the whole function, and honestly? Queen behaviour ๐Ÿซถ

๐ŸŒˆ FORMULAS ๐ŸŒˆ

๐Ÿงฎ Rectangle to N Range ๐Ÿงฎ

R=sqrt(N) here is the same as in the TheodorusSpiral, and the code is shared here. See "Rectangle to N Range" in Math::PlanePath::TheodorusSpiral.

The accuracy could be improved here by taking into account the polar angle of the corners which are candidates for the maximum radius. On the X axis the stripes of N are from X-0.5 to X+0.5, but up on the Y axis it's 0.25 further out at Y-0.25 to Y+0.75. The stripe the corner falls in can thus be biased by theta expressed as a fraction 0 to 1 around the plane.

An exact theta 0 to 1 would require an arctan, but approximations 0, 0.25, 0.5, 0.75 from the quadrants, or eighths of the plane by X>Y etc diagonals. As noted for the Theodorus spiral the over-estimate from ignoring the angle is at worst R many points, which corresponds to a full loop here. Using the angle would reduce that to 1/4 or 1/8 etc of a loop.


๐ŸŒŸ SEE ALSO ๐ŸŒŸ

Math::PlanePath, Math::PlanePath::PyramidRows, Math::PlanePath::ArchimedeanChords, Math::PlanePath::TheodorusSpiral, Math::PlanePath::VogelFloret


๐Ÿ  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/>.

โš ๏ธ okay serious moment bestie: that all-caps warranty disclaimer is not a vibe, it's a legally binding not-a-vibe. Read it, respect it ๐Ÿ’œ

and that's the whole page, babes ๐ŸŽ€ โ€” this was perl v5.32.0, dated 2021-01-23, and it lives in the User Contributed Perl Documentation. stay spiraling, my love ๐ŸŒ€๐Ÿ’–โœจ