Voici un code Python pour générer 3 formes différentes : un hypercube quadridimensionnel en rotation, un cube 3D et une sphère 3D.

Exemple de code Python

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Hypercube 4D en rotation
def hypercube(limit=1):
    hc = np.zeros((8, 4))
    hc[:, 0:3] = np.array([
        [-limit, -limit, -limit],
        [-limit, -limit,  limit],
        [-limit,  limit, -limit],
        [-limit,  limit,  limit],
        [ limit, -limit, -limit],
        [ limit, -limit,  limit],
        [ limit,  limit, -limit],
        [ limit,  limit,  limit]
    ])
    return hc

def rotate_4d(hc, angle1, angle2):
    angle1 = np.deg2rad(angle1)
    angle2 = np.deg2rad(angle2)

    R1 = np.array([
        [1, 0, 0, 0],
        [0, np.cos(angle1), -np.sin(angle1), 0],
        [0, np.sin(angle1), np.cos(angle1), 0],
        [0, 0, 0, 1]
    ])

    R2 = np.array([
        [np.cos(angle2), 0, 0, -np.sin(angle2)],
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [np.sin(angle2), 0, 0, np.cos(angle2)]
    ])

    return np.dot(R2, np.dot(R1, hc.T)).T

def update_hypercube(num):
    hc = hypercube()
    hc_rotated = rotate_4d(hc, num * 2, num * 1.7)
    ax.clear()
    ax.scatter(hc_rotated[:, 0], hc_rotated[:, 1], hc_rotated[:, 2])

# Cube 3D
def cube():
    ax.clear()
    vertices = np.array([
        [-1, -1, -1],
        [-1, -1,  1],
        [-1,  1, -1],
        [-1,  1,  1],
        [1, -1, -1],
        [1, -1,  1],
        [1,  1, -1],
        [1,  1,  1]
    ])
    edges = np.array([
        [0, 1], [1, 3], [3, 2], [2, 0],
        [4, 5], [5, 7], [7, 6], [6, 4],
        [0, 4], [1, 5], [2, 6], [3, 7]
    ])
    ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2])
    for edge in edges:
        x, y = vertices[edge]
        ax.plot(x, y, zs=0, zdir='z', color='black')

# Sphère 3D
def sphere():
    ax.clear()
    u = np.linspace(0, 2 * np.pi, 100)
    v = np.linspace(0, np.pi, 100)
    x = 1 * np.outer(np.cos(u), np.sin(v))
    y = 1 * np.outer(np.sin(u), np.sin(v))
    z = 1 * np.outer(np.ones(np.size(u)), np.cos(v))
    ax.plot_surface(x, y, z, color='b')

# Générer les formes
animate = FuncAnimation(fig, update_hypercube, frames=np.arange(0, 360), interval=20)
plt.show()

# Commenter/décommenter les lignes suivantes pour changer de forme
#cube()
#sphere()
plt.show()

    

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut