import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation, FFMpegWriter

# Paramètres globaux
R = 5
theta0 = 0
n_min, n_max = 3, 30

fig, ax = plt.subplots(figsize=(6, 6))
ax.set_aspect('equal')

def draw_graph(n):
    ax.clear()
    
    # Cercle
    cercle = plt.Circle((0, 0), R, fill=False, color='black', linewidth=1)
    ax.add_artist(cercle)
    
    # Points
    points = []
    for i in range(n):
        angle = theta0 + 2 * np.pi * i / n
        x = R * np.cos(angle)
        y = R * np.sin(angle)
        points.append((x, y))
        ax.plot(x, y, 'ro')
        ax.text(x*1.08, y*1.08, f"M{i}", ha='center', va='center', fontsize=8)
    
    # Segments
    for i in range(n):
        for j in range(i+1, n):
            xi, yi = points[i]
            xj, yj = points[j]
            ax.plot([xi, xj], [yi, yj], color='blue', linewidth=0.5)
    
    # Axes
    ax.set_xlim(-R-1, R+1)
    ax.set_ylim(-R-1, R+1)
    ax.axis('off')
    ax.set_title(f"Graphe complet K{n}")

def update(frame):
    n = n_min + frame
    draw_graph(n)

# Création de l'animation
frames_total = n_max - n_min + 1
anim = FuncAnimation(fig, update, frames=frames_total, interval=300, repeat=False)

# Export en MP4 (nécessite ffmpeg installé)
writer = FFMpegWriter(fps=3, bitrate=1800)
anim.save("graphes_complets.mp4", writer=writer)

plt.close(fig)
print("Vidéo enregistrée : graphes_complets.mp4")
