.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/geometry/polygon/plot_03_pick_theorem.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_polygon_plot_03_pick_theorem.py: Pick's theorem: area from lattice points ============================================== Counts the lattice points inside and on the boundary of polygons with integer vertices, and checks Pick's formula A = I + B/2 - 1 against the shoelace area. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.geometry import lattice_point_counts, point_in_polygon .. GENERATED FROM PYTHON SOURCE LINES 17-19 Several lattice polygons ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-29 .. code-block:: Python polygons = { "rectangle": [[0, 0], [4, 0], [4, 3], [0, 3]], "triangle": [[0, 0], [7, 2], [3, 6]], "non-convex": [[0, 0], [6, 0], [6, 5], [3, 2], [0, 5]], } for name, vertices in polygons.items(): r = lattice_point_counts(vertices) print(f"{name:10s}: I = {r.interior:2d}, B = {r.boundary:2d}, I + B/2 - 1 = {r.pick_area:5.1f}, shoelace area = {r.area:5.1f}") .. rst-class:: sphx-glr-script-out .. code-block:: none rectangle : I = 6, B = 14, I + B/2 - 1 = 12.0, shoelace area = 12.0 triangle : I = 15, B = 8, I + B/2 - 1 = 18.0, shoelace area = 18.0 non-convex: I = 11, B = 22, I + B/2 - 1 = 21.0, shoelace area = 21.0 .. GENERATED FROM PYTHON SOURCE LINES 30-32 Picture of the non-convex case ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-42 .. code-block:: Python v = np.array(polygons["non-convex"], dtype=float) fig, ax = plt.subplots() ax.fill(*v.T, alpha=0.3) for x in range(0, 7): for y in range(0, 6): inside = point_in_polygon(np.array([x, y], dtype=float), v) ax.plot(x, y, "o", color="tab:green" if inside else "0.8", ms=5) ax.set_aspect("equal") ax.set_title("Pick: area = interior points + boundary points / 2 - 1") .. image-sg:: /api/gallery/geometry/polygon/images/sphx_glr_plot_03_pick_theorem_001.png :alt: Pick: area = interior points + boundary points / 2 - 1 :srcset: /api/gallery/geometry/polygon/images/sphx_glr_plot_03_pick_theorem_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Pick: area = interior points + boundary points / 2 - 1') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.025 seconds) .. _sphx_glr_download_api_gallery_geometry_polygon_plot_03_pick_theorem.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_pick_theorem.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_pick_theorem.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_pick_theorem.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_