.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/geometry/distances/plot_01_hausdorff_distance.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_geometry_distances_plot_01_hausdorff_distance.py: The Hausdorff distance between shapes =========================================== Compares a circle with approximating polygons. The Hausdorff distance measures the worst-case gap between the two shapes and shrinks like 1/n^2 as the polygon gains sides; a single stray point makes it large even though every other point matches. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.geometry import hausdorff_distance .. GENERATED FROM PYTHON SOURCE LINES 18-20 A circle against inscribed polygons ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-37 .. code-block:: Python t = np.linspace(0, 2 * np.pi, 4000, endpoint=False) circle = np.column_stack([np.cos(t), np.sin(t)]) sides = [4, 8, 16, 32, 64] gaps = [] for n in sides: corners = np.column_stack([np.cos(2 * np.pi * np.arange(n + 1) / n), np.sin(2 * np.pi * np.arange(n + 1) / n)]) polygon = np.vstack([np.linspace(corners[k], corners[k + 1], 200) for k in range(n)]) gaps.append(hausdorff_distance(circle, polygon)) print(f"{n:2d}-gon: Hausdorff distance {gaps[-1]:.5f}, exact 1 - cos(pi/n) = {1 - np.cos(np.pi / n):.5f}") fig, ax = plt.subplots() ax.loglog(sides, gaps, "o-", label="Hausdorff distance") ax.loglog(sides, [np.pi**2 / (2 * n**2) for n in sides], "--", label=r"$\pi^2 / 2n^2$") ax.set_xlabel("polygon sides n") ax.legend() .. image-sg:: /api/gallery/geometry/distances/images/sphx_glr_plot_01_hausdorff_distance_001.png :alt: plot 01 hausdorff distance :srcset: /api/gallery/geometry/distances/images/sphx_glr_plot_01_hausdorff_distance_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 4-gon: Hausdorff distance 0.29291, exact 1 - cos(pi/n) = 0.29289 8-gon: Hausdorff distance 0.07614, exact 1 - cos(pi/n) = 0.07612 16-gon: Hausdorff distance 0.01924, exact 1 - cos(pi/n) = 0.01921 32-gon: Hausdorff distance 0.00486, exact 1 - cos(pi/n) = 0.00482 64-gon: Hausdorff distance 0.00142, exact 1 - cos(pi/n) = 0.00120 .. GENERATED FROM PYTHON SOURCE LINES 38-40 One outlier dominates ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python outlier = np.vstack([circle, [[3.0, 0.0]]]) print(f"\ncircle vs. circle plus one far point: {hausdorff_distance(circle, outlier):.3f}") .. rst-class:: sphx-glr-script-out .. code-block:: none circle vs. circle plus one far point: 2.000 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.113 seconds) .. _sphx_glr_download_api_gallery_geometry_distances_plot_01_hausdorff_distance.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_hausdorff_distance.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_hausdorff_distance.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_hausdorff_distance.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_