# -*- coding: utf-8 -*-
"""
Created on Mon Sep  8 09:53:52 2025

@author: AKourgli
"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# Paramètres physiques
c = 3e8           # vitesse de la lumière
f = 1e9           # fréquence 1 GHz
omega = 2 * np.pi * f

# Figure
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12,6))
for ax in (ax1, ax2):
    ax.set_xlim(-10, 10)
    ax.set_ylim(-10, 10)
    ax.set_aspect("equal")

ax1.set_title("Onde sphérique - Polarisation linéaire")
ax2.set_title("Onde sphérique - Polarisation circulaire")

# Émetteur au centre
ax1.plot(0, 0, "ro", label="Émetteur")
ax2.plot(0, 0, "ro", label="Émetteur")

# Textes
text1 = ax1.text(-9, -9, "", fontsize=9)
text2 = ax2.text(-9, -9, "", fontsize=9)

# Listes pour stocker les objets animés
circles1, arrows1 = [], []
circles2, arrows2 = [], []

# Fonction d’animation
def update(frame):
    global circles1, arrows1, circles2, arrows2
    # Effacer les cercles et flèches précédents
    for c in circles1 + circles2: c.remove()
    for a in arrows1 + arrows2: a.remove()
    circles1, arrows1, circles2, arrows2 = [], [], [], []
    
    # Fronts d'onde sphériques
    radii = np.arange(2, 10, 2) + 0.2*frame
    
    for r in radii:
        intensity = 1/(r**2)
        
        # === Polarisation linéaire (flèches verticales) ===
        circle1 = plt.Circle((0,0), r, color="b", fill=False,
                             lw=2*intensity*10, alpha=0.5)
        ax1.add_patch(circle1)
        circles1.append(circle1)
        # Flèches de champ électrique (verticales)
        for angle in np.linspace(0, 2*np.pi, 12, endpoint=False):
            x, y = r*np.cos(angle), r*np.sin(angle)
            arrow = ax1.arrow(x, y, 0, 0.8*intensity*5,
                              head_width=0.3, head_length=0.3,
                              color="g", alpha=0.7)
            arrows1.append(arrow)
        
        # === Polarisation circulaire (flèches tournantes) ===
        circle2 = plt.Circle((0,0), r, color="b", fill=False,
                             lw=2*intensity*10, alpha=0.5)
        ax2.add_patch(circle2)
        circles2.append(circle2)
        # Flèches de champ électrique (rotation)
        phase = omega*frame*1e-9  # phase pour la rotation
        for angle in np.linspace(0, 2*np.pi, 12, endpoint=False):
            x, y = r*np.cos(angle), r*np.sin(angle)
            # Champ E tourne : composantes sinus/cosinus
            Ex = 0.8*intensity*5*np.cos(phase)
            Ey = 0.8*intensity*5*np.sin(phase)
            arrow = ax2.arrow(x, y, Ex, Ey,
                              head_width=0.3, head_length=0.3,
                              color="g", alpha=0.7)
            arrows2.append(arrow)
    
    text1.set_text("Polarisation linéaire : E vertical")
    text2.set_text("Polarisation circulaire : E tourne")
    
    return circles1 + arrows1 + circles2 + arrows2

ani = animation.FuncAnimation(fig, update, frames=40, interval=200, blit=False)

plt.show()


# ---------- Sauvegarde ----------
ani.save("maxwell_equations.gif", writer="pillow", fps=20)
print("✅ Animation sauvegardée en GIF (maxwell_equations.gif)")
