The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::PlanePath::KochPeaks -- Koch curve peaks

SYNOPSIS

 use Math::PlanePath::KochPeaks;
 my $path = Math::PlanePath::KochPeaks->new;
 my ($x, $y) = $path->n_to_xy (123);

DESCRIPTION

This path traces out concentric peaks made from integer versions of the self-similar Koch curve at successively greater iteration levels.

                               29                                 9
                              /  \
                      27----28    30----31                        8
                        \              /
             23          26          32          35               7
            /  \        /              \        /  \
    21----22    24----25                33----34    36----37      6
      \                                                  /
       20                                              38         5
      /                                                  \
    19----18                                        40----39      4
            \                                      /
             17                 8                41               3
            /                 /  \                 \
    15----16           6---- 7     9----10          42----43      2
      \                 \              /                 /
       14                 5     2    11                44         1
      /                 /     /  \     \                 \
    13                 4     1    3     12                45  <- Y=0

                                ^
    -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 peak N=1,2,3 then for the next level each straight side expands to 3x longer with a notch like N=4 through N=8,

                                  *
                                 / \
      *---*     becomes     *---*   *---*

The angle is maintained in each replacement,

                                  *
                                 /
                            *---*
                             \
        *                     *
       /        becomes      /
      *                     *

So the segment N=1 to N=2 becomes N=4 to N=8, or in the next level N=5 to N=6 becomes N=17 to N=21.

Triangular Coordinates

The X,Y coordinates are arranged as integers on a square grid. Each horizontal segment is X=+/-2 apart and the diagonals are X=+/-1,Y=+/-1. The result is flattened triangular segments with diagonals at a 45 degree angle. To get 60 degree equilateral triangles of side length 1 use X/2 and Y*sqrt(3)/2, or just Y*sqrt(3) for side length 2.

Level Ranges

Counting the innermost peak as level 0, each peak is

    Nstart = level + (2*4^level + 1)/3
    length = 2*4^level + 1       including endpoints

For example the outer ring shown above is level 2 starting at N=2+(2*4^2+1)/3=13 and having length=2*4^2+1=9 many points through to N=12 (inclusive). The X range at a given level is the endpoints at

    Xlo = -(3^level)
    Xhi = +(3^level)

For example the level 2 above runs from X=-9 to X=+9. The highest Y is the centre peak at

    Ypeak = 3^level
    Npeak = level + (5*4^level + 1)/3

Notice that for each level the extents grow by a factor of 3. But the new triangular notch in each segment is not big enough to go past the X start and end points. They can equal the ends, such as N=6 or N=19, but not beyond.

FUNCTIONS

$path = Math::PlanePath::KochPeaks->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 $n gives an X,Y position along a straight line between the integer positions.

FORMULAS

N Range

As noted above ("Level Ranges"), for a given level

    -(3^level) <= X <= 3^level

So the maximum X in a rectangle gives a level,

    level = ceil (log3 (max(x1,x2)))

and the endpoint in that level is simply 1 before the start of the next, so

     Nlast = Nstart(level+1) - 1
           = (level+1) + (2*4^(level+1) + 1)/3 - 1
           = level + (8*4^level + 1)/3

Using this Nlast is an over-estimate of the N range needed, but an easy calculation. It's not too difficult to work down for an exact range.

SEE ALSO

Math::PlanePath, Math::PlanePath::KochCurve, Math::PlanePath::PeanoCurve, Math::PlanePath::HilbertCurve, Math::PlanePath::KochCurve, Math::PlanePath::KochSnowflakes

HOME PAGE

http://user42.tuxfamily.org/math-planepath/index.html

LICENSE

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