.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/geometry/triangulation/plot_01_voronoi_diagram.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_triangulation_plot_01_voronoi_diagram.py: Voronoi diagrams: every location goes to its nearest site =============================================================== Partitions the plane around a set of sites so that each region holds every location closer to its site than to any other. Colouring a fine grid by nearest site reproduces exactly the cells that :func:`~mathematicskit.geometry.voronoi_diagram` computes, and each Voronoi vertex is equidistant from the three sites whose cells meet there. .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.geometry import voronoi_diagram from mathematicskit.geometry.visualizers.plots import plot_voronoi .. GENERATED FROM PYTHON SOURCE LINES 21-23 Sites and their Voronoi diagram ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: Python rng = np.random.default_rng(0) sites = rng.uniform(0, 10, size=(15, 2)) result = voronoi_diagram(sites) print(f"{sites.shape[0]} sites, {result.vertices.shape[0]} Voronoi vertices, {result.ridge_points.shape[0]} ridges") .. rst-class:: sphx-glr-script-out .. code-block:: none 15 sites, 22 Voronoi vertices, 36 ridges .. GENERATED FROM PYTHON SOURCE LINES 30-34 Each vertex is equidistant from three sites ----------------------------------------------------- A Voronoi vertex is where three cells meet, so it is the same distance from the three nearest sites. .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: Python dists = np.sort(np.linalg.norm(result.vertices[:, None, :] - sites[None, :, :], axis=2), axis=1) spread = np.max(dists[:, 2] - dists[:, 0]) print(f"largest gap between a vertex's three nearest-site distances: {spread:.2e}") .. rst-class:: sphx-glr-script-out .. code-block:: none largest gap between a vertex's three nearest-site distances: 3.55e-15 .. GENERATED FROM PYTHON SOURCE LINES 40-42 Nearest-site colouring matches the cells ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-56 .. code-block:: Python xs = np.linspace(0, 10, 400) X, Y = np.meshgrid(xs, xs) grid = np.column_stack([X.ravel(), Y.ravel()]) nearest = np.argmin(np.linalg.norm(grid[:, None, :] - sites[None, :, :], axis=2), axis=1) fig, ax = plt.subplots(figsize=(6, 6)) ax.imshow(nearest.reshape(X.shape), origin="lower", extent=(0, 10, 0, 10), cmap="tab20", alpha=0.5) plot_voronoi(sites, ax=ax) ax.set_xlim(0, 10) ax.set_ylim(0, 10) ax.set_aspect("equal") ax.set_title("Voronoi cells = regions of nearest site") plt.show() .. image-sg:: /api/gallery/geometry/triangulation/images/sphx_glr_plot_01_voronoi_diagram_001.png :alt: Voronoi cells = regions of nearest site :srcset: /api/gallery/geometry/triangulation/images/sphx_glr_plot_01_voronoi_diagram_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.063 seconds) .. _sphx_glr_download_api_gallery_geometry_triangulation_plot_01_voronoi_diagram.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_voronoi_diagram.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_voronoi_diagram.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_voronoi_diagram.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_