AoPSWiki:Sandbox

Essential Reference for Using Asymptote in AoPS

A focused asymptote reference with examples and short code.


Original source from eagle702 bulletin board post. Adapted and edited by aquadragon.


Introduction


Why Asymptote is called Asymptote?

http://asymptote.sourceforge.net/FAQ/section1.html#whyasy

Question 1.4. Why was the name Asymptote chosen?

Well, it isn't the perfect graphics package, but we do think it is getting there asymptotically...

Tips

  • To see the asymptote code of any diagram made with asymptote, click on the diagram and it will come up.
  • If your code is not working check to make sure you have a semicolon after each line, no extra parentheses or brackets, and no open parentheses or brackets.
  • Another thing to check if your code is not working, is to make sure every variable you've used in your code is in the pair.


Beginning your code

To start your asymptote diagram you have to include [asy] at the beginning, and [/asy] at the ending.


Setting points

To create points in your diagram you can use

 A = (0,0);

Say you want to have point $B$ $4$ units to the right of point $A$, then you can use

 B = (4,0);

Setting points on your diagram is like setting points on a graph. The first number is the $x$-coordinate of the point and the second number is the $y$-coordinate of the point.

Note: You should not use E, S, N, or W as variables, instead you should use EE, SS, NN, and WW, but label them as E, S, N, W. This is because E, S, N, and W are used to indicate directions in asymptote.


Pair part 1

If you include variables in your diagram you have to have $\verb#pair#$ somewhere in your code.

Example: You have two points $A$ and $B$, and you're drawing a line connecting them, you need to include

  pair A,B; 

in your diagram to make it work.


Connecting two points, coloring the line'

Once you have set variables $A$ and $B$, you can connect them by writing

 draw(A--B);

Example:

[asy] pair A,B; A=(0,0); B=(1,0); draw(A--B); [/asy]

If you want to make the line you're using to connect them red, you can use

draw(A--B--cycle, red);

This will make the line red.

Example:

[asy] pair A,B; A=(0,0); B=(1,0); draw(A--B--cycle, red); [/asy]

You can also make a line dashed by writing draw(A--B--cycle, dashed)

Example:

[asy] pair A,B; A=(0,0); B=(1,0); draw(A--B--cycle, dashed); [/asy]

If you wanted to give the line multiple characteristics, such as a red dashed line, you write + in between the characteristics. draw(A--B--cycle, dashed+red)

Example:

[asy] pair A,B; A=(0,0); B=(1,0); draw(A--B--cycle, dashed+red); [/asy]


Labeling, Midpoint

To label a point you can use

label("$A$",A,dir(135));

The $A$ means you will label the point $A$. The A (no dollar signs) means you will be labeling the point A. The dir(135) determines where on the point the variable will be. Or you can use label("$A$",A,N);. The N means that the label will be above (North) of the point.

Example:

[asy] pair A,B; A=(0,0); B=(1,0); draw(A--B); label("$A$",A,N); label("$B$",B,N); [/asy]

If you want to draw a variable (x) or number at the center of a line you can use

label("$x$", midpoint(A--B), NE);

The NE at the end means North-East, this will decide where on the line the variable will be (above it to the right for NE).

Example:

[asy] pair A,B; A=(0,0); B=(1,0); draw(A--B); label("$A$",A,N); label("$B$",B,N); label("$x$", midpoint(A--B), NE); [/asy]


Angle Measures

If you want to draw an angle measure on an angle use

label(scale(.75)*"$x$", Z, 2.5*dir(150));

In this line of code, there is a very important command. The "dir" command.

The angle for the dir command always starts facing east. So dir(0) would label the part exactly east of what you are labeling. Similarly dir(90) would label it exactly north, dir(180) would label it exactly west, and dir(270) would label it exactly south.

This is called the unit circle; the unit circle is a large concept that will be discussed in AoPS's Precalculus course. Basically, because a circle has $360^\circ$, the starting point is to the right of the circle and the degrees move counterclockwise around the circle. This also means that dir(0) is the same as dir(360). :)

Here's a diagram that will make this tricky concept more understandable.

The starting point, which we will call $w$ is at dir(0). dir(90) is the starting point rotated $90^\circ$ counter clockwise from the starting position; we will call this point $x$. Similarly, dir(180) is the starting point rotated $180^\circ$ counterclockwise from the starting position; we will call this point $y$. Finally, dir(270) is the starting point rotated $270^\circ$ counterclockwise from the starting position; we will call this point $z$.

Here are the variables on the unit circle:

[asy] size(150); import TrigMacros; rr_cartesian_axes(-5,5,-5,5, usegrid = false); draw(Circle((0,0),4),p=black+1bp); label("$w$", (4, 0), NE); label("$x$", (0, 4), NE); label("$y$", (-4, 0), NW); label("$z$", (0, -4), SE); [/asy]

The dir(150), at the end you have to play around with, it will move around the angle to see where the degree mark is. The $Z$ in it means that it is on point $Z$. The $x$ means that the angle is $x$ degrees. The scale(.75) defines the size of the angle mark. The 2.5* defines how far from the point the angle sign is.

Example:

[asy] pair A,B,C; A=(0,1); B=(0,0); C=(1,0); draw(A--B--C--A); label("$A$",A,N); label("$B$",B,W); label("$C$",C,E); label(scale(.75)*"$90^\circ$", B, 2.5*dir(50)); [/asy]


Distance Function

My distance function real dist(pair a,pair b){ return sqrt(abs(a.x-b.x)^2+abs(a.y-b.y)^2); } Took me some much time just to write a distance function. Hopefully, everybody else doesn't have to go through lots of research to make this. A note Apparently a pair has an x property and a y property. I kind of discovered this it by testing. I think it might be documented somewhere.


