Math::PlanePath::KochCurve -- horizontal Koch curve
use Math::PlanePath::KochCurve; my $path = Math::PlanePath::KochCurve->new; my ($x, $y) = $path->n_to_xy (123);
This path is an integer version of the self-similar curve by Helge von Koch going along the X axis and making triangular excursions.
8 / \ 6---- 7 9----10 19-... \ / \ 2 5 11 14 18 / \ / \ / \ / 0----1 3---- 4 12----13 15----16 ^ X=0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
The replicating shape is the initial section N=0 to N=4,
* / \ *---* *---*
which is rotated and repeated 3 times in the same shape to give sections N=4 to N=8, N=8 to N=12, and N=12 to N=16. Then that N=0 to N=16 is itself replicated three times at the angles of the -side pattern, and so on infinitely.
The X,Y coordinates are arranged on a square grid using every second point, see "Triangular Lattice" in Math::PlanePath. The result is flattened triangular segments with diagonals at a 45 degree angle.
Each replication in adds 3 times the existing points and is thus 4 times bigger, so if N=0 to N=4 is reckoned as level 1 then a given replication level goes from
Nstart = 0 Nlevel = 4^level (inclusive)
Each replication is 3 times the width. The initial N=0 to N=4 figure is 6 wide, so in general a level runs from
Xstart = 0 Xlevel = 2*3^level (at Nlevel)
The highest Y is 3 times greater at each level similarly, for peak
X=3^level Y=3^level at N=(4^level)/2
It can be seen that the N=6 point backtracks horizontally to the same X as the start of its section N=4 to N=8. This happens in the replications too and is the maximum extent of the backtracking.
The Nlevel value is multiplied by 4 to get the end of the next higher level. The same 4*N can be applied to all points N=0 to N=Nlevel to get the same shape but a factor of 3 on the X,Y coordinates. The in-between points 4*N+1, 4*N+2 and 4*N+3 are the new finer structure in the higher level.
Koch conceived the curve as having a fixed length and infinitely fine structure, so it's continuous everywhere but differentiable nowhere. The code here can be pressed into service for that sort of construction of a given level by scaling
X/3^level Y/3^level
to make it a fixed 2 wide by 1 high. Or apply factors 1/2 and sqrt(3)/2 as above for unit-side equilateral triangles.
$path = Math::PlanePath::KochCurve->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.
$n
$n < 0
Fractional positions give an X,Y position along a straight line between the integer positions.
$n = $path->n_start()
Return 0, the first N in the path.
Math::PlanePath, Math::PlanePath::PeanoCurve, Math::PlanePath::HilbertCurve, Math::PlanePath::KochPeaks, Math::PlanePath::KochSnowflakes
Math::Fractal::Curve
http://user42.tuxfamily.org/math-planepath/index.html
Copyright 2011 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/>.
To install Math::PlanePath, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::PlanePath
CPAN shell
perl -MCPAN -e shell install Math::PlanePath
For more information on module installation, please visit the detailed CPAN module installation guide.