Difference between revisions of "Asymptote: Macros and Packages"

 
m (Added information about making a function with no output)
 
(23 intermediate revisions by 9 users not shown)
Line 2: Line 2:
  
 
==Definitions==
 
==Definitions==
You can define your own functions in Asymptote as well.  As an example, let's say you wanted to make a function called <tt>newfunction</tt> that takes a pair <math>(a,b)</math> and a real value <math>r</math> as input, and returns the pair <math>(a+r,b+r)</math>.  In addition, you want it to simply return the pair <math>(a,b)</math> if no value of <math>r</math> is specified, so you want <math>r</math> to default to <math>0</math>.  The code would be as follows:
+
You can define your own functions in Asymptote.  As an example, let's say you wanted to make a function called <tt>newfunction</tt> that takes a pair <math>(a,b)</math> and a real value <math>r</math> as input, and returns the pair <math>(a+r,b+r)</math>.  In addition, you want it to simply return the pair <math>(a,b)</math> if no value of <math>r</math> is specified, so you want <math>r</math> to default to <math>0</math>.  The code would be as follows:
 
   pair newfunction(pair z, real r=0)
 
   pair newfunction(pair z, real r=0)
 
   {
 
   {
Line 15: Line 15:
 
See if it works!
 
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 <math>r</math> was set to <math>0</math> by <math>r=0</math>, and the actual function procedure goes in between <tt>{}</tt>. This is the general format for a function definition.
+
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 <math>r</math> was set to <math>0</math> by <math>r=0</math>, and the actual function procedure goes in between <tt>{}</tt>. To define a function with no output simply put <tt>void</tt> before the function name. This is the general format for a function definition.
  
 
==Packages==
 
==Packages==
  Asymptote comes with several packages that contain useful functions for various purposes.  For example, the package <tt>graph.asy</tt> contains the function  
+
Asymptote comes with several packages that contain useful functions for various purposes.  For example, the package <tt>graph.asy</tt> contains the function  
 
   Circle(pair p, real r, int n=400);
 
   Circle(pair p, real r, int n=400);
 
which is a more accurate circle (having 400 nodes by default) than the built-in <tt>circle</tt> command.  To use this function and others in graph.asy, simply put the command
 
which is a more accurate circle (having 400 nodes by default) than the built-in <tt>circle</tt> command.  To use this function and others in graph.asy, simply put the command
  include graph;
+
  import graph;
at the top of your Asymptote document.
+
at the top of your Asymptote document. Graph also has more advanced functions such as the ability to [http://artofproblemsolving.com/wiki/index.php?title=Asymptote:_Graphing Graph a function]
  
You can create your own package by simply creating a new .asy file (say <tt>MyMacros.asy</tt>) with your own definitions in it, and saving it in the directory in which Asymptote is installed (<tt>C:\Program Files\Asymptote</tt> by default).  Then </tt>include MyMacros;</tt> in your document, and you'll be set!
+
You can create your own package by simply creating a new .asy file (say <tt>MyMacros.asy</tt>) with your own definitions in it, and saving it in the directory in which Asymptote is installed (<tt>C:\Program Files\Asymptote</tt> by default).  Then <tt>import MyMacros;</tt> in your document, and you'll be set!
 
    
 
    
 
===The Olympiad Package===
 
===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 [[http://web.mit.edu/monks/www/olympiad.asy here]], and saving the page as ``olympiad.asy'' in your Asymptote directory (\verb1C:\Program Files\Asymptote1 by default).  This package includes the following definitions:
+
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 [http://math.berkeley.edu/~monks/images/olympiad.asy here] or [http://www.artofproblemsolving.com/Forum/viewtopic.php?f=519&t=165767  here] (the latter link has a few usage examples).
    \begin{table}[H]
+
 
    \begin{center}
+
This package includes the following definitions:
    \begin{tabular}{|l|l|} \hline
+
 
    \textbf{Command} & \textbf{Description} \\ \hline
+
[[Image:Olympiad1.gif]]
      \verb1origin1                            & The pair \verb1(0,0)1. \\\hline
+
 
      \verb1waypoint(path p, real r)1          & The point <math>r</math> of the way along\\
+
[[Image:Olympiad2.gif]]
                                              & path p with respect to length, \\
+
 
                                              & where <math>0\le r\le1</math>. \\\hline
+
[[Image:Olympiad3.gif]]
      \verb1midpoint(path p)1                  & The midpoint of path p. \\\hline
+
 
      \verb1foot(pair P, A, B)1                & The foot of the perpendicular\\
+
[[Image:Olympiad4.gif]]
                                              & from point <math>P</math> to line <math>AB</math>.\\\hline
+
 
      \verb1bisectorpoint(pair A, B, C)1      & A point on the angle bisector \\
+
[[Image:Olympiad5.gif]]
                                              & of <math>\angle ABC</math> that is a unit \\
 
                                              & distance from <math>B</math>. \\\hline
 
      \verb1bisectorpoint(pair A, B)1          & A point on the perpendicular \\
 
                                              & bisector of segment AB that is \\
 
                                              & a unit distance from line AB. \\\hline
 
      \end{tabular}
 
    \caption{Useful points defined in the olympiad.asy package}
 
    \end{center}
 
    \end{table}
 
   
 
    \begin{table}[H]
 
    \begin{center}
 
    \begin{tabular}{|l|l|} \hline
 
    \textbf{Command} & \textbf{Description} \\ \hline
 
    \verb1circumcenter(pair A, B, C)1        & The circumcenter of <math>\triangle ABC</math>.\\\hline
 
      \verb1circumradius(pair A, B, C)1        & The circumradius of <math>\triangle ABC</math>.\\\hline
 
      \verb1circumcircle(pair A, B, C)1        & The circumcircle of <math>\triangle ABC</math>.\\\hline
 
      \verb1incenter(pair A, B, C)1            & The incenter of <math>\triangle ABC</math>.\\\hline
 
      \verb1inradius(pair A, B, C)1            & The inradius of <math>\triangle ABC</math>.\\\hline
 
      \verb1incircle(pair A, B, C)1            & The incircle of <math>\triangle ABC</math>.\\\hline
 
      \verb~tangent(pair P, pair O,~          & The nth point of tangency\\
 
      \verb~        real r, int n=1)~          & from a point P to the circle\\
 
                                              & with center O and radius r\\
 
                                              & where n can be 1 or 2 -\\
 
                                              & the points of tangency are\\
 
                                              & labeled in counterclockwise\\
 
                                              & order around the circle.\\\hline
 
      \verb1cyclic(pair A, B, C, D)1          & A boolean function that\\
 
                                              & returns true if ABCD is \\
 
                                              & a cyclic quadrilateral.* \\\hline
 
      \end{tabular}
 
    \caption{Circle-related definitions in the olympiad.asy package}
 
    \end{center}
 
    \end{table}
 
   
 
    \begin{table}[H]
 
    \begin{center}
 
    \begin{tabular}{|l|l|} \hline
 
    \textbf{Command} & \textbf{Description} \\ \hline
 
      \verb1concurrent(pair A, B, C, D, E, F)1 & A boolean function that\\
 
                                              & returns true if AB, CD, EF are \\
 
                                              & concurrent or mutually parallel.* \\\hline
 
      \verb1collinear(pair A, B, C)1          & A boolean function that\\
 
                                              & returns true if A, B, and \\
 
                                              & C are collinear. \\\hline    
 
      \end{tabular}
 
    \caption{Collinearity and concurrency from the olympiad.asy package}
 
    \end{center}
 
    \end{table}
 
   
 
    \begin{table}[H]
 
    \begin{center}
 
    \begin{tabular}{|l|l|} \hline
 
    \textbf{Command} & \textbf{Description} \\ \hline
 
      \verb1centroid(pair A, B, C)1            & The centroid of <math>\triangle ABC</math>.\\\hline
 
      \verb1orthocenter(pair A, B, C)1        & The orthocenter of <math>\triangle ABC</math>. \\\hline    
 
      \end{tabular}
 
    \caption{Triangle-related definitions in the olympiad.asy package}
 
    \end{center}
 
    \end{table}\\
 
    \begin{table}[H]
 
    \begin{center}
 
    \begin{tabular}{|l|l|} \hline
 
    \textbf{Command} & \textbf{Description} \\ \hline
 
      \verb1rightanglemark(pair A, B, C, 1    & Marks right angle ABC with\\
 
      \verb1              real s=8)1          & a right angle mark of length s\\\hline
 
      \verb1anglemark(pair A, B, C, 1          & Marks angle ABC with several\\
 
      \verb~          real t=8 ... real[] s)~  & circular arcs of radii specified\\
 
                                              & in the last argument, an array \\
 
                                              & of real values. \\\hline      
 
      \verb~pathticks(path g, int n=1,~        & Marks path g with n ticks spaced\\
 
      \verb~      real r=.5, spacing=6, s=8)~ & spacing apart with length s, at\\
 
                                              & the point r of the way along g \\
 
                                              & with respect to arc length. \\\hline
 
      \end{tabular}
 
    \caption{Tick marks and angle marks in the olympiad.asy package}
 
    \end{center}
 
    \end{table}
 
  
 
'''Note:''' A sequence of variables without type declarations indicates that they are the same type as the variable preceding it.  For example, the notation <tt>concurrent(pair A, B, C, D, E, F)</tt> indicates that all of the variables should have type pair.
 
'''Note:''' A sequence of variables without type declarations indicates that they are the same type as the variable preceding it.  For example, the notation <tt>concurrent(pair A, B, C, D, E, F)</tt> indicates that all of the variables should have type pair.
  
These boolean functions test for equality within <math>10^{-5}</math> ps points in order to avoid approximation errors.
+
<tt>*</tt> These boolean functions test for equality within <math>10^{-5}</math> ps points in order to avoid approximation errors.
 +
 
 +
[[Asymptote: Advanced|Next: Advanced]]

Latest revision as of 00:43, 24 April 2019

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

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