constructing spline curves in 2d and 3d -凯发k8网页登录
this example shows how to use the cscvn
command 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 per column.
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 = [];
connecting the points
next, construct the curve using the cscvn
command and plot it using fnplt
.
hold on fnplt(cscvn(xy),'r',2) hold off
you could also use the getcurve
command 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.
npts = 13; t = linspace(0,8*pi,npts); 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 = []; box on
connecting the points
here is the 3d spline curve through these points provided by cscvn
. by appending the first point to the end of the list, we get a smooth closed curve.
hold on fnplt(cscvn(xyz(:,[1:end 1])),'r',2) hold off