function P3()
% Checks out E2Pdist

% Sample Ellipse:
A = 4; B = 5; C = 7; D = -15; E = 8; F = -30;
% Set up contour plot data...
n = 100;
a = -6;  b = 8; x = linspace(a,b,n);
c = -10; d = 5; y = linspace(c,d,n);
Fvals = zeros(n,n);
for i=1:n
    for j=1:n
        Fvals(j,i) = A*x(i)^2 + B*x(i)*y(j) + C*y(j)^2 + D*x(i) + E*y(j) + F;
    end
end

close all
% Example 1.
x0 = 8; y0 = 2;
[xStar,yStar,dist] = E2Pdist(A,B,C,D,E,F,x0,y0);
figure
contour(x,y,Fvals,[0 0],'k'); axis([-4 10 -10 4]); axis equal; grid on; hold on
plot(x0,y0,'*r',xStar,yStar,'*r',[x0 xStar],[y0 yStar],'r')
title(sprintf('xStar = %20.15f, yStar = %20.15f, dist = %20.15f',xStar,yStar,dist))

% Example 2.
x0 = 2; y0 = 0;
[xStar,yStar,dist] = E2Pdist(A,B,C,D,E,F,x0,y0);
figure
contour(x,y,Fvals,[0 0],'k'); axis([-4 10 -10 4]); axis equal; grid on; hold on
plot(x0,y0,'*r',xStar,yStar,'*r',[x0 xStar],[y0 yStar],'r')
title(sprintf('xStar = %20.15f, yStar = %20.15f, dist = %20.15f',xStar,yStar,dist))

% Example 3.
x0 = -4; y0 = -2;
[xStar,yStar,dist] = E2Pdist(A,B,C,D,E,F,x0,y0);
figure
contour(x,y,Fvals,[0 0],'k'); axis([-4 10 -10 4]); axis equal; grid on; hold on
plot(x0,y0,'*r',xStar,yStar,'*r',[x0 xStar],[y0 yStar],'r')
title(sprintf('xStar = %20.15f, yStar = %20.15f, dist = %20.15f',xStar,yStar,dist))

% Example 4.
x0 = 2; y0 = -8;
[xStar,yStar,dist] = E2Pdist(A,B,C,D,E,F,x0,y0);
figure
contour(x,y,Fvals,[0 0],'k'); axis([-4 10 -10 4]); axis equal; grid on; hold on
plot(x0,y0,'*r',xStar,yStar,'*r',[x0 xStar],[y0 yStar],'r')
title(sprintf('xStar = %20.15f, yStar = %20.15f, dist = %20.15f',xStar,yStar,dist))
