Math::...owhead(3pm)

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

โœจ NAME โœจ

okay bestie, this manual is for Math::PlanePath::SierpinskiArrowhead (3pm), a User Contributed Perl Documentation page. And the name is literally the whole vibe: it's a self-similar triangular path traversal. Periodt. ๐Ÿ’…

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

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

๐ŸŒธ DESCRIPTION ๐ŸŒธ

This path is an integer version of Sierpinski's curve from

Waclaw Sierpinski, "Sur une Courbe Dont Tout Point est un Point de Ramification", Comptes Rendus Hebdomadaires des Sรฉances de l'Acadรฉmie des Sciences, volume 160, January-June 1915, pages 302-305. <http://gallica.bnf.fr/ark:/12148/bpt6k31131/f302.image.langEN>

The path is self-similar triangular parts leaving middle triangle gaps giving the Sierpinski triangle shape. It's giving fractal realness. โœจ

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

            -8 -7 -6 -5 -4 -3 -2 -1 X=0 1  2  3  4  5  6  7  8

The base figure is the N=0 to N=3 shape. It's repeated up in mirror image as N=3 to N=6 then across as N=6 to N=9. At the next level the same is done with the N=0 to N=9 shape, up as N=9 to N=18 and across as N=18 to N=27, etc. Self-similarity ate. ๐Ÿ’–

The X,Y coordinates are on a triangular lattice done in integers by using every second X, per "Triangular Lattice" in Math::PlanePath.

The base pattern is a triangle like

           3---------2 - - - - .
            \         \
                C  /   \  B  /
              \      D  \
                 /       \ /
                . - - - - 1
                 \       /
                     A  /
                   \   /
                      /
                     0

Higher levels go into the triangles A,B,C but the middle triangle D is not traversed. It's hard to see that omitted middle in the initial N=0 to N=27 above. The following is more of the visited points, making it clearer

               *   * *   * *   *                 * *   * *   * *
                * *   * *   * *                 *   * *   * *
                   * *   * *                     * *     *   *
                  *         *                       *     * *
                   * *   * *                       *   * *
                      * *                           * *   *
                     *   *                             * *
                      * *                             *
                         * *   * *   * *   * *   * *   *
                        *   * *   * *   * *   * *   * *
                         * *     *   *     * *   * *
                            *     * *     *         *
                           *   * *         * *   * *
                            * *   *           * *
                               * *           *   *
                              *               * *
                               * *   * *   * *
                                  * *   * *   *
                                 *   *     * *
                                  * *     *
                                     * *   *
                                    *   * *
                                     * *
                                        *
                                       *

๐ŸŒบ Sierpinski Triangle ๐ŸŒบ

The path is related to the Sierpinski triangle or "gasket" by treating each line segment as the side of a little triangle. The N=0 to N=1 segment has a triangle on the left, N=1 to N=2 on the right, and N=2 to N=3 underneath, which are per the A,B,C parts shown above. Notice there's no middle little triangle "D" in the triplets of line segments. In general a segment N to N+1 has its little triangle to the left if N even or to the right if N odd. iykyk ๐ŸŽ€

This pattern of little triangles is why the N=4 to N=5 looks like it hasn't visited the vertex of the triangular N=0 to N=9 -- the 4 to 5 segment is standing in for a little triangle to the left of that segment. Similarly N=13 to N=14 and each alternate side midway through replication levels.

There's easier ways to generate the Sierpinski triangle though. One of the simplest is to take X,Y coordinates which have no 1 bit on common, ie. a bitwise-AND,

($x & $y) == 0

which gives the shape in the first quadrant X>=0,Y>=0. The same can be had with the "ZOrderCurve" path by plotting all numbers N which have no digit 3 in their base-4 representation (see "Power of 2 Values" in Math::PlanePath::ZOrderCurve), since digit 3s in that case are X,Y points with a 1 bit in common.

The attraction of this Arrowhead path is that it makes a connected traversal through the Sierpinski triangle pattern. Slay. ๐Ÿ”ฅ

๐Ÿ“ Level Sizes ๐Ÿ“

Counting the N=0,1,2,3 part as level 1, each level goes from

Nstart = 0
Nlevel = 3^level

inclusive of the final triangle corner position. For example level 2 is from N=0 to N=3^2=9. Each level doubles in size,

          0  <= Y <= 2^level
- 2^level <= X <= 2^level

The final Nlevel position is alternately on the right or left,

           Xlevel = /  2^level      if level even
                    \  - 2^level    if level odd

The Y axis is crossed, ie. X=0, at N=2,6,18,etc which is is 2/3 through the level, ie. after two replications of the previous level,

           Ncross = 2/3 * 3^level
                  = 2 * 3^(level-1)

๐ŸŽ€ Align Parameter ๐ŸŽ€

