summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/bpolynomial/bpolynomial.mp
blob: e4f27a434a15e7d6afc1fde998997a32dfc33b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
%%% bpolynomial.mp
%%% Copyright 2007 Stephan Hennig <stephanhennig@arcor.de>
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3 of this license
% or (at your option) any later version.  The latest version of this
% license is in http://www.latex-project.org/lppl.txt
%

%%% Identify yourself.
if known bpolynomial_fileversion: endinput fi;
string bpolynomial_fileversion;
bpolynomial_fileversion := "v0.5 (2007/12/12)";
message "Loading bpolynomial " & bpolynomial_fileversion;


%%% Main user macro for defining polynomials.
%%% Arguments are a suffix and the coefficients
%%%  of the function a*x^3 + b*x^2 + c*x + d.
vardef newBPolynomial@#(expr a, b, c, d)=
  bpolynomial__defineBPolynomial.@#(a, b, c, d);
  bpolynomial__defineBPolynomial.@#'(0, 3a, 2b, c);
  bpolynomial__defineBPolynomial.@#''(0, 0, 6a, 2b);
  bpolynomial__defineBPolynomial.@#'''(0, 0, 0, 6a);
enddef;


%%% This macro returns the path of a Bezier curve that matches
%%% a function a*x^3 + b*x^2 + c*x + d between two points A and D.
%%% This macro is the heart of this package and is used by
%%% several other macros.
%%% Arguments are the coefficients of the polynomial and the
%%% start and end point of the graph/path.
vardef bpolynomial__getBezierFromPolynomial(expr a, b, c, d, A, D)=
save xA,xB,xC,xD,yA,yB,yC,yD;
save xl,yl,xr,yr,dx;
numeric xA,xB,xC,xD,yA,yB,yC,yD;
numeric xl,yl,xr,yr,dx;
  xl := xpart A;
  yl := ypart A;
  xr := xpart D;
  yr := ypart D;
  dx := xpart D - xpart A;
  %%% Original equation system for x values.
%   xA                  = xl;
%   3(xB - xA)          = dx;
%   3(xC - 2xB + xA)    = 0;
%   xD - 3xC + 3xB - xA = 0;
  %%% Modified equation system.
  xA := xl;
  xB := xl + dx/3;
  xC := xr - dx/3;
  xD := xr;
  %%% Original equation system for y values.
%   yA                  = ((a*xl + b)*xl + c)*xl + d;
%   3(yB - yA)          = dx*((3a*xl + 2b)*xl + c);
%   3(yC - 2yB + yA)    = dx*dx*(3a*xl + b);
%   yD - 3yC + 3yB - yA = a*dx*dx*dx;
  %%% Modified equation system.
  yA               := yl;
  3(yB - yA)       = dx*((3a*xl + 2b)*xl + c);
  3(yC - 2yB + yA) = dx*dx*(3a*xl + b);
  yD               := yr;
  %%% Return path A..controls B and C..D.
  (xA,yA)..controls (xB,yB) and (xC,yC)..(xD,yD)
enddef;


%%% This macro returns the path of a Bezier curve that matches
%%% a function a*x^3 + b*x^2 + c*x + d in the range [xl, xr].
%%% Arguments are the coefficients of the polynomial and the
%%% range boundaries of the graph/path.
vardef getBezierFromPolynomial(expr a, b, c, d, xl, xr)=
  bpolynomial__getBezierFromPolynomial(a, b, c, d,
    (xl, ((a*xl+b)*xl+c)*xl+d),
    (xr, ((a*xr+b)*xr+c)*xr+d))
enddef;


%%% This macro returns the path of a Bezier curve that matches
%%% a function a*x^3 + b*x^2 + c*x + d in the range [xl, xr].
%%% Arguments are the coefficients of the polynomial and the
%%% range boundaries of the graph/path.
vardef getBezierFromSqrRoot(expr u, v, w, xl, xr)=
save yl, yr;
numeric yl,yr;
    if (xl >= -v):
      yl := xl;
    else:
      message "Package bpolynomial warning:  Replacing lower range boundary " & decimal xl & " by " & decimal -v & "!";
      yl := -v;
    fi
    if (xr >= -v):
      yr := xr;
    else:
      message "Package bpolynomial warning:  Replacing upper range boundary " & decimal xr & " by " & decimal -v & "!";
      yr := -v;
    fi
  bpolynomial__getBezierFromPolynomial(0, 1/u/u, -2*w/u/u, (w/u)*(w/u)-v,
    (u*sqrt(yl+v)+w, yl),
    (u*sqrt(yr+v)+w, yr)) reflectedabout ((0,0),(1,1))
