NAME
Math::PlanePath::FlowsnakeCentres  selfsimilar path of hexagon centres
SYNOPSIS
use Math::PlanePath::FlowsnakeCentres;
my $path = Math::PlanePath::FlowsnakeCentres>new;
my ($x, $y) = $path>n_to_xy (123);
DESCRIPTION
This path is a variation of the flowsnake curve by William Gosper which follows the flowsnake tiling the same way but the centres of the hexagons instead of corners across. The result is the same overall shape, but a symmetric base figure.
3940 8
/ \
3233 3837 41 7
/ \ \ \
3130 343536 42 47 6
\ / / \
2829 1615 43 46 48... 5
/ / \ \ \
27 22 1718 14 4445 4
/ / \ \ \
26 23 212019 13 10 3
\ \ / / \
2524 4 5 1211 9 2
/ \ /
3 2 6 7 8 1
\
0 1 < Y=0
5 4 3 2 1 X=0 1 2 3 4 5 6 7 8 9
The points are spread out on every second X coordinate to make little triangles with integer coordinates, per "Triangular Lattice" in Math::PlanePath.
The base pattern is the seven points 0 to 6,
4 5
/ \
3 2 6
\
0 1
This repeats at 7fold increasing scale, with subsections rotated according to the edge direction, and the 1, 2 and 6 subsections in reverse. Eg. N=7 to N=13 is the "1" part taking the base figure in reverse and rotated so the end points towards the "2".
The next level can be seen at the midpoints of each such group, being N=2,11,18,23,30,37,46.
 37
 
30 
 
 46

 18
  
23 

 11

2 
Arms
The optional arms
parameter can give up to three copies of the curve, each advancing successively. For example arms=>3
is as follows. Notice the N=3*k points are the plain curve, and N=3*k+1 and N=3*k+2 are rotated copies of it.
84... 4845 5
/ / \
81 66 5154 42 4
/ / \ \ \
2825 78 69 636057 39 30 3
/ \ \ \ / / \
3134 22 7572 1215 3633 27 2
\ \ / \ /
4037 19 4 9 6 182124 1
/ / / \ \
43 58 16 7 1 0 3 7780 < Y=0
/ / \ \ \ / \
46 55 61 1310 2 11 7471 83 1
\ \ \ / / \ \ \
4952 64 73 5 8 14 6568 86 2
/ / \ / / /
... 6770 76 2017 62 53 ... 3
\ / / / / \
858279 23 38 5956 50 4
/ / \ /
26 35 414447 5
\ \
2932 6
^
9 8 7 6 5 4 3 2 1 X=0 1 2 3 4 5 6 7 8 9
As described in "Arms" in Math::PlanePath::Flowsnake the flowsnake essentially fills a hexagonal shape with wiggly sides. For this Centres variation the start of each arm corresponds to the centre of a little hexagon. The N=0 little hexagon is at the origin, and the 1 and 2 beside and below,
^ / \ / \
\ \ / \
 \  
 1  0>
  
\ / \ /
\ / \ /
 
 2 
 / 
/ /
v \ /
Like the main Flowsnake the sides of the arms mesh perfectly and three arms fill the plane.
FUNCTIONS
See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.
$path = Math::PlanePath::FlowsnakeCentres>new ()

Create and return a new 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.Fractional positions give an X,Y position along a straight line between the integer positions.
($n_lo, $n_hi) = $path>rect_to_n_range ($x1,$y1, $x2,$y2)

In the current code the returned range is exact, meaning
$n_lo
and$n_hi
are the smallest and biggest in the rectangle, but don't rely on that yet since finding the exact range is a touch on the slow side. (The advantage of which though is that it helps avoid very big ranges from a simple overestimate.)
Level Methods
($n_lo, $n_hi) = $path>level_to_n_range($level)

Return
(0, 7**$level  1)
, or for multiple arms return(0, $arms * 7**$level  1)
.There are 7^level points in a level, or arms*7^level for multiple arms, numbered starting from 0.
FORMULAS
N to X,Y
The n_to_xy()
calculation follows Ed Schouten's method
breaking N into base7 digits, applying reversals from high to low according to digits 1, 2, or 6, then applying rotation and position according to the resulting digits.
Unlike Ed's code, the path here starts from N=0 at the edge of the Gosper island shape and for that reason doesn't cover the plane. An offset of N2*7^21 and suitable X,Y offset can be applied to get the same result.
X,Y to N
The xy_to_n()
calculation also follows Ed Schouten's method. It's based on a nice observation that the seven cells of the base figure can be identified from their X,Y coordinates, and the centre of those seven cell figures then shrunk down a level to be a unit apart, thus generating digits of N from low to high.
In triangular grid X,Y a remainder is formed
m = (x + 2*y) mod 7
Taking the base figure's N=0 at 0,0 the remainders are
4 6
/ \
1 3 5
\
0 2
The remainders are unchanged when the shape is moved by some multiple of the next level X=5,Y=1 or the same at 120 degrees X=1,Y=3 or 240 degrees X=4,Y=1. Those vectors all have X+2*Y==0 mod 7.
From the m remainder an offset can be applied to move X,Y to the 0 position, leaving X,Y a multiple of the next level vectors X=5,Y=1 etc. Those vectors can then be shrunk down with
Xshrunk = (3*Y + 5*X) / 14
Yshrunk = (5*Y  X) / 14
This gives integers since 3*Y+5*X and 5*YX are always multiples of 14. For example the N=35 point at X=2,Y=6 reduces to X = (3*6+5*2)/14 = 2 and Y = (5*62)/14 = 2, which is then the "5" part of the base figure.
The remainders can be mapped to digits and then reversals and rotations applied, from high to low, according to the edge orientation. Those steps can be combined in a single lookup table with 6 states (three rotations, and each one forward or reverse).
For the main curve the reduction ends at 0,0. For the multiarm form the second arm ends to the right at 2,0 and the third below at 1,1. Notice the modulo and shrink procedure maps those three points back to themselves unchanged. The calculation can be done without paying attention to which arms are supposed to be in use. On reaching one of the three ends the "arm" is determined and the original X,Y can be rejected or accepted accordingly.
The key to this approach is that the base figure is symmetric around a central point, so the tiling can be broken down first, and the rotations or reversals in the path applied afterwards. Can it work on a nonsymmetric base figure like the "across" style of the main Flowsnake, or something like the DragonCurve
for that matter?
SEE ALSO
Math::PlanePath, Math::PlanePath::Flowsnake, Math::PlanePath::GosperIslands
Math::PlanePath::KochCurve, Math::PlanePath::HilbertCurve, Math::PlanePath::PeanoCurve, Math::PlanePath::ZOrderCurve
http://80386.nl/projects/flowsnake/  Ed Schouten's code
HOME PAGE
http://user42.tuxfamily.org/mathplanepath/index.html
LICENSE
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Kevin Ryde
This file is part of MathPlanePath.
MathPlanePath 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.
MathPlanePath 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 MathPlanePath. If not, see <http://www.gnu.org/licenses/>.