| 1 |
\section{Data types} |
| 2 |
\label{sec:datatypes} |
| 3 |
|
| 4 |
\subsection{Standard data types} |
| 5 |
There are ten data types in \MP{}: \textit{numeric}, \textit{pair}, |
| 6 |
\textit{path}, \textit{transform}, \textit{rgbcolor}, |
| 7 |
\textit{cmykcolor}, \textit{string}, \textit{boolean}, \textit{picture}, |
| 8 |
and \textit{pen}. These data types allow users to store fragments of |
| 9 |
the graphics for later use. We will briefly discuss each of these data |
| 10 |
types and elaborate on how they are used in a typical \MP{} program. |
| 11 |
|
| 12 |
\renewcommand{\labelitemi}{$\diamond$} |
| 13 |
\begin{itemize} |
| 14 |
\item \textit{numeric}\Dash numbers |
| 15 |
\item \textit{pair}\Dash ordered pairs of numerics |
| 16 |
\item \textit{path}\Dash B\'{e}zier curves (and lines) |
| 17 |
\item \textit{picture}\Dash pictures |
| 18 |
\item \textit{transform}\Dash transformations such as shifts, rotations, |
| 19 |
and slants |
| 20 |
\item \textit{rgbcolor} or \textit{color}\Dash triplets with each |
| 21 |
component between $0$ and $1$ (red, green, and blue) |
| 22 |
\item \textit{cmykcolor}\Dash quadruplets with each component between |
| 23 |
$0$ and $1$ (cyan, magenta, yellow, and black) |
| 24 |
\item \textit{string}\Dash strings of characters |
| 25 |
\item \textit{boolean}\Dash ``true'' or ``false'' values |
| 26 |
\item \textit{pen}\Dash stroke properties |
| 27 |
\end{itemize} |
| 28 |
|
| 29 |
Virtually all programming languages provide a way of storing and |
| 30 |
retrieving numerical values. This is precisely the purpose of the |
| 31 |
\textit{numeric} data type in \MP. Since graphics drawn with \MP{} are |
| 32 |
simply two dimensional pictures, it is clear that an ordered pair is |
| 33 |
needed to identify each point in the picture. The \textit{pair} data |
| 34 |
type provides this functionality. Each point in the plane consists of |
| 35 |
an $x$ (i.e., abscissa) part and a $y$ (i.e., ordinate) part. \MP{} |
| 36 |
uses the standard syntax for defining points in the plane, e.g., $(x,y)$ |
| 37 |
where both $x$ and~$y$ are numeric data typed variables. |
| 38 |
|
| 39 |
In order to store paths between points, the \textit{path} data type is |
| 40 |
used. All paths in \MP{} are represented as cubic B\'{e}zier curves. |
| 41 |
Cubic B\'{e}zier curves are simply parametric splines of the form |
| 42 |
$(x(t),y(t))$ where both $x(t)$ and $y(t)$ are piecewise cubic |
| 43 |
polynomials of a common parameter $t$. Since B\'{e}zier curves are |
| 44 |
splines, they pairwise interpolate the points. Furthermore, cubic |
| 45 |
B\'{e}zier curves are diverse enough to provide a ``smooth'' path |
| 46 |
between all of the points for which it interpolates. \MP{} provides |
| 47 |
several methods for affecting the B\'{e}zier curve between a list of |
| 48 |
points. For example, piecewise linear paths (i.e., linear splines) can |
| 49 |
be drawn between a list of points since all linear polynomials are also |
| 50 |
cubic polynomials. Furthermore, if a specific direction for the path is |
| 51 |
desired at a given point, this constraint can be forced on the |
| 52 |
B\'{e}zier curve. |
| 53 |
|
| 54 |
The \textit{picture} data type is used to store an entire picture for |
| 55 |
later use. For example, in order to create animations, usually there |
| 56 |
are objects that remain the same throughout each frame of the animation. |
| 57 |
So that these objects do not have to be manually drawn for each frame, a |
| 58 |
convenient method for redrawing them is to store them into a picture |
| 59 |
variable for later use. |
| 60 |
|
| 61 |
When constructing pairs, paths, or pictures in \MP{}, it is often |
| 62 |
convenient to apply affine transformations to these objects. As |
| 63 |
mentioned above, \autoref{fig:circles} can be constructed by rotating |
| 64 |
the same circle several times before drawing it. \MP{} provides |
| 65 |
built-in affine transformations as ``building blocks'' from which other |
| 66 |
transformations can be constructed. These include shifts, rotations, |
| 67 |
horizontal and vertical scalings, and slantings. |
| 68 |
|
| 69 |
For creating colored graphics, \MP{} provides two data types: |
| 70 |
\textit{rgbcolor} and \textit{cmykcolor}. These data types correspond |
| 71 |
to the two supported color models \RGB{} and \CMYK. While using the |
| 72 |
\RGB{} color model, fractions of the primary colors |
| 73 |
\textit{red}~\showcol{red}, \textit{green}~\showcol{green}, and |
| 74 |
\textit{blue}~\showcol{blue} are ``additively mixed''. Similarly, in |
| 75 |
the \CMYK{} color model, the primary colors |
| 76 |
\textit{cyan}~\showcol{cyan}, \textit{magenta}~\showcol{magenta}, |
| 77 |
\textit{yellow}~\showcol{yellow}, and \textit{black}~\showcol{black} are |
| 78 |
``subtractively mixed.'' The former model is suitable for on-screen |
| 79 |
viewing whereas the latter model is preferred in high-quality print. |
| 80 |
Both color types are ordered tuples, $(c_1,c_2,c_3)$ and |
| 81 |
$(c_1,c_2,c_3,c_4)$, with components~$c_i$ being \textit{numeric}s |
| 82 |
between $0$ and $1$. For example, in the \RGB{} color model, a light |
| 83 |
orange tone can be referred to as |(1,.6,0)|~\showcol[rgb]{1,.6,0}, |
| 84 |
whereas in the \CMYK{} color model |(0,.6,1,0)|~\showcol[cmyk]{0,.6,1,0} |
| 85 |
corresponds to a clearly different orange tone. If a particular color |
| 86 |
is to be used several times throughout a figure, it is natural to store |
| 87 |
this color into a variable of type \textit{rgbcolor} or |
| 88 |
\textit{cmykcolor}. |
| 89 |
|
| 90 |
The data type \textit{color} is a convenient synonym for |
| 91 |
\textit{rgbcolor}. Additionally, there are five built-in \RGB{} colors |
| 92 |
in \MP{}: |black|, |white|, |red|, |green|, and |blue|. So, the |
| 93 |
expression |.4(red+blue)| refers to a dark violet~\showcol[rgb]{.4,0,.4} |
| 94 |
in the \RGB{} color model and in the example above |(1,.6,0)| could be |
| 95 |
replaced by |red+.6green|. |
| 96 |
|
| 97 |
The most common application of \textit{string} data types is reusing a |
| 98 |
particular string that is typeset (or labeled). The \textit{boolean} |
| 99 |
data type is the same as in other programming languages and is primarily |
| 100 |
used in conditional statements for testing. Finally, the \textit{pen} |
| 101 |
data type is used to affect the actual stroke paths. The default unit |
| 102 |
of measurement in \MP{} is $1\,\mathrm{bp}=1/72\mathrm{\ in}$, and the |
| 103 |
default thickness of all stroked paths is $0.5\,\mathrm{bp}$. An |
| 104 |
example for using the \textit{pen} data type may include changing the |
| 105 |
thickness of several stroked paths. This new pen can be stored and then |
| 106 |
referenced for drawing each of the paths. |
| 107 |
|
| 108 |
The following code declares a variable of type \textit{numeric}, one of |
| 109 |
type \textit{pair}, and two \textit{string} variables: |
| 110 |
|
| 111 |
\begin{lstlisting}[style=MP] |
| 112 |
numeric idx; |
| 113 |
pair v; |
| 114 |
string s, name; |
| 115 |
\end{lstlisting} |
| 116 |
|
| 117 |
Note, variables of type \textit{numeric} need not necessarily be |
| 118 |
declared. A formerly undeclared variable is automatically assumed to be |
| 119 |
\textit{numeric} at first use. |
| 120 |
|
| 121 |
|
| 122 |
\subsection{Arrays} |
| 123 |
Just like many other programming languages MetaPost provides a way to |
| 124 |
access variables by index. After the following variable declaration |
| 125 |
|
| 126 |
\begin{lstlisting}[style=MP] |
| 127 |
pair a[]; |
| 128 |
\end{lstlisting} |
| 129 |
it is possible to store points in the ``array''~|a| with numeric values |
| 130 |
as index. The console output of |
| 131 |
|
| 132 |
\begin{lstlisting}[style=MP] |
| 133 |
a[1] := (0,1); |
| 134 |
a[2] := (0,5); |
| 135 |
a[3] := (10,20); |
| 136 |
show a[1]; |
| 137 |
show a1; |
| 138 |
j := 2; |
| 139 |
show a[j] + a[j+1]; |
| 140 |
\end{lstlisting} |
| 141 |
is |
| 142 |
|
| 143 |
\begin{lstlisting}[style=text] |
| 144 |
>> (0,1) |
| 145 |
>> (0,1) |
| 146 |
>> (10,25) |
| 147 |
\end{lstlisting} |
| 148 |
|
| 149 |
Notice, the point stored at array index~1 can be referred to as~|a[1]| |
| 150 |
as well as just~|a1|, omitting the brackets. The latter |
| 151 |
convenient---and often practised---notation works as long as the index |
| 152 |
is a plain numeric value. If the index is a numeric \emph{variable} or |
| 153 |
an expression, however, the brackets have to be present, since, e.g., |
| 154 |
|aj| would clearly refer to an unrelated variable of that name instead |
| 155 |
of index~|j| of variable~|a|. |
| 156 |
|
| 157 |
Aside, MetaPost, as a macro language, doesn't really provide true arrays. |
| 158 |
However, from a user's perspective, the MetaPost way of indexing |
| 159 |
variables perfectly looks like an array. |