Debian Codegraph @

texmf-dist/doc/metapost/bpolynomial/bpolynomial.tex

raw
1 %%% bpolynomial.tex
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 \RequirePackage[resetfonts]{cmap}
10 \documentclass{article}
11 \usepackage[T1]{fontenc}
12 \usepackage{ae}
13 \usepackage{amsmath}
14 \usepackage{amssymb}
15 \newcommand*{\cmd}[1]{\texttt{#1}}
16 \newcommand*{\name}[1]{\textsf{#1}}
17 \newcommand*{\pkg}{\name{bpolynomial}}
18 \newcommand{\user}[1]{\emph{#1}}
19 \newcommand*{\B}{B\'ezier}
20 \usepackage{xcolor}
21 \colorlet{framecol}{black!50}
22 \usepackage{listings}
23 \lstloadlanguages{MetaPost,[LaTeX]TeX}
24 \lstset{language=MetaPost, basicstyle=\small\ttfamily, keywordstyle={}, commentstyle={}, columns=flexible, showspaces=false, showstringspaces=false, frame=single, rulecolor=\color{framecol}, aboveskip=2ex, belowskip=2ex, framesep=2ex, xleftmargin=2ex, xrightmargin=2ex}
25 \lstnewenvironment{listing}[1][]
26 {\lstset{#1}}
27 {}
28 \usepackage{multicol}
29 \usepackage{url}
30 \usepackage{graphicx}
31 \setcounter{topnumber}{1}
32 \setcounter{bottomnumber}{0}
33 \usepackage{ifpdf}
34 \ifpdf
35 \DeclareGraphicsRule{*}{mps}{*}{}
36 \fi
37
38 \begin{document}
39 \title{The \pkg\ package\thanks{This document describes \pkg\ v0.5, last revised 2007/12/12.}}
40 \author{Stephan Hennig\thanks{[email protected]}}
41 \maketitle
42
43 \begin{abstract}
44 The MetaPost package \pkg\ helps plotting polynomial and root functions. It provides macros to calculate one-segment \B\ curves exactly matching a given cubic polynomial or square or cubic root function. Additionally, tangents on all functions and derivatives of polynomials can be calculated.
45 \end{abstract}
46
47 \setcounter{secnumdepth}{3}
48 \setcounter{tocdepth}{2}
49 \begin{multicols}{2}
50 \tableofcontents
51 \end{multicols}
52
53
54 \section{Introduction}\label{sec:intro}
55 MetaPost has a variable type \cmd{path} that can be used for drawing smooth and visualy pleasing curves. When plotting graphs, the problem users are confronted with is how to define a suitable path representing a given function $f(x)$?
56
57 The \name{splines} package by Dan Luecking provides macros for drawing smooth piece-wise \B\ curves through arbitrary sample points of a function.~\cite{mp:splines} Since \B\ curves are polynomials of degree three, for cubic polynomials we can do better and find a matching one-segment \B\ curve. This package eases the task of finding a \B\ curve corresponding to a given function of the following types:
58 \begin{align}
59 f(x) & = ax^3 + bx^2 + cx + d \\
60 f(x) & = u \sqrt{x + v} + w \\
61 f(x) & = u \sqrt[3]{x + v} + w
62 \end{align}
63
64
65 \section{Usage}\label{sec:usage}
66 The \pkg\ package provides two flavours of user interfaces:
67 \begin{itemize}
68 \item An advanced interface, that first requires a function to be defined and then provides access to the \B\ curve corresponding to the function, its tangent or derivative in a given intervall.
69 \item A plain interface, that gives instant access to the \B\ curve matching a given function in an intervall.
70 \end{itemize}
71 The plain interface is less powerful, but might be convenient in certain circumstances. It is briefly described in section~\ref{sec:plaininterface}. The average user most likely wants to use the advanced interface, so we will discuss that first.
72
73 \subsection{Plotting polynomials}\label{sec:polynomials}
74 \subsubsection{Defining polynomials}\label{sec:newBPolynomial}
75 As was said before, calculating the \B\ curve of a polynomial function first requires a function to be defined. The macro for defining a polynomial
76 \begin{equation}
77 f(x) = ax^3 + bx^2 + cx + d
78 \end{equation}
79 is called
80 \begin{center}
81 \cmd{newBPolynomial}
82 \end{center}
83 and takes five arguments. First argument is a suffix and the remaining four arguments are the four coefficients of the polynomial function $a$, $b$, $c$, $d$.
84
85 A definition of a polynomial
86 \begin{equation}
87 f(x) = 2x^3 + 0x^2 - 3x - 1
88 \end{equation}
89 associated with suffix \cmd{f} exemplary looks like this
90 \begin{listing}
91 newBPolynomial.f(2, 0, -3, -1);
92 \end{listing}
93
94 The suffix argument \cmd{f} serves as an identifier for deriving names of other macros, that are defined by \cmd{newBPolynomial}. To be more precise, command
95 \begin{center}
96 \cmd{newBPolynomial.<suffix>}
97 \end{center}
98 defines three new macros
99 \begin{center}
100 \begin{tabular}{l}
101 \cmd{<suffix>.getPath} \\
102 \cmd{<suffix>.eval} \\
103 \cmd{<suffix>.getTangent} \\
104 \end{tabular}
105 \end{center}
106 that do the real work. These macros are described in the following sections.
107
108 \subsubsection{Getting the \B\ curve of a polynomial}\label{sec:getPath}
109 Once a polynomial $\langle$suffix$\rangle$ is defined, the macro to request a \B\ curve matching the function on an intervall $[x_l, x_r]$ is
110 \begin{center}
111 \cmd{<suffix>.getPath}
112 \end{center}
113 This macro takes two argument, the intervall boundaries $x_l$ and $x_r$, and returns a polynomial shaped \B\ curve corresponding to function $\langle$suffix$\rangle$. Command \cmd{<suffix>.getPath} can be called as often as required with varying intervall boundaries and always returns a path corresponding to the new intervall.
114
115 Let's have a look at an example. Plotting our polynomial $f(x)$ on the intervall $(-2, 2)$ can be done with the following code (figure~\ref{fig:cubic}).
116 \begin{listing}
117 newBPolynomial.f(2, 0, -3, -1);
118 draw f.getPath(-2, 2) transformed T;
119 \end{listing}
120
121 \begin{figure}
122 % \begin{minipage}[t]{.45\linewidth}
123 \centering
124 \includegraphics{examples.1}
125 \caption{A cubic polynomial.}
126 \label{fig:cubic}
127 % \end{minipage}
128 \end{figure}
129
130 \emph{Hint:} Since the \pkg\ package never uses $\langle$suffix$\rangle$ as a complete identifier, you can use that as the name of a path variable to store the path returned by \cmd{<suffix>.getPath} for later drawing. Any other path (array) variable serves the same purpose, though.
131 \begin{listing}
132 newBPolynomial.f(2, 0, -3, -1);
133 path f;
134 f := f.getPath(-2, 2);
135 draw f transformed T;
136 \end{listing}
137
138 Note, since the base unit of MetaPost is a big point ($1\,\text{bp}=\frac{1}{72}\,\text{in}$) in most cases functions have to be scaled to a proper size before plotting. It is \emph{not} recommended, however, to apply scaling to polynomial coefficients, since current MetaPost versions can't handle large numbers very well.%
139 \footnote{At the time of writing the latest release is MetaPost v1.002.}
140 Instead, scaling should be applied to paths during \cmd{draw} operations. If not stated otherwise, in this manual, scaling is applied by an affine transform
141 \begin{center}
142 \cmd{T = identity xscaled 10mm yscaled 1mm;}
143 \end{center}
144
145 \subsubsection{Getting a tangent on a polynomial}\label{sec:getTangent}
146 Another macro set up during a function definition is
147 \begin{center}
148 \cmd{<suffix>.getTangent}
149 \end{center}
150 This macro returns a path tangent to a function $\langle$suffix$\rangle$ at a specific point. Arguments are an $x$-coordinate, where the tangent is placed, and two values $\epsilon_l$, $\epsilon_r$ that specify the neighbourhood around $x$. The tangent path then covers the intervall $[x+\epsilon_l,x+\epsilon_r]$. This syntax has been choosen to make it easy to move a tangent along a function, keeping its neighbourhood at a fixed size.
151
152 As an example, the following code draws a tangent that touches $f$ at $x=-0.5$ with a neighbourhood $\epsilon = \pm 1$ (figure~\ref{fig:tangent}).%
153 \footnote{Both types of arguments to \cmd{f.getTangent}---$x$ and $\epsilon$ values---have been put in separate pairs of parentheses to make the code more readable. Even if the second pair of parentheses looks like a coordinate of type \cmd{pair}---it isn't. For MetaPost this syntax is equivalent to
154 \begin{center}
155 \cmd{draw f.getTangent(-0.5, -1, 1) transformed T;}
156 \end{center}
157 }
158 \begin{listing}
159 newBPolynomial.f(2, 0, -3, -1);
160 draw f.getPath(-2, 2) transformed T;
161 draw f.getTangent(-0.5)(-1, 1) transformed T;
162 \end{listing}
163
164 \begin{figure}
165 \begin{minipage}[t]{.45\linewidth}
166 \centering
167 \includegraphics{examples.2}
168 \caption{Cubic polynomial with a tangent.}
169 \label{fig:tangent}
170 \end{minipage}\hfill%
171 \begin{minipage}[t]{.45\linewidth}
172 \centering
173 \includegraphics{examples.3}
174 \caption{And a labelled point.}
175 \label{fig:labelled}
176 \end{minipage}
177 \end{figure}
178
179 \subsubsection{Evaluating polynomials}\label{sec:eval}
180 Additionally to getting the \B\ curve or tangent of a polynomial, polynomial functions can also be evaluated numerically at a specific location. Defining a function $\langle$suffix$\rangle$ sets up a macro
181 \begin{center}
182 \cmd{<suffix>.eval}
183 \end{center}
184 that takes as argument an $x$-coordinate and returns the function value at that location. As an example, labelling an arbitrary point on $f$ can be done as follows (figure~\ref{fig:labelled}).
185 \begin{listing}
186 dotlabeldiam := 2bp;
187 labeloffset := 10bp;
188 newBPolynomial.f(2, 0, -3, -1);
189 draw f.getPath(-2, 2) transformed T;
190 x := -0.5;
191 show (x, f.eval(x));
192 draw f.getTangent(x)(-1, 1) transformed T;
193 dotlabel.top(btex $(-0.5, 0.25)$ etex, (x, f.eval(x)) transformed T);
194 \end{listing}
195
196 For simplicity, the label has been provided explicitly in this example (after reading the coordinates off the \cmd{log} file). But it is also possible to attach the correct coordinates automatically with the help of the MetaPost package \name{LaTeXMP} and \LaTeX\ package \name{numprint}. While the former helps passing dynamically generated text from MetaPost to \LaTeX, the latter can be used to format and round numbers.~\cite{latex:numprint, mp:latexmp}.
197
198 \subsection{Plotting square and cubic roots}\label{sec:roots}
199 Two other types of functions can be described by \B\ curves: square and cubic roots%
200 \footnote{The reason square and cubic roots can be expressed in terms of \B\ curves is that both are inverse functions of cubic polynomials. More information can be found in appendix~\ref{app:roots}.}%
201 . The \pkg\ package provides support for square and cubic root functions of the following type
202 \begin{align}
203 f_{sqr}(x) = u \sqrt{x + v} + w\label{eq:square} \\
204 f_{cub}(x) = u \sqrt[3]{x + v} + w\label{eq:cubic}
205 \end{align}
206 through macros
207 \begin{center}
208 \begin{tabular}{l}
209 \cmd{newBSqrRoot} \\
210 \cmd{newBCubRoot} \\
211 \end{tabular}
212 \end{center}
213
214 These macros are similar to \cmd{newBPolynomial}. Both macros take as arguments a suffix $\langle$suffix$\rangle$ and the three parameters $u$, $v$ and $w$ of the respective function. They define three macros
215 \begin{center}
216 \begin{tabular}{l}
217 \cmd{<suffix>.getPath} \\
218 \cmd{<suffix>.eval} \\
219 \cmd{<suffix>.getTangent} \\
220 \end{tabular}
221 \end{center}
222 that can be used the same way as for polynomial functions.
223
224 There is one notable difference between polynomial and root functions. While polynomials $z^k$ are defined for all arguments $z \in \mathbb{R}$, roots $\sqrt[k]{z}$ are only defined for non-negative arguments $z \in \mathbb{R_+}$ and therefore the functions from equations~\ref{eq:square} and~\ref{eq:cubic} are only defined for
225 \begin{equation}
226 x \geq -v.\label{eq:rootargument}
227 \end{equation}
228
229 In case the arguments to macros \cmd{<suffix>.getPath}, \cmd{<suffix>.getTangent} or \cmd{<suffix>.eval} violate equation~\ref{eq:rootargument} all macros write a warning to the log file, but go on with their calculations replacing the arguments in question (or the resulting range boundaries) by $-v$.
230
231 The following example plots two functions $s(x) = \sqrt{x}$ and $c(x) = \sqrt[3]{x}$ with a tangent on $s$ at $x=3$ (figure~\ref{fig:squareroot}).
232 \begin{listing}
233 T := identity scaled 10mm;
234 newBSqrRoot.s(1,0,0);
235 newBCubRoot.c(1,0,0);
236 draw s.getPath(0,6) transformed T;
237 draw c.getPath(0,6) transformed T;
238 draw s.getTangent(3)(-2, 2) transformed T;
239 \end{listing}
240
241 \begin{figure}
242 \begin{minipage}[t]{.55\linewidth}
243 \centering
244 \includegraphics{examples.5}
245 \caption{Square and cubic roots with a tangent.}
246 \label{fig:squareroot}
247 \end{minipage}\hfill%
248 \begin{minipage}[t]{.4\linewidth}
249 \centering
250 \includegraphics{examples.4}
251 \caption{First derivative of a cubic polynomial with tangent.}
252 \label{fig:derivative}
253 \end{minipage}
254 \end{figure}
255
256 \subsection{Dealing with derivatives}\label{sec:derivatives}
257 Since plotting function often involves plotting derivatives of functions, too, the \pkg\ package provides support for plotting derivatives of polynomials and tangents thereof. Unfortunately, derivatives of root functions cannot be described by \B\ curves, so those are not supported.
258
259 In section~\ref{sec:newBPolynomial} it was said macro \cmd{newBPolynomial} defines three new macros. But this is not the full story. In fact, the command
260 \begin{center}
261 \cmd{newBPolynomial.<suffix>}
262 \end{center}
263 defines twelve macros, three of them we already know, \cmd{<suffix>.getPath}, \cmd{<suffix>.eval}, \cmd{<suffix>.getTangent}. The remaining nine macros are similar, but correspond to the first, second and third derivative of a polynomial $\langle$suffix$\rangle$, resp. To access these macros just add the required number of prime characters to the suffix name (three at maximum). For instance, to get the path corresponding to the first derivative of polynomial $\langle$suffix$\rangle$ call
264 \begin{center}
265 \cmd{<suffix>'.getPath}
266 \end{center}
267 and to get a tangent on the first derivative call
268 \begin{center}
269 \cmd{<suffix>'.getTangent}.
270 \end{center}
271
272 In total these are the macros defined by \cmd{newBPolynomial.<suffix>}:
273 \begin{center}
274 \begin{minipage}{.77\textwidth}
275 \begin{multicols}{2}
276 \cmd{<suffix>.getPath} \\
277 \cmd{<suffix>.eval} \\
278 \cmd{<suffix>.getTangent} \\
279 \cmd{<suffix>'.getPath} \\
280 \cmd{<suffix>'.eval} \\
281 \cmd{<suffix>'.getTangent} \\
282 \cmd{<suffix>'{}'.getPath} \\
283 \cmd{<suffix>'{}'.eval} \\
284 \cmd{<suffix>'{}'.getTangent} \\
285 \cmd{<suffix>'{}'{}'.getPath} \\
286 \cmd{<suffix>'{}'{}'.eval} \\
287 \cmd{<suffix>'{}'{}'.getTangent}
288 \end{multicols}
289 \end{minipage}
290 \end{center}
291
292 As an example, the following code draws a tangent on the first derivative of a polynomial \cmd{f} (figure~\ref{fig:derivative}).
293
294 \begin{listing}
295 newBPolynomial.f(2, 0, -3, -1);
296 draw f'.getPath(-2, 2) transformed T;
297 draw f'.getTangent(-0.25)(-1, 1) transformed T;
298 \end{listing}
299
300
301 \subsection{Accessing function parameters}\label{sec:parameters}
302 The parameters passed to one of the function defining macros are saved for internal calculations. These variables can be accessed by users, too, but should not be changed.
303
304 Defining a polynomial with
305 \begin{center}
306 \cmd{newBPolynomial.<suffix>(<a>, <b>, <c>, <d>)}
307 \end{center}
308 defines variables
309 \begin{center}
310 \begin{minipage}{.77\textwidth}
311 \begin{multicols}{2}
312 \cmd{<suffix>.a} \\
313 \cmd{<suffix>.b} \\
314 \cmd{<suffix>.c} \\
315 \cmd{<suffix>.d} \\
316 \cmd{<suffix>'.a} \\
317 \cmd{<suffix>'.b} \\
318 \cmd{<suffix>'.c} \\
319 \cmd{<suffix>'.d} \\
320 \cmd{<suffix>'{}'.a} \\
321 \cmd{<suffix>'{}'.b} \\
322 \cmd{<suffix>'{}'.c} \\
323 \cmd{<suffix>'{}'.d} \\
324 \cmd{<suffix>'{}'{}'.a} \\
325 \cmd{<suffix>'{}'{}'.b} \\
326 \cmd{<suffix>'{}'{}'.c} \\
327 \cmd{<suffix>'{}'{}'.d}
328 \end{multicols}
329 \end{minipage}
330 \end{center}
331 and sets them to the value of the corresponding coefficient.
332
333 For root functions the following variables are used:
334 \begin{center}
335 \begin{tabular}{l}
336 \cmd{<suffix>.u} \\
337 \cmd{<suffix>.v} \\
338 \cmd{<suffix>.w} \\
339 \end{tabular}
340 \end{center}
341
342 Additionally, variables
343 \begin{center}
344 \begin{tabular}{l}
345 \cmd{<suffix>.a} \\
346 \cmd{<suffix>.b} \\
347 \cmd{<suffix>.c} \\
348 \cmd{<suffix>.d} \\
349 \end{tabular}
350 \end{center}
351 contain the coefficients of the inverse polynomial function.
352
353 \subsection{The plain interface}\label{sec:plaininterface}
354 The \pkg\ package provides a plain interface, too. This interface avoids the necessity to define a function prior to calculating paths, but is limited to calculating \B\ curves of explicitly given polynomials and root functions---tangents and derivatives are not supported. The plain interface consists of three macros
355 \begin{center}
356 \begin{tabular}{l}
357 \cmd{getBezierFromPolynomial} \\
358 \cmd{getBezierFromSqrRoot} \\
359 \cmd{getBezierFromCubRoot} \\
360 \end{tabular}
361 \end{center}
362 that take as arguments the parameters of the polynomial or root function plus the range the path should cover.
363
364 For instance, the first example could as well have been drawn with the following code
365 \begin{listing}
366 draw getBezierFromPolynomial(2, 0, -3, -1)(-2, 2) transformed T;
367 \end{listing}
368
369 All three macros call a macro \verb+bpolynomial_getBezierFromPolynomial+ behind the scenes, that does the necessary calculations and, in fact, is the heart of this package. The mathematics behind that macro are described in appendix~\ref{app:mathematics}.
370
371
372 \section{Examples}\label{sec:examples}
373 This section contains some more elaborate examples. The code of all examples is copied here, so that you can easily compare it with the resulting figures. If you want to play around with the code you can also find it in file \cmd{examples.mp}, that actually contains the code of all examples in this manual.
374
375 \emph{One additional note in advance:} While playing with figure~\ref{fig:transparency}, the author noticed a subtle rendering bug in Adobe Reader~7.0.9, that caused the graphs and the filled area not matching exactly. In print or with GSview~4.8 and Ghostscript~8.60 everything looked fine. The problem is actually unrelated to the filling, but there seem to be some numeric issues in the \B\ curve rendering algorithm of Adobe Reader. A work-around is to modifiy problematic paths, \emph{e.\,g.}, by slightly changing their plotting range.
376
377 The first example demonstrates how \pkg\ and John Hobby's \name{graph} package\cite{mp:graph} can be used together to plot polynomials in a coordinate system. The \cmd{draw} command has just to be replaced by \cmd{gdraw}. The latter command additionally clips paths to the boundaries of the coordinate system (see figure~\ref{fig:graph.mp}). Finally, in this example a table of points is printed to the console and \cmd{log} file.
378
379 \begin{figure}
380 \centering
381 \includegraphics{examples.21}
382 \caption{Packages \pkg\ and \name{graph} interacting.}
383 \label{fig:graph.mp}
384 \end{figure}
385
386 \begin{listing}
387 path f, g;
388 xmin := -7; xmax := 7;
389 ymin := -7; ymax := 7;
390 %%% Define polynomials f and g.
391 newBPolynomial.f(0.3, 0, -3, -1);
392 f := f.getPath(xmin, xmax);
393 newBPolynomial.g(0, 0.5, -2, 0);
394 g := g.getPath(xmin, xmax);
395 %%% Draw graph.
396 draw begingraph(10cm, 6cm);
397 setrange(xmin,ymin, xmax,ymax);
398 autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
399 drawoptions(withpen pencircle scaled 1bp);
400 gdraw f;
401 gdraw g dashed evenly scaled 2;
402 drawoptions();
403 endgraph;
404 show f;
405 %%% Write table with some points of f to log file.
406 show "Polynomial: " & decimal f.a & "x^3 + " &
407 decimal f.b & "x^2 + " & decimal f.c & "x + " & decimal f.d;
408 for x=-5 upto 5:
409 show (x, f.eval(x));
410 endfor
411 \end{listing}
412
413 Note command \cmd{show f} that writes path~\cmd{f} to the \cmd{log} file. Inspecting that we can easily verify \cmd{f} consists of just one path segment:
414 \begingroup\small
415 \begin{verbatim}
416 (-7,-82.90105)..controls (-2.33333,108.90105) and (2.33333,-110.90105)
417 ..(7,80.90105)
418 \end{verbatim}
419 \endgroup
420
421 In the next example, a cubic polynomial $f$ is plotted together with its derivatives $f'$, $f''$ and $f'''$. Additionally, for all four functions the tangents are drawn at $x=2$. Admittedly, the plot is a little bit crowded. But it should only serve as an example (figure~\ref{fig:derivatives}).
422
423 \begin{figure}
424 \centering
425 \includegraphics{examples.22}
426 \caption{A cubic polynomial with derivatives and tangents.}
427 \label{fig:derivatives}
428 \end{figure}
429
430 \begin{listing}
431 xmin := -6; xmax := 6;
432 ymin := -6; ymax := 6;
433 newBPolynomial.f(0.3, -0.5, -0.5, -1);
434 draw begingraph(10cm, 6cm);
435 setrange(xmin,ymin, xmax,ymax);
436 autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
437 drawoptions(withpen pencircle scaled 1bp);
438 %%% Draw f and its derivatives f', f'', f'''.
439 gdraw f.getPath(xmin, xmax);
440 gdraw f'.getPath(xmin, xmax) dashed evenly scaled 2;
441 gdraw f''.getPath(xmin, xmax) dashed withdots
442 withpen pencircle scaled 2bp;
443 gdraw f'''.getPath(-5, 5) withcolor .6white;
444 %%% Draw tangents and mark points.
445 x := 2;
446 drawoptions(withcolor (1, 0.6, 0.6));
447 gdraw f.getTangent(x)(-2, 2);
448 gdraw f'.getTangent(x)(-1, 1);
449 gdraw f''.getTangent(x)(-2, 2);
450 gdraw f'''.getTangent(x)(-2, 2);
451 drawoptions(withcolor (0.6, 0.6, 1));
452 dotlabeldiam := 2.5bp;
453 gdotlabel("", (x, f.eval(x)));
454 gdotlabel("", (x, f'.eval(x)));
455 gdotlabel("", (x, f''.eval(x)));
456 gdotlabel("", (x, f'''.eval(x)));
457 drawoptions();
458 endgraph;
459 \end{listing}
460
461 How about some eye candy, \emph{e.\,g.}, transparency effects? In the next example the area enclosed by two functions is filled with a transparent colour (figure~\ref{fig:transparency}).
462
463 Note, advanced PDF features like transparency and shadings are provided by package \name{metafun}.~\cite{mp:metafun} Figures using such features have to be converted to stand-alone PDF files with the \cmd{mptopdf} utility before including them into a \LaTeX\ document.
464
465 \begin{figure}
466 \centering
467 \includegraphics{examples-23}
468 \caption{Applying a transparent fill to an area enclosed by two functions.}
469 \label{fig:transparency}
470 \end{figure}
471
472 \begin{listing}
473 path f, g, A;
474 xmin := -3; xmax := 6;
475 ymin := -3; ymax := 6;
476 newBPolynomial.f(-0.25, 0.5, 2, -1);
477 newBPolynomial.g(0, 0.5, -2, 0);
478 f := f.getPath(-2.5, 5.5);
479 g := g.getPath(-1.5, 5.5);
480 %%% Find area between f and g.
481 A := buildcycle(g, reverse f);
482 draw begingraph(10cm, 6cm);
483 setrange(xmin,ymin, xmax,ymax);
484 autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white;
485 %%% Fill area with transparent colour.
486 gfill A withcolor transparent (1, .3, (1, 0.5, 0));
487 drawoptions(withpen pencircle scaled 1bp);
488 gdraw f;
489 gdraw g dashed evenly scaled 2;
490 drawoptions();
491 endgraph;
492 \end{listing}
493
494 As an alternative to solid fills areas can be emphasized by hatch patterns. Hatching support is provided by package \name{hatching}.~\cite{mp:hatching} Unfortunately, packages \name{graph} and \name{hatching} do not work together. Therefore, in the next example a simple coordinate system had to be drawn manually (figure~\ref{fig:hatching.mp}).
495
496 \begin{figure}
497 \centering
498 \includegraphics{examples.14}
499 \caption{Hatching an area enclosed by two functions.}
500 \label{fig:hatching.mp}
501 \end{figure}
502
503 \begin{listing}
504 path f, g, A;
505 T := identity xscaled 10mm yscaled 6mm;
506 %%% Draw coordinate system.
507 xmin := -3; xmax := 6;
508 ymin := -3; ymax := 6;
509 drawoptions(withpen pencircle scaled 1bp withcolor 0.8white);
510 drawarrow ((xmin,0)--(xmax,0)) transformed T;
511 drawarrow ((0,ymin)--(0,ymax)) transformed T;
512 newBPolynomial.f(-0.25, 0.5, 2, -1);
513 newBPolynomial.g(0, 0.5, -2, 0);
514 f := f.getPath(-2.5, 4.2);
515 g := g.getPath(-1, 5);
516 A := buildcycle(g, reverse f);
517 %%% Fill area with pattern.
518 drawoptions();
519 hatchoptions(withcolor (0.6, 0.3, 0.3));
520 hatchfill A transformed T
521 withcolor (-45, 2mm, -0.5bp) withcolor (45, 2mm, -0.5bp);
522 drawoptions(withpen pencircle scaled 1bp);
523 draw f transformed T;
524 draw g transformed T dashed evenly scaled 2;
525 \end{listing}
526
527
528 \appendix
529 \section{Calculating \B\ curves}\label{app:mathematics}
530 \subsection{Polynomials}\label{app:polynomials}
531 %Internally, paths are \B\ curves and MetaPost is able to calculate the points along such a curve.%
532 %\footnote{Since PostScript has a concept of \B\ curves, too, for MetaPost drawing a path is simply an act of copying the parameters of the corresponding \B\ curve into PostScript output. But nonetheless MetaPost \emph{can} calculate points on a \B\ curve.}
533 %
534 A \B\ curve $P(t)$ with end points $A=(x_A,y_A)$ and $D=(x_D,y_D)$ and control points $B=(x_B,y_B)$ and $C=(x_C,y_C)$ is defined as~\cite{mfbook}
535 \begin{equation}
536 P(t) = \left(
537 \begin{array}{@{}c@{}}
538 x\\
539 y\\
540 \end{array}
541 \right)(t) = (1-t)^3A + 3(1-t)^2tB + 3(1-t)t^2C + t^3D,\quad 0\leq t\leq 1.
542 \end{equation}
543
544 This equation can be rewritten as
545 \begin{equation}
546 P(t) = A + 3(B-A)t + 3(C-2B+A)t^2 + (D-3C+3B-A)t^3,\quad 0\leq t\leq 1.
547 \end{equation}
548
549 An arbitrary function $y=f(x)$ can be written in parameter form as
550 \begin{equation}
551 F(t) = \left(
552 \begin{array}{@{}c@{}}
553 x \\
554 y \\
555 \end{array}
556 \right)(t) = \left(
557 \begin{array}{@{}c@{}}
558 x(t) \\
559 f\big(x(t)\big) \\
560 \end{array}
561 \right),\quad t\in \mathbb{R}
562 \end{equation}
563 with parameter $t$.
564
565 For a polynomial function
566 \begin{equation}
567 f(x) = ax^3 + bx^2 + cx + d,\quad x\in [x_0, x_1]
568 \end{equation}
569 we have
570 \begin{equation}
571 x(t) = x_0 + (x_1-x_0)t,\quad 0\leq t\leq 1
572 \end{equation}
573 and hence
574 \begin{equation}
575 F(t) = \left(
576 \begin{array}{@{}c@{}}
577 x_0 + (x_1-x_0)t \\
578 ax(t)^3 + bx(t)^2 + cx(t) + d \\
579 \end{array}
580 \right),\quad 0\leq t\leq 1.
581 \end{equation}
582 Writing F(t) down explicitly is left as an exercise for the interested reader.
583
584 Finally, setting
585 \begin{equation}
586 P(t) = F(t)
587 \end{equation}
588 and sorting the coefficients of the $t^k$ one arrives at the following \emph{original} equation system:
589 \begin{align}
590 x_A & = x_0 \label{eq:xA} \\
591 3(x_B-x_A) & = x_1 - x_0 \label{eq:xB} \\
592 3(x_C-2x_B+x_A) & = 0 \label{eq:xC} \\
593 x_D-3x_C+3x_B-x_A & = 0 \label{eq:xD} \\
594 y_A & = ax_0^3 + bx_0^2 + cx_0 + d \label{eq:yA} \\
595 3(y_B-y_A) & = 3ax_0^2(x_1-x_0) + 2bx_0(x_1-x_0) + c(x_1-x_0) \label{eq:yB} \\
596 3(y_C-2y_B+y_A) & = 3ax_0(x_1-x_0)^2 + b(x_1-x_0)^2 \label{eq:yC} \\
597 y_D-3y_C+3y_B-y_A & = a(x_1-x_0)^3 \label{eq:yD}
598 \end{align}
599 Note, there are only constants on the right-hand side of all equations. That is, this equation system is linear in the eight variables $x_A$, $x_B$, $x_C$, $x_D$, $y_A$, $y_B$, $y_C$, $y_D$.
600
601 Since MetaPost can solve linear equation systems, hacking equations~\ref{eq:xA} to~\ref{eq:yD} into MetaPost code and requesting a path segment
602 \begin{center}\ttfamily
603 ($x_A$,$y_A$)..controls ($x_B$,$y_B$) and ($x_C$,$y_C$)..($x_D$,$y_D$)
604 \end{center}
605 returns the polynomial shaped curve we are looking for.
606
607 Internally, the \pkg\ package does not solve the original equation system, but a \emph{modified} variant, that is numerically slightly more robust.
608
609 Equations~\ref{eq:xA} to~\ref{eq:xD} can be written down explicitly as
610 \begin{align}
611 x_A & = x_0 \label{eq:xA'} \\
612 x_B & = x_0 + \frac{1}{3}(x_1-x_0) \label{eq:xB'} \\
613 x_C & = x_1 - \frac{1}{3}(x_1-x_0) \label{eq:xC'} \\
614 x_D & = x_1 \label{eq:xD'}
615 \end{align}
616
617 Additionally, we know that $D=(x_D,y_D)$ is a point on the polynomial. Therefore, equation~\ref{eq:yD} of the original system can be replaced by
618 \begin{align}
619 y_D & = ax_1^3 + bx_1^2 + cx_1 + d \label{eq:yD'}
620 \end{align}
621
622 Equations~\ref{eq:yA} to~\ref{eq:yC} of the original equation system and the new equations~\ref{eq:xA'} to~\ref{eq:yD'} constitute the modified equation system, that is solved in \pkg.
623
624 \subsection{Square and cubic roots}\label{app:roots}
625 When requesting the \B\ curve of a root function, the \pkg\ package first calculates the inverse function, then calculates the corresponding path and finally reflects that on the function $f(x)=x$ to get the inverse again.
626
627 The inverse functions of the root functions from equations~\ref{eq:square} and~\ref{eq:cubic} are
628 \begin{align}
629 \widetilde f_{sqr}(y) & = \left(\frac{y-w}{u}\right)^2 - v = 0 y^3 + \frac{1}{u^2} y^2 + \frac{-2w}{u^2} y + \frac{w^2}{u^2} - v \\
630 \widetilde f_{cub}(y) & = \left(\frac{y-w}{u}\right)^3 - v = \frac{1}{u^3} y^3 + \frac{-3w}{u^3} y^2 + \frac{3w^2}{u^3} y + \frac{w^3}{u^3} - v
631 \end{align}
632 which both are clearly polynomials.
633
634 \nobreak
635 \bigskip
636 \raggedright
637 \parbox{\linewidth}{\itshape
638 Happy \TeX ing!\par
639 Stephan Hennig
640 }
641
642
643 \begin{thebibliography}{999}
644 \bibitem{mp:metafun} \textsc{Hagen}, Hans, \emph{metafun}, \url{http://www.pragma-ade.com/general/manuals/metafun-p.pdf}
645 \bibitem{latex:numprint} \textsc{Harders}, Harald, \emph{The numprint package}, 2007, \url{CTAN:macros/latex/contrib/numprint/numprint.pdf}
646 \bibitem{mp:graph} \textsc{Hobby}, John~D., \emph{Drawing graphs with MetaPost}, \url{http://www.tug.org/docs/metapost/mpgraph.pdf}
647 \bibitem{mp:hatching} \textsc{Jackowski}, B., \emph{hatching.mp}, \url{CTAN:graphics/metapost/contrib/macros/hatching/README}
648 \bibitem{mfbook} \textsc{Knuth}, Donald~E., \emph{The METAFONTbook}, Addison-Wesley, Reading, Massachusetts, 1986, (Computers \& Typesetting, C)
649 \bibitem{mp:splines} \textsc{Luecking}, Dan, \emph{Macros to compute splines}, 2005, \url{CTAN:graphics/metapost/contrib/macros/splines/splines.pdf}
650 \bibitem{mp:latexmp} \textsc{Morawski}, Jens-Uwe, \emph{latexMP}, 2005, \url{CTAN:graphics/metapost/contrib/macros/latexmp/doc/latexmp.pdf}
651 \end{thebibliography}
652
653 \end{document}
654
655 %%% Local Variables:
656 %%% mode: latex
657 %%% TeX-PDF-mode: t
658 %%% TeX-master: t
659 %%% End: