#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr  7 18:37:02 2026

@author: eddiesaudrais
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button

# Paramètres initiaux
B0 = 1.0
n0 = 1
m0 = 1
phi0 = 0.0  # en degrés
f0 = 1.0

t = np.linspace(0, 1, 2000)

# Création de la figure
fig, ax = plt.subplots(figsize=(6, 6))
plt.subplots_adjust(left=0.25, bottom=0.35)

# Courbe initiale
x = np.cos(2 * np.pi * n0 * f0 * t)
y = B0 * np.cos(2 * np.pi * m0 * f0 * t + np.deg2rad(phi0))
(line,) = ax.plot(x, y, lw=2)

ax.set_aspect('equal')
ax.set_title("Courbes de Lissajous, $X=\cos(n\omega_0t)$ et $Y=B\cos(m\omega_0t+\phi)$")
ax.grid(True)
ax.set_xlabel("$X$")
ax.set_ylabel("$Y$")

# Axes pour sliders
ax_B = plt.axes([0.25, 0.25, 0.65, 0.03])
ax_n = plt.axes([0.25, 0.20, 0.65, 0.03])
ax_m = plt.axes([0.25, 0.15, 0.65, 0.03])
ax_phi = plt.axes([0.25, 0.10, 0.65, 0.03])

# Sliders
slider_B = Slider(ax_B, 'B', 0.0, 1.0, valinit=B0, valstep=0.02)
slider_n = Slider(ax_n, 'n', 1, 10, valinit=n0, valstep=1)
slider_m = Slider(ax_m, 'm', 1, 10, valinit=m0, valstep=1)
slider_phi = Slider(ax_phi, 'φ (°)', 0, 360, valinit=phi0, valstep=1)

# Fonction de mise à jour
def update(val):
    B = slider_B.val
    n = int(slider_n.val)
    m = int(slider_m.val)
    phi = np.deg2rad(slider_phi.val)
    
    x = np.cos(2 * np.pi * n * f0 * t)
    y = B * np.cos(2 * np.pi * m * f0 * t + phi)
    
    line.set_xdata(x)
    line.set_ydata(y)
    
    fig.canvas.draw_idle()

# Lier sliders à la mise à jour
slider_B.on_changed(update)
slider_n.on_changed(update)
slider_m.on_changed(update)
slider_phi.on_changed(update)

# Bouton reset
reset_ax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(reset_ax, 'Reset')

def reset(event):
    slider_B.reset()
    slider_n.reset()
    slider_m.reset()
    slider_phi.reset()

button.on_clicked(reset)

plt.show()