Next: , Previous: Installation, Up: Top   [Contents][Index]


3 Tutorial

3.1 Drawing in batch mode

To draw a line from coordinate (0,0) to coordinate (100,100), create a text file test.asy containing

draw((0,0)--(100,100));

Then execute the command

asy -V test

Alternatively, MSDOS users can drag and drop test.asy onto the Desktop asy icon (or make Asymptote the default application for the extension asy).

This method, known as batch mode, outputs a PostScript file test.eps. If you prefer PDF output, use the command line

asy -V -f pdf test

In either case, the -V option opens up a viewer window so you can immediately view the result:


diagonal

Here, the -- connector joins the two points (0,0) and (100,100) with a line segment.

3.2 Drawing in interactive mode

Another method is interactive mode, where Asymptote reads individual commands as they are entered by the user. To try this out, enter Asymptote’s interactive mode by clicking on the Asymptote icon or typing the command asy. Then type

draw((0,0)--(100,100));

followed by Enter, to obtain the above image. At this point you can type further draw commands, which will be added to the displayed figure, erase to clear the canvas,

input test;

to execute all of the commands contained in the file test.asy, or quit to exit interactive mode. You can use the arrow keys in interactive mode to edit previous lines. The tab key will automatically complete unambiguous words; otherwise, hitting tab again will show the possible choices. Further commands specific to interactive mode are described in Interactive mode.

3.3 Figure size

In Asymptote, coordinates like (0,0) and (100,100), called pairs, are expressed in PostScript "big points" (1 bp = 1/72 inch) and the default line width is 0.5bp. However, it is often inconvenient to work directly in PostScript coordinates. The next example produces identical output to the previous example, by scaling the line (0,0)--(1,1) to fit a rectangle of width 100.5 bp and height 100.5 bp (the extra 0.5bp accounts for the line width):

size(100.5,100.5);
draw((0,0)--(1,1));

diagonal

One can also specify the size in pt (1 pt = 1/72.27 inch), cm, mm, or inches. Two nonzero size arguments (or a single size argument) restrict the size in both directions, preserving the aspect ratio. If 0 is given as a size argument, no restriction is made in that direction; the overall scaling will be determined by the other direction (see size):

size(0,100.5);
draw((0,0)--(2,1),Arrow);

bigdiagonal

To connect several points and create a cyclic path, use the cycle keyword:

size(3cm);
draw((0,0)--(1,0)--(1,1)--(0,1)--cycle);

square

For convenience, the path (0,0)--(1,0)--(1,1)--(0,1)--cycle may be replaced with the predefined variable unitsquare, or equivalently, box((0,0),(1,1)).

To make the user coordinates represent multiples of exactly 1cm:

unitsize(1cm);
draw(unitsquare);

3.4 Labels

Adding labels is easy in Asymptote; one specifies the label as a double-quoted LaTeX string, a coordinate, and an optional alignment direction:

size(3cm);
draw(unitsquare);
label("$A$",(0,0),SW);
label("$B$",(1,0),SE);
label("$C$",(1,1),NE);
label("$D$",(0,1),NW);

labelsquare

Asymptote uses the standard compass directions E=(1,0), N=(0,1), NE=unit(N+E), and ENE=unit(E+NE), etc., which along with the directions up, down, right, and left are defined as pairs in the Asymptote base module plain (a user who has a local variable named E may access the compass direction E by prefixing it with the name of the module where it is defined: plain.E).

3.5 Paths

This example draws a path that approximates a quarter circle, terminated with an arrowhead:

size(100,0);
draw((1,0){up}..{left}(0,1),Arrow);

quartercircle

Here the directions up and left in braces specify the incoming and outgoing directions at the points (1,0) and (0,1), respectively.

In general, a path is specified as a list of points (or other paths) interconnected with --, which denotes a straight line segment, or .., which denotes a cubic spline (see Bezier curves). Specifying a final ..cycle creates a cyclic path that connects smoothly back to the initial node, as in this approximation (accurate to within 0.06%) of a unit circle:

path unitcircle=E..N..W..S..cycle;

An Asymptote path, being connected, is equivalent to a Postscript subpath. The ^^ binary operator, which requests that the pen be moved (without drawing or affecting endpoint curvatures) from the final point of the left-hand path to the initial point of the right-hand path, may be used to group several Asymptote paths into a path[] array (equivalent to a PostScript path):

size(0,100);
path unitcircle=E..N..W..S..cycle;
path g=scale(2)*unitcircle;
filldraw(unitcircle^^g,evenodd+yellow,black);


superpath

The PostScript even-odd fill rule here specifies that only the region bounded between the two unit circles is filled (see fillrule). In this example, the same effect can be achieved by using the default zero winding number fill rule, if one is careful to alternate the orientation of the paths:

filldraw(unitcircle^^reverse(g),yellow,black);

The ^^ operator is used by the box(triple, triple) function in the module three.asy to construct the edges of a cube unitbox without retracing steps (see three):

import three;

currentprojection=orthographic(5,4,2,center=true);

size(5cm);
size3(3cm,5cm,8cm);

draw(unitbox);

dot(unitbox,red);

label("$O$",(0,0,0),NW);
label("(1,0,0)",(1,0,0),S);
label("(0,1,0)",(0,1,0),E);
label("(0,0,1)",(0,0,1),Z);

cube

See section graph (or the online Asymptote gallery and external links posted at http://asymptote.sourceforge.net) for further examples, including two-dimensional and interactive three-dimensional scientific graphs. Additional examples have been posted by Philippe Ivaldi at http://www.piprime.fr/asymptote. A user-written Asymptote tutorial is available at

http://www.artofproblemsolving.com/Wiki/index.php/Asymptote:_Basics

Next: , Previous: Installation, Up: Top   [Contents][Index]