echo on
%SPCRVDEM Spline curve demo.

%   latest change: September 28, 1990
%   cb :  9 may '95 (use .' instead of '); 14oct96; 8aug97
%   Copyright 1987-2000 by C. de Boor and The MathWorks, Inc.
%   $Revision: 1.10 $

home, close, 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];

temp = [ 0 0; 1 0; 1 1; 0 1 ]; coefs = [temp;temp].';
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 equal, grid off
title('a spline curve'), pause

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

pause                                         % Touch any key to continue

t = [3:.4:6]; lt = length(t);
cv = fnval( curve, t );
cdv = cv + fnval( fnder(curve), t );
temp = NaN; temp = temp(1,ones(1,lt));
hold on
plot(reshape([cv(1,:);cdv(1,:);temp],3*lt,1),...
     reshape([cv(2,:);cdv(2,:);temp],3*lt,1))
xlabel('... and its tangent vector at selected points.')
               pause                            % Touch any key to continue
hold off, clf

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

% 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,:),'k'), axis([-2 2 -2.1 2.2]),  grid off
title('control polygon')
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,:),'r','linew',1.5)
xlabel(' ... and the corresponding quadratic spline curve'), 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,:),'b','linew',2)

xlabel(' ... and the corresponding spline curves, quadRatic and cuBic'), 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), 'g',1.5 )

xlabel(' ... and now with the interpolating spline curve (in green) added')
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) ]), 'm',1.5)
xlabel([' ... and now the interpolating curve (magenta) is made to ',...
      'turn faster by addition of another point'])
pause

hold off, echo off, close
