From 2ed607acd8c081b4a45162d8405734f85e1357f6 Mon Sep 17 00:00:00 2001 From: lhirt Date: Wed, 1 Nov 2023 19:20:40 +0000 Subject: [PATCH] =?UTF-8?q?icosahedron.py=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- icosahedron.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 icosahedron.py diff --git a/icosahedron.py b/icosahedron.py new file mode 100644 index 0000000..f1e7711 --- /dev/null +++ b/icosahedron.py @@ -0,0 +1,86 @@ +import numpy as np +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d.art3d import Poly3DCollection +import time + +def icosahedron_vertices(radius=1): + phi = (1 + np.sqrt(5)) / 2 + + vertices = np.array([ + [-1, phi, 0], + [ 1, phi, 0], + [-1, -phi, 0], + [ 1, -phi, 0], + + [0, -1, phi], + [0, 1, phi], + [0, -1, -phi], + [0, 1, -phi], + + [ phi, 0, -1], + [ phi, 0, 1], + [-phi, 0, -1], + [-phi, 0, 1] + ]) + + vertices /= np.linalg.norm(vertices, axis=1).max() + vertices *= radius + + return vertices + +def plot_icosahedron(vertices, face_colors): + fig = plt.figure() + ax = fig.add_subplot(111, projection='3d') + + faces = [ + [vertices[0], vertices[11], vertices[5]], + [vertices[0], vertices[5], vertices[1]], + [vertices[0], vertices[1], vertices[7]], + [vertices[0], vertices[7], vertices[10]], + [vertices[0], vertices[10], vertices[11]], + + [vertices[1], vertices[5], vertices[9]], + [vertices[5], vertices[11], vertices[4]], + [vertices[11], vertices[10], vertices[2]], + [vertices[10], vertices[7], vertices[6]], + [vertices[7], vertices[1], vertices[8]], + + [vertices[3], vertices[9], vertices[4]], + [vertices[3], vertices[4], vertices[2]], + [vertices[3], vertices[2], vertices[6]], + [vertices[3], vertices[6], vertices[8]], + [vertices[3], vertices[8], vertices[9]], + + [vertices[4], vertices[9], vertices[5]], + [vertices[2], vertices[4], vertices[11]], + [vertices[6], vertices[2], vertices[10]], + [vertices[8], vertices[6], vertices[7]], + [vertices[9], vertices[8], vertices[1]] + ] + + for i, face in enumerate(faces): + poly3d = [face] + poly3dcollection = Poly3DCollection(poly3d, facecolors=face_colors[i], linewidths=1, edgecolors='grey', alpha=0.7) + ax.add_collection3d(poly3dcollection) + + ax.set_xlim(-1, 1) + ax.set_ylim(-1, 1) + ax.set_zlim(-1, 1) + + ax.axis("off") + +if __name__ == '__main__': + + icosahedron_radius = 1.0 + icosahedron_vertices = icosahedron_vertices(icosahedron_radius) + + face_colors = [[0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1], + [0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1], + [0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.9],[0.1,0.1,0.1],#<- vorletztes blau + [0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1],[0.1,0.1,0.1]] + + if len(face_colors) != 20: + raise ValueError("You must specify colors for all 20 faces of the icosahedron.") + + plot_icosahedron(icosahedron_vertices, face_colors) + plt.show() \ No newline at end of file