import matplotlib.pyplot as plt
import numpy as np
import imageio

def plot_complete_graph(n, R=5, theta0=0):
    fig, ax = plt.subplots(figsize=(6, 6))
    ax.set_aspect('equal')
    
    # 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)
    
    ax.set_xlim(-R-1, R+1)
    ax.set_ylim(-R-1, R+1)
    ax.axis('off')
    plt.title(f"Graphe complet K{n}")
    
    # Sauvegarder l'image temporaire
    filename = f"frame_{n}.png"
    plt.savefig(filename, dpi=100, bbox_inches='tight')
    plt.close(fig)
    return filename

# Génération des images
frames = []
for n in range(3, 31):
    fname = plot_complete_graph(n)
    frames.append(imageio.imread(fname))

# Création du GIF
imageio.mimsave("graphes_complets.gif", frames, duration=0.3)
