% Script to generate a parametric interpolant of the slinky data.
%
% Ian Mitchell, modified from TA code, CPSC 303, 2/10/08.

% Generate the data.
n = 501;
a = 0.3;
w = 50;
h = 0.2;
t = linspace(0, 3*pi, n);

xi = (1 + a * cos(w * t)) .* cos(t);
yi = (1 + a * cos(w * t)) .* sin(t);
zi = -h * t + a * sin(w * t);

figure;
plot3(xi, yi, zi, 'k.');
hold on;
plot3(xi, yi, zi, 'r-');
xlabel('x');  ylabel('y');  zlabel('z');
title('Data for parametric interpolant problem.');
grid on;
pause;

% How many times to oversample the data for a smoother looking plot?
oversample = 5;

% Parameter for the data samples.  Note that we can choose whatever
% parameter vector we want -- we do not have to go from 0 to 1.
tau = linspace(0, 1, n);

% Parameter at which we will plot the interpolants.
tau_many = linspace(0, 1, 5 * (n-1) + 1);

% Get the interpolants on the finely sampled grid.
xi_many=spline(tau, xi, tau_many);
yi_many=spline(tau, yi, tau_many);
zi_many=spline(tau, zi, tau_many);

% Some bounds to make the plots look nice.
minx=min(xi)-0.1;
maxx=max(xi)+0.1;
miny=min(yi)-0.1;
maxy=max(yi)+0.1;
minz=min(zi)-0.1;
maxz=max(zi)+0.1;
minval=min(minx,min(miny,minz))-0.1;
maxval=max(maxx,max(maxy,maxz))+0.1;

% Plot the interpolants against their parameters.
figure;

subplot(3, 1, 1);
plot(tau_many, xi_many, 'r-', 'LineWidth', 2.0);
hold on;
plot(tau, xi, 'k.');
axis([0, 1, minx, maxx]);
ylabel('x');
title('Parametric Interpolation: 2D View');

subplot(3, 1, 2);
plot(tau_many, yi_many, 'g-', 'LineWidth', 2.0);
hold on;
plot(tau, yi, 'k.');
axis([0, 1, miny, maxy]);
ylabel('y');

subplot(3, 1, 3);
plot(tau_many, zi_many, 'b-', 'LineWidth', 2.0);
hold on;
plot(tau, zi, 'k.');
axis([0, 1, minz, maxz]);
xlabel('\tau');
ylabel('z');

% Plot the parametric interpolant in its natural domain.
figure;
plot3(xi_many, yi_many, zi_many, 'b-', 'LineWidth', 2.0);
hold on;
plot3(xi, yi, zi, 'k.');
xlabel('x');
ylabel('y');
zlabel('z');
axis([minx, maxx, miny, maxy, minz, maxz]);
title('Parametric Interpolation: 3D View');
grid on;
