Asymptote

Asymptote (Vector Graphics Language)

(Redirected from Asymptote: About)
Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

Asymptote is a powerful vector graphics language designed for creating mathematical diagrams and figures. It can output images in either eps or pdf format, and is compatible with the standard mathematics typesetting language, LaTeX. It is also a complete programming language, and has cleaner syntax than its predecessor, MetaPost, which was a language used only for two-dimensional graphics.

Here is an example of an image that can be produced using Asymptote:

Figure1.jpgIn a sense, Asymptote is the ruler and compass of typesetting.
You can use Asymptote on the AoPSWiki right now, by enclosing the Asymptote code within <asy>...</asy> tags. For example, the following code

<asy>
draw((0,0)--(3,7),red);
dot((0,0));
dot((3,7));
label("Produced with Asymptote "+version.VERSION,point(S),2S);
</asy>

created the picture

[asy] draw((0,0)--(3,7),red); dot((0,0)); dot((3,7)); label("Produced with Asymptote "+version.VERSION,point(S),2S); [/asy]And on the AoPS forums you can use [asy]..[/asy]

[asy]
draw((0,0)--(3,7),red);
dot((0,0));
dot((3,7));
label("Produced with Asymptote "+version.VERSION,point(S),2S);
[/asy]

[asy] pair A,B,C,X,Y,Z; A = (0,0); B = (1,0); C = (0.3,0.8); draw(A--B--C--A); X = (B+C)/2; Y = (A+C)/2; Z = (A+B)/2; draw(A--X, red); draw(B--Y,red); draw(C--Z,red);[/asy]

See also

Next: Getting Started

 

 


 

Asymptote: Getting Started

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

To get started using Asymptote, it’s a good idea to go through this guide in the following steps:

  1. Follow the instructions on the Downloads and Installation page
  2. Once you have downloaded and installed everything, you may wish to try out interactive mode and optionally configure TeXnicCenter to use Asymptote, as described on the Interactive Mode page
  3. Look over Basics and skim through Reference or Examples to learn the basics of Asymptote coding.
  4. Make a bunch of images!

If you wish to go on and become an Asymptote master, the sections on Advanced Configuration, Logical Operators and Loops, and Macros and Packages are essential.

The Asymptote manual, containing more detailed and advanced instructions, can be found at http://asymptote.sourceforge.net/asymptote.pdf

If you’re a problem solver, be sure to check out the Olympiad package!

Next: Download and Install

 


 

Asymptote: Getting Started/Windows/Downloads and Installation

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to
NOTE: The following instructions assume that you are using a Windows machine. Instructions for MAC and Unix users can be found under Documentation $\rightarrow$ Installation here.

To begin using Asymptote, you must first download and install it. To view the eps-format images you produce, you will also need to download an eps viewer such as GSview. (GSview is convenient because it can display both eps and pdf files, and with GhostScript installed, Asymptote can easily output the images in pdf format as well.)

Asymptote

To download and install Asymptote on your Windows machine:

  1. Click here and wait for the download to begin. This will bring you to SourceForge, which will help you download asymptote-X.YZ-setup.exe, where X.YZ is the version.
  2. When the download is complete, browse to D:\downloads\Asymptote, or wherever you saved the file, and double-click on the .exe file (asymptote-X.YZ-setup.exe). This will open an installer window, where you can choose the folder that Asymptote will be installed to (or simply use the default C:\Program Files\Asymptote), and choose whether you wish to have shortcuts added to the desktop and start menu. When you have finished, click Install.

Asymptote is ready to start producing images, but you still need a way to view these images. (NOTE: if you already have a previewer capable of viewing .eps and .pdf files, you do not need to download the file below.)

GSview

GSview is a standard viewer for .eps files, which is the standard output format for Asymptote images. To download and install GSview,

  1. Go to this download site, and click on the “Obtaining GSView” link. Then click on the first link down, gsvXYw32.exe, where X.Y is the version. A download window will pop up in your browser. Choose to save the file, and take note of where on your hard drive you saved it to. Let’s say you saved it to the folder D:\downloads\Ghostscript. (If you are unable to get the file gsvXYw32.exe from this site, try this site instead.)
  2. When the download is complete, browse to D:\downloads\Ghostscript in your files and double-click on the .exe file gsvXYw32.exe. This will bring you to an installation window. Click Setup.
  3. After it extracts the necessary files, there will be a new installation window. Click Next twice, and there will be two checkboxes, which can set GSview to be the default eps or pdf previewer. If you wish to use the more common Acrobat Reader, or some other pdf reader, to view pdf files, only leave the first box checked. However, you can check both if desired.
  4. Click Next, and choose the directory in which you want GSview installed (or leave the default setting, C:\Program Files\Ghostgum).
  5. Click Next twice, and choose the Start Menu folder to which a shortcut will be added. (The default is Ghostgum.)
  6. Click Finish, and GSview will be installed.

Ghostscript

Ghostscript may also need to be installed on your system. You may download it here by clicking on the second link down, GPL Ghostscript. Then, click on the first link down, and copy the file to your computer. After that, run it to install Ghostscript. You may then need to set the version of Ghostscript in GSview. Do this by choosing the Easy Configure option in the Options menu in GSview.

You now have installed everything you need to use Asymptote on the most basic level, as described in the next section.

Next: Interactive Mode

 


 

Asymptote: Getting Started/Windows/Interactive Mode

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

At this point, you are ready to use Asymptote in “Interactive Mode”:

  1. Click on the Asymptote icon or run the program asy.exe. You will see a black window, prompting you with a > sign.
  2. Type:
    • draw((0,0)--(100,100));
  3. Press Enter again, and GSview should pop up, displaying a diagonal line about 1.3 inches in length. (The command drew the path connecting points (0,0) and (100,100) in Cartesian coordinates; to learn more about points, paths, and the draw command, see the Basics section.)

One can continue to use Asymptote in this interactive mode, with each new draw or label command adding something to the diagram. However, this is horribly inefficient. In order to use Asymptote efficiently, it is best to use a powerful editor such as TeXnicCenter (see Art of Problem Solving’s [instructions] in the LaTeX guide) or Crimson Editor to write files containing code for an entire diagram in Asymptote, and compile the file with Asymptote. There are many ways to do this, and many such editors available, but this guide will explain how to configure TeXnicCenter to quickly compile your Asymptote code, in order to be consistent with Art of Problem Solving’s LaTeX guide.

Using TeXnicCenter for Asymptote

First, download and install TeXnicCenter according to the instructions given [here]. Now, use the following steps to set up TeXnicCenter to compile your Asymptote file with the press of a button:

  1. Go to the Tools menu, and click Customize.
  2. You will see a tab at the top of the Customize window that says Tools. Click on this tab.
  3. Click on the New (Insert) button – the one that looks like a dotted rectangle with a star – to create a new Tool. Name the tool whatever you like, say Asymptote.
  4. Click on your newly created Asymptote tool. There are three information blocks to fill in:
    1. In the Command block, type in "C:\Program Files\Asymptote\asy.exe" (with the quotes, to ensure that it is a single file name) if you used the default installation folder, or browse to the directory in which you installed Asymptote in order to enter the full path name of asy.exe.
    2. In the Arguments block, type or copy-and-paste the following line:
      • -batchView -tex "latex" %tc
      • This tells Asymptote to produce and view an eps file given the corresponding Asymptote code in your current document.
    3. In the Initial directory block, type %dc.
  5. Now you will want to make a shortcut key, or hotkey, to run this tool. Click on the Keyboard tab in the Customize window.
  6. Set the Category to Tools, and your Asymptote tool will appear. Click on Asymptote and then on the Press New Shortcut Key block. Then press Alt+A (or any other convenient shortcut key that you wish to use), and click Assign.

