Math::...nating(3pm)

πŸ“¦
version 129 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

Math::...nating β€” the CornerAlternating era πŸ’…

okay bestie, before anything else: this page is from the User Contributed Perl Documentation set, and the whole thing is literally called Math::...nating(3pm) in the header, which is a serve, we love an abbreviated title. Anyway. Sit down, grab a strawberry πŸ“, and let's talk about Math::PlanePath::CornerAlternating β€” the path that lays points in layers around a square, out from a corner, in the first quadrant, alternating up and down. It's giving spiral but make it ✨bouncy✨.


✨ NAME ✨

Math::PlanePath::CornerAlternating β€” points shaped around a corner, alternately. πŸŽ€


✨ SYNOPSIS ✨

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

that's it. three lines. iconic behavior honestly. 🫢


✨ DESCRIPTION ✨

so like. this path is points in layers around a square, going outwards from a corner, living in the first quadrant, and it flips between upward and downward each time. every row/column "gnomon" you add onto a square makes a square that's one bigger. no cap.

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

this is like the Corner path, BUT here the gnomons go back and forward, and β€” this is the important part β€” so that points are always a unit step apart. every single neighbor is one step away. slay. πŸ’–

🌸 Wider 🌸

there's an optional wider => $integer that makes the path wider horizontally, so it becomes a rectangle instead of a square. for example:

     4  |  29--30--31--32--33--34--35--36  ...
        |   |                           |   |
     3  |  28--27--26--25--24--23--22  37  44      wider => 3
        |                           |   |   |
     2  |  11--12--13--14--15--16  21  38  43
        |   |                   |   |   |   |
     1  |  10---9---8---7---6  17  20  39  42
        |                   |   |   |   |   |
    Y=0 |   1---2---3---4---5  18--19  40--41
        +--------------------------------------
          X=0   1   2   3   4   5   6   7   8

each gnomon's horizontal part is wider many steps longer. for the wider=3 shown above, the extra points are 2,3,4 in the first row, and then 5..10 are the next gnomon. and every gnomon is still 2 longer than the previous one, because this widening is a constant amount in each. math is mathing fr fr. ✨

🌸 N Start 🌸

default is to number the points starting at N=1, exactly like the pic above. but you can pass an optional n_start to give a different start with the same shape and everything. for example to start at 0:

       4  |  16  17  18  19  20
       3  |  15  14  13  12  21      n_start => 0
       2  |   4   5   6  11  22
       1  |   3   2   7  10  23
     Y=0  |   0   1   8   9  24
           ---------------------
            X=0   1   2   3   4

with Nstart=0, the pronic numbers are on the X=Y leading diagonal. cute!


✨ FUNCTIONS ✨

heads up bestie: go read "FUNCTIONS" in Math::PlanePath for the behaviour that's common to all the path classes. πŸ’–

  • "$path = Math::PlanePath::CornerAlternating->new ()"
  • "$path = Math::PlanePath::CornerAlternating->new (wider => $w, n_start => $n)"

create and return a new path object. that's the whole job. πŸ‘‘

  • "($x,$y) = $path->n_to_xy ($n)"

return the X,Y coordinates of point number $n on the path.

for "$n < n_start()" the return is an empty list. and a fractional $n gives you an X,Y position along a straight line between the integer positions. (yes, between. it's interpolating. she's flexible like that.)

  • "$n = $path->xy_to_n ($x,$y)"

return the point number for coordinates "$x,$y".

$x and $y are each rounded to the nearest integer, which has the effect of treating each point as a square of side 1, so the quadrant x>=-0.5 and y>=-0.5 is entirely covered. fully covered. no gaps. periodt.

  • "($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 the smallest and biggest in the rectangle. no estimating, no approximating, she knows. πŸ’…


✨ FORMULAS ✨

most of the calculations are similar to the Corner path (except without the 0.5 fractional part), plus a reversal that gets applied when the d gnomon number is odd. and when wider>0, that reversal has to allow for the horizontals and verticals being different lengths. ikykyk.

🌸 Rectangle N Range 🌸

for "rect_to_n_range()", the largest gnomon is either the top or the right of the rectangle, depending on where the top right corner x2,y2 falls relative to the leading diagonal:

    |  A---B /    x2<y2         |       /       x2>y2
    |  |   |/     top           |  +------B     right
    |  |   |      row           |  |  /   |     side
    |  |  /|     biggest        |  | /    |     biggest
    |  +---+     gnomon         |  +------C     gnomon
    |   /                       |  /
    +---------                  +-----------

then the maximum is at A or B, or B or C, according to which way that gnomon goes β€” so, odd or even.

if it happens that B is on the diagonal, so x2=y2, then it's either A or C according as the gnomon is odd or even:

    |        /
    |  A----+     x2=y2
    |  |   /|
    |  |  / |
    |  +----C
    |   /
    +-----------

and for wider > 0, the diagonal shifts across, so x2-wider <=> y2 is the relevant test. πŸ¦‹


✨ OEIS ✨

this path lives in Sloane's Online Encyclopedia of Integer Sequences as,

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

wider=0, n_start=1 (the defaults)
  A220603    X+1 coordinate
  A220604    Y+1 coordinate
  A213088    X+Y sum
  A081346    N on X axis
  A081345    N on Y axis
  A002061    N on X=Y diagonal, extra initial 1
  A081344    permutation N by diagonals
  A194280      inverse
  A020703    permutation N at transpose Y,X

  A027709    boundary length of N unit squares
  A078633    grid sticks of N points

n_start=0
  A319290    X coordinate
  A319289    Y coordinate
  A319514    Y,X coordinate pairs
  A329116    X-Y diff
  A053615    abs(X-Y) diff
  A000196    max(X,Y), being floor(sqrt(N))
  A339265    dX-dY increments (runs +1,-1)
  A002378    N on X=Y diagonal, pronic numbers
  A220516    permutation N by diagonals

n_start=2
  A014206    N on X=Y diagonal, pronic+2

wider=1, n_start=1
  A081347    N on X axis
  A081348    N on Y axis
  A080335    N on X=Y diagonal
  A093650    permutation N by diagonals

wider=1, n_start=0
  A180714    X-Y diff

wider=2, n_start=1
  A081350    N on X axis
  A081351    N on Y axis
  A081352    N on X=Y diagonal
  A081349    permutation N by diagonals

look at all those sequences. she's in the encyclopedia, bestie. main character energy for real. πŸ‘‘


✨ SEE ALSO ✨

  • Math::PlanePath
  • Math::PlanePath::Corner
  • Math::PlanePath::DiagonalsAlternating

✨ HOME PAGE ✨

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

✨ LICENSE ✨

ok so this is the part where we all behave. read it, it matters. πŸ“œ

Copyright 2021 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/>.


and that's the whole page, babe πŸŽ€ perl v5.32.0, dated 2021-01-23 β€” that's the version and the date this bad girl was published, so jot that down. go forth and alternate your corners. mwah. πŸ’–βœ¨πŸ¦‹πŸŒΈ