%%% bpolynomial.mp %%% Copyright 2007 Stephan Hennig % % 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;