summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/bpolynomial/bpolynomial.mp
blob: 6840bb040384d15686fe2f9e53183a4def54fc29 (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
%%% 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
%

if known bpolynomial_fileversion: endinput fi;
string bpolynomial_fileversion;
bpolynomial_fileversion := "v0.3 (2007/11/26)";
message "Loading bpolynomial " & bpolynomial_fileversion;


%%% This macro defines two macros @#.eval and @#.getPath.
%%% Parameters are the coefficients of the polynomial a*x^3 + b*x^2 + c*x + d.
vardef newBPolynomial@#(expr ca,cb,cc,cd)=
numeric @#.a, @#.b, @#.c, @#.d;
  
  %%% Save coefficients for later access.
  %%% For instance, variable @#.a refers to coefficient a of polynomial @#.
  @#.a := ca;
  @#.b := cb;
  @#.c := cc;
  @#.d := cd;
  
  %%% Define a macro that returns polynomial values.
  %%% Parameter is an x value.
  vardef @#.eval(expr x)=
    (((@#.a*x + @#.b)*x + @#.c)*x + @#.d)
  enddef;
  
  %%% Define a macro that returns a path of the polynomial
  %%% on a given intervall [xl, xr].
  vardef @#.getPath(expr xl,xr)=
  save xA,xB,xC,xD,yA,yB,yC,yD;
  save dx;
  numeric xA,xB,xC,xD,yA,yB,yC,yD;
  numeric dx;
    dx := xr - xl;
    %%% 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*((3@#.a*xl + 2@#.b)*xl + @#.c);
%     3(yC - 2yB + yA)    = dx*dx*(3@#.a*xl + @#.b);
%     yD - 3yC + 3yB - yA = @#.a*dx*dx*dx;
    %%% Modified equation system.
    yA               := @#.eval(xl);
    3(yB - yA)       = dx*((3@#.a*xl + 2@#.b)*xl + @#.c);
    3(yC - 2yB + yA) = dx*dx*(3@#.a*xl + @#.b);
    yD               := @#.eval(xr);
    %%% Return path A..controls B and C..D.
    (xA,yA)..controls (xB,yB) and (xC,yC)..(xD,yD)
  enddef;
  
enddef;