#-----------------------------------------------------------------------
# Réflexion d'un paquet d'onde sur une interface vide-conducteur
#-----------------------------------------------------------------------
# Renseignements/bugs : Guillaume Dewaele <agreg(at)sci-phy.org>
#-----------------------------------------------------------------------




# Titre

titre = "Portrait de phase de l'oscillateur harmonique"

#-----------------------------------------------------------------------

# Bibliothèques utilisées

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as ani
from itertools import count

#-----------------------------------------------------------------------

# Création du paquet d'onde

# Vitesse de l'animation

speed = 0.05


a=0.3


def x(t):
    return a*np.cos(t)
def y(t):
    return -a*0.7*np.sin(t)
def X(theta):
    return np.sin(theta)
def Y(theta):
    return -np.cos(theta)
def tige(theta):
    s=np.linspace(0,1,100)
    return s*X(theta),s*Y(theta)


# Détection utilisation hors Pyzo

if '__iep__' not in globals() :
    matplotlib.interactive(False)

# Création d'une figure

fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)

t=0
crv1 = ax1.scatter(X(x(t)), Y(x(t)), color ='red', zorder =10,s=100)
crv2 = ax2.scatter(x(t), y(t), color ='red', zorder =10,s=100)

tigex, tigey = tige(x(t))
line1, = ax1.plot(tigex, tigey)
ell=ax2.plot(x(np.linspace(0,10,100)),y(np.linspace(0,10,100)) )

# Décoration

plt.title(titre)
ax1.set_xlim(-1,1)
ax1.set_ylim(-1.25,0.25)
ax1.axis('off')


ax2.set_ylim(-0.5,0.5)
ax2.set_xlim(-0.5,0.5)
ax2.axhline(y=0, color='k')
ax2.axvline(x=0, color='k')
ax2.set_xlabel(r'$x=\theta$')
ax2.set_ylabel(r'$y=d \theta/ dt$')


ax1.set_title('Oscillateur harmonique')
# Animation

def SizeChanged(ax, old=[]) :
    current = [ ax.bbox.width, ax.bbox.height ]
    if old != current :
        old[:] = current
        return True
    return False

def Update(t) :
    crv1.set_offsets([X(x(t)), Y(x(t))])
    crv2.set_offsets([x(t), y(t)])
    tigex, tigey = tige(x(t))
    line1.set_data(tigex, tigey)
    if SizeChanged(plt.gca()) :
        plt.gcf().canvas.draw()
    return [crv1, crv2, line1]

def Init() :
    crv1.set_offsets([X(x(t)),Y(x(t))])
    crv2.set_offsets([x(t), y(t)])
    tigex, tigey = tige(x(t))
    line1.set_data(tigex, tigey)
    return [crv1, crv2, line1]

anim = ani.FuncAnimation(fig, Update, count(0.0, speed), interval=10, blit=True, init_func=Init)

# Détection utilisation hors Pyzo

if '__iep__' not in globals() :
    plt.show()


