% Program to plot reflection of rays originating at one of the foci of an
% ellipse. The ellipse ratio a/b is an input parameter 

clear; close all;

prompt = 'Ratio of major to minor axes [>1; default 5]: ';
aob = input(prompt);
if isempty(aob)
    aob = 5;
end

aaxis = 1.0; % semimajor axis
baxis = aaxis/aob;  % semiminor axis
str1 = 'ellipseFocussing_a2b_';
str2 = num2str(aob);
str3 = '.mp4';
strTot = [str1,str2,str3];
vid = VideoWriter(strTot,'MPEG-4');
vid.FrameRate = 60;
vid.Quality = 100;
open(vid);
figure('ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]);
theta = (-pi:pi/200:pi)'; % causes the chords to make one full period forwards/backwards

xcoord = (cos(theta)).*aaxis;
ycoord = baxis.*(1 - xcoord.^2./aaxis^2).^0.5;
foci = (aaxis^2 - baxis^2)^0.5;

% Plot

for i = 1:size(xcoord,1)
    hold off;
    plot(xcoord,ycoord,'color',[0,0,0], 'LineWidth', 2.0); % top half of ellipse
    axis([-aaxis-0.1 aaxis+0.1 -aaxis-0.1 aaxis+0.1]);
    axis square % square plot
    axis equal
    axis off
    hold on;
    plot(xcoord,-ycoord,'color',[0,0,0], 'LineWidth', 2.0); % bottom half of ellipse
    xchord = [-foci xcoord(i) foci]; % x-coordinates of ray path between foci
    ychord = [0 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
    % Store the frame
    frame = getframe(gcf);
    writeVideo(vid,frame);
end
hold off;
% Output the movie as an mpg file
close(vid);
