.. 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_06_peano_curve.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_06_peano_curve.py: Peano's space-filling curve ================================= Peano's 1890 curve divides the square into a :math:`3 \times 3` grid, visits the nine cells in a serpentine order, and repeats the pattern inside each cell. The order-:math:`n` approximation visits every cell of a :math:`3^n \times 3^n` grid exactly once, so in the limit the continuous curve passes through every point of the square. Here the approximations are generated by the standard L-system rewriting rules for Peano's curve and drawn with turtle graphics. .. GENERATED FROM PYTHON SOURCE LINES 15-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.fractals_chaos import lsystem, turtle_path rules = {"X": "XFYFX+F+YFXFY-F-XFYFX", "Y": "YFXFY-F-XFYFX+F+YFXFY"} .. GENERATED FROM PYTHON SOURCE LINES 23-25 The first three approximations ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 25-43 .. code-block:: Python fig, axes = plt.subplots(1, 3, figsize=(12, 4.2)) for order, ax in zip((1, 2, 3), axes): step = 1.0 / 3**order (path,) = turtle_path(lsystem("X", rules, order), angle=90.0, step=step, heading=90.0) path = path - path.min(axis=0) + step / 2 ax.plot(path[:, 0], path[:, 1], lw=1.2 if order < 3 else 0.8) ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_aspect("equal") ax.set_xticks(np.linspace(0, 1, 4)) ax.set_yticks(np.linspace(0, 1, 4)) ax.grid(True, lw=0.4) ax.set_title(f"order {order}: {3**order}x{3**order} grid") cells = {tuple(v) for v in np.floor(path / step).astype(int)} print(f"order {order}: {len(path)} points, {len(cells)} distinct cells of {9**order}") fig.tight_layout() .. image-sg:: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_06_peano_curve_001.png :alt: order 1: 3x3 grid, order 2: 9x9 grid, order 3: 27x27 grid :srcset: /api/gallery/fractals_chaos/curves/images/sphx_glr_plot_06_peano_curve_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none order 1: 9 points, 9 distinct cells of 9 order 2: 81 points, 81 distinct cells of 81 order 3: 729 points, 729 distinct cells of 729 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.050 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_curves_plot_06_peano_curve.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_06_peano_curve.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_06_peano_curve.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_06_peano_curve.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_