Asymptote: Basics

Asymptote (Vector Graphics Language)
Getting Started - Basics - Drawing - Reference - Examples - Macros and Packages - Advanced Asymptote - 3D Graphics - Help

Useful functions - CSE5 Package - How to


On the AoPS forums, you must start the Asymptote diagram with the [asy] tag and end with the [/asy] tag. Each command in Asymptote must be seperated by a semicolon (;). This tells Asymptote where each command ends. For example, the command draw(A--B) draws a line segment from point $A$ to $B$ given as coordinate pairs. Thus, if you wanted to draw two line segments, say one of them from (0,0) to (50,50) (in units of PostScript points - see below for the explanation of units and size) and the other from (50,0) to (0,50), you can create the following document:


The two commands do not need to be on separate lines; it is the semicolon that separates them. However, putting the commands on separate lines does not change the output, so it is often useful to separate your commands in this way in order to organize your code. Whitespaces before and after commands are also not read by Asymptote, so any line can be indented as far as desired for clarity's sake.

To write comments in your code that do not affect the output at all, simply start a line with two forward slashes: // as in

// The following line is drawn from (0,0) to (50,50)

Multi-line comment can be declared with /* and ending in */.

/* The  
following line 
is drawn from 
(0,0) to (50,50) */

This is useful for finding where your mistakes are because you can comment out parts, and if it has a mistake, then the diagram will not produce an error.

Variables and Data Types

Asymptote parses your code into substrings which have a certain data type, for example a real (like $1.5$) or a pair (like $(2,3)$). Each new variable that you declare must be declared as a data type that Asymptote recognizes, by the command [datatype] [variable];. For example, if you wanted to declare the variable $n$ to have type integer, you can use the command

 int n;

After it is declared, you can store a specific value in a variable using the $=$ symbol, as in


These two commands can be abbreviated by the single command int n=3;, and several integers can also be declared at once (int m,n,d;) or even many declarations and assignations at once: int a,b=2,c,d=5;.

As another example of variable declaration, consider the picture of the X from the Syntax section above. The same picture can be made as follows:

   pair A,B,C,D;

In this particular example, variable declarations made the code longer, but as you will see, declaring variables will significantly clean up your code in messier diagrams.

The most commonly used data types in Asymptote are given in the following table:


Size and Unitsize

Asymptote is a primarily coordinate-based graphics language. Each point is a pair $(a,b)$ where $a$ is the $x$-coordinate and $b$ is the $y$-coordinate.

However, there are many ways to choose a Cartesian coordinate system for the plane; one must pick the placement of origin and the scale on each of the $x$- and $y$-axes. Asymptote will place your image in the center of your output page after it is drawn, so placement of origin is actually irrelevant. By default, the unit length in both the $x$ and $y$ directions is the PostScript bigpoint, which has length $1/72$ inches. Thus, if you do not change the scaling on the picture, the points $(0,0)$ and $(72,0)$ are exactly one inch apart when drawn in Asymptote. However, drawing in bigpoints is inconvenient if you wish to draw a figure that is exactly 3cm wide.

The function unitsize can be used to specify the unit length for your picture. This function takes up to 3 arguments: the picture you want to scale the axes for (if this isn't specified, it defaults to currentpicture, the picture you are drawing on), the unit length in the x direction, and the unit length in the y direction. If only one real argument is given, both the x and y unit sizes are set to this number. Thus the command


will tell Asymptote that from now on, your unit length is $1$ inch. Be careful when you are redefining your unit length - now that unitsize is set to $72$, the points $(0,0)$ and $(72,0)$ are actually $72$ inches apart!

Asymptote has the built-in constants pt (1/72.27 inches), inch, cm, and mm for convenience when defining lengths, so the above command can also be stated


The other useful function is size, which specifies the exact width and height of the box that your picture (if unspecified as a first argument, this will again default to currentpicture) will be fit into. If only one number is given, both the width and the height will be set to this number. For example, the command


or just


will fit the diagram to a 5cm x 5cm box regardless of the specified unitsizes.

As an example, make an Asymptote document containing the following two lines:


and see what happens as you change the $2$ inch size to several other values.

Next: Drawing


Stay Connected

Subscribe to get news and
updates from AoPS, or Contact Us.
© 2017
AoPS Incorporated
Invalid username
Login to AoPS