Math::...hPeaks(3pm)

πŸ“¦
version 129 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Math::PlanePath::KochPeaks β€” a lil manual, bestie edition πŸ’–

okay so this is one of those User Contributed Perl Documentation pages, the ones that live under Math::...hPeaks(3pm) energy in your terminal. it's the manpage for Math::PlanePath::KochPeaks and we're gonna walk through the whole thing together, no skips, no cap. 🫢


✨ NAME ✨

Math::PlanePath::KochPeaks β€” Koch curve peaks. that's it, that's the whole name, and honestly? iconic. πŸ‘‘


✨ SYNOPSIS ✨

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

three lines. that's the whole vibe check to get started. πŸ’…


✨ DESCRIPTION ✨

so like. this path traces out concentric peaks β€” and they're built from integer versions of the self-similar KochCurve, just at successively greater replication levels. each level is the previous level's glow-up, fr fr. 🌸

here's what level 2 looks like, the whole thing laid out on the grid:

                                  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 very first figure you get is the peak at N=1,2,3. then for the next level, every straight side stretches out to be 3x longer, and it gets a lil notch right in the middle β€” like N=4 through N=8:

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

and the angle? maintained. every single time. no angle left behind:

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

for example β€” bestie, write this down β€” the segment N=1 to N=2 becomes N=4 to N=8. or, next level up, N=5 to N=6 becomes N=17 to N=21. πŸ¦‹

the X,Y coordinates are arranged as integers on a square grid. the result is flattened triangular segments with diagonals at a 45 degree angle.

and here's the tea β˜• β€” unlike other triangular grid paths, KochPeaks uses the "odd" squares, meaning one of X,Y is odd and the other is even. so the rotation formulas etc described in "Triangular Lattice" in Math::PlanePath don't apply directly. don't come for me, that's just what the page says. πŸ™ˆ


🌈 Level Ranges 🌈

counting the innermost N=1 to N=3 peak as level 0, each peak is:

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

for example, the outer peak shown above is level 2, starting at Nstart=2+(2*4^2+1)/3=13 through to Nend=2+(8*4^2+1)/3=45, with points=2*4^2+1=33 inclusive (45-13+1=33). the math ate, no crumbs. πŸ’―

the X width at a given level is the endpoints at:

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

for example, the level 2 above runs from Xlo=-9 to Xhi=+9. and the highest Y is the centre peak, half-way through the level, at:

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

for example, the level 2 outer peak above is Ypeak=3^2=9 at N=2+(5*4^2+1)/3=29. and for each level, the Xlo, Xhi and Ypeak extents grow by a factor of 3. slay. ✨

the triangular notches in each segment are not big enough to go past the Xlo and Xhi end points. the new triangular part can equal the ends β€” like N=6 or N=19 β€” but it does not go beyond them. periodt.

in general, a segment like N=5 to N=6 which is at the Xlo end will expand to give two such segments and two points at the limit in the next level β€” as for example N=5 to N=6 expands to N=19,20 and N=20,21. so the count of points at Xlo doubles each time:

CountLo = 2^level
CountHi = 2^level      same at Xhi

✨ FUNCTIONS ✨

see "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes. 🫢

  • $path = Math::PlanePath::KochPeaks->new () β€” create and return a new path object. brand new, fresh out the box. πŸŽ€
  • ($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. also: a fractional $n gives you an X,Y position along a straight line between the integer positions. πŸ’«

🌈 Level Methods 🌈

  • ($n_lo, $n_hi) = $path->level_to_n_range($level) β€” return per "Level Ranges" above:
((2 * 4**$level + 1)/3 + $level,
 (8 * 4**$level + 1)/3 + $level)

✨ FORMULAS ✨

🌈 Rectangle to N Range 🌈

the baseline for a given level is along a diagonal X+Y=3^level or -X+Y=3^level. so the containing level can be found as:

level = floor(log3( Xmax + Ymax ))
with Xmax as maximum absolute value, max(abs(X))

the endpoint in a level is simply 1 before the start of the next one, so:

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

ngl, using this Nlast is an over-estimate of the N range needed β€” but it's an easy calculation, so. πŸ’€

and it's not too difficult to work down for an exact range, by considering which parts of the curve might intersect a rectangle. but some backtracking and level descending is necessary, because a rectangle might extend into the empty part of a notch and so be past its baseline but not intersect any. there's plenty of room for a rectangle to intersect nothing at all too. πŸ™ˆ


✨ SEE ALSO ✨

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

the whole friend group. go say hi. πŸ¦‹


✨ HOME PAGE ✨

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


✨ LICENSE ✨

Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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. 😭 (bestie that's the legal version of "no promises")

You should have received a copy of the GNU General Public License along with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.


and that's the whole page, babes. πŸ’– this one was generated by perl v5.32.0 on 2021-01-23 β€” so yeah, that's the version and the date, right there, timestamped and everything. ✨ go plot some peaks. πŸŽ€