Next: Guides and paths, Up: Programming
Asymptote supports the following data types (in addition to
user-defined types):
voidbooltrue and
false. For example:
bool b=true;
defines a boolean variable b and initializes it to the value
true. If no initializer is given:
bool b;
the value false is assumed.
int0
is assumed. The maximum integer is intMax.
real0.0. Real numbers have precision
realEpsilon, with realDigits significant digits.
The smallest positive real number is realMin and the largest
positive real number is realMax.
pair(x,y).
The real and imaginary parts of a pair z can read as z.x
and z.y. We say that x and y are virtual members of
the data element pair; they cannot be directly modified, however.
The implicit initializer for pairs is (0.0,0.0).
There are a number of ways to take the complex conjugate of a pair:
pair z=(3,4);
z=(z.x,-z.y);
z=z.x-I*z.y;
z=conj(z);
A number of built-in functions are defined for pairs:
pair conj(pair z)z;
real length(pair z)|z| of its argument z.
For example,
pair z=(3,4);
write(length(z));
produces the result 5. A synonym for length(pair) is abs(pair);
real angle(pair z)z in radians in the interval
[-pi,pi];
real degrees(pair z, bool warn=true)z in degrees in the interval [0,360)
or 0 if warn is false and z.x=z.y=0 (rather than
producing an error);
pair unit(pair z)z;
pair expi(real angle)angle measured in radians;
pair dir(real angle)angle measured in degrees;
real xpart(pair z)z.x;
real ypart(pair z)z.y;
pair realmult(pair z, pair w)(z.x*w.x,z.y*w.y);
real dot(pair z, pair w)z.x*w.x+z.y*w.y;
pair minbound(pair z, pair w)(min(z.x,w.x),min(z.y,w.y));
pair maxbound(pair z, pair w)(max(z.x,w.x),max(z.y,w.y)).
triple(x,y,z) used for
three-dimensional drawings. The respective components of a triple
v can read as v.x, v.y, and v.z.
The implicit initializer for triples is (0.0,0.0,0.0).
Here are the built-in functions for triples:
real length(triple v)|v| of the vector v.
A synonym for length(triple) is abs(triple);
real polar(triple v)v measured from the z axis in radians;
real azimuth(triple v)v measured from the x axis in radians;
real colatitude(triple v)v measured from the z axis in degrees;
real latitude(triple v)v measured from the xy plane in degrees;
real longitude(triple v, bool warn=true)v measured from the x axis in degrees;
or 0 if warn is false and v.x=v.y=0 (rather than
producing an error);
triple unit(triple v)v;
triple expi(real colatitude, real longitude)(colatitude,longitude)
measured in radians;
triple dir(real colatitude, real longitude)(colatitude,longitude)
measured in degrees;
real xpart(triple v)v.x;
real ypart(triple v)v.y;
real zpart(triple v)v.z;
real dot(triple u, triple v)u.x*v.x+u.y*v.y+u.z*v.z;
triple cross(triple u, triple v)(u.y*v.z-u.z*v.y,u.z*v.x-u.x*v.z,u.x*v.y-v.x*u.y);
triple minbound(triple u, triple v)(min(u.x,v.x),min(u.y,v.y),min(u.z,v.z));
triple maxbound(triple u, triple v)(max(u.x,v.x),max(u.y,v.y),max(u.z,v.z)).
stringstring class.
Strings delimited by double quotes (") are subject to the
following mapping to allow the use of double quotes in TeX (e.g. for
using the babel package, see babel):
Strings delimited by single quotes (') have the same mappings as
character strings in ANSI C:
The implicit initializer for strings is the empty string "".
In the following string functions, position 0 denotes the start of the string.
int length(string s)s;
int find(string s, string t, int pos=0)t in string
s at or after position pos, or -1 if t is not a
substring of s;
int rfind(string s, string t, int pos=-1)t in string
s at or before position pos (if pos=-1, at the end
of the string s), or -1 if t is not a substring of s;
string insert(string s, int pos, string t)t at position
pos in s;
string erase(string s, int pos, int n)n
(if n=-1, to the end of the string s) at
position pos in s;
string substr(string s, int pos, int n=-1)s starting at position pos
and of length n (if n=-1, until the end of the
string s);
string reverse(string s)s;
string replace(string s, string before, string after)before in the
string s changed to the string after;
string replace(string s, string[][] table)s all
occurrences of the string before in an array table of
string pairs {before,after} to the corresponding
string after;
string format(string s, int n)n formatted according to the C-style
format string s using the current locale;
string format(string s, real x)x formatted according to the C-style format
string s using the current locale (see the documentation for
the C-function fprintf), except that only one data field is
allowed, trailing zeros are removed by default (unless # is
specified) and TeX is used to typeset scientific notation;
string time(string *format="%a %b %d %T %Z %Y")strftime according to the string format using the current
locale. Thus
write(time());
write(time("%a %b %d %H:%M:%S %Z %Y"));
are equivalent ways of outputting the current time in the default
format used by the UNIX date command.
int seconds(string t="", string *format="")t formatted
according to the string format using the current locale,
or the current time if t is the empty string. For example,
write(1+(seconds()-seconds("Jan 1","%b %d"))/(24*60*60))
outputs today's ordinal date, measured from the beginning of the year.
string time(int seconds, string *format="%a %b %d %T %Z %Y")seconds seconds after the Epoch
(Thu Jan 01 00:00:00 UTC 1970) formatted by the ANSI C routine
strftime according to the string s using the current
locale. For example, to print the date corresponding to 24 hours ago:
write(time(seconds()-24*60*60));
string string(real x, int digits)x using precision digits (rather than
realDigits) to a string, using the C locale.
abort(string *s);s is nonempty, a diagnostic message
constructed from the source file, line number, and s is printed.
As in C/C++, complicated types may be abbreviated with typedef
(see the example in Functions).