import numpy as np, matplotlib.pyplot as plt, matplotlib.animation as animation, time
from numpy import pi, sin, cos, exp, log

t0 = time.process_time()

def til_intervall(x_punkt,start=-np.pi,stop=np.pi):
    intervall = stop-start
    punkt_i_intervall = x_punkt - (intervall) * ((x_punkt + (intervall/2)) // (intervall))

    return punkt_i_intervall

def fourier(x,N=1):
    f = (pi**2)/6
    for n in range(1,N):
        a = (2/((n**2)))*((-1)**n)
        b = (-pi/n)*((-1)**n) + (2/(pi*n**3))*(((-1)**n)-1)
        f += a*cos(n*x) + b*sin(n*x)

    return f

def fourier2(x,N=1):
    f = 0
    for n in range(1,N):
        a = (-4/(n*pi)**3)*((-1)**n -1)
        f += a * sin(n*pi*x)
    return f

def fourier3(x,N=1):
    f=0.75
    for n in range(1,N):
        a = -4/(n*pi)**2 * ((-1)**n - cos(n*pi/2))
        f += a*cos(n*pi*x/2)
    return f

def f_func(t):
    t = til_intervall(t,start=-1, stop = 1)
    if t >= 0:
        return t-t**2
    return t+t**2

def funkyfunc(x):
    x = til_intervall(x,start=-2,stop=2)
    if x < -1:
        return x+2
    if x < 1:
        return 1
    return 2-x

def bolge(x,t):
    return 0.5*(exp(-(x+t)**2) + exp(-(x-t)**2))

if __name__ == '__main__':

    bolgefunk = np.vectorize(bolge)

    x_list = np.linspace(-100,100,1000)
    t_list = np.linspace(0,10,100)

    y_list = np.zeros((100,1000))

    for i in range(100):
        y_list[i] = bolgefunk(x_list,t_list[i])

    print(time.process_time()-t0)

    fig, ax = plt.subplots()
    k=0
    a = 1

    def animate(i):
        global k,a
        if k >= 0:
            try:
                y = y_list[k]
            except IndexError:
                a = -1
                k = len(y_list) - 1
                y = y_list[k]
        else:
            a = 1
            k += a
            y = y_list[k]

        k+=a

        ax.clear()
        plt.plot(x_list,y)

        plt.ylim(0,1)
        plt.xlim(-10,10)


    ani = animation.FuncAnimation(fig, animate, frames=360, interval=100)

    plt.show()