enddef;


%%% This macro returns the path of a Bezier curve that matches
%%% a function a*x^3 + b*x^2 + c*x + d in the range [xl, xr].
%%% Arguments are the coefficients of the polynomial and the
%%% range boundaries of the graph/path.
vardef getBezierFromCubRoot(expr u, v, w, xl, xl)=
save yl, yr;
numeric yl,yr;
    if (xl >= -v):
      yl := xl;
    else:
      message "Package bpolynomial warning:  Replacing lower range boundary " & decimal xl & " by " & decimal -v & "!";
      yl := -v;
    fi
    if (xr >= -v):
      yr := xr;
    else:
      message "Package bpolynomial warning:  Replacing upper range boundary " & decimal xr & " by " & decimal -v & "!";
      yr := -v & "!";
    fi
  bpolynomial__getBezierFromPolynomial(1/u/u/u, -3w/u/u/u, 3(w/u)*(w/u)/u, (w/u)*(w/u)*(w/u)-v,
    (u*((yl+v)**(1/3))+w, yl),
    (u*((yr+v)**(1/3))+w, yr)) reflectedabout ((0,0),(1,1))
enddef;


%%% This internal macro defines a new polynomial.
%%% Arguments are a suffix macro and the coefficients
%%% of the polynomial a*x^3 + b*x^2 + c*x + d.
vardef bpolynomial__defineBPolynomial@#(expr ca,cb,cc,cd)=
numeric @#.a, @#.b, @#.c, @#.d;
  %%% Save coefficients for later access.
  %%% Variable @#.a refers to coefficient a of polynomial @#.
  @#.a := ca;
  @#.b := cb;
  @#.c := cc;
  @#.d := cd;
  
  %%% This macro returns values of polynomial @#.
  %%% Argument is an x value.
  vardef @#.eval(expr x)=
    (((@#.a*x + @#.b)*x + @#.c)*x + @#.d)
  enddef;
  
  %%% This macro returns the path corresponding to polynomial @#
  %%% on the intervall [xl, xr].
  vardef @#.getPath(expr xl,xr)=
    bpolynomial__getBezierFromPolynomial(@#.a, @#.b, @#.c, @#.d, (xl, @#.eval(xl)), (xr, @#.eval(xr)))
  enddef;
  
  %%% This macro returns a path tangent to @# at point (x, f(x))
  %%% covering the interval [x+xm, x+xp].
  vardef @#.getTangent(expr x, xm, xp)=
  save m, y;
  numeric m, y;
    m := (3@#.a*x + 2@#.b)*x + @#.c;
    y := @#.eval(x);
    (x+xm, y + m*xm) -- (x+xp, y + m*xp)
  enddef;
  
enddef;


%%% This macro defines a new square root.
%%% Arguments are a suffix macro and the parameters
%%% of the function u*(x + v)^(1/2) + w.
vardef newBSqrRoot@#(expr cu,cv,cw)=
numeric @#.a, @#.b, @#.c, @#.d;
numeric @#.u, @#.v, @#.w;
  %%% Save parameters for later access.
  %%% Variable @#.v refers to parameters of square root @#.
  %%% Variables @#.a to @#.d store the coefficients of the
  %%% corresponding polynomial.
  @#.u := cu;
  @#.v := cv;
  @#.w := cw;
  @#.a := 0;
  @#.b := 1/cu/cu;
  @#.c := -2*cw/cu/cu;
  @#.d := (cw/cu)*(cw/cu)-cv;
  
  %%% This macro returns values of polynomial @#.
  %%% Argument is an x value.
  vardef @#.eval(expr x)=
    if (x >= -@#.v):
      @#.u*sqrt(x + @#.v) + @#.w
    else:
      message "Package bpolynomial warning:  Cannot evaluate function at x = " & decimal x & "!";
      @#.w
    fi
  enddef;
  
  %%% This macro returns the path corresponding to square root @#
  %%% on the intervall [yl, yr].  The path of the corresponing
  %%% polynomial is computed and then transformed.
  vardef @#.getPath(expr xl,xr)=
  save yl, yr;
  numeric yl, yr;
    if (xl >= -@#.v):
      yl := xl;
    else:
      message "Package bpolynomial warning:  Replacing lower range boundary " & decimal xl & " by " & decimal -@#.v & "!";
      yl := -@#.v;
    fi
    if (xr >= -@#.v):
      yr := xr;
    else:
      message "Package bpolynomial warning:  Replacing upper range boundary " & decimal xr & " by " & decimal -@#.v & "!";
      yr := -@#.v;
    fi
    bpolynomial__getBezierFromPolynomial(@#.a, @#.b, @#.c, @#.d, (@#.eval(yl), yl), (@#.eval(yr), yr))
    reflectedabout ((0,0),(1,1))
  enddef;
  
  %%% This macro returns a path tangent to square root @#
  %%% at point (x, f(x)) covering the interval [x+xm, x+xp].
  vardef @#.getTangent(expr x, epsl, epsr)=
  save m, y;
  numeric m, y;
    if (x >= -@#.v):
      m := @#.u/(2sqrt(x + @#.v));
      y := @#.eval(x);
      (x+epsl, y + m*epsl) -- (x+epsr, y + m*epsr)
    else:
      message "Package bpolynomial warning:  Cannot draw tangent at x = " & decimal x & "!";
      (-@#.v, @#.w)--(-@#.v, @#.w+1)
    fi
  enddef;
  
enddef;

  
%%% This macro defines a new cubic root.
%%% Arguments are a suffix macro and the parameters
%%% of the function u*(x + v)^(1/3) + w.
vardef newBCubRoot@#(expr cu,cv,cw)=
numeric @#.a, @#.b, @#.c, @#.d;
numeric @#.u, @#.v, @#.w;
  %%% Save parameters for later access.
  %%% Variable @#.v refers to parameters of cubic root @#.
  %%% Variables @#.a to @#.d store the coefficients of the
  %%% corresponding polynomial.
  @#.u := cu;
  @#.v := cv;
  @#.w := cw;
  @#.a := 1/cu/cu/cu;
  @#.b := -3cw/cu/cu/cu;
  @#.c := 3(cw/cu)*(cw/cu)/cu;
  @#.d := (cw/cu)*(cw/cu)*(cw/cu)-cv;
  
  %%% This macro returns values of polynomial @#.
  %%% Argument is an x value.
  vardef @#.eval(expr x)=
    if (x >= -@#.v):
      @#.u*((x+@#.v)**(1/3)) + @#.w
    else:
      message "Package bpolynomial warning:  Cannot evaluate function at x = " & decimal x & "!";
      @#.w
    fi
  enddef;
  
  %%% This macro returns the path corresponding to cubic root @#
  %%% on the intervall [yl, yr].  The path of the corresponing
  %%% polynomial is computed and then transformed.
  vardef @#.getPath(expr xl,xr)=
  save yl, yr;
  numeric yl, yr;
    if (xl >= -@#.v):
      yl := xl;
    else:
      message "Package bpolynomial warning:  Replacing lower range boundary " & decimal xl & " by " & decimal -@#.v & "!";
      yl := -@#.v;
    fi
    if (xr >= -@#.v):
      yr := xr;
    else:
      message "Package bpolynomial warning:  Replacing upper range boundary " & decimal xr & " by " & decimal -@#.v & "!";
      yr := -@#.v;
    fi
    bpolynomial__getBezierFromPolynomial(@#.a, @#.b, @#.c, @#.d, (@#.eval(yl), yl), (@#.eval(yr), yr))
      reflectedabout ((0,0),(1,1))
  enddef;
  
  %%% This macro returns a path tangent to cubic root @#
  %%% at point (x, f(x)) covering the interval [x+xm, x+xp].
  vardef @#.getTangent(expr x, epsl, epsr)=
  save m, y;
  numeric m, y;
    if (x >= -@#.v):
      m := @#.u/3/((x + @#.v)**(2/3));
      y := @#.eval(x);
      (x+epsl, y + m*epsl) -- (x+epsr, y + m*epsr)
    else:
      message "Package bpolynomial warning:  Cannot draw tangent at x = " & decimal x & "!";
      (-@#.v, @#.w)--(-@#.v, @#.w+1)
    fi
  enddef;
  
enddef;