Main Content

Constructing Spline Curves in 2D and 3D

This example shows how to use thecscvncommand from Curve Fitting Toolbox™ to construct cubic spline curves in two and three dimensions.

Selecting the Points

This example will show how to draw a smooth curve through a list of points, in the order in which they occur. First, we select some random points in the plane, and store them in a matrix, one point percolumn.

npts = 10; xy = [randn(1,npts); randn(1,npts)]; plot(xy(1,:),xy(2,:),'ro','LineWidth',2); text(xy(1,:), xy(2,:),[repmat(' ',npts,1), num2str((1:npts)')]) ax = gca; ax.XTick = []; ax.YTick = [];

Figure contains an axes object. The axes object contains 11 objects of type line, text.

Connecting the Points

Next, construct the curve using thecscvncommand and plot it usingfnplt.

holdonfnplt(cscvn(xy),'r',2) holdoff

Figure contains an axes object. The axes object contains 12 objects of type line, text.

You could also use thegetcurvecommand if you wanted to input the list of points interactively.

3-D Spline Curves

It's just as easy to create spline curves in three dimensions. This time, we'll do something less random. First, we generate the points.

《不扩散核武器条约》= 13;t = linspace(0 8 *π,《不扩散核武器条约》);z = linspace(-1,1,npts); omz = sqrt(1-z.^2); xyz = [cos(t).*omz; sin(t).*omz; z]; plot3(xyz(1,:),xyz(2,:),xyz(3,:),'ro','LineWidth',2); text(xyz(1,:),xyz(2,:),xyz(3,:),[repmat(' ',npts,1), num2str((1:npts)')]) ax = gca; ax.XTick = []; ax.YTick = []; ax.ZTick = []; boxon

Figure contains an axes object. The axes object contains 14 objects of type line, text.

Connecting the Points

Here is the 3D spline curve through these points provided bycscvn. By appending the first point to the end of the list, we get a smoothclosedcurve.

holdonfnplt(cscvn(xyz(:,[1:end 1])),'r',2) holdoff

Figure contains an axes object. The axes object contains 15 objects of type line, text.