% Cartoon of expanding dipole wavefronts
clear; close all;

figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
vid = VideoWriter('expandingDipoleWavefronts.mp4','MPEG-4');
vid.Quality = 100;
vid.FrameRate = 30;
open(vid);
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7);

LL = 16;
wavey = 0:LL/1000:LL;
wavex = 0.0*wavey;

u = 0:pi/50:2*pi; % phi azimuthal
v = 0:pi/50:2*pi; % theta polar
[u,v]=meshgrid(u,v);
N = 400;

lp = [0.4 -0.4 0.7];
ec = [0.25 0.28 0.5]; % myBlue
fc = [1.0 0.83 0]; % gold


for t = 0:1:N
    hold off;
    x = (t/50)*(1 + cos(v)).*cos(u);
    y = (t/50)*(1 + cos(v)).*sin(u);
    z = (t/50)*sin(v);
    
    fa = 0.03+0.10*(N - t)/N;
    ea = 0.05+0.20*(N - t)/N;
    surf(x,y,z,'FaceAlpha',fa,'FaceColor',fc,'EdgeAlpha',ea,'EdgeColor',ec,'FaceLighting','gouraud','DiffuseStrength',1);
    light('Position',lp,'Style','infinite');
    view(35,25);
    hold on;
    % oscillating electron sphere
    r = 0.32;
    osc = 0.5*sin(2*pi*t/(N/4));
    [x,y,z] = sphere; surf(x*r,y*r,z*r+osc,'FaceColor',ec,'LineStyle','none');
    % incoming E-field
    wavez = -sin(2*pi*(4*wavey/LL) + (8*pi*t/N));
    plot3(wavex,-wavey,wavez,'color','red', 'LineWidth', 2.0);
    
    if (t > N/4)
        hold on;
        x2 = ((t-N/4)/50)*(1 + cos(v)).*cos(u);
        y2 = ((t-N/4)/50)*(1 + cos(v)).*sin(u);
        z2 = ((t-N/4)/50)*sin(v);
        
        fa = 0.05+0.14*(N - (t-N/4))/N;
        ea = 0.07+0.28*(N - (t-N/4))/N;
        surf(x2,y2,z2,'FaceAlpha',fa,'FaceColor',fc,'EdgeAlpha',ea,'EdgeColor',ec,'FaceLighting','gouraud','DiffuseStrength',1);
        %light('Position',lp,'Style','infinite');
    end
    if (t > N/2)
        hold on;
        x2 = ((t-N/2)/50)*(1 + cos(v)).*cos(u);
        y2 = ((t-N/2)/50)*(1 + cos(v)).*sin(u);
        z2 = ((t-N/2)/50)*sin(v);
        
        fa = 0.05+0.20*(N - (t-N/2))/N;
        ea = 0.07+0.40*(N - (t-N/2))/N;
        surf(x2,y2,z2,'FaceAlpha',fa,'FaceColor',fc,'EdgeAlpha',ea,'EdgeColor',ec,'FaceLighting','gouraud','DiffuseStrength',1);
        %light('Position',lp,'Style','infinite');
    end
    if (t > 3*N/4)
        hold on;
        x2 = ((t-3*N/4)/50)*(1 + cos(v)).*cos(u);
        y2 = ((t-3*N/4)/50)*(1 + cos(v)).*sin(u);
        z2 = ((t-3*N/4)/50)*sin(v);
        
        fa = 0.05+0.25*(N - (t-3*N/4))/N;
        ea = 0.07+0.50*(N - (t-3*N/4))/N;
        surf(x2,y2,z2,'FaceAlpha',fa,'FaceColor',fc,'EdgeAlpha',ea,'EdgeColor',ec,'FaceLighting','gouraud','DiffuseStrength',1);
        %light('Position',lp,'Style','infinite');
    end
    axis square
    axis off
    
    xlim([-LL LL]);
    ylim([-LL LL]);
    zlim([-LL LL]);
    
    % Store the frame
    frame = getframe(gcf);
    writeVideo(vid,frame);
end
%hold off;
% Output the movie as an mpg file
close(vid);