#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  3 20:55:59 2018

One-slit phasor animation.
The blue lines are the phasors from the slits and the orange
line is the total. It's length is proportional to the brightness.
With only one slit the first phasor does rotate.
It also plots (in green) the total intensity at each point in the pattern.
NOTE that in all these animations the slits are treated as infinitesimal
so that there is no single-slit modulation of the pattern.

@author: bcollett
"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

minmax = 2.5

fig, ax = plt.subplots()
ax.set_xlim(-1.5, minmax+2)
ax.set_ylim(-minmax, minmax)
ax.set_title("Animated Phasor Plot for One Slit")
dtheta = 0.008

y = np.zeros(5)+ 100
x=np.array([0,1,2,3,4])
y1 = np.zeros(2)+100
x1=np.array([0,4])
#x2=np.zeros(10000)
#x2[2]=0.1
#y2=np.zeros(10000)-3
x2=[]
y2=[]
line1, = ax.plot(x, y,'o-')
line2, = ax.plot(x1, y1)
line3, =ax.plot(x2,y2)


def init():  # only required for blitting to give a clean slate.
    line1.set_xdata([0,1])
    line1.set_ydata([0,0])
    line2.set_xdata([0,0])
    line2.set_ydata([0,1])
    line3.set_xdata(x2)
    line3.set_ydata(y2)
    return line1,line2,line3


def animate(i):
    theta = dtheta * i
    c1 = np.cos(theta)
    s1 = np.sin(theta)
    ctot = c1
    stot = s1
    x2.append(theta/4-1.5)
    y2.append((ctot*ctot+stot*stot))
#    x2[i+2]=theta/3
#    y2[i+2]=np.sqrt(ctot*ctot+stot*stot)-3
    line1.set_xdata([0, ctot])
    line1.set_ydata([0, stot])
    line2.set_xdata([0, ctot])
    line2.set_ydata([0, stot])
    line3.set_xdata(x2)
    line3.set_ydata(y2)
    return line1,line2,line3


ani = animation.FuncAnimation(
   fig, animate, init_func=init, interval=1, blit=False, save_count=50)

# To save the animation, use e.g.
#
# ani.save("movie.mp4")
#
# or
#
# from matplotlib.animation import FFMpegWriter
# writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
# ani.save("movie.mp4", writer=writer)

plt.show()