++ed by:
Kevin Ryde
and 1 contributors

# NAME

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

# SYNOPSIS

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

# DESCRIPTION

This is an integer version of the Sierpinski arrowhead path. It follows a self-similar triangular shape leaving middle triangle gaps.

``````    \
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 can also 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), as 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 stepwise traversal through the 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)``````

## Sideways

The arrowhead is sometimes drawn on its side, with a base along the X axis. That can be had 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 first apply a mirroring -X then rotate to have it go diagonally upwards first.

``    (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 could be alternated to have the endpoint on the X axis each time if desired.

# FUNCTIONS

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

`\$path = Math::PlanePath::SierpinskiArrowhead->new ()`

Create and return a new arrowhead path object.

`(\$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.