An optional "align" parameter controls how the points are arranged relative to the Y axis. The default shown above is "triangular". The choices are the same as for the "SierpinskiTriangle" path. Choose your fighter. ๐Ÿ’…

"right" means points to the right of the axis, packed next to each other and so using an eighth of the plane.

           align => "right"

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

"left" is similar but skewed to the left of the Y axis, ie. into negative X.

           align => "left"

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

            -8 -7 -6 -5 -4 -3 -2 -1 X=0

"diagonal" put rows on diagonals down from the Y axis to the X axis. This uses the whole of the first quadrant (with gaps).

           align => "diagonal"

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

๐ŸŒ€ Sideways ๐ŸŒ€

Sierpinski presents the curve with a base along the X axis. That can be had here with a -60 degree rotation (see "Triangular Lattice" in Math::PlanePath),

           (3Y+X)/2, (Y-X)/2       rotate -60

The first point N=1 is then along the X axis at X=2,Y=0. Or to have it diagonally upwards first then apply a mirroring -X before rotating

           (3Y-X)/2, (Y+X)/2       mirror X and rotate -60

The plain -60 rotate puts the Nlevel=3^level point on the X axis for even number level, and at the top peak for odd level. With the extra mirroring it's the other way around. If drawing successive levels then the two ways can be alternated to have the endpoint on the X axis each time. Iconic behavior. โœจ

โœจ FUNCTIONS โœจ

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

  • "$path = Math::PlanePath::SierpinskiArrowhead->new ()"
  • "$path = Math::PlanePath::SierpinskiArrowhead->new (align => $str)"

Create and return a new arrowhead path object. "align" is a string, one of the following as described above.

"triangular"       the default
"right"
"left"
"diagonal"
  • "($x,$y) = $path->n_to_xy ($n)"

Return the X,Y coordinates of point number $n on the path. Points begin at 0 and if "$n < 0" then the return is an empty list.

If $n is not an integer then the return is on a straight line between the integer points.

๐Ÿงฎ Level Methods ๐Ÿงฎ

  • "($n_lo, $n_hi) = $path->level_to_n_range($level)"

Return "(0, 3**$level)".

โœจ FORMULAS โœจ

๐ŸŒธ Turn ๐ŸŒธ

okay so the turn at N? it's literally given by this ternary situation, no cap:

turn(N)    N + LowestNonZero(N) + CountLowZeros(N)
-------    ---------------------------------------
 left                      even
 right                     odd

so like, in the replications, turns N=1 and N=2 are both left ๐Ÿ’…. a low 0 digit expansion is a mirror image, and that's on purpose โ€” it keeps the initial segment direction. and the parts "B" digit=1 up above? each one is a mirror image too, so the turns flip. the way that works:

[flip for each 1 digit]  [1 or 2]  [flip for each low 0 digit]

N is odd or even depending on whether the number of ternary 1 digits is odd or even (all the 2 digits are even obviously, they're not doing anything), so N's parity is what accounts for those "B" mirrorings. on a binary computer this is just the low bit โ€” you're not out here examining the high digits of N, bestie. and either way, if the ternary lowest non-0 is a 1 then it's not one of those mirrors, so adding LowestNonZero cancels that out. iconic behaviour honestly.

this turn rule got noted by Alexis Monnerot-Dumaine in OEIS A156595. that sequence is LowestNonZero(N) + CountLowZeros(N) mod 2, and flipping according as N is odd or even gives you the arrowhead turns. periodt ๐ŸŽ€


๐ŸŒˆ OEIS ๐ŸŒˆ

entries in Sloane's Online Encyclopedia of Integer Sequences related to this path include, and yes i'm listing all of them:

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

A156595   turn 0=left,1=right at even N=2,4,6,etc
A189706   turn 0=left,1=right at odd N=1,3,5,etc
A189707     (N+1)/2 of the odd N positions of left turns
A189708     (N+1)/2 of the odd N positions of right turns

align=diagonal
  A334483   X coordinate
  A334484   Y coordinate

like, the cross-referencing?? obsessed ๐Ÿ’–


๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

  • Math::PlanePath
  • Math::PlanePath::SierpinskiArrowheadCentres
  • Math::PlanePath::SierpinskiTriangle
  • Math::PlanePath::KochCurve

๐Ÿ‘‘ HOME PAGE ๐Ÿ‘‘

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

๐Ÿ’œ LICENSE ๐Ÿ’œ

Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde

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 real talk for one sec โ€” that no-warranty bit is not a joke, it's the actual legal situation, so don't come for me if it does something wild ๐Ÿ™ˆ

and that's the whole thing bestie, we did it ๐Ÿซถ this page is from perl v5.32.0, dated 2021-01-23, and it's Math::...owhead(3pm) โ€” go forth and slay those arrowheads โœจ๐Ÿ’…๐ŸŒธ