# Mathematical graphics with Asymptote

Asymptote is a vector graphics programming language.

It's features include:

• complex multiplication; E.g. pair j=(0,1); pair a = j^2; (Multiplication by j => coordinate is rotated by 90 degrees)
• embedded equations using LaTeX
• a simple and elegant syntax for path-specification inspired by and similar to MetaPost

# Examples

Often the best way to get a feel for a language is just to try some examples. Some simple ones are presented below.

To test these and see the results you will need to compile them.

1. Open a text editor, copy+paste the text below (or download an example from here)
2. Save your file with the extension '.asy' and whatever name you want.
3. Then compile it using a command such as: asy example.asy. Replace 'example.asy' with the name of your file.

If you are using a version of Asymptote different to the one that the examples below were written for, you might witness errors while trying to compile. If the cause of the error isn't obvious, perhaps head on over to the Asymptote forum, which can be accessed via Sourceforge: http://asymptote.sourceforge.net/ .

## Example 1 - Basics

In this example we will draw the picture shown below.

The default output format when using the asy command is encapsulated PostScript (which has the .eps file extension). We can change to PDF by using settings.outformat = "pdf".

import graph;
import patterns;

size(5cm);

settings.outformat = "pdf";

int i, n = 5;

draw(unitcircle, grey);

dotfactor *= 1.5;

pair[] points;

for (i=0; i<n; i=i+1) {
points[i] = expi(2*pi*i/n);
}

path figure = graph(points) -- cycle;

dotfactor *= 1.5;
filldraw(figure, pattern("hatch"));
dot(points, red);

axes(Arrow, above=true);

xtick("1", 1);
ytick("$i$", 1);

xtick(Label("0", align=SE), 0);

shipout(outprefix()+"-fig1");

## Example 2 - Polar curve

It is also easy to draw polar graphs with Asymptote. The example below shows one way to do this. The key line is where we use the polargraph() function rather than the graph() function.

import graph;

size(9cm, IgnoreAspect);

settings.outformat = "pdf";

pen tpen = rgb(.8,.8,.8)+linewidth(.7pt);
pen cpen = linewidth(1.2pt);

real c1(real theta){
real r = sin(theta);
return r;
}
real c2(real theta) {
return cos(theta);
}
real c3(real theta) {
real a = 1;
return a*cos(2*theta);
}

guide g1 = polargraph(c1, 0, pi);
guide g2 = polargraph(c2, 0, pi);
guide g3 = polargraph(c3, 0, 2*pi) -- cycle;

draw(g1, cpen+blue);
draw(g2, cpen+yellow);
filldraw(g3, rgb(.82,.82,1)+opacity(.5), cpen+red);

xaxis("$x$", LeftTicks, Arrow, above=true);
yaxis("$y$", LeftTicks, Arrow, above=true);

xaxis(BottomTop, invisible, LeftTicks(extend=true, ptick=tpen, pTick=tpen, step=1/10));
yaxis(LeftRight, invisible, LeftTicks(extend=true, ptick=tpen, pTick=tpen, step=1/10));

## More examples

More examples can be found in my Git repository .

Tags: