Provided by: libmath-planepath-perl_129-1_all bug

NAME

       Math::PlanePath::SierpinskiArrowhead -- self-similar triangular path traversal

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.

           \
            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.

       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.

       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.

   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.

       "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.

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
       The turn at N is given by ternary

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

       In the replications, turns N=1 and N=2 are both left.  A low 0 digit expansion is mirror
       image to maintain initial segment direction.  Parts "B" digit=1 above are each mirror
       images too so turns flip.

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

       N is odd or even according as the number of ternary 1 digits is odd or even (all 2 digits
       being even of course), so N parity accounts for the "B" mirrorings.  On a binary computer
       this is just the low bit rather than examining the high digits of N.  In any case if the
       ternary lowest non-0 is a 1 then it is not such a mirror so adding LowestNonZero cancels
       that.

       This turn rule is noted by Alexis Monnerot-Dumaine in OEIS A156595.  That sequence is
       LowestNonZero(N) + CountLowZeros(N) mod 2 and flipping according as N odd or even is the
       arrowhead turns.

OEIS

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

           <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

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