Congratulations! You are now ready to create your first Asymptote document.

To do so, create a new file in TeXnicCenter and save it as test.asy. Type the following code into the document:

draw((0,0)--(100,100));
draw((0,100)--(100,0));
dot((50,50));
label("$P$",(50,50),S);

Finally, save the file and press Alt+A (or whatever hot key you assigned in step 6 above), and you should see the following image:

PointX.jpg

If you wish to include such images directly inside your LaTeX documents, see the section under Advanced Configuration on LaTeX.

Now for the fun part – you are ready to learn the Asymptote language and produce beautiful figures!

Next: Learn Asymptote!

 


 

Asymptote: Basics

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

Syntax

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:

draw((0,0)--(50,50));
draw((0,50)--(50,0));

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)
draw((0,0)--(50,50));

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

/* The  
following line 
is drawn from 
(0,0) to (50,50) */
draw((0,0)--(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

 n=3;

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;
   A=(0,0);
   B=(50,50);
   C=(0,50);
   D=(50,0);
   draw(A--B);
   draw(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:

Table1.gif

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

unitsize(72);

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

unitsize(1inch);

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

size(5cm,5cm);

or just

size(5cm);

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:

unitsize(2inch); 
draw(unitsquare);

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

 


 

Asymptote: Drawing

Dots

Let us start off with the most basic of this basic command: drawing a dot.

To draw a dot, simply write the following code:

dot((0,0));

[asy] dot((0,0)); [/asy]

You can fix certain attributes to this dot, such as color:

dot((0,0),green);

[asy] dot((0,0),green); [/asy]

Lines

Now let’s draw a path, or a line segment.

draw((0,0)--(5,5));

[asy] draw((0,0)--(5,5)); [/asy]

Once again, we can set certain attributes, such as color and linewidth, both at the same time.

draw((0,0)--(5,5),green+linewidth(3));

[asy] draw((0,0)--(5,5),green+linewidth(1)); [/asy]

Now if this diagram is too large, we can size it to be smaller:

size(100); draw((0,0)--(5,5),green+linewidth(1));

[asy] size(100); draw((0,0)--(5,5),green+linewidth(1)); [/asy]

We can also create multiple paths with one line, if we want a triangle or a square, for example:

draw((0,0)--(5,5)--(5,0)--cycle);

[asy] draw((0,0)--(5,5)--(5,0)--cycle); [/asy]

Note that this uses the cycle command, meaning the path returns to its original point, in this case (0,0).

Circles

In this article, draw(circle((0,0),5));

We see that the first draw() command creates the circle, which uses the circle() command. Within the circle command, we see the center point is located at the cartesian plane point (0,0), and it has a radius of 5.

This code produces:

[asy] draw(circle((0,0),5)); [/asy]

Once again, we can fix certain attributes to this code:

draw(circle((0,0),5),red+linewidth(1));

[asy] draw(circle((0,0),5),red+linewidth(1)); [/asy]

And we can fill the inside:

filldraw(circle((0,0),5),green,red+linewidth(1));

[asy] filldraw(circle((0,0),5),green,red+linewidth(1)); [/asy]

Ellipse

Another rounded figure we can create is the ellipse.

draw(ellipse((0,0),5,3));

In this case, the (0,0) is the center of the ellipse, the 5 is the length of the major axis and the 3 is the length of the minor axis. This results in:

[asy] draw(ellipse((0,0),5,3)); [/asy]

Once again, we can fix attributes and fill the inside.

[asy] filldraw(ellipse((0,0),5,3),green,red+linewidth(1)); [/asy]

 


 

Asymptote: Reference

Points

A point is associated with a cartesian coordinate pair in Asymptote. There are two useful functions that allow one to use polar coordinates as well:

 dir(theta)

returns the point (cos(theta),sin(theta)) where theta is in degrees, and

 expi(theta)

returns the complex number $e^{i\theta}$, i.e. (cos(theta),sin(theta)) where theta is measured in radians.

Paths

A path (or guide – see Variables and Data Types for the difference) in Asymptote is simply a piecewise cubic function of a parameter $t$, parameterized as $t$ ranges from $0$ to the number of nodes (say $n$) that determine the path. The most basic way to make paths is by joining points (which can be thought of as paths with length 0) or paths p, q together with one of the following operators:

p--q
p..q
p^^q

The first (--) connects the end of path p to the beginning of q with a straight line. The second (..) connects them with a Bezier cubic spline interpolation so that paths are joined smoothly. The third (^^) actually does not connect the paths at all, but rather re-parameterizes so that the two paths are treated as one. The symbol cycle connected to a path tells Asymptote to form a cyclic path by joining the endpoint with $t=n$ to that with $t=0$.

The following example includes the benefits of all three types of path joins:

unitsize(1inch);
path T,ct,tt;
T=(0,0)--(1,0)--(1/2,sqrt(3)/2)--cycle;
ct=(0,0)..(1,0)--(1/2,sqrt(3)/2)..cycle;
tt=shift(sqrt(3)/6*dir(30))*(scale(1/2)*T);
draw(T);
draw(shift(2*right)*ct);
fill(reverse(shift(4*right)*tt)^^(shift(4*right)*T),blue);

outputs: [asy] unitsize(1inch); path T,ct,tt; T=(0,0)--(1,0)--(1/2,sqrt(3)/2)--cycle; ct=(0,0)..(1,0)--(1/2,sqrt(3)/2)..cycle; tt=shift(sqrt(3)/6*dir(30))*(scale(1/2)*T); draw(T); draw(shift(2*right)*ct); fill(reverse(shift(4*right)*tt)^^(shift(4*right)*T),blue); [/asy]

Pens and Coloring

A pen is just that – the style with which Asymptote draws your paths or pictures. Pens can have various colors, dash patterns (linetypes), and linewidths. The following table gives a variety of examples of pen types and their output:

Table2.gif

We can also add pens with the + operator. For example, the command

 draw((0,0)--(100,100),orange+dashed+linewidth(1));

will produce the image

Pen14.gif

The default pen has linetype solid=””, linewidth .5, and color black.

Basic drawing commands

Asymptote has four basic drawing commands: draw, fill, clip, and label. The most important of these is draw.

Draw

The function draw can take many arguments. The following is the structure of a draw command, where anything with = denotes the default value:

 void draw(picture pic=currentpicture, Label L="", path g,
         align align=NoAlign, pen p=currentpen,
         arrowbar arrow=None, arrowbar bar=None, margin margin=NoMargin,
         Label legend="", marker marker=nomarker);

So with draw, you can draw path g to the picture pic along with a string label L, with pen p, etc. Of course, most of this is unnecessary for a basic diagram. The most important thing to notice is that the only non-optional argument of draw is the path g. The command draw(g); where g is a path simply draws the path on top of your current picture.

Fill

The structure of fill is much simpler than that of draw:

  void fill(picture pic=currentpicture, path g,pen p=currentpen);

This fills a cyclic path g in picture pic (see the section on Paths above) with a specified pen p (most often a color – see Pens and Coloring above). For example, the command

fill((0,0)--(0,1)--(1,1)--(1,0)--cycle,red);

where unitsize is 1 inch outputs:

Fill1.gif

Clip

The structure of clip is also simple:

 void clip(picture pic=currentpicture, path g, pen p=currentpen);

This crops a picture so that the boundary is the given path g, with fill rule p. A fill rule tells when a point is inside the boundary of the path; the default fill rule, zerowinding or fillrule(0), checks to see if the number of horizontal intersections from the point to the right is equal to the number of downward intersections with the path. Another useful fill rule is evenodd, or fillrule(1), which checks if the total number of such intersections is even.

For example, say we drew a smiley:

import graph;
unitsize(1inch);
filldraw(Circle((0,0),1),yellow,black);
fill(Circle((-.3,.4),.1),black);
fill(Circle((.3,.4),.1),black);
draw(Arc((0,0),.5,-140,-40),blue);

and we wanted to clip this to a star-shaped boundary. Then we add the lines:

path star;
star=expi(0)--(scale((3-sqrt(5))/2)*expi(pi/5))--expi(2*pi/5)--
     (scale((3-sqrt(5))/2)*expi(3*pi/5))--expi(4pi/5)--
     (scale((3-sqrt(5))/2)*expi(5*pi/5))--expi(6*pi/5)--
     (scale((3-sqrt(5))/2)*expi(7*pi/5))--expi(8*pi/5)--
     (scale((3-sqrt(5))/2)*expi(9*pi/5))--cycle;
clip(currentpicture,scale(1.7)*rotate(18)*star);
draw(scale(1.7)*rotate(18)*star);

and we get:

Clip1.gif

If we wanted to clip to a pattern in an alternating fashion, we could use the evenodd fill rule:

path zones[];
zones=(Circle((0,0),.2)^^Circle((0,0),.6)^^Circle((0,0),.8)^^Circle((0,0),1));
clip(currentpicture,zones,evenodd);

This produces:

Clip2.gif

Label

Label is used for, well, labeling your diagram.

Structure:

void label(picture pic=currentpicture, Label L, pair position,
          align align=NoAlign, pen p=nullpen, filltype filltype=NoFill);

This places a label L (usually a string, which can include LaTeX code!) at the point position, aligned by default so that the center of the label is at the position, with optional pen and filltype for its bounding box. The alignment options, however, can be very useful; any pair can be given as a direction for alignment, and the built-in alignment directions are the compass directions N, E, S, W, NE, NW, SE, SW, ENE, etc. For example, the image below:

Label1.gif

was produced by 8 double commands of the form

 label("S",(1,0),S);
 dot((1,0));

See also: Asymptote:Labeling

Pictures

The basic drawing commands all draw onto pictures, the default picture being currentpicture, which is the one that you see when you build your image. However, you can define a new picture in order to treat several drawn parts as a single object, which can be shifted and transformed as a whole relative to currentpicture. One picture (pic1) can be added to another (pic2) with the command add(pic2,pic1), and by default add(pic) simply adds pic to currentpicture. For example:

 picture pic;
 size(3cm);
 draw(pic,unitsquare);
 draw(pic,unitcircle);
 add(shift(3*right)*pic);
 draw(unitsquare);
 draw(unitcircle);

will display:

Picture1.gif

Transforms

Transforms are objects that can be applied to pairs, paths, pictures, and other transforms by the * operator. The most commonly used transforms are: Transforms.gif

Another useful transform that is not listed above:

reflect(pair a, pair b);
reflects about the line a--b.

Next: Examples

 


 

Asymptote: Useful commands and their Output

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

For more complicated examples, which are often quite neat, see this page in the Art of Problem Solving Test Forum, which has various diagrams from contests. Clicking on them will reveal the code used to make them, so this is an excellent resource for students looking to make more advanced diagrams.
For each of the following, we have put a blue dot at the origin in order to indicate relative location of the output on the coordinate plane. In other words, assume that before each of the examples below is the command

dot((0,0),blue);

In addition, a comment after a line such as //math - extension indicates that the command (in this case extension) used in that line is defined in the math package, thus motivating the import math; (or other appropriate package) line at the top of the example.


Example 1:

dot((20,0));

Output 1: [asy]dot((20,0));[/asy]


Example 2:

draw((0,0)--(50,0),BeginArrow);
draw((0,-10)--(50,-10),MidArrow);
draw((0,-20)--(50,-20),EndArrow);
draw((0,-30)--(50,-30),Arrows);

Output 2: [asy] size(75);draw((0,0)--(50,0),BeginArrow);  draw((0,-10)--(50,-10),MidArrow);  draw((0,-20)--(50,-20),EndArrow);  draw((0,-30)--(50,-30),Arrows);[/asy]


Example 3:

draw((0,0)--(50,0));
arrow((30,0),dir(180),green);

Output 3: Figure4.gif


Example 4:

import math;
pair A,B,C,D,E;
A=(0,0); C=(50,0); B=(10,10); D=(40,20);
E=extension(A,B,C,D);  // math - extension
// extension(A,B,C,D) returns the intersection of lines AB and CD
draw(A--B); draw(C--D);
draw(B--E--D,orange);

Output 4: Figure5.gif


Example 5:

import graph;
draw(Circle((0,0),20)); // graph - Circle

Output 5: Figure6.gif


Example 6:

path p=(0,0)..(20,15)..(40,-5)..(50,0);
draw(p);
draw(rotate(90)*p,green);
draw(rotate(180,(-5,0))*p,orange);
draw(shift((5,20))*p,magenta);
draw(shift((0,-25))*yscale(1.4)*p,red);

Output 6: Figure7.gif


Example 7:

import olympiad;
unitsize(50);
pair A,B,C,O,I;
A=origin; B=2*right; C=1.5*dir(70);
O=circumcenter(A,B,C); // olympiad - circumcenter
I=incenter(A,B,C); // olympiad - incenter
draw(A--B--C--cycle);
dot(O);
dot(I);
draw(circumcircle(A,B,C)); // olympiad - circumcircle
draw(incircle(A,B,C)); // olympiad - incircle
label("$I$",I,W);
label("$O$",O,S);

Output 7: [asy]  import olympiad;  size(75);  pair A,B,C,O,I;  A=origin; B=2*right; C=1.5*dir(70);  O=circumcenter(A,B,C); /* olympiad - circumcenter */  I=incenter(A,B,C); /* olympiad - incenter */  draw(A--B--C--cycle);  dot(O);  dot(I);  draw(circumcircle(A,B,C)); /* olympiad - circumcircle */  draw(incircle(A,B,C)); /* olympiad - incircle */  label("$I$",I,W);  label("$O$",O,S); [/asy]


Example 8:

import three;
unitsize(1cm);
size(50);
currentprojection=orthographic(1/2,-1,1/2); // three - currentprojection, orthographic
draw((0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle,red);
draw((0,0,0)--(0,0,1));
draw((0,1,0)--(0,1,1));
draw((1,1,0)--(1,1,1));
draw((1,0,0)--(1,0,1));
draw((0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle,green);


Example 9:

import three;
unitsize(1cm);
size(50);
currentprojection=orthographic(1/2,-1,1/2); /* three - currentprojection, orthographic */
draw((0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle,red);
draw((0,0,0)--(0,0,1));
draw((0,1,0)--(0,1,1));
draw((1,1,0)--(1,1,1));
draw((1,0,0)--(1,0,1));
draw((0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle,green);

Output 9: [asy] import three;  unitsize(1cm);  size(50);  currentprojection=orthographic(1/2,-1,1/2); /* three - currentprojection, orthographic */  draw((0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle,red);  draw((0,0,0)--(0,0,1));  draw((0,1,0)--(0,1,1));  draw((1,1,0)--(1,1,1));  draw((1,0,0)--(1,0,1));  draw((0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle,green);[/asy]

Example 10, buildcycle:
fill(buildcycle(curveI,reverse(curveIII),curveII),yellow);

Output 10, buildcycle: [asy] unitsize(1cm); pair A1=(2,5),A2=(10,1); pair B1=(1,1),B2=(10,5); pair C1=(1,2),C2=(10,3.5); path curveI = A1 .. controls (6,5) .. A2; path curveII = B1 .. controls (3,4) .. B2; path curveIII = C1 .. controls (3,3) and (7,0) .. (9,2) ..controls C2 .. C2; fill(buildcycle(curveI,reverse(curveIII),curveII),yellow); draw(curveI, red); draw(curveII, blue); draw(curveIII, green); pair ip_1 = intersectionpoint(curveI, curveII); pair ip_2 = intersectionpoint(curveII, curveIII); pair ip_3 = intersectionpoint(curveI, curveIII); label("$A_1$",A1,1.5W,red); label("$A_2$",A2,1SE,red); label("$B_1$",B1,1.5SSW,heavyblue); label("$B_2$",B2,1.12E,heavyblue); label("$C_1$",C1,1.5W,heavygreen); label("$C_2$",C2,1.25E,heavygreen); dot(ip_1^^ip_2^^ip_3); label("$S_1$",ip_1, 1.5*NNE); label("$S_2$",ip_2, 1.5*SSE); label("$S_3$",ip_3, 1.85*dir(-93)); dot(A1^^A2, mediumred); dot(B1^^B2, mediumblue); dot(C1^^C2, mediumgreen); [/asy]
Examples in Forum

Clicking on the images the the forum or on images here in the previous examples will show you as to-copy-and-paste the Asymptote source. (For this you have to be registered at artofproblemsolving.com.)

Triangles, lines, tick marks, angle marks

See Also

Many more Asymptote examples

Next: Macros and Packages

 


 

Asymptote: Macros and Packages

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

Definitions

You can define your own functions in Asymptote. As an example, let’s say you wanted to make a function called newfunction that takes a pair $(a,b)$ and a real value $r$ as input, and returns the pair $(a+r,b+r)$. In addition, you want it to simply return the pair $(a,b)$ if no value of $r$ is specified, so you want $r$ to default to $0$. The code would be as follows:

 pair newfunction(pair z, real r=0)
 {
  real a,b;
  a=z.x;
  b=z.y;
  return (a+r,b+r);
 }

Put this definition in an asymptote document and then test it using some command like

draw(newfunction((20,30))--newfunction((20,30),30)--(0,0)--cycle);  

See if it works!

Notice that the function must be declared a pair since it returns a pair, and each of the variables must be declared some data type too. The default value of $r$ was set to $0$ by $r=0$, and the actual function procedure goes in between {}. To define a function with no output simply put void before the function name. This is the general format for a function definition.

Packages

Asymptote comes with several packages that contain useful functions for various purposes. For example, the package graph.asy contains the function

 Circle(pair p, real r, int n=400);

which is a more accurate circle (having 400 nodes by default) than the built-in circle command. To use this function and others in graph.asy, simply put the command

import graph;

at the top of your Asymptote document. Graph also has more advanced functions such as the ability to Graph a function

You can create your own package by simply creating a new .asy file (say MyMacros.asy) with your own definitions in it, and saving it in the directory in which Asymptote is installed (C:\Program Files\Asymptote by default). Then import MyMacros; in your document, and you’ll be set!

The Olympiad Package

We have created an Olympiad package for Asymptote which includes macros for all the constructions that come up most often in Olympiad geometry problems! You can obtain the package olympiad.asy by clicking here or here (the latter link has a few usage examples).

This package includes the following definitions:

Olympiad1.gif

Olympiad2.gif

Olympiad3.gif

Olympiad4.gif

Olympiad5.gif

Note: A sequence of variables without type declarations indicates that they are the same type as the variable preceding it. For example, the notation concurrent(pair A, B, C, D, E, F) indicates that all of the variables should have type pair.

* These boolean functions test for equality within $10^{-5}$ ps points in order to avoid approximation errors.

Next: Advanced

 


 

Asymptote: Advanced

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

These pages cover the more advanced features of Asymptote: advanced configuration options, language features, and Asymptote’s ability to be extended through custom functions, macros, and packages.

Next: Help

 

Asymptote: Advanced Configuration

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

There are two things that Asymptote users commonly would like to do with their figures: make the images output in a different format, and include Asymptote code directly into LaTeX files.

Contents

PDF format and beyond

Asymptote can easily make your images in pdf format rather than eps format, with the help of a program called GhostScript.

GhostScript

To download and install GhostScript on Windows:

  1. Go to github.com/ArtifexSoftware/ghostpdl-downloads/releases and choose the version that fits your OS architecture (for 32-bit, choose gsNNNw32.exe; for 64-bit, gsNNNw64.exe, where NNN is version N.NN of Ghostscript).
  2. Click on the link for the .exe file, and a download window will pop up in your browser. Choose to save the file, and take note of where on your hard drive you saved it to. Let’s say you saved it to the folder D:\downloads\Ghostscript.
  3. When the download is complete, browse to D:\downloads\Ghostscript in your files and double-click on the .exe file (gs854w32.exe). This will bring you to an installation window. Click Setup.
  4. After the installer extracts the required files, you will see a new window with the option to change the installation folder (the default will be C:\Program Files\gs) and an option to select the folder in the start menu to which a shortcut will be added. When you have finished, click Install.

PDF output configuration

Although ghostview can be configured as the pdf viewer, we recommend using the default pdf viewer, Acrobat Reader.

Now open TeXnicCenter. Go to the Tools menu and click Customize, then go to the Tools tab. Click to modify your Asymptote tool that you made in the Interactive Mode section. In the Arguments line, put

 -batchView -tex "pdflatex" %tc-*.asy

Newer versions may require the following line

 -batchView -tex "pdflatex" %tc.asy

Now if you open the file test.asy in TeXnicCenter again, which contains the code

draw((0,0)--(100,100));
draw((0,100)--(100,0));
dot((50,50));
label("P",(50,50),S);

and press Alt+A (or your corresponding hotkey), voila! You have made a pdf document with your image.

In order to make Asymptote output files in .jpg or other formats, you must use a program such as ImageMagick that is capable of converting .eps files to many other image formats. If you have ImageMagick installed (you can obtain it [here] and install it from the download), you can simply type at an MSDOS command prompt:

convert test.eps test.jpg

and the file test.jpg will be produced. See [the ImageMagick home page] for more information on converting images between file formats.

Using Asymptote in LaTeX

One of Asymptote’s main advantages as a graphics language is its incredible compatibility with LaTeX. Once you have it configured correctly, you can simply type a \begin{asy} ... \end{asy} block in a figure environment in your LaTeX file, and an image will be produced in your document!

First you need to get LaTeX to see Asymptote. In order to do this, you must add the package asymptote.sty to your LaTeX package directory (this file can be found in your Asymptote directory, by default C:\Program Files\Asymptote). For example, if you are using MikTeX (recommended) and MikTeX is installed to C:\Program Files on your computer, you can use the following steps to get MikTeX to see Asymptote:

Note: This guide assumes you are using Windows Vista or later with MiKTeX $\ge$2.9 and GhostScript installed.

  1. Browse to the folder C:\Program Files\MiKTeX 2.x\tex\latex\ (replace 2.x by the version on your machine).
  2. Here, create a new folder and call it asymptote.
  3. Open your new folder. For full functionality, copy the files asymptote.sty and asycolors.sty from C:\Program Files\Asymptote into this new asymptote folder.
  4. Open Start Menu and search for MikTeX Options (Admin), then double-click to run. If you can’t find the program there, go to C:\Program Files\MiKTeX 2.x\miktex\bin, navigate to the subfolder x64 or win32, and click on mo_admin.exe (replace 2.x with the version of MikTeX on your machine).
  5. In the General tab, you will see a section labeled Maintenance with a button that says Refresh FNDB. Click it to refresh your file name database. MikTeX should now see your Asymptote style files.

Now, you should be ready to include your figures in your LaTeX document. Create a test file called example.tex, and copy-and-paste the following code into the document:

\documentclass{article}
\usepackage[pdftex]{graphicx}
\usepackage{asymptote}
\begin{document}
Hello.  
I like to make pics with Asymptote like this one:
\begin{figure}[h]
  \begin{asy}
    import graph;
    size(1inch);
    filldraw(circle((0,0),1),yellow,black);
    fill(circle((-.3,.4),.1),black);
    fill(circle((.3,.4),.1),black);
    draw(arc((0,0),.5,-140,-40));
  \end{asy}
\end{figure}
\par It makes me happy, 
since I can still type my normal LaTeX stuff around it: 
\(\int_0^{\pi}{\sin{x}}\,dx=2\)
\end{document}

Notice that the \end{asy} line is justified all the way to the left, on a line by itself. This is VERY IMPORTANT for the asymptote.sty package to find the end of the Asymptote code; there can be no whitespaces or other characters on the line containing \end{asy}.

You are not quite done yet – LaTeX will not output the image the first time it compiles the document. To compile this file, you must:

  1. Pass it once through LaTeX without viewing the output (click Build in TeXnicCenter; this extracts your asy code into a .asy file.)
  2. Run Asymptote on the new file example.asy (pressing Alt+A as normal in TeXnicCenter will work, since it only looks at the title example.)
  3. Finally, run it again through LaTeX (click Build and View Current File in TeXnicCenter). Your output should look like:

Hello. I like to make pics with Asymptote like this one:

Smiley.jpg

It makes me happy, since I can still type my normal LaTeX stuff around it:

${\int_0^{\pi}{\sin{x}}\,{dx}=2}$


If you do not want to have to go through all 3 steps above every time you want to compile your LaTeX document, you have several options: (1) You can create your Asymptote files separately in eps format, and include them into your LaTeX document with the \includegraphics command. (2) You can create a DOS batch file to run the three commands in order, and call this batch file using a TeXnicCenter tool on your current file. The advantage of the former is that your LaTeX document will take less time to compile since the images are already made, and the advantage of the latter is the clean, readable format of the \begin{asy} ... \end{asy} blocks in your code. This guide does not provide detail on implementing such methods. See [here] for a good reference on DOS batch files and Windows batch commands.

Showing Asymptote error messages in TeXnicCenter

There are three methods:

(1) When running Asymptote from inside TexnicCenter, the DOS window closes too quickly to see any error messages. These messages may be crucial to correcting the code. TeXnicCenter can be persuaded to show the messages with a new tool, or, if you wish, overwriting the original Asymptote tool.

The following instructions assume that Asymptote and TeXnicCenter have been saved in the default directories C:\Program Files\Asymptote and C:\Program Files\TeXnicCenter; change them as necessary.

  • In Notepad (or TeXnicCenter) create a new file called asymptote.bat and save it in C:\Program Files\Asymptote. Copy and paste the following into the file:
echo off 
echo Asymptote is working ... please wait 
"C:\Program Files\Asymptote\redir.exe" -e %1 "C:\progra~1\Asymptote\asy.exe" -batchView -tex "pdflatex" -align C -f "pdf" %2 
find /i ".asy" %3 >nul 
If errorlevel 1 goto end 
"C:\Program Files\TeXnicCenter\TEXCNTR.EXE" /ddecmd "[open('%3')]" 
:end
  • Download [redir.zip], extract redir.exe, virus check it and save in C:\Program Files\Asymptote
  • In TexnicCenter, Tools->Customise…->Tools put in either a new tool or change the current Asymptote tool:
    • Commands: “C:\Program Files\Asymptote\asymptote.bat”
    • Arguments: %tc_asy.log %tc “%dc\%tc_asy.log”
    • Initial directory: %dc
then press Close

Now when you run Asymptote on file.tex from inside TexnicCenter the error messages, if any, will be saved in file_asy.log. If there are any error messages then this file will open in TeXnicCenter showing you the messages. When you have eliminated all the errors nothing will show in TeXnicCenter.

(2) This method creates a new build environment so that you can compile .asy documents and have the messages sent through TeXnicCenter.

  • Go to Build > Define Output Profiles…
  • Click Add and name your new profile ‘Asymptote’
  • Your (La)TeX settings should be:
    • Run (La)TeX in this profile IS selected
    • Path to compiler should be wherever you asy.exe lives. Probably something like C:\Program Files (x86)\Asymptote\asy.exe
    • Command line arguments: -f “pdf” -vv -tex=pdflatex %pm
    • Do not use BibTeX or MakeIndex

(3) This method has more steps to implement, but is worth it. It is only applicable when you have Asymptote code inside the Asymptote Environment in LaTeX code. Simply implement the following steps to have it so that you can compile Asymptote images along with the LaTeX just by clicking ‘Build.’ It also displays error messages right on the TeXnicCenter output box. (It will appear at the very end.)

  • Go to Build > Define Output Profiles… (or press Alt+F7).
  • Select your current output profile (most likely it will be LaTeX => PDF).
  • Click on ‘Copy’ at the bottom.
  • Make the profile’s name ‘LaTeX => PDF(Asymptote)’ (without the single quotes).
  • Click on the ‘Postprocessor’ tab.
  • Click on the button that looks like a dotted square (when you hover your mouse over it, the description should be ‘New (Insert)’).
  • Type in ‘Asymptote’.
    • Executable: C:\Program Files\Asymptote\asy.exe
    • Arguments: -batchView -tex “pdflatex” %tm-*.asy
  • Click on the New button again.
  • Type in ‘LaTeXComp’.
    • Executable: C:\Program Files\MiKTeX 2.7\miktex\bin\pdflatex.exe
    • Arguments: -interaction=nonstopmode “%pm”
  • Click on the New button again.
  • Type in ‘BibTex’.
    • Executable: C:\Program Files\MiKTeX 2.7\miktex\bin\bibtex.exe
    • Arguments: “%bm”
  • Click on the New button again.
  • Type in ‘MakeIndex’.
    • Executable: C:\Program Files\MiKTeX 2.7\miktex\bin\makeindex.exe
    • Arguments: “%bm”
  • Click ‘Ok’ at the bottom.
  • Go to Tools > Select Output Profile…
  • Select the profile you just created.

Now you’re done. All you have to do is click on ‘Build’ to compile both your LaTeX and embedded Asymptote code. What I would suggest is to only use this profile whenever you start a new asymptote environment or you updated a previously existing environment. This way, the building process will occur faster. (When you use this profile, your LaTeX document is compiled, then your Asymptote code is compiled, then your LaTeX document is compiled once more. So this will cause your build time to be increased by around 5-10 seconds.)

Note that if you have any errors in the Asymptote code, they will appear at the very bottom in the output box in TeXnicCenter.

 


 

Asymptote: Logical Operators and Loops

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

Asymptote uses loops and logical operators that are almost identical to those in C++. Loops are absolutely essential if you want to make diagrams that look like this:

[asy] import graph; real r=5;  size(r*cm); picture smiley; filldraw(smiley,Circle((0,0),1),yellow,black); fill(smiley,Circle((-.3,.4),.1),black); fill(smiley,Circle((.3,.4),.1),black); draw(smiley,Arc((0,0),.5,-140,-40)); for (int i=0; i<5; ++i) {  for (int j=0; j<5; ++j)  {   if (floor((i-j)/2)==((i-j)/2))   {   add(scale(r/10*cm)*smiley,(i,j));   }  } } [/asy] This particular example was produced with the following code:

import graph;
real r=5; 
size(r*cm);
picture smiley;
filldraw(smiley,Circle((0,0),1),yellow,black);
fill(smiley,Circle((-.3,.4),.1),black);
fill(smiley,Circle((.3,.4),.1),black);
draw(smiley,Arc((0,0),.5,-140,-40));
for (int i=0; i<5; ++i)
{
 for (int j=0; j<5; ++j)
 {
  if (floor((i-j)/2)==((i-j)/2))
  {
  add(scale(r/10*cm)*smiley,(i,j));
  }
 }
}

Above, we created a picture called smiley and added it to currentpicture many times using a for loop, as the indices $i$ and $j$ each ranged from $0$ to $4$. Basically, the arguments in the parentheses for the first for loop first declare $i$ to be an integer and assign to i the value $0$. Then, if $i<5$, it executes what is inside the {} brackets and when it is finished, it adds $1$ to $i$ (++i). This process repeats until the boolean statement $i<5$ has the value false, i.e. 5 times (hence the 5 columns of smileys). The if statement is self-explanatory; if $\lfloor(i-j)/2\rfloor=(i-j)/2$ (which checks if $i$ and $j$ have the same parity or not), then the smiley is added, and if not it skips the brackets that follow.

For more information on logical operators and loops, see [here].

 


 

Asymptote: Macros and Packages

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

Definitions

You can define your own functions in Asymptote. As an example, let’s say you wanted to make a function called newfunction that takes a pair $(a,b)$ and a real value $r$ as input, and returns the pair $(a+r,b+r)$. In addition, you want it to simply return the pair $(a,b)$ if no value of $r$ is specified, so you want $r$ to default to $0$. The code would be as follows:

 pair newfunction(pair z, real r=0)
 {
  real a,b;
  a=z.x;
  b=z.y;
  return (a+r,b+r);
 }

Put this definition in an asymptote document and then test it using some command like

draw(newfunction((20,30))--newfunction((20,30),30)--(0,0)--cycle);  

See if it works!

Notice that the function must be declared a pair since it returns a pair, and each of the variables must be declared some data type too. The default value of $r$ was set to $0$ by $r=0$, and the actual function procedure goes in between {}. To define a function with no output simply put void before the function name. This is the general format for a function definition.

Packages

Asymptote comes with several packages that contain useful functions for various purposes. For example, the package graph.asy contains the function

 Circle(pair p, real r, int n=400);

which is a more accurate circle (having 400 nodes by default) than the built-in circle command. To use this function and others in graph.asy, simply put the command

import graph;

at the top of your Asymptote document. Graph also has more advanced functions such as the ability to Graph a function

You can create your own package by simply creating a new .asy file (say MyMacros.asy) with your own definitions in it, and saving it in the directory in which Asymptote is installed (C:\Program Files\Asymptote by default). Then import MyMacros; in your document, and you’ll be set!

The Olympiad Package

We have created an Olympiad package for Asymptote which includes macros for all the constructions that come up most often in Olympiad geometry problems! You can obtain the package olympiad.asy by clicking here or here (the latter link has a few usage examples).

This package includes the following definitions:

Olympiad1.gif

Olympiad2.gif

Olympiad3.gif

Olympiad4.gif

Olympiad5.gif

Note: A sequence of variables without type declarations indicates that they are the same type as the variable preceding it. For example, the notation concurrent(pair A, B, C, D, E, F) indicates that all of the variables should have type pair.

* These boolean functions test for equality within $10^{-5}$ ps points in order to avoid approximation errors.

Next: Advanced

 


 

Asymptote: 3D graphics

Three

Three is a module in Asymptote that allows the user to create three dimensional graphics. Usually all you must do is import three,
import three;

then change from using doubles eg. (x,y) to using triples eg. (x,y,z) as coordinates. Some functions do not work when three is active. For example: To fill a surface, one must define a surface and draw that, instead of using filldraw. This is also described at http://www.artofproblemsolving.com/Forum/viewtopic.php?f=519&t=399845.

Data types

three defines the data types:

  • path3, (3D version of path)
  • guide3, (3D version of guide)
  • and surface (a surface bounded by a path(3))

and other, less important ones.

Definitions

three defines the surfaces:

  • unitcube
  • unitsphere
  • unitdisk
  • unitplane
  • unitcylinder
  • unitcone
  • unitsolidcone
  • and unithemisphere.

These can be drawn like you would normally draw an object in 2D

draw(unitcube,green);

Transforms also work

draw(shift(2,3,4)*scale(5,20,7)*unitcone,paleblue);

[asy] import three; draw(shift(2,3,4)*scale(5,20,7)*unitcone,paleblue);[/asy]

Projection

You can use
currentprojection=orthographic(x,y,z);
to change the current view.
currentprojection=perspective(x,y,z);
does the same thing, but it distorts the picture to imitate actual perspective.

Example:

base code:

import three;
/* perspective line /*
draw(unitcube,palegrey);

Using
currentprojection=orthographic(1,1/2,1/2);
We get a unit cube as: [asy] import three; currentprojection=orthographic(1,1/2,1/2);  draw(unitcube,palegrey); [/asy] Using
currentprojection=perspective(1,1/2,1/2);
We get a unit cube as: [asy] import three; currentprojection=perspective(1,1/2,1/2);  draw(unitcube,palegrey); [/asy]

Note: When current projection is not given, three tries to find the “best” view.

Interactive Projection

When using Asymptote on your computer (not on AoPS), you can add some code that lets you rotate/pan/zoom with the mouse.

import settings;
leftbutton=new string[] {"rotate","zoom","shift","pan"};
middlebutton=new string[] {"menu"};
rightbutton=new string[] {"zoom/menu","rotateX","rotateY","rotateZ"};
wheelup=new string[] {"zoomin"};
wheeldown=new string[] {"zoomout"};

When compiling to PDF, it will allow you to rotate/pan/zoom with the mouse.

Arrows and bars

Arrows and bars in 3D are the same as in 2D except you add a 3 to the end of the name. Example.

import three;
draw((0,0,0)--(1,1,1),green,Arrows3);
draw((0,1,0)--(1,0,1),blue,Bars3);

[asy] import three; draw((0,0,0)--(1,1,1),green,Arrows3); draw((0,1,0)--(1,0,1),blue,Bars3); [/asy]

Examples

import three;
unitsize(1cm);
size(200);
currentprojection=perspective(1/3,-1,1/2);
draw((0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle,red);
draw((0,0,0)--(0,0,1),red);
draw((0,1,0)--(0,1,1),red);
draw((1,1,0)--(1,1,1),red);
draw((1,0,0)--(1,0,1),red);
draw((0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle,red);
draw((0,0,0)--(1,0,0)--(1,1,0)--cycle,red);
draw((0,0,0)--(1,1,0)--(1,1,1)--cycle,blue);
label("$o$",(0,0,0),NW);
label("$x=1$",(0.5,0,0),S);
label("$y=1$",(1,1,0.5),E);
label("$z=1$",(1,0.5,0),SE);
label("$c$",(0.5,0.5,0.5),N);

Which renders to [asy] import three; unitsize(1cm); size(200); currentprojection=orthographic(1/3,-1,1/2); draw((0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle,red); draw((0,0,0)--(0,0,1),red); draw((0,1,0)--(0,1,1),red); draw((1,1,0)--(1,1,1),red); draw((1,0,0)--(1,0,1),red); draw((0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle,red); draw((0,0,0)--(1,0,0)--(1,1,0)--cycle,red); draw((0,0,0)--(1,1,0)--(1,1,1)--cycle,blue); label("$o$",(0,0,0),NW); label("$x=1$",(0.5,0,0),S); label("$y=1$",(1,1,0.5),E); label("$z=1$",(1,0.5,0),SE); label("$c$",(0.5,0.5,0.5),N);[/asy]

For other examples, see Platonic solids and 2000 AMC 12 Problems/Problem 25.

Other 3D Modules

Other modules in Asymptote that are for 3D are:

  • graph3
  • grid3
  • contour3
  • and solids.

 


 

Asymptote: Help

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

Troubleshooting

Problem 1: You have added the Asymptote tool to TeXnicCenter, but when you hit your hotkey, you get an ‘Cannot execute the command’ error.

What might be going on: TeXnicCenter doesn’t see the Asymptote executable.

Possible Solution: Go back to the Tools -> Customize window (click Tools, then Customize in TeXnicCenter). Then, click the Tools tab, then select Asymptote. Instead of cutting and pasting the text for the Command line, click the … button after the Command entry box. Then, navigate to the asy.exe executable. You’ll probably have to go up several levels to C:, then click Program Files, then Asymptote, then the file asy with the stylized red A next to it.
Problem 2: I have everything installed and the hotkey doesn’t give me an error in TeXnicCenter, but when I hit the hotkey to compile my code, all that happens is that a window flashes briefly then disappears. Nothing else seems to happen.

What might be going on: Your file is saved as a .tex file, not a .asy file. (TeXnicCenter will even make it look like it’s an .asy file, but it’s really saved as an .asy.tex file.)

Solution: Click file, then Save As, then type your filename with the .asy extension (such as test.asy), the set the ‘Save as type’ field to ‘All files’. This should make it a .asy file.

If this does not work then (for Windows XP) go to My Computer, Tools, Folder Options…, File Types. Click on New and type ASY, then OK. Making sure that the file type ASY is highlighted, click the Change button to the right of “Opens with:”. From here go to browse and find the Asymptote executable program file click Open then OK. Now, retry the File, Save As extension change method outlined above.

It would also help to show file extensions so that file.tex, file.asy, file.pdf aren’t all listed in Explorer and Save boxes as file.

Show File Extension in Windows XP gives a simple walkthrough to show how to do this.
Problem 3: You can draw images with Asymptote, but it doesn’t compile your file whenever you have a label containing LaTeX code (for example, the line label("$P$",(50,50),S); as in test.asy.)

What might be going on: Either Asymptote is not finding your LaTeX compiler, or you have installed your TeX distribution (such as MikTeX) in a folder that Asymptote cannot find.

Solution: Let’s say you installed miktex in the folder C:\Program Files\MiKTeX 2.6 (the default). (If you have a different version of MiKTeX, change the 2.6 to the appropriate number. Look in C:\Program Files for the MiKTeX directory to see what this number should be.) Then there should be a subfolder miktex, containing the folder bin, which contains your latex compiler. When Asymptote is run, it looks for a file called config.asy in the folder C:\Documents and Settings\%USERNAME%\.asy\ where %USERNAME% is your Windows user name. (In Windows Vista, this folder is C:\Users\%USERNAME%\.asy\, where %USERNAME% is your Windows username.) Double-click on the .asy folder, right click and choose New $\rightarrow$ Text Document. This will create a file called New Text Document.txt. Right-click on this file to rename it as config.asy, then open this file with Notepad or a similar editor. Type or copy-and-paste the following lines into the document:

import settings;
texpath="C:\Program Files\MiKTeX 2.6\miktex\bin";
dvips="C:\Program Files\MiKTeX 2.6\miktex\bin\dvips";

When you have finished, save the file config.asy.

As mentioned in the solution to Problem 1, you must make sure that the config.asy file is an ASY file, and not a TXT file that is named config.asy. Look in the File Type column in the Details view to see what type of file config.asy is. If your regular text editor insists on making config.asy a TXT document, use TeXnicCenter to create config.asy.
Problem 4: You run Asymptote and get error: could not load module 'plain'.

What might be going on: Asymptote cannot find the files it needs to run. It may need telling which directory it is installed in.

Solution: You need to create or edit the config.asy file. See the solution to Problem 3 for how to find or create it. Assuming that Asymptote is installed at C:\Program Files\Asymptote then

a) if you already have a config.asy file add the following line somewhere after “import settings;”:

dir="C:\Program Files\Asymptote";

or

b) if you have created a new config.asy file then add the following lines:

import settings;
dir="C:\Program Files\Asymptote";

See the note at the end of Problem 3 regarding creating config.asy.
Problem 4b: You run Asymptote and get
error: could not load module 'plain'
error: could not load module 'file'
.

What might be going on: Asymptote does not like file names that contain spaces.

Solution: Replace all spaces in the file name with underscores.
Problem 5: Asymptote suddenly starts misbehaving giving strange errors, going into infinite loops, and not recognizing standard package commands.

What might be going on: A file with the same name as one of the standard package files (like graph.asy or math.asy) was created either by you or by $\LaTeX$ in your working directory or some other directory which Asymptote searches before going to its own directory, so that file is imported instead of the package file.

Prevention: Never name your asy or tex files graph.asy, math.tex and such (these two names seem to be the most common)

Solution: Find all extra files with the same name as standard packages and either .asy or .tex extension and rename or delete them.

External Sources

The following are excellent resources on Asymptote for topics not covered in this guide:

Next: Useful functions

 


 

Asymptote: Useful functions

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

Intersection points

pair[] intersectionpoints(path, path);

Examples:

size(8cm,0);
import math;
import graph;
real r,s;
pair a,b, common;
path circ1, circ2;
r=1; s=1;
a=(0,0);
b=(1,0);
circ1=circle(a,r);
circ2=circle(b,s);
draw(circ1,linewidth(1bp));
draw(circ2,1bp+green);
pair [] x=intersectionpoints(circ1, circ2);
dot(x[0],3bp+blue);
dot(x[1],3bp+blue);
draw(x[0] -- x[1],1bp+red);

[asy]size(200,200); import math; import graph; real r,s; pair a,b, common; path circ1, circ2; r=1; s=1; a=(0,0); b=(1,0); circ1=circle(a,r); circ2=circle(b,s); draw(circ1,linewidth(1bp)); draw(circ2,1bp+green); pair [] x=intersectionpoints(circ1, circ2); dot(x[0],3bp+blue); dot(x[1],3bp+blue); draw(x[0] -- x[1],1bp+red);[/asy]

path g=(-3,-sqrt(3))--(3,-sqrt(3))--(0,2*sqrt(3))--cycle;
draw(g,black);
path p=-2/3*(-3,-sqrt(3))--(-2/3)*(3,-sqrt(3))--(-2/3)*(0,2*sqrt(3))--cycle;
draw(p,black);
pair [] x=intersectionpoints(g, p);
fill(x[0]--x[1]--x[2]--x[3]--x[4]--x[5]--cycle,black);
dot((0,0),white);

[asy] path g=(-3,-sqrt(3))--(3,-sqrt(3))--(0,2*sqrt(3))--cycle; draw(g,black); path p=-2/3*(-3,-sqrt(3))--(-2/3)*(3,-sqrt(3))--(-2/3)*(0,2*sqrt(3))--cycle; draw(p,black); pair [] x=intersectionpoints(g, p); fill(x[0]--x[1]--x[2]--x[3]--x[4]--x[5]--cycle,black); dot((0,0),white); [/asy] Note that this function will cause an error if the paths involved do not intersect.
Next: CSE5

 


 

Asymptote: CSE5

Asymptote (Vector Graphics Language)
Getting StartedBasicsDrawingReferenceExamplesMacros and Packages Advanced Asymptote3D GraphicsHelpUseful functionsCSE5 PackageHow to

The Asymptote CSE5 package is another package that is supported on the AoPS forum. You can download it here. The original idea for writing CSE5 was to enable writing terse codes for complicated pictures. Also some commands in CSE5 (like IP()) work slightly better than the standard Asymptote routines in some cases (the typical example is finding the intersection point of a circle and a tangent line). At last, CSE5 allows to handle some runtime errors like an attempt to find an intersection point of two paths without common points, which may be useful if you try to draw a picture just from its verbal description and do not want to make any calculations beforehand. CSE5 contains doubles of each function; a function with a full name and a function with an abbreviated name.

Contents

Function List

MarkPoint          ( MP  );
MarkCurve          ( MC  );
MarkAngle          ( MA  );
Drawing            ( D   );
DrawPathArray      ( DPA );
IntersectionPoint  ( IP  );
OtherPoint         ( OP  );
IntersectionPoints ( IPs );
WayPoint           ( WP  );
Line               ( L   );
CirclebyRadius     ( CR  );
CirclebyPoint      ( CP  );
CopyClean          ( CC  );

Function descriptions

MarkAngle ( MA )

pair MA(real a,Label s,int f,pen p,pair B,pair A,pair C,real r,int m,pair P,pen q)

The MA() or MarkAngle() command is a competitor of the olympiad anglemark(). The usage is MA( a,s,f,p,B,A,C,r,m,S,q ) (the necessary arguments are bold and the optional ones are italic). Here

a is the angle by which you want to rotate the label in degrees (0 by default);

s is the string labelling the angle (empty string by default); the dollar (or now rather \(…\)) delimeters will be added automatically as in other cse5 labelling commands;

f is the fontsize for your label (currently 9 by default; can be also reset by the anglefontsize=… command);

p is the pen you wish to use for the label (red by default; can be also reset by the anglefontpen=… command);

B,A,C are the pairs determining the angle $\angle BAC$ with the vertex $A$. The arcs are drawn counterclockwise from the side $AB$ to the side $AC$;

r the radius of the largest arc in the anglemark;

m the number of arcs in the anglemark ($1$ by default). Can be set to $0$, in which case no arcs will be drawn, $1$ through $7$, in which case $m$ arcs will be drawn, and anything $>7$ in which case the sector will be filled with color;

S allows you to position the label manually with respect to the middle point of the largest arc. The automatic positioning is where you, probably, want it to be the most: on the bisector of the angle right beyond the largest arc in the anglemark.

q is the pen used for drawing the arcs or filling the sector (green by default; can be also reset by the anglepen=… command).

MA() returns the vertex of the angle.

Example of usage:

size(200);
pair A=dir(-40), B=dir(0);
for(int k=0;k<9;++k)
{
pair C=dir(40*(k+1));
D(A--D(MA(k*40+90,"\beta_{"+string(k)+"}",12,black,C,B,A,0.1,k,orange))--C);
A=B;B=C;
}

results in [asy] size(200); pair A=dir(-40), B=dir(0); for(int k=0;k<9;++k) { pair C=dir(40*(k+1)); D(A--D(MA(k*40+90,"\beta_{"+string(k)+"}",12,black,C,B,A,0.1,k,orange))--C); A=B;B=C; } [/asy]

See Also

 

 


 

Asymptote: How to

Additional Asymptote Learning

Basics

Drawing

Filling

Labeling

Marking Angles

Using Asymptote on Aops Wiki

Intermediate

Graphing

Olympiad Package Basics

Advanced