| 1 |
%%% bpolynomial.mp |
| 2 |
%%% Copyright 2007 Stephan Hennig <[email protected]> |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the conditions of |
| 5 |
% the LaTeX Project Public License, either version 1.3 of this license |
| 6 |
% or (at your option) any later version. The latest version of this |
| 7 |
% license is in http://www.latex-project.org/lppl.txt |
| 8 |
% |
| 9 |
|
| 10 |
%%% Identify yourself. |
| 11 |
if known bpolynomial_fileversion: endinput fi; |
| 12 |
string bpolynomial_fileversion; |
| 13 |
bpolynomial_fileversion := "v0.5 (2007/12/12)"; |
| 14 |
message "Loading bpolynomial " & bpolynomial_fileversion; |
| 15 |
|
| 16 |
|
| 17 |
%%% Main user macro for defining polynomials. |
| 18 |
%%% Arguments are a suffix and the coefficients |
| 19 |
%%% of the function a*x^3 + b*x^2 + c*x + d. |
| 20 |
vardef newBPolynomial@#(expr a, b, c, d)= |
| 21 |
bpolynomial__defineBPolynomial.@#(a, b, c, d); |
| 22 |
bpolynomial__defineBPolynomial.@#'(0, 3a, 2b, c); |
| 23 |
bpolynomial__defineBPolynomial.@#''(0, 0, 6a, 2b); |
| 24 |
bpolynomial__defineBPolynomial.@#'''(0, 0, 0, 6a); |
| 25 |
enddef; |
| 26 |
|
| 27 |
|
| 28 |
%%% This macro returns the path of a Bezier curve that matches |
| 29 |
%%% a function a*x^3 + b*x^2 + c*x + d between two points A and D. |
| 30 |
%%% This macro is the heart of this package and is used by |
| 31 |
%%% several other macros. |
| 32 |
%%% Arguments are the coefficients of the polynomial and the |
| 33 |
%%% start and end point of the graph/path. |
| 34 |
vardef bpolynomial__getBezierFromPolynomial(expr a, b, c, d, A, D)= |
| 35 |
save xA,xB,xC,xD,yA,yB,yC,yD; |
| 36 |
save xl,yl,xr,yr,dx; |
| 37 |
numeric xA,xB,xC,xD,yA,yB,yC,yD; |
| 38 |
numeric xl,yl,xr,yr,dx; |
| 39 |
xl := xpart A; |
| 40 |
yl := ypart A; |
| 41 |
xr := xpart D; |
| 42 |
yr := ypart D; |
| 43 |
dx := xpart D - xpart A; |
| 44 |
%%% Original equation system for x values. |
| 45 |
% xA = xl; |
| 46 |
% 3(xB - xA) = dx; |
| 47 |
% 3(xC - 2xB + xA) = 0; |
| 48 |
% xD - 3xC + 3xB - xA = 0; |
| 49 |
%%% Modified equation system. |
| 50 |
xA := xl; |
| 51 |
xB := xl + dx/3; |
| 52 |
xC := xr - dx/3; |
| 53 |
xD := xr; |
| 54 |
%%% Original equation system for y values. |
| 55 |
% yA = ((a*xl + b)*xl + c)*xl + d; |
| 56 |
% 3(yB - yA) = dx*((3a*xl + 2b)*xl + c); |
| 57 |
% 3(yC - 2yB + yA) = dx*dx*(3a*xl + b); |
| 58 |
% yD - 3yC + 3yB - yA = a*dx*dx*dx; |
| 59 |
%%% Modified equation system. |
| 60 |
yA := yl; |
| 61 |
3(yB - yA) = dx*((3a*xl + 2b)*xl + c); |
| 62 |
3(yC - 2yB + yA) = dx*dx*(3a*xl + b); |
| 63 |
yD := yr; |
| 64 |
%%% Return path A..controls B and C..D. |
| 65 |
(xA,yA)..controls (xB,yB) and (xC,yC)..(xD,yD) |
| 66 |
enddef; |
| 67 |
|
| 68 |
|
| 69 |
%%% This macro returns the path of a Bezier curve that matches |
| 70 |
%%% a function a*x^3 + b*x^2 + c*x + d in the range [xl, xr]. |
| 71 |
%%% Arguments are the coefficients of the polynomial and the |
| 72 |
%%% range boundaries of the graph/path. |
| 73 |
vardef getBezierFromPolynomial(expr a, b, c, d, xl, xr)= |
| 74 |
bpolynomial__getBezierFromPolynomial(a, b, c, d, |
| 75 |
(xl, ((a*xl+b)*xl+c)*xl+d), |
| 76 |
(xr, ((a*xr+b)*xr+c)*xr+d)) |
| 77 |
enddef; |
| 78 |
|
| 79 |
|
| 80 |
%%% This macro returns the path of a Bezier curve that matches |
| 81 |
%%% a function a*x^3 + b*x^2 + c*x + d in the range [xl, xr]. |
| 82 |
%%% Arguments are the coefficients of the polynomial and the |
| 83 |
%%% range boundaries of the graph/path. |
| 84 |
vardef getBezierFromSqrRoot(expr u, v, w, xl, xr)= |
| 85 |
save yl, yr; |
| 86 |
numeric yl,yr; |
| 87 |
if (xl >= -v): |
| 88 |
yl := xl; |
| 89 |
else: |
| 90 |
message "Package bpolynomial warning: Replacing lower range boundary " & decimal xl & " by " & decimal -v & "!"; |
| 91 |
yl := -v; |
| 92 |
fi |
| 93 |
if (xr >= -v): |
| 94 |
yr := xr; |
| 95 |
else: |
| 96 |
message "Package bpolynomial warning: Replacing upper range boundary " & decimal xr & " by " & decimal -v & "!"; |
| 97 |
yr := -v; |
| 98 |
fi |
| 99 |
bpolynomial__getBezierFromPolynomial(0, 1/u/u, -2*w/u/u, (w/u)*(w/u)-v, |
| 100 |
(u*sqrt(yl+v)+w, yl), |
| 101 |
(u*sqrt(yr+v)+w, yr)) reflectedabout ((0,0),(1,1)) |
| 102 |
enddef; |
| 103 |
|
| 104 |
|
| 105 |
%%% This macro returns the path of a Bezier curve that matches |
| 106 |
%%% a function a*x^3 + b*x^2 + c*x + d in the range [xl, xr]. |
| 107 |
%%% Arguments are the coefficients of the polynomial and the |
| 108 |
%%% range boundaries of the graph/path. |
| 109 |
vardef getBezierFromCubRoot(expr u, v, w, xl, xl)= |
| 110 |
save yl, yr; |
| 111 |
numeric yl,yr; |
| 112 |
if (xl >= -v): |
| 113 |
yl := xl; |
| 114 |
else: |
| 115 |
message "Package bpolynomial warning: Replacing lower range boundary " & decimal xl & " by " & decimal -v & "!"; |
| 116 |
yl := -v; |
| 117 |
fi |
| 118 |
if (xr >= -v): |
| 119 |
yr := xr; |
| 120 |
else: |
| 121 |
message "Package bpolynomial warning: Replacing upper range boundary " & decimal xr & " by " & decimal -v & "!"; |
| 122 |
yr := -v & "!"; |
| 123 |
fi |
| 124 |
bpolynomial__getBezierFromPolynomial(1/u/u/u, -3w/u/u/u, 3(w/u)*(w/u)/u, (w/u)*(w/u)*(w/u)-v, |
| 125 |
(u*((yl+v)**(1/3))+w, yl), |
| 126 |
(u*((yr+v)**(1/3))+w, yr)) reflectedabout ((0,0),(1,1)) |
| 127 |
enddef; |
| 128 |
|
| 129 |
|
| 130 |
%%% This internal macro defines a new polynomial. |
| 131 |
%%% Arguments are a suffix macro and the coefficients |
| 132 |
%%% of the polynomial a*x^3 + b*x^2 + c*x + d. |
| 133 |
vardef bpolynomial__defineBPolynomial@#(expr ca,cb,cc,cd)= |
| 134 |
numeric @#.a, @#.b, @#.c, @#.d; |
| 135 |
%%% Save coefficients for later access. |
| 136 |
%%% Variable @#.a refers to coefficient a of polynomial @#. |
| 137 |
@#.a := ca; |
| 138 |
@#.b := cb; |
| 139 |
@#.c := cc; |
| 140 |
@#.d := cd; |
| 141 |
|
| 142 |
%%% This macro returns values of polynomial @#. |
| 143 |
%%% Argument is an x value. |
| 144 |
vardef @#.eval(expr x)= |
| 145 |
(((@#.a*x + @#.b)*x + @#.c)*x + @#.d) |
| 146 |
enddef; |
| 147 |
|
| 148 |
%%% This macro returns the path corresponding to polynomial @# |
| 149 |
%%% on the intervall [xl, xr]. |
| 150 |
vardef @#.getPath(expr xl,xr)= |
| 151 |
bpolynomial__getBezierFromPolynomial(@#.a, @#.b, @#.c, @#.d, (xl, @#.eval(xl)), (xr, @#.eval(xr))) |
| 152 |
enddef; |
| 153 |
|
| 154 |
%%% This macro returns a path tangent to @# at point (x, f(x)) |
| 155 |
%%% covering the interval [x+xm, x+xp]. |
| 156 |
vardef @#.getTangent(expr x, xm, xp)= |
| 157 |
save m, y; |
| 158 |
numeric m, y; |
| 159 |
m := (3@#.a*x + 2@#.b)*x + @#.c; |
| 160 |
y := @#.eval(x); |
| 161 |
(x+xm, y + m*xm) -- (x+xp, y + m*xp) |
| 162 |
enddef; |
| 163 |
|
| 164 |
enddef; |
| 165 |
|
| 166 |
|
| 167 |
%%% This macro defines a new square root. |
| 168 |
%%% Arguments are a suffix macro and the parameters |
| 169 |
%%% of the function u*(x + v)^(1/2) + w. |
| 170 |
vardef newBSqrRoot@#(expr cu,cv,cw)= |
| 171 |
numeric @#.a, @#.b, @#.c, @#.d; |
| 172 |
numeric @#.u, @#.v, @#.w; |
| 173 |
%%% Save parameters for later access. |
| 174 |
%%% Variable @#.v refers to parameters of square root @#. |
| 175 |
%%% Variables @#.a to @#.d store the coefficients of the |
| 176 |
%%% corresponding polynomial. |
| 177 |
@#.u := cu; |
| 178 |
@#.v := cv; |
| 179 |
@#.w := cw; |
| 180 |
@#.a := 0; |
| 181 |
@#.b := 1/cu/cu; |
| 182 |
@#.c := -2*cw/cu/cu; |
| 183 |
@#.d := (cw/cu)*(cw/cu)-cv; |
| 184 |
|
| 185 |
%%% This macro returns values of polynomial @#. |
| 186 |
%%% Argument is an x value. |
| 187 |
vardef @#.eval(expr x)= |
| 188 |
if (x >= -@#.v): |
| 189 |
@#.u*sqrt(x + @#.v) + @#.w |
| 190 |
else: |
| 191 |
message "Package bpolynomial warning: Cannot evaluate function at x = " & decimal x & "!"; |
| 192 |
@#.w |
| 193 |
fi |
| 194 |
enddef; |
| 195 |
|
| 196 |
%%% This macro returns the path corresponding to square root @# |
| 197 |
%%% on the intervall [yl, yr]. The path of the corresponing |
| 198 |
%%% polynomial is computed and then transformed. |
| 199 |
vardef @#.getPath(expr xl,xr)= |
| 200 |
save yl, yr; |
| 201 |
numeric yl, yr; |
| 202 |
if (xl >= -@#.v): |
| 203 |
yl := xl; |
| 204 |
else: |
| 205 |
message "Package bpolynomial warning: Replacing lower range boundary " & decimal xl & " by " & decimal -@#.v & "!"; |
| 206 |
yl := -@#.v; |
| 207 |
fi |
| 208 |
if (xr >= -@#.v): |
| 209 |
yr := xr; |
| 210 |
else: |
| 211 |
message "Package bpolynomial warning: Replacing upper range boundary " & decimal xr & " by " & decimal -@#.v & "!"; |
| 212 |
yr := -@#.v; |
| 213 |
fi |
| 214 |
bpolynomial__getBezierFromPolynomial(@#.a, @#.b, @#.c, @#.d, (@#.eval(yl), yl), (@#.eval(yr), yr)) |
| 215 |
reflectedabout ((0,0),(1,1)) |
| 216 |
enddef; |
| 217 |
|
| 218 |
%%% This macro returns a path tangent to square root @# |
| 219 |
%%% at point (x, f(x)) covering the interval [x+xm, x+xp]. |
| 220 |
vardef @#.getTangent(expr x, epsl, epsr)= |
| 221 |
save m, y; |
| 222 |
numeric m, y; |
| 223 |
if (x >= -@#.v): |
| 224 |
m := @#.u/(2sqrt(x + @#.v)); |
| 225 |
y := @#.eval(x); |
| 226 |
(x+epsl, y + m*epsl) -- (x+epsr, y + m*epsr) |
| 227 |
else: |
| 228 |
message "Package bpolynomial warning: Cannot draw tangent at x = " & decimal x & "!"; |
| 229 |
(-@#.v, @#.w)--(-@#.v, @#.w+1) |
| 230 |
fi |
| 231 |
enddef; |
| 232 |
|
| 233 |
enddef; |
| 234 |
|
| 235 |
|
| 236 |
%%% This macro defines a new cubic root. |
| 237 |
%%% Arguments are a suffix macro and the parameters |
| 238 |
%%% of the function u*(x + v)^(1/3) + w. |
| 239 |
vardef newBCubRoot@#(expr cu,cv,cw)= |
| 240 |
numeric @#.a, @#.b, @#.c, @#.d; |
| 241 |
numeric @#.u, @#.v, @#.w; |
| 242 |
%%% Save parameters for later access. |
| 243 |
%%% Variable @#.v refers to parameters of cubic root @#. |
| 244 |
%%% Variables @#.a to @#.d store the coefficients of the |
| 245 |
%%% corresponding polynomial. |
| 246 |
@#.u := cu; |
| 247 |
@#.v := cv; |
| 248 |
@#.w := cw; |
| 249 |
@#.a := 1/cu/cu/cu; |
| 250 |
@#.b := -3cw/cu/cu/cu; |
| 251 |
@#.c := 3(cw/cu)*(cw/cu)/cu; |
| 252 |
@#.d := (cw/cu)*(cw/cu)*(cw/cu)-cv; |
| 253 |
|
| 254 |
%%% This macro returns values of polynomial @#. |
| 255 |
%%% Argument is an x value. |
| 256 |
vardef @#.eval(expr x)= |
| 257 |
if (x >= -@#.v): |
| 258 |
@#.u*((x+@#.v)**(1/3)) + @#.w |
| 259 |
else: |
| 260 |
message "Package bpolynomial warning: Cannot evaluate function at x = " & decimal x & "!"; |
| 261 |
@#.w |
| 262 |
fi |
| 263 |
enddef; |
| 264 |
|
| 265 |
%%% This macro returns the path corresponding to cubic root @# |
| 266 |
%%% on the intervall [yl, yr]. The path of the corresponing |
| 267 |
%%% polynomial is computed and then transformed. |
| 268 |
vardef @#.getPath(expr xl,xr)= |
| 269 |
save yl, yr; |
| 270 |
numeric yl, yr; |
| 271 |
if (xl >= -@#.v): |
| 272 |
yl := xl; |
| 273 |
else: |
| 274 |
message "Package bpolynomial warning: Replacing lower range boundary " & decimal xl & " by " & decimal -@#.v & "!"; |
| 275 |
yl := -@#.v; |
| 276 |
fi |
| 277 |
if (xr >= -@#.v): |
| 278 |
yr := xr; |
| 279 |
else: |
| 280 |
message "Package bpolynomial warning: Replacing upper range boundary " & decimal xr & " by " & decimal -@#.v & "!"; |
| 281 |
yr := -@#.v; |
| 282 |
fi |
| 283 |
bpolynomial__getBezierFromPolynomial(@#.a, @#.b, @#.c, @#.d, (@#.eval(yl), yl), (@#.eval(yr), yr)) |
| 284 |
reflectedabout ((0,0),(1,1)) |
| 285 |
enddef; |
| 286 |
|
| 287 |
%%% This macro returns a path tangent to cubic root @# |
| 288 |
%%% at point (x, f(x)) covering the interval [x+xm, x+xp]. |
| 289 |
vardef @#.getTangent(expr x, epsl, epsr)= |
| 290 |
save m, y; |
| 291 |
numeric m, y; |
| 292 |
if (x >= -@#.v): |
| 293 |
m := @#.u/3/((x + @#.v)**(2/3)); |
| 294 |
y := @#.eval(x); |
| 295 |
(x+epsl, y + m*epsl) -- (x+epsr, y + m*epsr) |
| 296 |
else: |
| 297 |
message "Package bpolynomial warning: Cannot draw tangent at x = " & decimal x & "!"; |
| 298 |
(-@#.v, @#.w)--(-@#.v, @#.w+1) |
| 299 |
fi |
| 300 |
enddef; |
| 301 |
|
| 302 |
enddef; |