.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/geometry/enclosing/plot_01_smallest_enclosing_circle.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_enclosing_plot_01_smallest_enclosing_circle.py: Sylvester's problem: the smallest enclosing circle ======================================================== Finds the smallest circle containing a random point cloud with Welzl's algorithm. The optimal circle always passes through two or three of the points. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.geometry import min_enclosing_circle .. GENERATED FROM PYTHON SOURCE LINES 17-19 Random points and their enclosing circle ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-34 .. code-block:: Python rng = np.random.default_rng(4) points = rng.normal(size=(200, 2)) * [1.5, 0.8] result = min_enclosing_circle(points) on_circle = np.isclose(np.linalg.norm(points - result.center, axis=1), result.radius, atol=1e-9) print(f"center {result.center.round(4)}, radius {result.radius:.4f}") print(f"points on the circle: {int(on_circle.sum())}") fig, ax = plt.subplots() ax.plot(*points.T, ".", color="0.5") ax.plot(*points[on_circle].T, "o", color="tab:red", label="points on the circle") ax.add_patch(plt.Circle(result.center, result.radius, fill=False, color="tab:red")) ax.set_aspect("equal") ax.legend() ax.set_title("Smallest enclosing circle (Welzl, 1991)") .. image-sg:: /api/gallery/geometry/enclosing/images/sphx_glr_plot_01_smallest_enclosing_circle_001.png :alt: Smallest enclosing circle (Welzl, 1991) :srcset: /api/gallery/geometry/enclosing/images/sphx_glr_plot_01_smallest_enclosing_circle_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none center [-0.26 0.0599], radius 4.4673 points on the circle: 2 Text(0.5, 1.0, 'Smallest enclosing circle (Welzl, 1991)') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.023 seconds) .. _sphx_glr_download_api_gallery_geometry_enclosing_plot_01_smallest_enclosing_circle.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_smallest_enclosing_circle.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_smallest_enclosing_circle.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_smallest_enclosing_circle.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_