% Cartoons of E- and B-fields propagating together along two wavelengths as
% a linearly polarized plane wave, or different light polarizations for the
% E-field only

clear; close all;

display('Choose one of the following: E & B together (E), lin horiz (H), lin vert (V),');
prompt = 'lin tilted (T), left circ (L), right circ (R)? ';
str = input(prompt,'s');

myBlue = [0.4 0.44 0.73];

str1 = 'EMpropagation_';
str3 = '.mp4';
fileName = [str1,str,str3]; % Name of file depends on polarization choice
vid = VideoWriter(fileName,'MPEG-4');
vid.Quality = 100;
vid.FrameRate = 30;
open(vid);
figure('ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7);
set(gca, 'Projection','perspective');

lp = [0.4 0.4 0.7];
LL = 4*pi; % two wavelengths
wavex = 0:pi/90:LL;
wavey = 0.0*wavex;

for i = 0:pi/45:4*pi
    newplot;
    mArrow3([0 0 0],[4*pi 0 0],'color','k','stemWidth',0.064,'tipWidth',0.14,'facealpha',0.8); % conv current
    hold on;
    
    if (str == 'E') || (str == 'e')
        wavez = 4*sin((wavex) + (-i));
        % Horizontal E-field
        plot3(wavex,wavez,wavey,'color','red', 'LineWidth', 2.5); % E-field
        % Vertical B-field
        plot3(wavex,wavey,wavez,'color',myBlue, 'LineWidth', 2.5); % B-field
    elseif (str == 'H') || (str == 'h')
        % Horizontal polarization
        wavez = 4*sin((wavex) + (-i));
        plot3(wavex,wavez,wavey,'color','red', 'LineWidth', 2.5); % E-field
    elseif (str == 'V') || (str == 'v')
        % Vertical polarization
        wavez = 4*sin((wavex) + (-i));
        plot3(wavex,wavey,wavez,'color','red', 'LineWidth', 2.5); % E-field
    elseif (str == 'L') || (str == 'l')
        % Circular left polarization
        wavez = 4*sin((wavex) + (-i));
        wavey = 4*cos((wavex) + (-i));
        plot3(wavex,wavey,wavez,'color','red', 'LineWidth', 2.5); % E-field
    elseif (str == 'R') || (str == 'r')
        % Circular right polarization
        wavez = -4*sin((wavex) + (-i));
        wavey = -4*cos((wavex) + (-i));
        plot3(wavex,wavez,wavey,'color','red', 'LineWidth', 2.5); % E-field
    elseif (str == 'T') || (str == 't')
        % Tilted linear polarization
        wavey = 3*sin((wavex) + (-i));
        wavez = 3*sin((wavex) + (-i));
        plot3(wavex,wavey,wavez,'color','red', 'LineWidth', 2.5); % E-field
    end
    axis square;
    axis off;
    light('Position',lp,'Style','infinite');
    xlim([0 LL]);
    ylim([-LL/2 LL/2]);
    zlim([-LL/2 LL/2]);
    view(50,25);
    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off;
end

% Output the movie as an mpg file
close(vid);