Casting

(Source: fath2012) Casting This is not really a command but helps you convert a data type to another data type if you can. [asy] pair a = (5,5); int b = 2; dot(a); label((string) a,a,N); label((string) b,a,S); [/asy] pair a = (5,5); int b = 2; dot(a); label((string) a,a,N); label((string) b,a,S);


Right Angle Mark

If you want to draw a right angle mark use this:

draw(rightanglemark(A,B,C,1.5));

The A,B,C means that it will draw the right angle mark on $\angle ABC$. The $1.5$ defines the size of the right angle mark.

Example:

[asy] pair A,B,C; A=(0,1); B=(0,0); C=(1,0); draw(A--B--C--A); label("$A$",A,N); label("$B$",B,W); label("$C$",C,E); draw(rightanglemark(A,B,C,1.5)); [/asy]



Intersection

If you want to draw a point on an intersection of two lines you can use:

F = intersectionpoint(A -- C, B -- D);

In this code $F$ is the intersection point of lines $AC$ and $BD$.

Example:

[asy] pair A, B, C, D, F; A = (1,0); B = -A; C = (0.7,0.7); D = -C; draw(A--B); draw(C--D); label("$A$",A,E); label("$B$",B,N); label("$C$",C,N); label("$D$",D,S); label("$F$",F,SE); F = intersectionpoint(A -- B, C -- D); [/asy]



Filling

To show you how to fill in a part of a diagram with a color I'll use an example from Week 3 Problem 5 in AoPS's Introduction to Geometry course.

[asy] fill((0,0)--(0,5)--(1,5)--(5,0)--cycle,red); fill((7,0)--(1,5)--(7,5)--cycle,red); for (int i=0;i<=7;i+=1) { draw((i,0)--(i,5)); }; for (int j=0;j<=5;j+=1) { draw((0,j)--(7,j)); }; draw((7,0)--(1,5)--(5,0)); [/asy]

Let's walkthrough how the left side of the rectangle is red.

Whoever made the asymptote code for this used the line

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

The coordinates mean that it will fill in the quadrilateral at points (0,0); (0,5); (1,5); and (5,0). The red at the end means that the color will be red, here's what it would look like if I put blue instead of red (on the left and right side).

[asy] fill((0,0)--(0,5)--(1,5)--(5,0)--cycle,blue); fill((7,0)--(1,5)--(7,5)--cycle,blue); for (int i=0;i<=7;i+=1) { draw((i,0)--(i,5)); }; for (int j=0;j<=5;j+=1) { draw((0,j)--(7,j)); }; draw((7,0)--(1,5)--(5,0)); [/asy]

If you want to make it a color like light blue, light brown, dark blue, etc. do not put a space between the two words.

If you wanted to make both sides dark blue you would put

fill((0,0)--(0,5)--(1,5)--(5,0)--cycle,darkblue); fill((7,0)--(1,5)--(7,5)--cycle,darkblue);

Here's what it would look like:

[asy] fill((0,0)--(0,5)--(1,5)--(5,0)--cycle,darkblue); fill((7,0)--(1,5)--(7,5)--cycle,darkblue); for (int i=0;i<=7;i+=1) { draw((i,0)--(i,5)); }; for (int j=0;j<=5;j+=1) { draw((0,j)--(7,j)); }; draw((7,0)--(1,5)--(5,0)); [/asy]

(It looks more like navy to me. But inserting "navy" does not work so I guess that's why.)

You don't have to have both sides the same color though, you could have the left side blue and the right side pink.

[asy] fill((0,0)--(0,5)--(1,5)--(5,0)--cycle,blue); fill((7,0)--(1,5)--(7,5)--cycle,pink); for (int i=0;i<=7;i+=1) { draw((i,0)--(i,5)); }; for (int j=0;j<=5;j+=1) { draw((0,j)--(7,j)); }; draw((7,0)--(1,5)--(5,0)); [/asy]

Or vice versa.

[asy] fill((0,0)--(0,5)--(1,5)--(5,0)--cycle,pink); fill((7,0)--(1,5)--(7,5)--cycle,blue); for (int i=0;i<=7;i+=1) { draw((i,0)--(i,5)); }; for (int j=0;j<=5;j+=1) { draw((0,j)--(7,j)); }; draw((7,0)--(1,5)--(5,0)); [/asy]

Try out different colors to see which ones you prefer for your asymptote diagrams. Have fun!


Dot

If you want to draw a dot on a point on your diagram use

dot(A, p=black+3bp);

This will draw a black dot on point $A$.

Example:

[asy] pair A; A=(0,0); dot(A, p=black+3bp); label("$A$",A,N); [/asy]

You can also use

dot(A);


Expand

#13 Size


Expand

#14 Tick Marks

Expand

#15 Arrows

Expand

#16 Shifting Coordinates

Expand

#17 Rotating

Expand

#18 Circles

Expand

#19 Dashed Lines

Expand

#20 Angle Mark

Expand

#21 "for" Command

Expand

#22 Extension

Expand

#23 How to draw an equilateral triangle

Expand

#24 How to draw an isosceles triangle

Expand

#25 How to draw a scalene triangle

Expand

#26 Labeling Angles

Expand

#27 Drawing triangles using SSS, SAS, AAS, and ASA

Expand

#28 Arcs

Expand

#29 The Nine Point Circle

Expand

#30 Pair Part 2

Expand

#31 Finding the midpoint

Expand

#32 Foot Command

Expand

#33 Introduction to 3D Geometry