📄 spcrvdem.html
字号:
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>More Spline Curves</title> <meta name="generator" content="MATLAB 7.1"> <meta name="date" content="2005-07-27"> <meta name="m-file" content="spcrvdem"> <link rel="stylesheet" type="text/css" href="../../matlab/demos/private/style.css"> </head> <body> <div class="header"> <div class="left"><a href="matlab:edit spcrvdem">Open spcrvdem.m in the Editor</a></div> <div class="right"><a href="matlab:echodemo spcrvdem">Run in the Command Window</a></div> </div> <div class="content"> <h1>More Spline Curves</h1> <introduction> <p>Use of SPMAK, SPCRV or CSCVN to generate spline curves.</p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">A simple spline curve</a></li> <li><a href="#2">Add some tangent vectors</a></li> <li><a href="#3">A word of caution</a></li> <li><a href="#4">A remedy</a></li> <li><a href="#5">SPCRV: control polygon ...</a></li> <li><a href="#6">... and corresponding spline curve</a></li> <li><a href="#8">Raising the order</a></li> <li><a href="#9">CSCVN</a></li> </ul> </div> <h2>A simple spline curve<a name="1"></a></h2> <p>The Spline Toolbox can handle v e c t o r - v a l u e d splines. A d-vector valued univariate spline provides a curve in d-space. In this mode, d = 2 is most common, as it gives plane curves. </p> <p>Here is an example, in which a spline with 2-dimensional coefficients is constructed and plotted.</p><pre class="codeinput">knots = [1,1:9,9];curve = spmak( knots, repmat([ 0 0; 1 0; 1 1; 0 1 ], 2,1).' );t = linspace(2,8,121); values = fnval(curve,t);plot(values(1,:),values(2,:),<span class="string">'linew'</span>,2)axis([-.2 1.2 -.2 1.2]), axis <span class="string">equal</span>, grid <span class="string">off</span>title(<span class="string">'A spline curve'</span>)</pre><img vspace="5" hspace="5" src="spcrvdem_01.png"> <h2>Add some tangent vectors<a name="2"></a></h2> <p>We also draw the tangent vector to the curve at some points.</p><pre class="codeinput">t = 3:.4:6.2; lt = length(t);cv = fnval( curve, t );cdv = fnval( fnder(curve), t );hold <span class="string">on</span>quiver(cv(1,:),cv(2,:), cdv(1,:),cdv(2,:))xlabel(<span class="string">'... and its tangent vector at selected points.'</span>)hold <span class="string">off</span></pre><img vspace="5" hspace="5" src="spcrvdem_02.png"> <h2>A word of caution<a name="3"></a></h2> <p>You may have noticed that, in this example, I did not use FNPLT to plot the curve, but instead plotted some point on the curve obtained by FNVAL: t = linspace(2,8,121); values = fnval(curve,t); plot(values(1,:),values(2,:),'linew',2) Using FNPLT directly with this particular curve gives the red curve above. The explanation? The spline is of order 4, yet the end knots in the knot sequence knots = [1,1:9,9]; have only multiplicity 2. Therefore, all the B-splines of order 4 for this knot sequence are 0 at the endpoints of the basic interval. This makes the curve start and stop at (0,0). </p><pre class="codeinput">hold <span class="string">on</span>fnplt(curve,<span class="string">'r'</span>,.5)xlabel(<span class="string">'... and its plot (red) via FNPLT.'</span>)hold <span class="string">off</span></pre><img vspace="5" hspace="5" src="spcrvdem_03.png"> <h2>A remedy<a name="4"></a></h2> <p>Since, in this case, we are really interested only in the curve segment corresponding to the parameter interval [2 .. 8], we can use FNBRK to extract that part, and then have no difficulty plotting it with FNPLT: </p><pre class="codeinput">mycurve = fnbrk(curve,[2 8]);hold <span class="string">on</span>, fnplt(mycurve,<span class="string">'y'</span>,2.5)xlabel(<span class="string">'... and its interesting part (yellow) plotted via FNPLT.'</span>), hold <span class="string">off</span></pre><img vspace="5" hspace="5" src="spcrvdem_04.png"> <h2>SPCRV: control polygon ...<a name="5"></a></h2> <p>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 toolbox 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 above picture shows such a control polygon. The next slide shows the corresponding spline curve of order 3. </p><pre class="codeinput">points = [0 0; 1 0; 1 1; 0 2; -1 1; -1 0; 0 -1; 0 -2].';plot(points(1,:),points(2,:),<span class="string">'k'</span>), axis([-2 2 -2.1 2.2]), grid <span class="string">off</span>title(<span class="string">'Control polygon'</span>)</pre><img vspace="5" hspace="5" src="spcrvdem_05.png"> <h2>... and corresponding spline curve<a name="6"></a></h2> <p>We have added the corresponding spline curve of order 3 provided by SPCRV.</p><pre class="codeinput">hold <span class="string">on</span>values = spcrv(points,3);plot(values(1,:),values(2,:),<span class="string">'r'</span>,<span class="string">'linew'</span>,1.5)xlabel(<span class="string">' ... and the corresponding quadratic spline curve'</span>)</pre><img vspace="5" hspace="5" src="spcrvdem_06.png"> <p>You notice that the curve touches each segment of the control polygon at its midpoint, and follows the shape outlined by the control polygon. </p> <h2>Raising the order<a name="8"></a></h2> <p>Raising the order k will pull the curve away from the control polygon and make it smoother, but also shorter. Here we added the corresponding spline curve of order 4. </p><pre class="codeinput">value4 = spcrv(points,4);plot(value4(1,:),value4(2,:),<span class="string">'b'</span>,<span class="string">'linew'</span>,2)xlabel(<span class="string">' ... and the corresponding spline curves, quadRatic and cuBic'</span>)</pre><img vspace="5" hspace="5" src="spcrvdem_07.png"> <h2>CSCVN<a name="9"></a></h2> <p>On the other hand, the command CSCVN provides an interpolating curve. Here is the resulting parametric `natural' cubic spline curve: </p><pre class="codeinput">fnplt( cscvn(points), <span class="string">'g'</span>,1.5 )xlabel(<span class="string">' ... and now with the interpolating spline curve (in green) added'</span>)</pre><img vspace="5" hspace="5" src="spcrvdem_08.png"> <p>By adding the point (.95,-.05) near the second control point, (1,0), we can make this curve turn faster there:</p><pre class="codeinput">[d,np] = size(points);fnplt( cscvn([ points(:,1) [.95; -.05] points(:,2:np) ]), <span class="string">'m'</span>,1.5)xlabel([<span class="string">'The interpolating curve (magenta) is made to '</span>,<span class="keyword">...</span> <span class="string">'turn faster by addition of another point'</span>])plot(.95,-.05,<span class="string">'*'</span>)legend(<span class="string">'control polygon'</span>,<span class="string">'quadratic spline curve'</span>,<span class="string">'cubic spline curve'</span>,<span class="keyword">...</span> <span class="string">'interpolating spline curve'</span>,<span class="string">'faster turning near (1,0)'</span>,4)title(<span class="string">''</span>)hold <span class="string">off</span></pre><img vspace="5" hspace="5" src="spcrvdem_09.png"> <p class="footer">Copyright 1987-2005 C. de Boor and The MathWorks, Inc.<br> Published with MATLAB® 7.1<br></p> </div> <!--##### SOURCE BEGIN #####%% More Spline Curves% Use of SPMAK, SPCRV or CSCVN to generate spline curves.% Copyright 1987-2005 C. de Boor and The MathWorks, Inc.% $Revision: 1.18.4.2 $%% A simple spline curve% The Spline Toolbox can handle v e c t o r - v a l u e d splines.% A d-vector valued univariate spline provides a curve 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 and plotted.knots = [1,1:9,9];curve = spmak( knots, repmat([ 0 0; 1 0; 1 1; 0 1 ], 2,1).' );t = linspace(2,8,121); values = fnval(curve,t);plot(values(1,:),values(2,:),'linew',2)axis([-.2 1.2 -.2 1.2]), axis equal, grid offtitle('A spline curve')%% Add some tangent vectors% We also draw the tangent vector to the curve at some points.t = 3:.4:6.2; lt = length(t);cv = fnval( curve, t );cdv = fnval( fnder(curve), t );hold onquiver(cv(1,:),cv(2,:), cdv(1,:),cdv(2,:))xlabel('... and its tangent vector at selected points.')hold off%% A word of caution% You may have noticed that, in this example, I did not use FNPLT to plot the% curve, but instead plotted some point on the curve obtained by FNVAL:% t = linspace(2,8,121); values = fnval(curve,t);% plot(values(1,:),values(2,:),'linew',2)% Using FNPLT directly with this particular curve gives the red curve above.% The explanation?% The spline is of order 4, yet the end knots in the knot sequence% knots = [1,1:9,9];% have only multiplicity 2. Therefore, all the B-splines of order 4 for this% knot sequence are 0 at the endpoints of the basic interval. This makes the% curve start and stop at (0,0).hold onfnplt(curve,'r',.5)xlabel('... and its plot (red) via FNPLT.')hold off%% A remedy% Since, in this case, we are really interested only in the curve segment% corresponding to the parameter interval [2 .. 8], we can use FNBRK to extract% that part, and then have no difficulty plotting it with FNPLT:mycurve = fnbrk(curve,[2 8]);hold on, fnplt(mycurve,'y',2.5)xlabel('... and its interesting part (yellow) plotted via FNPLT.'), hold off%% SPCRV: control polygon ...% 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 toolbox 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 above picture shows such a control polygon. The next slide shows the% corresponding spline curve of order 3.points = [0 0; 1 0; 1 1; 0 2; -1 1; -1 0; 0 -1; 0 -2].';plot(points(1,:),points(2,:),'k'), axis([-2 2 -2.1 2.2]), grid offtitle('Control polygon')%% ... and corresponding spline curve% We have added the corresponding spline curve of order 3 provided by SPCRV.hold onvalues = spcrv(points,3);plot(values(1,:),values(2,:),'r','linew',1.5)xlabel(' ... and the corresponding quadratic spline curve')%%% 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% Raising the order k will pull the curve away from the control polygon% and make it smoother, but also shorter.% Here we added the corresponding spline curve of order 4.value4 = spcrv(points,4);plot(value4(1,:),value4(2,:),'b','linew',2)xlabel(' ... and the corresponding spline curves, quadRatic and cuBic')%% CSCVN% On the other hand, the command CSCVN provides an interpolating curve.% 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')%%% By adding the point (.95,-.05) near the second control point, (1,0), 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(['The interpolating curve (magenta) is made to ',... 'turn faster by addition of another point'])plot(.95,-.05,'*')legend('control polygon','quadratic spline curve','cubic spline curve',... 'interpolating spline curve','faster turning near (1,0)',4)title('')hold offdisplayEndOfDemoMessage(mfilename)##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -