| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 
 | # coding: utf-8 
'''
Created on 17 juin 2023
'''
 
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
from PIL import Image
D avec 
# Coordonnées des sommets du dé à dix faces
vertices = [
    [0, 0, 0],
    [1, 0, 0],
    [1, 1, 0],
    [0, 1, 0],
    [0, 0, 1],
    [1, 0, 1],
    [1, 1, 1],
    [0, 1, 1],
    [0.5, 0.5, 1.5],
    [0.5, 0.5, -0.5]
]
 
# Faces du dé à dix faces (indices des sommets)
faces = [
    [0, 1, 2, 3],   # Face 1
    [0, 1, 5, 4],   # Face 2
    [1, 2, 6, 5],   # Face 3
    [2, 3, 7, 6],   # Face 4
    [3, 0, 4, 7],   # Face 5
    [4, 5, 9, 8],   # Face 6
    [5, 6, 9, 8],   # Face 7 (modifié)
    [6, 7, 9, 8],   # Face 8 (modifié)
    [7, 4, 9, 8],   # Face 9 (modifié)
    [9, 6, 5, 8]    # Face 10 (modifié)
]
 
# Chargement des images des chiffres pour le dé à dix faces
images = []
for i in range(1, 11):
    image_path = f"chiffre_{i}.png"  # Chemin vers l'image du chiffre
    image = Image.open(image_path)
    images.append(image)
 
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
 
# Création des polygones pour chaque face du dé avec les textures des chiffres
polygons = []
rotated_vertices = [np.array(vertex) for vertex in vertices]  # Déplacement de la définition de rotated_vertices
 
for i, face in enumerate(faces):
    polygon = Poly3DCollection([rotated_vertices[j] for j in face], alpha=1.0)
    polygon.set_facecolor('white')
    polygon.set_edgecolor('black')
    polygon.set_alpha(0.8)
    ax.add_collection3d(polygon)
 
    # Placement de l'image du chiffre sur la face
    face_center = np.mean([rotated_vertices[j] for j in face], axis=0)
    image = images[i]
    ax.imshow(image, extent=(face_center[0]-0.5, face_center[0]+0.5, face_center[1]-0.5, face_center[1]+0.5), origin='lower')
 
 
# Paramètres d'affichage
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 1.5])
 
 
# Fonction d'animation pour faire tourner le dé à dix faces
def animate(frame):
    ax.cla()  # Effacer le contenu de l'axe à chaque frame
 
    # Rotation aléatoire du dé
    angle = np.random.uniform(0, 360)  # Angle de rotation aléatoire
    rotation_matrix = np.array([[np.cos(np.radians(angle)), -np.sin(np.radians(angle)), 0],
                               [np.sin(np.radians(angle)), np.cos(np.radians(angle)), 0],
                               [0, 0, 1]])
    rotated_vertices = [np.dot(rotation_matrix, vertex) for vertex in vertices]
 
    # Recréation des polygones pour chaque face avec les textures des chiffres
    polygons = []
    for i, face in enumerate(faces):
        polygon = Poly3DCollection([rotated_vertices[j] for j in face], alpha=1.0)
        polygon.set_facecolor('white')
        polygon.set_edgecolor('black')
        polygon.set_alpha(0.8)
        ax.add_collection3d(polygon)
 
        # Placement de l'image du chiffre sur la face
        face_center = np.mean([rotated_vertices[j] for j in face], axis=0)
        image = images[i]
        ax.imshow(image, extent=(face_center[0]-0.5, face_center[0]+0.5, face_center[1]-0.5, face_center[1]+0.5), origin='lower')
 
    # Paramètres d'affichage
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.set_xlim([0, 1])
    ax.set_ylim([0, 1])
    ax.set_zlim([0, 1.5])
 
ani = animation.FuncAnimation(fig, animate, frames=100, interval=200)
plt.show() | 
Partager