% Program to plot reflection of incident parallel rays on a parabolic
% surface coming to a focus at a point

clear; close all;

aconst = 5.0; % constant in x = ay^2
nR = 0.99;

vid = VideoWriter('parabolaFocussingTransmission.mp4','MPEG-4');
vid.FrameRate = 30;
vid.Quality = 100;
open(vid);
figure('ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]);
ycoord = (-0.2:0.00999999:0.2)'; % range of y-values of incoming horizontal beam
xcoord = aconst.*ycoord.^2; % x-value for a given y-value on surface of parabola

% Max x-value
grad = (4*aconst*xcoord(size(ycoord,1)))^-0.5;
theta = atan(grad);
thetaP = acos(nR*cos(theta));
delta = thetaP - theta;
xmax = xcoord(size(ycoord,1)) + ycoord(size(ycoord,1))/tan(delta);


for i = 1:size(ycoord,1)
    hold off;
    grad = (4*aconst*xcoord(i))^-0.5;
    theta = atan(grad);
    thetaP = acos(nR*cos(theta));
    delta = thetaP - theta;
    xf = xcoord(i) + abs(ycoord(i))/tan(delta);
    fill([xcoord' -0.5 -0.5 min(xcoord)],[ycoord' 0.2 -0.2 -0.2],[0.5 0.5 0.5],'LineStyle','none');
    hold on
    plot(xcoord,ycoord,'color',[0,0,0], 'LineWidth', 2.0); % plot parabola
    axis([-0.7 xmax -(xmax/2-0.05)/20 (xmax/2+0.05)/20]);
    axis square % square plot
    axis off
    hold on;
    xchord = [-0.7 xcoord(i) xf]; % x-coordinates of ray path between foci
    ychord = [ycoord(i) ycoord(i) 0]; % y-coordinates of ray path between foci
    plot(xchord,ychord, 'color',[1 0 0], 'Linewidth', 2.0); % reflection off upper half
    plot(xchord,-ychord, 'color',[1 0 0], 'Linewidth', 2.0); % reflection off lower half
    str1 = 'n = ';
    str2 = num2str(nR,'% 4.3f');
    strTot = [str1,str2];
    hText = text(xmax*.8, -0.16, strTot, 'FontSize',14);
    hText = text(xmax*.8, -0.195, 'x = 5y^2', 'FontSize',14);
    
    % Store the frame
    frame = getframe(gcf);
    writeVideo(vid,frame);
end
hold off;
% Output the movie as an mpg file
close(vid);
