behrens_bienenwabe/icosahedron.py

86 lines
2.8 KiB
Python

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()