%% Plot ellipse
close all

% Input params.
a = 1.2; % Semi-major axis (longer axis)
b = 1; % Semi-minor axis (shorter axis)

if (b > a)
    disp("a must be >b!");
    return;
end

% Parametric definition
t = linspace(0, 2*pi, 1000);
x = a*cos(t);
y = b*sin(t);

% Plot the ellipse.
plot(x,y);
hold on
grid on
axis equal

% Plot the foci
c = sqrt(a^2-b^2);
F1 = -c;
F2 =  c;

plot(F1, 0, 'o');
plot(F2, 0, 'o');

% Compute eccentricity
e = sqrt(1-(b/a)^2);
title(["Eccentricity e = ", num2str(e)]); 


% Plot all types of conic section
figure 
% Circle
x = a*cos(t);
y = a*sin(t);
plot(x,y);
hold on
grid on
% Center
plot(0, 0, 'o');
%Ellipse
x = a*cos(t);
y = b*sin(t);
plot(x,y);
% Foci
plot(F1, 0, '*r');
plot(F2, 0, '*r');
% Directricies
x = a/e; 
plot([x x], [-b b], 'k');
plot([-x -x], [-b b], 'k');
% Latus Rectum (chord parallel to directrix, through focus)


%Parabola
t = -1:0.001:1;
F = a; % Single focus, defined to be a.
x = a*t.^2;
y = 2*a*t;
plot(x,y, 'k');

% Hyperbola
x = a*cosh(t);
y = b*sinh(t);
plot(x, y);
plot(-x, y);
% Plot foci
c = a^2+b^2;
plot(c, 0, '*');
plot(-c, 0, '*');


axis equal