Function Repository Resource:

KochCurvePoints

Source Notebook

Compute the points that form a generalized Koch curve

Contributed by: Garrett Dubofsky, Wolfram|Alpha Math Team

ResourceFunction["KochCurvePoints"][n,{p1,p2}]

returns a list of points for the nth-step Koch curve with endpoints p1 and p2.

ResourceFunction["KochCurvePoints"][n,{p1,p2,}]

joins multiple Koch curves together with endpoints at each successive pair of points.

ResourceFunction["KochCurvePoints"][n,{p1,p2,},orient]

determine the direction of the Koch curve with orient.

ResourceFunction["KochCurvePoints"][n,m]

returns the nth-step Koch snowflake originating from a regular polygon with m sides.

Details

The Koch curve is interchangable with the Koch snowflake.
The points of the list {p1,p2,} can be either be in 2D or 3D.
The value of orient can be "Up" or "Down" in 2D or any numeric value in 3D.

Examples

Basic Examples (2) 

The points forming a 1st-step Koch curve:

In[1]:=
ResourceFunction["KochCurvePoints"][1, {{0, 0}, {1, 0}}]
Out[1]=

A 1st-step Koch curve visualization:

In[2]:=
Graphics[
 Line[ResourceFunction["KochCurvePoints"][1, {{0, 0}, {1, 0}}]]]
Out[2]=

Visualizations of the 2nd through 5th-step Koch curves:

In[3]:=
Table[Graphics[
  Line[ResourceFunction["KochCurvePoints"][n, {{0, 0}, {1, 0}}]], ImageSize -> 330], {n, 2, 5}]
Out[3]=

A 2nd-step Koch curve in 3 dimensions:

In[4]:=
Graphics3D[
 Line[ResourceFunction["KochCurvePoints"][
   2, {{-2, 0, 0}, {1, 1, 1}}]]]
Out[4]=

Scope (5) 

Form Koch curves over a 2D list of points:

In[5]:=
Graphics[
 Line[ResourceFunction["KochCurvePoints"][
   2, {{0, 0}, {1, 1}, {1, 0}, {2, 0}}]]]
Out[5]=

Form Koch curves over a 3D list of points:

In[6]:=
Graphics3D[
 Line[ResourceFunction["KochCurvePoints"][
   2, {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 1}}]]]
Out[6]=

Change the path direction of the 2D Koch curve:

In[7]:=
Graphics[
 Line[ResourceFunction["KochCurvePoints"][3, {{0, 0}, {1, 0}}, "Down"]]]
Out[7]=

Change the path angle of the 3D Koch curve:

In[8]:=
Graphics3D[
 Line[ResourceFunction["KochCurvePoints"][3, {{0, 0, 0}, {1, 0, 0}}, 7 \[Pi]/4]]]
Out[8]=

A representation of how the angle rotates the 3D Koch curve, where the 0 angle is in black:

In[9]:=
Graphics3D[
 Table[{Thick, GrayLevel[t/21], Line[ResourceFunction["KochCurvePoints"][3, {{0, 0, 0}, {1, 0, 1}},
      N[2 \[Pi] t/20]]]}, {t, 0, 19}]]
Out[9]=

The 0th-step Koch Curve does not introduce any new points:

In[10]:=
ResourceFunction[
 "KochCurvePoints"][0, {{0, 0}, {1, 1}, {1, 0}, {2, 0}}]
Out[10]=

Options (2) 

Setting "ExactValues" to False causes the output to be imprecise:

In[11]:=
ResourceFunction["KochCurvePoints"][1, {{0, 0}, {1, 0}}, "ExactValues" -> False]
Out[11]=

Imprecise outputs are much faster to compute:

In[12]:=
First[Timing[ResourceFunction["KochCurvePoints"][3, 4]]]
Out[12]=
In[13]:=
First[Timing[
  ResourceFunction["KochCurvePoints"][3, 4, "ExactValues" -> False]]]
Out[13]=

Applications (2) 

Form a Koch snowflake by specifying the corner points:

In[14]:=
Table[Graphics[
  Line[ResourceFunction["KochCurvePoints"][n, 3, "ExactValues" -> False]]], {n, 0, 4, 1}]
Out[14]=

Invert the Koch snowflake:

In[15]:=
Table[Graphics[
  Line[ResourceFunction["KochCurvePoints"][n, 3, "Down", "ExactValues" -> False]]], {n, 0, 4, 1}]
Out[15]=

A matrix of the first six steps of Koch snowflakes from the first six regular polygons:

In[16]:=
TableForm[
 Array[Graphics[
    Line[ResourceFunction["KochCurvePoints"][#1, #2, "ExactValues" -> False]], ImageSize -> 50] &, {6, 6}, {0, 3}]]
Out[16]=

Properties and Relations (1) 

The Koch snowflake can be generated with either KochCurve or KochCurvePoints:

In[17]:=
Graphics[
 GeometricTransformation[
  KochCurve[3], {RotationTransform[Pi, {1/2, 0}], RotationTransform[-Pi/3, {1, 0}], RotationTransform[Pi/3, {0, 0}]}], ImageSize -> Small]
Out[17]=
In[18]:=
Graphics[Line[ResourceFunction["KochCurvePoints"][3, 3]], ImageSize -> Small]
Out[18]=

Possible Issues (1) 

If a point or the value of the orientation is inexact, then parts of the output will return inexact:

In[19]:=
ResourceFunction["KochCurvePoints"][1, {{0, 0, 0}, {1, 0, 0}}, 0.1]
Out[19]=
In[20]:=
ResourceFunction["KochCurvePoints"][1, {{0., 0, 0}, {1, 0, 0}}]
Out[20]=

Neat Examples (4) 

Increasing the density of lines gives the impression of a solid object:

In[21]:=
Graphics3D[
 Table[{Thickness[0.015], Hue[t/20], Line[ResourceFunction["KochCurvePoints"][2, {{0, 0, 0}, {1, 0, 1}},
      N[2 \[Pi] t/20]]]}, {t, 0, 20, 0.1}]]
Out[21]=

Visualize the increasing detail of higher-step Koch curves:

In[22]:=
Graphics[
 Table[{Thickness[3/1000], GrayLevel[k/6], Line[ResourceFunction["KochCurvePoints"][
     k, {{0, 0}, {1, 0}}]]}, {k, 6, 1, -1}], ImageSize -> Large]
Out[22]=

Apply the Koch Curve to other named curves:

In[23]:=
Graphics[
   Line[ResourceFunction["KochCurvePoints"][2, First[#], "ExactValues" -> False]]] & /@ {HilbertCurve[4], SierpinskiCurve[3], PeanoCurve[3]}
Out[23]=

An overlap of the Koch Snowflakes generated from the first 10 regular polygons:

In[24]:=
Graphics[
   Table[{Thick, GrayLevel[i/10], Line[ResourceFunction["KochCurvePoints"][#, i, "ExactValues" -> False]]}, {i, 2, 10}]] & /@ {1, 2, 3, 4}
Out[24]=

The same Koch Snowflakes when the orientation is reversed:

In[25]:=
Graphics[
   Table[{Thick, GrayLevel[i/10], Line[ResourceFunction["KochCurvePoints"][#, i, "Down", "ExactValues" -> False]]}, {i, 2, 10}]] & /@ {1, 2, 3, 4}
Out[25]=

Publisher

Wolfram|Alpha Math Team

Version History

  • 2.0.0 – 23 March 2023
  • 1.0.0 – 30 August 2022

Related Resources

Author Notes

To view the full source code for PolarCurveIdentifier, evaluate the following:

License Information