% SPCRVDEM	Demonstrate spline curves.
% Copyright (c) 1990-92 by Carl de Boor and The MathWorks, Inc.
                                         % latest change: September 28, 1990
clc;clg;home;echo on
% The various M-files have been set up to deal with piecewise polynomial
% functions having values in R^d, i.e., with curves in d-space. In this mode,
%  d = 2  is most common, as it gives plane curves.

%     Here is an example, in which a spline with  2-dimensional coefficients
% is constructed ....

knots=[1,1:9,9];

coefs=[0 0;1 0;1 1;0 1]; coefs=[coefs;coefs]'
curve=spmak(knots,coefs);

% ... and then plotted:

t=[2:.2:8];values=fnval(curve,t);
plot(values(1,:),values(2,:));
axis([-1 2 -1 2]);axis('square');pause

% Now we also draw the tangent vector to the curve at some points.

pause;                                        % Touch any key to continue

dcurve=fnder(curve);
hold on
t=[3:.4:6];
cv=fnval(curve,t);
cdv=fnval(dcurve,t);
for j=1:length(t),
 plot(cv(1,j)+[0,cdv(1,j)],cv(2,j)+[0,cdv(2,j)])
end
pause
hold off;
pause;                                        % Touch any key to continue

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% I use spline curves extensively in the generation of illustrations in 
% which nothing more than a smooth curve of a certain roughly imagined
% shape is required. For this, the tool box contains a special M-file called
%  spcrv  which is independent of the rest of the setup. Given a sequence
% of points in the plane and, optionally, an order  k , it generates (by
% repeated midpoint knot insertion) the spline curve (of order  k ) whose
% control polygon is specified by the given sequence. The following picture
% shows such a control polygon and the corresponding spline curve of order 3

points=[0 0;1 0;1 1;0 2;-1 1;-1 0;0 -1;0 -2]';
pause;                                        % Touch any key to continue
plot(points(1,:),points(2,:),':');
axis([-2 2 -2 2]), pause

%   That's the control polygon. Now comes the curve which is a smoothed version
% of that polygon.
pause;                                        % Touch any key to continue
hold on
k=3;values=spcrv(points,k);
plot(values(1,:),values(2,:));pause
%  You notice that the curve touches each segment of the control polygon
% at its midpoint, and follows the shape outlined by the control polygon.
%   Raising the order  k  will pull the curve away from the control polygon
% and make it smoother, but also shorter.
%   Here is the same, with  k = 4 :

k=4;value4=spcrv(points,k);
pause;                                        % Touch any key to continue
plot(value4(1,:),value4(2,:),'-.');pause

%  On the other hand, the m-file  cscvn  will interpolate the given points.
% Here is the resulting parametric `natural' cubic spline curve:

fnplt(cscvn(points));pause

%   By adding the point  (.95,-.05)  near  (1,0), the second control point, we 
% can make this curve turn faster there:

[d,np]=size(points);
fnplt(cscvn([points(:,1) [.95;-.05] points(:,2:np)]),'-.');pause

hold off
