% Cartoon movie of electric-field vectors induced by a moving charge
% This version moves the electron perpendicularly out of the plane of the field grid

clear; close all;
vid = VideoWriter('atomicFormFactor1.mp4','MPEG-4');
vid.Quality = 100;
frmRate = 30;
vid.FrameRate = frmRate;
open(vid);
figure('units','pixels','position',[0 0 3840 2160],'ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]); % white background = [1 1 1]
set(gca,'linewidth',7);
lp = [2 1 1];
myBlue = [0.5 0.55 0.88];
myBlue2 = [0.61 0.67 0.92];
lightRed = [1 0.5 0.5];
lightBlue = [0.5 0.5 1];
pos1 = [-0.1 -0.2 1.2 1.5];

lambda = 1; % Wavelength in terms cartoon dimensions
x = -4:0.01:0; % x-coordinates of first incoming beam of radiation
y = 0*x; % y-coordinates...
z = 0*x; % z-coordinates...
% Oscillating index between 1 and size of coordinates vectors
colIndex = 1+floor(size(x,2)/2*(1+sin(2*pi*x/lambda)));

delx = -0.37; % x-offset in position of second volume element dV2 w.r.t. first volume element dV1 at (0,0,0)
dely = 0.3; % y-offset...

% Coordinates of second incident beam of radiation hitting second dV
x2 = -4:0.01:0+delx;
y2 = 0*x2+dely;
z2 = 0*x2;
colIndex2 = 1+floor(size(x2,2)/2*(1+sin(2*pi*x2/lambda)));

% Coordinates of scattered radiation from first incident beam at dV1
x3 = max(x):0.01:max(x)+4;
y3 = 0*x3;
z3 = 0*x3;
colIndex3 = 1+floor(size(x3,2)/2*(1+sin(2*pi*x/lambda)));

% Coordinates of scattered radiation from second incident beam at dV2
x4 = max(x2):0.01:max(x2)+4;
y4 = 0*x4+dely;
z4 = 0*x4;
colIndex4 = 1+floor(size(x4,2)/2*(1+sin(2*pi*(x+delx)/lambda)));

% Custom color maps between red and blue for each beam
blueRed = customcolormap([0 0.5 1],...
    {'#ff0000', '#000000', '#0000ff'},size(x,2)+1);
blueRed = flip(blueRed);
blueRed2 = customcolormap([0 0.5 1],...
    {'#ff0000', '#000000', '#0000ff'},size(x2,2)+1);
blueRed2 = flip(blueRed2);
blueRed3 = customcolormap([0 0.5 1],...
    {'#ff0000', '#000000', '#0000ff'},size(x3,2)+1);
blueRed3 = flip(blueRed3);
blueRed4 = customcolormap([0 0.5 1],...
    {'#ff0000', '#000000', '#0000ff'},size(x4,2)+1);
blueRed4 = flip(blueRed4);

subplot('position',pos1);
newplot
for theta = 0:0.5:100
    edens = randn(3,100000); % Gaussian distribution of points representing electron-density cloud

    scatter3(edens(1,:),edens(2,:),edens(3,:),8,'g','filled','o',...
        'MarkerEdgeAlpha',0,'MarkerFaceAlpha',0.1);
    hold on
    % Plot two incident beams on dV1 and dV2
    scatter3(x,y,z,14,blueRed(colIndex,:),'filled');
    scatter3(x2,y2,z2,14,blueRed2(colIndex2,:),'filled');
    % Redefine coordinates of first scattered beam according to scattering
    % angle
    x3new = x3*cosd(theta);
    y3new = x3*sind(theta);
    % Plot first scattered beam off dV1
    scatter3(x3new,y3new,z3,14,blueRed3(colIndex3,:),'filled');
    % Redefine coordinates of second scattered beam according to scattering
    % angle
    x4new = (x4-delx)*cosd(theta)+delx;
    y4new = (x4-delx)*sind(theta)+dely;
    % Plot second scattered beam off dV1
    scatter3(x4new,y4new,z4,10,blueRed4(colIndex4,:),'filled');

    % Plot dot-dashed lines at peaks and troughs of the two scattered beams
    line1 = plot3([1.75*lambda 1.75*lambda],[-0.25 0.25],[0 0],...
        'LineStyle','-.','LineWidth',2,'Color',lightRed);
    rotate(line1,[0 0 1],theta,[0 0 0])
    line1 = plot3([1.75*lambda 1.75*lambda],[-0.25+dely 0.25+dely],...
        [0 0],'LineStyle','-.','LineWidth',2,'Color',lightRed);
    rotate(line1,[0 0 1],theta,[delx dely 0])
    line1 = plot3([1.25*lambda 1.25*lambda],[-0.25 0.25],[0 0],...
        'LineStyle','-.','LineWidth',2,'Color',lightBlue);
    rotate(line1,[0 0 1],theta,[0 0 0])
    line1 = plot3([1.25*lambda 1.25*lambda],[-0.25+dely 0.25+dely],...
        [0 0],'LineStyle','-.','LineWidth',2,'Color',lightBlue);
    rotate(line1,[0 0 1],theta,[delx dely 0])

    % Plot the two volume elements dV1 and dV2
    [V,F] = platonic_solid(2,0.1);
    patch('Faces',F,'Vertices',V,'FaceColor','b','FaceAlpha',0,'EdgeColor','w','LineWidth',2);
    [V2,F2] = platonic_solid(2,0.1);
    patch('Faces',F,'Vertices',V,'FaceColor','b','FaceAlpha',0,'EdgeColor','w','LineWidth',2);
    V2(:,1) = V2(:,1)+delx;
    V2(:,2) = V2(:,2)+dely;
    patch('Faces',F2,'Vertices',V2,'FaceColor','b','FaceAlpha',0,'EdgeColor','w','LineWidth',2);

    axis equal
    axis off
    set(gca, 'Projection','perspective');
    set(gca,'View',[0,90]);
    set(gcf,'Color','k');
    xlim([-5 5]);
    ylim([-5 5]);
    zlim([-4 4]);

    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off
end
% % Output the movie as an mpg file
close(vid);

