Difference between revisions of "Asymptote: Basics"

 
m (Improved readability, especially for people without prior coding experience. Removed $$ tags around the equal sign, since it looked strange. Removed example which referenced prior code that didn't exist.)
 
(26 intermediate revisions by 15 users not shown)
Line 2: Line 2:
  
 
== Syntax ==
 
== Syntax ==
In Asymptote, every command you give it should be followed by a semicolon (;). This tells Asymptote how to separate one command from the nextFor example, the command <tt>draw(A--B)</tt> draws a line segment from point <math>A</math> to <math>B</math> given as coordinate pairs. Thus, if you wanted to draw two line segments, say one of them from <math>(0,0)</math> to <math>(50,50)</math> (in units of PostScript points - see below for the explanation of units and size) and the other from <math>(50,0)</math> to <math>(0,50)</math>, you can create the following document:
+
On AoPS, all Asymptote diagram are declared with the "[asy]" tag and ended with the "[/asy]" tag. Each command in Asymptote must be separated by a semicolon (;), similar to programming languages like C and Java. This convention tells Asymptote where each command ends.
 +
 
 +
 
 +
Commands do not necessarily need to be on separate lines, since semicolons are the only method by which Asymptote interprets new commandsHowever, putting the commands on separate lines is often useful to separate your commands to organize your code and improve readability for other AoPSers looking to interpret 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 (Lines that are not interpreted by Asymptote), start a line with two forward slashes.
 +
 
 +
 
 +
Multi-line comments can be declared with /* and ended with */.  
 +
 
 +
 
 +
Applying what we've learned, we can now write clear, legible, Asymptote code:
 +
 
 +
 
 +
 +
/*
 +
This is a program
 +
that draws a triangle
 +
*/
 +
//Below, we draw the first segment.
 
  draw((0,0)--(50,50));
 
  draw((0,0)--(50,50));
  draw((0,50)--(50,0));
+
//And the next two segments, written without a line break between them
 
+
  draw((50,0)--(50,50));draw((50,0)--(0,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 you 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.
 
  
 
==Variables and Data Types==
 
==Variables and Data Types==
  Asymptote parses your code into substrings which have a certain '''data type''', for example a ''real'' (like <math>1.5</math>) or a ''pair'' (like <math>(2,3)</math>).  Each new variable that you declare must be declared as a data type that Asymptote recognizes, by the command <tt>[datatype] [variable];</tt>.  For example, if you wanted to declare the variable <math>n</math> to have type integer, you can use the command
+
When coding in Asymptote, it's often helpful to store data in '''data types'''. For example, a ''real'' number like <math>1.5</math> or a ''pair'' of numbers like <math>(2,3)</math>.  Each new variable that you create must be declared using a data type, with the command <tt>[datatype] [variable];</tt>.  For example, if you wanted to declare the variable <math>n</math> to have type integer, you can use the command
 
   int n;
 
   int n;
  
After it is declared, you can store a specific value in a variable using the <math>=</math> symbol, as in
+
After it's declared, you can store a specific value in a variable using the = symbol, as in
 
   n=3;
 
   n=3;
  
These two commands can be abbreviated by the single command <tt>int n=3;</tt>, and several integers can also be declared at once (<tt>int m,n,d;</tt>).
+
These two commands can be abbreviated by the single command <tt>int n=3;</tt>, and several integers can also be declared at once (<tt>int m,n,d;</tt>) or even many declarations and assignations at once: <tt>int a,b=2,c,d=5;</tt>.
  
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:
+
The most commonly used data types in Asymptote are given in the following table:
    pair A,B,C,D;
+
<center>[[Image:Table1.gif]]</center>
    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:
+
==Size and Unitsize==
  \begin{tabular}{|c|l|l|}\hline
+
Asymptote is a primarily coordinate-based graphics languageEach point is a pair <math>(a,b)</math> where <math>a</math> is the <math>x</math>-coordinate and <math>b</math> is the <math>y</math>-coordinate.
  \textbf{Type} & \textbf{Description} & \textbf{Examples} \\ \hline
+
 
  bool & A statement, either true or false. & \verb1true1, \verb1false1, \verb31>23 (false) \\ \hline
+
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 <math>x</math>- and <math>y</math>-axisAsymptote 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 <math>x</math> and <math>y</math> directions is the PostScript bigpoint, which has length <math>1/72</math> inches.  Thus, if you do not change the scaling on the picture, the points <math>(0,0)</math> and <math>(72,0)</math> 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.
  string & A string of characters,& \verb1"Hi!"1,\\
 
    & enclosed by quotation marks. & \verb1"This is a string."1 \\\hline
 
  int & An integer value. & \verb -2 ,\verb -1 ,\verb 0 ,\verb 1 ,\verb 2 ,\verb 3  \\ \hline
 
  real & A real decimal number. & \verb 1.0 , \verb 5.48 , \verb 12345.6789 \\ \hline
 
  pair & A pair of real numbers or integers. & \verb (0,2) , \verb (-30,42.5)  \\ \hline
 
  triple & An ordered triple of numbers. & \verb (1,2,3) , \verb (-2.5,5,4) \\ \hline
 
  path & A fixed cubic spline. & \verb1(0,0)--(5,0)1 \\
 
    & & \verb2(0,1)..(1,0)..(1,1)--cycle2 \\ \hline
 
  guide & A cubic spline, like path, & \verb1(0,0)--(5,0)1 \\
 
    & but free to adjust for smoothness & \verb2(0,1)..(1,0)..(1,1)--cycle2 \\
 
    &  if joined to another guide. & \verb2(0,1)--(1,0)--(1,1)--cycle2 \\\hline
 
  picture & A canvas for drawing & \verb1currentpicture1, \\
 
  & in user coordinates.& any set of objects\\\hline
 
  frame & A canvas for drawing & \verb1currentpicture1,\\
 
  & in PostScript coordinates.& any set of objects\\\hline
 
  pen & An object consisting of a color, & \verb1currentpen1, \verb1red+dashed1,\\
 
  & thickness, and dash pattern & \verb1linetype("6 4")1\\
 
  & used for drawing & \\\hline
 
  transform & A transformation of the plane & \verb1scale(2)1, \verb1xscale(2)1, \\
 
    & that can be applied using * to & \verb1rotate(30)1, \verb1rotate(30,(2,3))1, \\
 
    & any object that can be drawn. & \verb1shift((2,4))1 \\\hline
 
  void & Used for declaration of functions& \\
 
    & having no arguments. & \\ \hline
 
 
 
  \end{tabular}
 
  
\subsection{Size and Unitsize}
+
The function <tt>unitsize</tt> 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 <tt>currentpicture</tt>, 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
  Asymptote is a primarily coordinate-based graphics language.  Each point is a pair <math>(a,b)</math> where <math>a</math> is the <math>x</math>-coordinate and <math>b</math> is the <math>y</math>-coordinate.
+
unitsize(72);
  \par 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 <math>x</math>- and <math>y</math>-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 <math>x</math> and <math>y</math> directions is the PostScript bigpoint, which has length <math>1/72</math> inches.  Thus, if you do not change the scaling on the picture, the points <math>(0,0)</math> and <math>(72,0)</math> 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.
 
  \par The function \verb1unitsize1 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 \verb1currentpicture1, 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 \\
 
  \verb&    unitsize(72); &\\
 
 
will tell Asymptote that from now on, your unit length is <math>1</math> inch.  Be careful when you are redefining your unit length - now that unitsize is set to <math>72</math>, the points <math>(0,0)</math> and <math>(72,0)</math> are actually <math>72</math> inches apart!
 
will tell Asymptote that from now on, your unit length is <math>1</math> inch.  Be careful when you are redefining your unit length - now that unitsize is set to <math>72</math>, the points <math>(0,0)</math> and <math>(72,0)</math> are actually <math>72</math> inches apart!
\par Asymptote has the built-in constants \verb1pt1 (1/72.27 inches), \verb1inch1, \verb1cm1, and \verb1mm1 for convenience when defining lengths, so the above command can also be stated:\\
+
 
\verb&    unitsize(1inch);&
+
Asymptote has the built-in constants <tt>pt</tt> (1/72.27 inches), <tt>inch</tt>, <tt>cm</tt>, and <tt>mm</tt> for convenience when defining lengths, so the above command can also be stated
\par The other useful function is \verb1size1, which specifies the exact width and height that your picture (if unspecified as a first argument, this will again default to \verb1currentpicture1) will be drawn to.  If only one number is given, that will be the width of the picture and the height will be left free to scale as necessary to keep the x- and y- unit sizes the same. For example, the command \\
+
unitsize(1inch);
\verb&    size(5cm,5cm);& \\
+
 
will fit the diagram to a <math>5</math>cm<math>\times 5</math>cm box regardless of the specified unitsizes.
+
The other useful function is <tt>size</tt>, which specifies the exact width and height of the box that your picture (if unspecified as a first argument, this will again default to <tt>currentpicture</tt>) 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
\par As an example, make an Asymptote document containing the following two lines:
+
size(5cm,5cm);
\begin{verbatim}
+
or just
unitsize(2inch);  
+
size(5cm);
draw(unitsquare);\end{verbatim}
+
will fit the diagram to a 5cm x 5cm box regardless of the specified unitsizes.
and see what happens as you change the <math>2</math> inch size to several other values.
+
 
 +
As an example, make an Asymptote document containing the following two lines:
 +
unitsize(2inch);  
 +
draw(unitsquare);
 +
and see what happens as you change the <math>2</math> inch size to several other values.
 +
 
 +
 
 +
[[Asymptote: Drawing | Next: Drawing]]

Latest revision as of 00:39, 11 March 2023

Asymptote (Vector Graphics Language)
Getting Started - Basics - Drawing - Labeling - Filling - Useful functions - Examples - Macros and Packages

Help - Reference - Advanced Asymptote - 3D Graphics - CSE5 Package - How to

Syntax

On AoPS, all Asymptote diagram are declared with the "[asy]" tag and ended with the "[/asy]" tag. Each command in Asymptote must be separated by a semicolon (;), similar to programming languages like C and Java. This convention tells Asymptote where each command ends.


Commands do not necessarily need to be on separate lines, since semicolons are the only method by which Asymptote interprets new commands. However, putting the commands on separate lines is often useful to separate your commands to organize your code and improve readability for other AoPSers looking to interpret 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 (Lines that are not interpreted by Asymptote), start a line with two forward slashes.


Multi-line comments can be declared with /* and ended with */.


Applying what we've learned, we can now write clear, legible, Asymptote code:


/*
This is a program
that draws a triangle
*/
//Below, we draw the first segment.
draw((0,0)--(50,50));
//And the next two segments, written without a line break between them
draw((50,0)--(50,50));draw((50,0)--(0,0));

Variables and Data Types

When coding in Asymptote, it's often helpful to store data in data types. For example, a real number like $1.5$ or a pair of numbers like $(2,3)$. Each new variable that you create must be declared using a data type, with 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's 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;.

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$-axis. 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