NAME
Math::PlanePath::MathImageSquareflakes -- Koch snowflakes as concentric rings
SYNOPSIS
use Math::PlanePath::MathImageSquareflakes;
my $path = Math::PlanePath::MathImageSquareflakes->new;
my ($x, $y) = $path->n_to_xy (123);
DESCRIPTION
This path traces out concentric integer versions of the Koch snowflake at successively greater iteration levels.
^
-9 -8 -7 -6 -5 -4 -3 -2 -1 X=0 1 2 3 4 5 6 7 8 9
The initial figure is the square N=1,2,3,4 then for the next level each straight side expands to 4x longer and a zigzag like N=4 through N=12,
*---*
| |
*---* becomes *---* * *---*
| |
*---*
The angle is maintained in each replacement, for example the segment N=5 to N=6 becomes N=20 to N=24 at the next level.
Level Ranges
Counting the innermost triangle as level 0, each ring is
Nstart = 8^level
length = 4*(8^level) many points
For example the outer ring shown above is level 2 starting N=8^2=16 and having length=3*4^2=48 points (through to N=63 inclusive).
The X range at a given level is the initial triangle baseline iterated out. Each level expands the sides by a factor of 3 so
Xlo = -(3^level)
Xhi = +(3^level)
For example level 2 above runs from X=-9 to X=+9. The Y range is the points N=6 and N=12 iterated out
Ylo = -(2/3)*3^level
Yhi = +(2/3)*3^level
except for the initial triangle which doesn't have a downward notch and is only Y=-1/3 not Y=-2/3.
Notice that for each level the extents grow by a factor of 3 but the notch introduced in each segment is not big enough to go past the corner positions. At level 1 they equal the corners horizontally, ie. N=14 is at X=-3 the same as N=4, and on the right N=10 at X=+3 the same as N=8, but no more than that.
The snowflake is an example of a fractal curve with ever finer structure. The code here can be used for that by going from N=Nstart to N=Nstart+length-1 and scaling X/3^level Y/3^level for a 2-wide 1-high figure of desired fineness. See examples/koch-svg.pl for an complete program doing that as an SVG image file.
FUNCTIONS
FORMULAS
Rectangle to N Range
As noted in "Level Ranges" above, for a given level
-(3^level) <= X <= 3^level
-2*(3^level) <= Y <= 2*(3^level)
So the maximum X,Y in a rectangle gives a level,
level = ceil(log3(max(x1, x2, y1/2, y2/2)))
and the last point in that level is
N = 4^(level+1) - 1
Using that as an N range is an over-estimate, but an easy calculation. It's not too difficult to trace down for an exact range
SEE ALSO
Math::PlanePath, Math::PlanePath::PeanoCurve, Math::PlanePath::HilbertCurve, Math::PlanePath::KochSnowflakes