% Cartoons of E- and B-fields propagating together along two wavelengths as
% a linearly s- or p-polarized plane wave

clear; close all;

prompt = 's-polarized (S), or p-polarized (P)? ';
str = input(prompt,'s');

myBlue = [0.4 0.44 0.73];

str1 = '-polarized.mp4';
fileName = [str,str1]; % Name of file depends on polarization choice

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

[x,y,z] = sphere(200);

lp = [0.4 0.4 0.7];
LL = 6*pi; % two wavelengths
wavex = -2*LL/3:pi/90:LL/3;
wavey = 0.0*wavex;
xx = [-1 1; 1 -1];
yy = [-1 -1; 1 1];
zz = [0 0; 0 0];

for i = 0:pi/45:4*pi-pi/45
    newplot;
    if (str == 'P') || (str == 'p')
        propAxis = mArrow3([-4*pi 0 0],[0 0 0],'color','k','stemWidth',0.064,...
            'tipWidth',0.14,'facealpha',0.8);
        rotate(propAxis,[0 1 0],20,[0 0 0]);
        hold on;
        
        wavez = 4*sin((wavex) + (-i));
        % p-polarization
        efield = plot3(wavex,wavez,wavey,'color','r', 'LineWidth', 2.5); % E-field
        rotate(efield,[0 1 0],20,[0 0 0]);
        bfield = plot3(wavex,wavey,wavez,'color',myBlue, 'LineWidth', 2.5); % B-field
        rotate(bfield,[0 1 0],20,[0 0 0]);
        
        % Draw surface
        for jj = 1:-0.01:0
            plotcube([1 1 0.01]*LL,[-0.5 -0.5 jj-1-0.01]*LL,(jj^8)/5,'g',0);
        end
        
        % Oscillating electron
        electron = surf(x/3,y/3+2*sin(i),z/3,'EdgeColor','none','FaceColor',myBlue,...
            'FaceLighting','gouraud','DiffuseStrength',1);
        
        % Dot-dashed lines
        plot3([-LL/2 LL/2],[0 0],[0 0],'Color','k','LineWidth',1.6,'LineStyle','-.');
        plot3([0 0],[-LL/4 LL/4],[0 0],'Color','k','LineWidth',1.6,'LineStyle','-.');
        
        % Add dummy lines to stop resizing of viewed volume
        dumLine = plot3([0 0],[-0.1 0.1],[LL LL],'Color','w');
        dumLine.Color(4) = 0.0; % alpha = 0; fully transparent
        dumLine2 = plot3([-2.3*LL/3 1.15*LL/3],[0 0],[0 0],'Color','w');
        dumLine2.Color(4) = 0.0; % alpha = 0; fully transparent
        
    elseif (str == 'S') || (str == 's')
        propAxis = mArrow3([-4*pi 0 0],[0 0 0],'color','k','stemWidth',0.064,...
            'tipWidth',0.14,'facealpha',0.8);
        rotate(propAxis,[0 1 0],10,[0 0 0]);
        hold on;
        
        wavez = 4*sin((wavex) + (-i));
        % s-polarization
        bfield = plot3(wavex,-wavez,wavey,'color',myBlue, 'LineWidth', 2.5); % E-field
        rotate(bfield,[0 1 0],10,[0 0 0]);
        efield = plot3(wavex,wavey,wavez,'color','r','LineWidth', 2.5); % B-field
        rotate(efield,[0 1 0],10,[0 0 0]);
        
        % Draw surface
        for jj = 1:-0.01:0
            plotcube([1 1 0.01]*LL,[-0.5 -0.5 jj-1-0.01]*LL,(jj^8)/5,'g',0);
        end
        
        % Oscillating electron
        electron = surf(x/3+2*sin(i)*sind(10),y/3,z/3+2*sin(i)*cosd(10),'EdgeColor','none','FaceColor',myBlue,...
            'FaceLighting','gouraud','DiffuseStrength',1);
        
        % Dot-dashed lines
        plot3([-LL/2 LL/2],[0 0],[0 0],'Color','k','LineWidth',1.6,'LineStyle','-.');
        plot3([0 0],[0 0],[-LL/4 LL/4],'Color','k','LineWidth',1.6,'LineStyle','-.');
        plot3([-LL/4*sind(10) LL/4*sind(10)],[0 0],[-LL/4*cosd(10) LL/4*cosd(10)],...
            'Color','k','LineWidth',1.6,'LineStyle','-.');
        
        % Add dummy lines to stop resizing of viewed volume
        dumLine = plot3([0 0],[-0.1 0.1],[LL LL],'Color','w');
        dumLine.Color(4) = 0.0; % alpha = 0; fully transparent
        dumLine2 = plot3([-2.3*LL/3 1.15*LL/3],[0 0],[0 0],'Color','w');
        dumLine2.Color(4) = 0.0; % alpha = 0; fully transparent
    end
    axis square;
    axis off;
    axis tight;
    axis equal;
    light('Position',lp,'Style','infinite');
    view(50,25);
    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off;
end

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