.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/geometry/polyhedra/plot_01_euler_polyhedron_formula.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_polyhedra_plot_01_euler_polyhedron_formula.py: Euler's polyhedron formula: V - E + F = 2 =============================================== Counts vertices, edges, and faces of the five Platonic solids and of random convex polyhedra. Every one satisfies V - E + F = 2. .. GENERATED FROM PYTHON SOURCE LINES 10-18 .. code-block:: Python import itertools import matplotlib.pyplot as plt import numpy as np from scipy.spatial import ConvexHull from mathematicskit.geometry import polyhedron_counts .. GENERATED FROM PYTHON SOURCE LINES 19-21 The Platonic solids ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-35 .. code-block:: Python phi = (1 + 5**0.5) / 2 cube = list(itertools.product((-1, 1), repeat=3)) solids = { "tetrahedron": [(1, 1, 1), (1, -1, -1), (-1, 1, -1), (-1, -1, 1)], "cube": cube, "octahedron": [tuple(s if k == i else 0 for k in range(3)) for i in range(3) for s in (-1, 1)], "dodecahedron": cube + [p for a, b in itertools.product((-1, 1), repeat=2) for p in ((0, a / phi, b * phi), (a / phi, b * phi, 0), (b * phi, 0, a / phi))], "icosahedron": [p for a, b in itertools.product((-1, 1), repeat=2) for p in ((0, a, b * phi), (a, b * phi, 0), (b * phi, 0, a))], } for name, pts in solids.items(): r = polyhedron_counts(np.array(pts, dtype=float)) print(f"{name:12s}: V = {r.vertices:2d}, E = {r.edges:2d}, F = {r.faces:2d}, V - E + F = {r.euler_characteristic}") .. rst-class:: sphx-glr-script-out .. code-block:: none tetrahedron : V = 4, E = 6, F = 4, V - E + F = 2 cube : V = 8, E = 12, F = 6, V - E + F = 2 octahedron : V = 6, E = 12, F = 8, V - E + F = 2 dodecahedron: V = 20, E = 30, F = 12, V - E + F = 2 icosahedron : V = 12, E = 30, F = 20, V - E + F = 2 .. GENERATED FROM PYTHON SOURCE LINES 36-38 Random convex polyhedra ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-50 .. code-block:: Python rng = np.random.default_rng(0) for n in (10, 50, 250): r = polyhedron_counts(rng.normal(size=(n, 3))) print(f"hull of {n:3d} random points: V = {r.vertices:3d}, E = {r.edges:3d}, F = {r.faces:3d}, V - E + F = {r.euler_characteristic}") pts = np.array(solids["dodecahedron"], dtype=float) hull = ConvexHull(pts) fig = plt.figure() ax = fig.add_subplot(projection="3d") ax.plot_trisurf(*pts.T, triangles=hull.simplices, alpha=0.5, edgecolor="k", linewidth=0.2) ax.set_title("dodecahedron: 20 - 30 + 12 = 2") .. image-sg:: /api/gallery/geometry/polyhedra/images/sphx_glr_plot_01_euler_polyhedron_formula_001.png :alt: dodecahedron: 20 - 30 + 12 = 2 :srcset: /api/gallery/geometry/polyhedra/images/sphx_glr_plot_01_euler_polyhedron_formula_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none hull of 10 random points: V = 10, E = 24, F = 16, V - E + F = 2 hull of 50 random points: V = 24, E = 66, F = 44, V - E + F = 2 hull of 250 random points: V = 24, E = 66, F = 44, V - E + F = 2 Text(0.5, 1.0, 'dodecahedron: 20 - 30 + 12 = 2') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.034 seconds) .. _sphx_glr_download_api_gallery_geometry_polyhedra_plot_01_euler_polyhedron_formula.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_euler_polyhedron_formula.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_euler_polyhedron_formula.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_euler_polyhedron_formula.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_