Debian Codegraph @

texmf-dist/metapost/splines/splines.mp

raw
1 %%
2 %% This is file `splines.mp',
3 %% generated with the docstrip utility.
4 %%
5 %% The original source files were:
6 %%
7 %% splines.dtx (with options: `package')
8 %%
9 %% -------------------------------------------------------------------
10 %%
11 %% Copyright 2002--2005, Daniel H. Luecking
12 %%
13 %% Splines.mp may be distributed and/or modified under the conditions of the
14 %% LaTeX Project Public License, either version 1.3 of this license or (at
15 %% your option) any later version. The latest version of this license is in
16 %% <http://www.latex-project.org/lppl.txt>
17 %% and version 1.3 or later is part of all distributions of LaTeX version
18 %% 2003/12/01 or later.
19 %%
20 %% Splines has maintenance status "author-maintained". The Current Maintainer
21 %% is Daniel H. Luecking. The Base Interpreter is MetaPost (or Metafont).
22 %%
23 if known splines_fileversion: endinput fi;
24 string splines_fileversion;
25 splines_fileversion := "2006/09/25, v0.2a";
26 message "Loading splines.mp " & splines_fileversion;
27
28 def list_to_array (suffix arr) (text list) =
29 arr := 0;
30 for _itm = list :
31 arr[incr arr] := _itm;
32 endfor
33 enddef;
34
35 def compute_spline (expr closed) (suffix points, pr, po) =
36 % interior equations:
37 for j= 2 upto points - 1 :
38 % equate first derivatives:
39 po[j] + pr[j] = 2 points[j];
40 % and second derivatives:
41 pr[j+1] + 2 pr[j] = 2 po[j] + po[j-1];
42 endfor
43 % for a closed curve, the first and last are also interior:
44 if closed:
45 po 1 + pr 1 = 2 points 1;
46 po[points] + pr[points] = 2 points[points];
47 pr 2 + 2 pr 1 = 2 po 1 + po[points];
48 pr 1 + 2 pr[points] = 2 po[points] + po[points-1];
49 fi
50 enddef;
51
52 vardef mksplinepath (expr closed) (suffix points, pr, po) =
53 points1..controls po1 and
54 for j = 2 upto points if not closed: -1 fi:
55 pr[j]..points[j]..controls po[j] and
56 endfor
57 if closed: pr 1..cycle else: pr[points]..points[points] fi
58 enddef;
59
60 vardef mkrelaxedspline (suffix pnts) =
61 save rs_pr, rs_po;
62 pair rs_po[], rs_pr[];
63 % Equate second derivative to zero at both end points
64 rs_pr 2 + pnts 1 = 2 rs_po 1 ;
65 pnts[pnts] + rs_po[pnts-1] = 2 rs_pr[pnts];
66 compute_spline (false) (pnts, rs_pr, rs_po);
67 mksplinepath (false) (pnts, rs_pr, rs_po)
68 enddef;
69
70 vardef mkclosedspline (suffix pnts) =
71 save cs_pr, cs_po;
72 pair cs_pr[], cs_po[];
73 compute_spline (true) (pnts, cs_pr, cs_po);
74 mksplinepath (true) (pnts, cs_pr, cs_po)
75 enddef;
76
77 vardef dospline (expr closed) (text the_list) =
78 save _sp; pair _sp[];
79 list_to_array (_sp) (the_list);
80 if closed :
81 mkclosedspline (_sp)
82 else:
83 mkrelaxedspline (_sp)
84 fi
85 enddef;
86
87 def compute_fcnspline (suffix points, dx, sl) =
88 % Get delta_x:
89 for j = 1 upto points - 1: dx[j] := xpart (points[j+1]-points[j]);
90 endfor
91 for j=2 upto points - 1:
92 sl[j + 1] * dx[j] + 2sl[j]*(dx[j] + dx[j-1]) + sl[j-1]*dx[j-1]
93 = 3*ypart(points[j+1] - points[j-1]);
94 endfor
95 enddef;
96
97 vardef mkfcnsplinepath (suffix points, dx, sl) =
98 points1..controls (points1 + (1, sl1)*dx1/3) and
99 for j = 2 upto points - 1:
100 (points[j] - (1, sl[j])*dx[j-1]/3) ..points[j]..
101 controls (points[j] + (1,sl[j])*dx[j]/3) and
102 endfor
103 (points[points] - (1,sl[points])*dx[points-1]/3)..points[points]
104 enddef;
105
106 vardef mkperiodicfcnspline (suffix pnts) =
107 save _sl, _dx; numeric _dx[], _sl[];
108 compute_fcnspline (pnts, _dx, _sl);
109 % periodicity equations:
110 _sl 1 = _sl[pnts];
111 _sl 2 * _dx 1 + 2 _sl 1 * _dx 1 + 2 _sl[pnts] * _dx[pnts-1]
112 + _sl[pnts-1] * _dx[pnts-1]
113 = 3 * ypart(pnts[2] - pnts[pnts-1]);
114 mkfcnsplinepath (pnts, _dx, _sl)
115 enddef;
116
117 vardef mkrelaxedfcnspline (suffix pnts) =
118 save _sl, _dx; numeric _dx[], _sl[];
119 compute_fcnspline (pnts, _dx, _sl);
120 % relaxation equations.
121 _sl 2 * _dx 1 + 2 _sl1 * _dx 1 = 3 * ypart(pnts2 - pnts1);
122 _sl[pnts-1] * _dx[pnts-1] + 2 _sl[pnts] * _dx[pnts-1]
123 = 3 * ypart(pnts[pnts] - pnts[pnts-1]);
124 mkfcnsplinepath (pnts, _dx, _sl)
125 enddef;
126
127 vardef fcnspline (expr periodic) (text the_list) =
128 save _fs; pair _fs[];
129 list_to_array (_fs) (the_list);
130 if periodic:
131 mkperiodicfcnspline (_fs)
132 else:
133 mkrelaxedfcnspline (_fs)
134 fi
135 enddef;
136
137 endinput.
138 %%
139 %% End of file `splines.mp'.