29.11.2012 Views

MetaFun - Pragma ADE

MetaFun - Pragma ADE

MetaFun - Pragma ADE

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

First there is the concept of variables and assignments. There are several data types, some of which<br />

we already have seen.<br />

numeric real number in the range −4096. . . + 4096<br />

boolean a variable that takes one of two states: true or false<br />

pair point or vector in 2--dimensional space<br />

path a piecewise collection of curves and line segments<br />

picture collection of stroked or filled paths<br />

string sequence of characters, like "metapost"<br />

color vector of three (rgb) or four (cmyk) numbers<br />

There are two additional types, transform and pen, but we will not discuss these in depth.<br />

transform transformation vector with six elements<br />

pen pen specification<br />

You can achieve interesting effects by using pens with certain shapes. For the moment you may<br />

consider a pen to be a path itself that is applied to the path that is drawn.<br />

The numeric data type is used so often that it is the default type of any non declared variable. This<br />

means that<br />

n := 10 ;<br />

is the same as<br />

numeric n ; n := 10 ;<br />

When writing collections of macros, it makes sense to use the second method, because you can<br />

never be sure if n isn't already declared as a picture variable, and assigning a numeric to a picture<br />

variable is not permitted.<br />

Because we often deal with collections of objects, such as a series of points, all variables can be<br />

organized in arrays. For instance:<br />

numeric n[] ; n[3] := 10 ; n[5] := 13 ;<br />

An array is a collection of variables of the same type that are assigned and accessed by indexing<br />

the variable name, as in n[3] := 5. Multi--dimensional arrays are also supported. Since you need<br />

a bit of imagination to find an application for 5--dimensional arrays, we restrict ourselves to a two-dimensional<br />

example.<br />

numeric n[][] ; n[2][3] := 10 ;<br />

A nice feature is that the bounds of such an array needs not to be set beforehand. This also means<br />

that each cell that you access is reported as unknown unless you have assigned it a value.<br />

Behind the screens there are not really arrays. It's just a matter of creating hash entries. It might<br />

not be obvious, but the following assignments are all equivalent:<br />

Variables Welcome to MetaPost<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!