| 1 |
\subsection{The \texttt{fill} command} |
| 2 |
|
| 3 |
Another common command in \MP{} is the |fill| command. This is used to |
| 4 |
fill closed paths (or cycles). In order to construct a cycle, |cycle| |
| 5 |
may be appended to the path declaration. For example, |
| 6 |
|
| 7 |
\begin{lstlisting}[style=MP] |
| 8 |
path p; |
| 9 |
p := z1{right}..z2{dir 45}..{up}z3--cycle; |
| 10 |
fill p withcolor red; |
| 11 |
draw p; |
| 12 |
\end{lstlisting} |
| 13 |
produces \autoref{fig:fill}. Notice that |p| is essentially the same |
| 14 |
curved path as in \autoref{fig:draw1} with the additional piece that |
| 15 |
connects |z3| back to |z1| with a line segment using |--cycle|. |
| 16 |
|
| 17 |
\begin{figure} |
| 18 |
\begin{withattachment}{fill.mp} |
| 19 |
\centering |
| 20 |
\includegraphics{fill.mps} |
| 21 |
\end{withattachment} |
| 22 |
\caption{\texttt{fill} example} |
| 23 |
\label{fig:fill} |
| 24 |
\end{figure} |
| 25 |
|
| 26 |
Just as it is necessary to fill closed paths, it may also be necessary |
| 27 |
to \textit{unfill} closed paths. For example, the annulus in |
| 28 |
\autoref{fig:annulus1} can be constructed by |
| 29 |
|
| 30 |
\begin{lstlisting}[style=MP] |
| 31 |
color bbblue; |
| 32 |
bbblue := (3/5,4/5,1); |
| 33 |
path p,q; |
| 34 |
p := fullcircle scaled (2*54); |
| 35 |
q := fullcircle scaled (2*27); |
| 36 |
fill p withcolor bbblue; |
| 37 |
unfill q; |
| 38 |
draw p; |
| 39 |
draw q; |
| 40 |
\end{lstlisting} |
| 41 |
|
| 42 |
The |fullcircle| path is a built-in path that closely |
| 43 |
approximates a circle in \MP{} with diameter 1\,bp traversed |
| 44 |
counter-clockwise. This path is not exactly a circle since it is |
| 45 |
parameterized by a B\'{e}zier curve and not by trigonometric functions; |
| 46 |
however, visually it is essentially indistinguishable from an exact |
| 47 |
circle. |
| 48 |
|
| 49 |
\begin{figure} |
| 50 |
\begin{withattachment}{annulus.mp} |
| 51 |
\centering |
| 52 |
\includegraphics{annulus-1.mps} |
| 53 |
\end{withattachment} |
| 54 |
\caption{\texttt{unfill} example} |
| 55 |
\label{fig:annulus1} |
| 56 |
\end{figure} |
| 57 |
|
| 58 |
Notice that |p| is a |fullcircle| of radius 54\,bp (3/4\,in) and |q| is |
| 59 |
a |fullcircle| of radius 27\,bp (3/8\,in). The annulus is constructed |
| 60 |
by filling |p| with the baby blue color |bbblue| and then unfilling |q|. |
| 61 |
The |unfill| command above is equivalent to |
| 62 |
|
| 63 |
\begin{lstlisting}[style=MP] |
| 64 |
fill q withcolor background; |
| 65 |
\end{lstlisting} |
| 66 |
where |background| is a built-in color which is |white| by default. |
| 67 |
|
| 68 |
Often the |unfill| command appears to be the natural method for |
| 69 |
constructing figures like \autoref{fig:annulus1}. However, the |fill| |
| 70 |
and |unfill| commands in \autoref{fig:annulus1} can be replaced by |
| 71 |
|
| 72 |
\begin{lstlisting}[style=MP] |
| 73 |
fill p--reverse q--cycle withcolor bbblue; |
| 74 |
\end{lstlisting} |
| 75 |
|
| 76 |
\begin{figure} |
| 77 |
\begin{withattachment}{annulus.mp} |
| 78 |
\centering |
| 79 |
\includegraphics{annulus-2.mps} |
| 80 |
\end{withattachment} |
| 81 |
\caption{Avoiding an \texttt{unfill}} |
| 82 |
\label{fig:annulus2} |
| 83 |
\end{figure} |
| 84 |
|
| 85 |
The path |p--reverse q--cycle| travels around |p| in a counter-clockwise |
| 86 |
directions (since this is the direction that |p| traverses) followed by |
| 87 |
a line segment to connect to |q|. It then traverses clockwise around |
| 88 |
|q| (using the |reverse| operator) and finally returns to the starting |
| 89 |
point along a line segment using |--cycle|. This path is illustrated in |
| 90 |
\autoref{fig:annulus2}. One reason for using this method to construct |
| 91 |
the annulus as opposed to the |unfill| command is to ensure |
| 92 |
\textit{proper transparency} when placing the figure in an external |
| 93 |
document with a non-white background. If the former method is used and |
| 94 |
the annulus is placed on a non-white background, say magenta, then the |
| 95 |
result is \autoref{fig:annulus3}. |
| 96 |
|
| 97 |
\begin{figure} |
| 98 |
\begin{withattachment}{annulus.mp} |
| 99 |
\centering |
| 100 |
\includegraphics{annulus-3.mps} |
| 101 |
\end{withattachment} |
| 102 |
\caption{Improper transparency using \texttt{unfill}} |
| 103 |
\label{fig:annulus3} |
| 104 |
\end{figure} |
| 105 |
|
| 106 |
It may be desired to have the interior of |q| be magenta instead of |
| 107 |
|white|. This could be accomplished by redefining |background|; |
| 108 |
however, the latter method described above is a much simpler solution. |