# Prog-02: Beautiful Parametric Equations
# Somchai P.
# Animated version

import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#------------------------------------
def setup_T(min_t, max_t, dt):
    T = []; t = min_t
    while t <= max_t:             
        T.append(t)
        t += dt
    if t != max_t: T.append(max_t)
    return T
#------------------------------------
def plot(x, y, min_t, max_t, dt):
    # adapt from https://stackoverflow.com/questions/28074461
    T = setup_T(min_t, max_t, dt)
    X = [x(t) for t in T] 
    Y = [y(t) for t in T]
    fig, ax = plt.subplots()
    line, = ax.plot(X, Y, color='blue')    
    ani = animation.FuncAnimation(fig, update, len(X),
                                  fargs=[X, Y, line],
                                  interval=25,
                                  repeat=False, blit=True)
    plt.show()

def update(k, X, Y, line):
    line.set_data(X[:k], Y[:k])
    return (line,)
#====================================

def x(t):
    xt = 16*math.sin(t)**3
    return xt

def y(t):
    yt = 13*math.cos(t) - 5*math.cos(2*t) - 2*math.cos(3*t) - math.cos(4*t)
    return yt

plot(x, y, 0, 2*math.pi, 0.1)
