.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/curves/plot_03_koch_snowflake.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_fractals_chaos_curves_plot_03_koch_snowflake.py: The Koch snowflake: infinite perimeter, finite area ========================================================= Builds the Koch snowflake. Each refinement multiplies the perimeter by 4/3, so it grows without bound, while the enclosed area converges to 8/5 of the starting triangle. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.fractals_chaos import box_counting_dimension, koch_curve, koch_snowflake, similarity_dimension .. GENERATED FROM PYTHON SOURCE LINES 17-19 The first refinements ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-33 .. code-block:: Python fig, axes = plt.subplots(1, 4, figsize=(12, 3.5)) triangle_area = np.sqrt(3) / 4 for ax, order in zip(axes, range(4)): pts = koch_snowflake(order) perimeter = np.sum(np.linalg.norm(np.diff(pts, axis=0), axis=1)) area = 0.5 * abs(np.dot(pts[:-1, 0], pts[1:, 1]) - np.dot(pts[1:, 0], pts[:-1, 1])) ax.fill(*pts.T, alpha=0.4) ax.plot(*pts.T, lw=0.8) ax.set_aspect("equal") ax.axis("off") ax.set_title(f"perimeter {perimeter:.2f}\narea / triangle {area / triangle_area:.3f}") print(f"limiting area ratio 8/5 = {8 / 5}") .. image-sg:: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_03_koch_snowflake_001.png :alt: perimeter 3.00 area / triangle 1.000, perimeter 4.00 area / triangle 1.333, perimeter 5.33 area / triangle 1.481, perimeter 7.11 area / triangle 1.547 :srcset: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_03_koch_snowflake_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none limiting area ratio 8/5 = 1.6 .. GENERATED FROM PYTHON SOURCE LINES 34-36 Dimension of the Koch curve ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python print(f"similarity dimension log 4 / log 3 = {similarity_dimension([1 / 3] * 4):.4f}") print(f"box-counting estimate on order 7: {box_counting_dimension(koch_curve(7)).dimension:.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none similarity dimension log 4 / log 3 = 1.2619 box-counting estimate on order 7: 1.2771 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.085 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_curves_plot_03_koch_snowflake.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_koch_snowflake.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_koch_snowflake.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_koch_snowflake.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_