home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 5.ddi / SPLINES.DI$ / SPCRVDEM.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  3.1 KB  |  83 lines

  1. % SPCRVDEM    Demonstrate spline curves.
  2. % Copyright (c) 1990-92 by Carl de Boor and The MathWorks, Inc.
  3.                                          % latest change: September 28, 1990
  4. clc;clg;home;echo on
  5. % The various M-files have been set up to deal with piecewise polynomial
  6. % functions having values in R^d, i.e., with curves in d-space. In this mode,
  7. %  d = 2  is most common, as it gives plane curves.
  8.  
  9. %     Here is an example, in which a spline with  2-dimensional coefficients
  10. % is constructed ....
  11.  
  12. knots=[1,1:9,9];
  13.  
  14. coefs=[0 0;1 0;1 1;0 1]; coefs=[coefs;coefs]'
  15. curve=spmak(knots,coefs);
  16.  
  17. % ... and then plotted:
  18.  
  19. t=[2:.2:8];values=fnval(curve,t);
  20. plot(values(1,:),values(2,:));
  21. axis([-1 2 -1 2]);axis('square');pause
  22.  
  23. % Now we also draw the tangent vector to the curve at some points.
  24.  
  25. pause;                                        % Touch any key to continue
  26.  
  27. dcurve=fnder(curve);
  28. hold on
  29. t=[3:.4:6];
  30. cv=fnval(curve,t);
  31. cdv=fnval(dcurve,t);
  32. for j=1:length(t),
  33.  plot(cv(1,j)+[0,cdv(1,j)],cv(2,j)+[0,cdv(2,j)])
  34. end
  35. pause
  36. hold off;
  37. pause;                                        % Touch any key to continue
  38.  
  39. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  40.  
  41. % I use spline curves extensively in the generation of illustrations in 
  42. % which nothing more than a smooth curve of a certain roughly imagined
  43. % shape is required. For this, the tool box contains a special M-file called
  44. %  spcrv  which is independent of the rest of the setup. Given a sequence
  45. % of points in the plane and, optionally, an order  k , it generates (by
  46. % repeated midpoint knot insertion) the spline curve (of order  k ) whose
  47. % control polygon is specified by the given sequence. The following picture
  48. % shows such a control polygon and the corresponding spline curve of order 3
  49.  
  50. points=[0 0;1 0;1 1;0 2;-1 1;-1 0;0 -1;0 -2]';
  51. pause;                                        % Touch any key to continue
  52. plot(points(1,:),points(2,:),':');
  53. axis([-2 2 -2 2]), pause
  54.  
  55. %   That's the control polygon. Now comes the curve which is a smoothed version
  56. % of that polygon.
  57. pause;                                        % Touch any key to continue
  58. hold on
  59. k=3;values=spcrv(points,k);
  60. plot(values(1,:),values(2,:));pause
  61. %  You notice that the curve touches each segment of the control polygon
  62. % at its midpoint, and follows the shape outlined by the control polygon.
  63. %   Raising the order  k  will pull the curve away from the control polygon
  64. % and make it smoother, but also shorter.
  65. %   Here is the same, with  k = 4 :
  66.  
  67. k=4;value4=spcrv(points,k);
  68. pause;                                        % Touch any key to continue
  69. plot(value4(1,:),value4(2,:),'-.');pause
  70.  
  71. %  On the other hand, the m-file  cscvn  will interpolate the given points.
  72. % Here is the resulting parametric `natural' cubic spline curve:
  73.  
  74. fnplt(cscvn(points));pause
  75.  
  76. %   By adding the point  (.95,-.05)  near  (1,0), the second control point, we 
  77. % can make this curve turn faster there:
  78.  
  79. [d,np]=size(points);
  80. fnplt(cscvn([points(:,1) [.95;-.05] points(:,2:np)]),'-.');pause
  81.  
  82. hold off
  83.