logo mwo g2d, Language Reference
Table of contents

How your program will make your graph
Programming instructions
Variables
Data types | numbers | point/vector | string | boolean | angle | color | geometrical objects | array
Object creators | polar | line | ray | segment | ellipse | brokenline | smoothline | polygon | triangle | square | rectangle | pentagon | hexagon | heptagon | octagon | facet | potato
Drawing objects | implicit drawing | draw | dots | dash | fill | filldraw | shadow | background decoration
Marks and arrows | mark | arrow
Drawing text | subscripts and superscripts | greek letters | naming a vector | naming an angle | mathematical symbols | Unicode characters | writing formulae | placing text | justifying text | text around a figure | auto-naming
The graphical settings | for the objects | for the text | for the tick | saving/restoring settings
Operations on numbers and on arrays | arithmetic/trigonometry/transcendental functions | comparison operators
Operations on points or vectors | norm | scalar | determinant | distance
Operations on colors | lighter | darker
Operations on booleans | or | and
Operations on strings | & | letters
Geometrical operations | translate | rotate | intersect | project | parallel | symmetric | bisector | distance
Program control | loops | tests | subroutines and global variables | termination

Appendix

Table of the mathematical symbols supported by the name and write commands.
How your program will make your graph
Programming with g2d is intended for defining a graphic by representing geometrical objects.

Defining those objects is one thing, that you do in the program. In the program, you use your own system of coordinates.

Selecting the rectangle which will be visible in the resulting graphics is another thing, that you do not do in the program.

Depending on the platform that you are using to operate g2d, you are given a user interface (typically, a form in a web page, or a dialog box) to specify, in your own system of coordinates, two quantities: the center of the figure and its half-size or radius. Think of those quantities as the center and radius of the circle inscribed in the figure's boundaries.

Common default values are (0, 0) for the figure's center, and 2 for its half-size or radius. The default settings are suitable for geometry which occurs not far from the unit circle.
toc
Programming instructions
Programming with g2d consists in writing a program. A program is a set of lines, or instructions (possibly separated with blank lines).

A line starting with two slash // is not considered as an instruction: it is called a comment, you can use it to comment your program. You can also include comments after an instruction, on the same line, by prefixing them with //. Commenting may be used to temporarily disable instructions.

// uncomment the lines below to draw the axes
// draw(Ox, Oy)
// mark(ux, Ox)
// mark(uy, Oy)

You will use three kinds of instructions: command, assignments, and program control instructions.

command: a line beginning with a command's name, followed by its comma-separated argument(s) between parentheses. Commands with no argument take an empty pair of parentheses (). The arguments of the command may themselves contain commands.

draw(polygon(V))
fill(intersect(circle(polar(1, 45°), 1/2), polygon(ux, O, uy)))

variable assignment: a line beginning with a variable's name followed with the equal sign =. The expression on the right of the equal sign may contain commands.

C = circle(O, 2)

program control: one of the lines which control the execution of the other instructions. Those are lines beginning with one of the following keywords.
for
repeat
if
function
end

One command may be considered as a program control instruction:

terminate() 
ends the program - even if followed by more instructions.
toc
Variables
You store quantities in variables such as A or theCircle. A variable may be any string (except a reserved term) starting with a letter and containing only letters and digits (not underscore). g2d ignores cases: if you define a point A, then a real number a, the latter will override the former, so the reference to the point will be lost.
To assign a value to a variable, use the equal sign =

x = 1

toc
Data types
numbers
To make a number, specify the usual decimal notation. Use period (.) as the decimal separator.

n = 2
p = -10
dx = 0.5

For very small or very large numbers, use the scientific notation with e as the prefix for the exponent.

xMin = - 2.3e3
eps = 1.6e-6

The following numbers are preset:
pi (π)
golden (the golden number)
toc
point/vector
A point or a vector is an array of two numbers. To make a point or a vector, specify two numbers or numerical expressions separated with a comma, between parentheses.

M = (0.5, 0.5)

You can define a point by its polar coordinates, with polar(r, a), where r is a positive number and a is an angle. For instance all the three lines below return the same point in M.

M = polar(1, pi/4)
M = polar(1, 45°)
M = sqrt(2) * (1, 1)

The following points (or vectors) are preset:
O the origin (0,0)
ux the horizontal unit vector (1,0)
uy the vertical unit vector (0,1)
toc
string
To make a string, encapsulate a sequence of any characters (except double-quote) between double-quotes. To display double-quotes, print two single quotes.

AName = "A_1"

The following string is preset:
return the character to make a new line
toc
boolean
Booleans are quantities which can take either of the two values true and false. Usually you make a boolean by storing the result of a comparison expression. Booleans are useful for testing with the if instruction.
Always encapsulate a boolean expression with parentheses.

Acute = (a < pi/2)

toc
angle
By default, you provide an angle as a real number, in radians.

a = pi/6

Alternately you can provide an angle in degrees, as a real number immediately followed by °.

b = a + 15°

toc
color
You can use any of the following descriptions for a color.

a real number in the range 0 to 1. This specifies a gray level: 0 means no light, black, 1 means full intensity, white.

an array of 3 numbers in the range 0 to 1. Example: (0.2, 0.4, 1). This specifies the red, green, and blue components of the color. The higher their values, the lighter the color. (0,0,0) is black, (1,1,1) is white.

an array of 4 numbers in the range 0 to 1. Example: (0.2, 0.4, 1, 0.5). This specifies the red, green, and blue components of the color, and the opacity: 0 means transparent, 1 means opaque. Thus, (0.2, 0.4, 1) is really a shortcut for (0.2, 0.4, 1, 1).

a preset color, in the following list.
none or transparent
haze (a semi-transparent gray)
red 
blue 
green 
magenta 
cyan 
yellow 
black 
gray or grey 
white 
orange 
purple 
brown 

toc
geometrical objects
A geometrical object is a quantity that was returned by one of the commands which make objects from scratch (see object creators) or which make objects by an operation on one or several existing object(s) (see geometrical operations.) To use a geometrical object you store into a variable the result returned by such a command.

The example below defines two circles and their intersection, then it draws the circles and fills their intersection.

c1 = circle(-uy/2, 1)
c2 = circle(uy/2, 1)
x = intersect(c1, c2)
draw(c1, c2)
fill(x)

The name of an object creator command says the kind of object it creates: line (infinite line), ray (semi-infinite line), segment (line segment), circle, disk, ellipse, arc, brokenline (broken line), smoothline (a smooth line), polygon (a closed broken line), potato (a closed smooth line). triangle, square, rectangle, pentagon, hexagon, heptagon, and octagon make polygons: closed broken lines. The intersect command makes a geometrical object of a special kind, an intersection.

Sometimes "a geometrical object" can be used instead of "a geometrical object, or a point".
toc
array
An array is a structure able of storing several quantities. The element of rank n of an array A is referred to as A[n]. An array is able of storing any kind of quantity: for instance a number, an other array, or a geometrical object.

The index of items in an array is cyclic: for an array of size N, A[0] is the same as A[N], A[1] is the same as A[N+1], etc.
For this reason, you can decide freely to use 0-based indexes or 1-based indexes - actually you may use both on the same array.

In order to be used, an array must first be allocated, which consists in declaring it and specifying, one way or another, its size. To declare an array, use one of the following methods.

implicit declaration: you provide directly the values of all the elements of the array, as a comma-separated list between parentheses. The size is implictly set to the number of items in the list you provide.
A = (ux + uy, -ux + uy, -ux - uy, ux - uy)

explicit declaration: you just declare an array of a given size, but you do not specify values for its elements. You can use either of the following equivalent forms to declare an array A, where N is a positive integer.
A[N]

A = array(N)


advanced declaration: at the same time where you declare the array, you fill it with calculated values. The following possibilities are available.
A = array(N, xmin, xmax) 
makes an array of N numbers evenly spaced from xmin to xmax.
A = array(N, xmin, xmax, random) 
the random keyword specifies to make an array of N numbers randomly choosen, with an even probability, in the range from xmin to xmax.
A = array(N, formula) 
makes an array of N numbers computed after a formula. formula must be a string specifying a numerical expression. The expression may use the following variables: i, x, z, a. i stands for the index of the element, from 1 to N. x stands for the index, rescaled so as to range from the minimum to the maximum value of the abscissa in the figure. z is similar, except that it ranges from -1 (first element) to +1 (last element). a is similar also, and ranges from -pi to +pi.
A = array(N, (Xformula, Yformula)) 
here, two formulas are provided (as a 2-array of strings). This makes an array of N points whose x-coordinates are computed after Xformula and y-coordinates are computed after Yformula. Xformula and Xformula follow the same rule as formula, above.

For example, the instruction below constructs and plots a list of points which displays an analytical curve.

smoothline(array(50, ( "x" , "0.1 * x^4 + 0.75 * x^3 + x^2 -1.5")))

To get the size of an array, use the size command.
size(A) 
toc
Object creators
Each of the commands below returns a geometrical object. A program stores a geometrical object into a variable to use the object later: either to draw it, or to perform some operation with it and make another object.

The following conventions are used for the parameters of the commands.
c, p, p1, p2, etc. are points, that is, 2-arrays,
u, u1, u2, etc. are vectors: 2-arrays, too, but which really mean vectors,
r is a positive real number
x is a real number
a, a1, a2 etc. are angles
n is a positive integer
polar(r, a)
The point with (r, a) as its polar coordinates, namely (r * cos(a), r * sin(a)).
line(p1, p2)
The infinite line which passes through p1 and p2.
The axes Ox and Oy are pre-defined, as if your program included the following lines.

Ox = line(O, ux)
Oy = line(O, uy)

ray(p1, p2)
The semi-infinite line which starts at p1 and passes through p2.
segment(p1, p2)
The line segment whose endpoints are p1 and p2.
circle(c, r)
circle(p1, p2, p3)
 
A circle specified, either by its center c and radius r, or by providing any three points belonging to the circle.
disk(c, r)
disk(p1, p2, p3)
 
A disk specified, either by its center c and radius r, or by providing any three points belonging to the its perimeter.
Disks and circles behave exactly the same: for instance fill produces the same figure when applied to a circle or to the corresponding disk. The only differences are that 1 - implicit drawing draws the circle's perimeter but fills the circle, and 2 - naturally, intersect produces different results.
arc(c, r, a1, a2)
arc(p1, p2, p3)
 
A circle arc: the arc of circle with center c and radius r, turning counterclockwise from angle a1 to angle a2, or the arc with endpoints p1 and p3 which passes through p2.
For example the instructions below all produce the same arc.

arc(O, 1, 0, pi/2)
arc(O, 1, 0, -270°)
arc(ux, (ux + uy)/sqrt(2), uy)
arc(uy, (ux + uy)/sqrt(2), ux)

ellipse(c, u1, u2)
The ellipse with center c and principal axes u1 and u2.
You need not pass orthogonal vectors: the ellipse is the unit circle in the coordinates system defined by c, u1, and u2.
brokenline(p1, p2, ...)
The broken line which joins p1 to p2 then to p3 etc.
smoothline(p1, p2, ...)
Same as brokenline, but the line is smooth.
polygon(p1, p2, ...)
Same as brokenline, but the line is closed.
triangle(c, r)
triangle(c, r, a0)
The equilateral triangle inscribed in the circle with center c and radius r. a0 specifies an angle (with respect to the horizontal) for one of the vertices. By default a0 is 90°: the triangle points upwards.
square(c, L)
square(p1, p2)
square(c, r, a0)
A square. To make a square with its edges horizontal and vertical, specify its center c and the size of its edges L. Alternately, you can specify the location of two opposite vertices p1 and p2, or you can define the square like another regular polygon, as the square inscribed in the circle with center c and radius r, a0 specifying an orientation for a vertex.
rectangle(c, Lh, Lv)
rectangle(p1, p2)
A rectangle with its edges horizontal and vertical. You can specify either the rectangle's center c and the sizes of its edges: Lh horizontal and Lv vertical, or you can specify the location of two opposite vertices p1 and p2.
pentagon(c, r)
pentagon(c, r, a0)
The regular pentagon inscribed in the circle with center c and radius r. a0 specifies an angle (with respect to the horizontal) for one of the vertices. By default a0 is 90°: the pentagon points upwards.
hexagon(c, r)
hexagon(c, r, a0)
The regular hexagon inscribed in the circle with center c and radius r. a0 specifies an angle (with respect to the horizontal) for one of the vertices. By default a0 is 90°: the hexagon points upwards.
heptagon(c, r)
heptagon(c, r, a0)
The regular heptagon inscribed in the circle with center c and radius r. a0 specifies an angle (with respect to the horizontal) for one of the vertices. By default a0 is 90°: the heptagon points upwards.
octagon(c, r)
octagon(c, r, a0)
The regular octagon inscribed in the circle with center c and radius r. a0 specifies an angle (with respect to the horizontal) for one of the vertices. By default a0 is 22.5° (π/8): two edges are horizontal and two are vertical.
facet(p1, p2, ...)
Same as polygon. Only different when implicit drawn: implicit drawing draws the polygon's border, while it fills the facet.
potato(p1, p2, ...)
Same as brokenline, but the line is closed and smooth.
toc
Drawing objects
When you want to draw a geometrical object, usually the object is stored as a variable and you apply draw, fill, shadow, or filldraw, as you will see below.

Instead, when you write simple programs, you can use implicit drawing, which makes shorter programs and is more natural. Implicit drawing consists in simply calling an object creator, without storing the returned quantity into a variable.
toc
implicit drawing
When you call an object creator without storing the returned quantity into a variable, the system understands that you want to draw the object.
For instance, the lines below are enough to draw the two axes.

line(O, ux)
line(O, uy)

Used for implicit drawing, circle and disk behave differently. circle draws the perimeter, disk fills the interior (and does not draw the perimeter.) The exact same difference is also between polygon and facet.

Implicit drawing draws the border for an arc, an ellipse, or a smooth line, and fills a potato.

toc
explicit drawing
To draw a geometric object stored as a variable, use one of the following commands.
draw 
draws an object's border as a solid line
dots 
draws an object's border as a dotted line. The size of the dots scales with the pen width and with the tick size.
dash 
draws an object's border as a dashed line. The dash lengths scale with the pen width and with the tick size.
fill 
fills an object's interior
filldraw
fills the object's interior, and draws its border as a solid line. You can use drawfill instead of filldraw.
shadow
fills the object's interior, and draws a shadow under the object. The shadow scales with the tick size.

The commands use the current graphical settings (pen size, colors, etc.).

Each command accepts, either one object, or several objects, or an array of objects. In general, passing several objects or passing an array is different, yet the two lines below are equivalent.

fill(A, B, C)
fill((A, B, C))

Note that with shadow it is not equivalent to use the command on an array of objects and to use successively on each object. The script below draws that shadow of A and B over the background.

shadow(A, B)

while the following block will draw A's shadow on the background but B's shadow may cover A.

shadow(A)
shadow(B)

Any graphic command (fill, draw/dots/dash, or filldraw/drawfill), when applied to a point, displays a + cross at the point's location.

When used for implicit drawing, circle and disk behave differently. circle draws the perimeter, disk fills the interior (and does not draw the perimeter.) The exact same difference is also between polygon and facet.

Implicit drawing draws the border for an arc, an ellipse, or a smooth line, and fills a potato.
toc
background decoration
By default, the background of your figure is white. You can have it colored with the background command. You can have it display a cloudy sky with the sky command. You can have it represent a millimeter paper, whose scale(s) you can adjust, with the millimeter command.

background(col)
Sets the color of the background to col.

background((orange + white)/2)

sky(percent)
Sets the background to display a cloudy sky. percent, a number in the range 0 .. 100, is the amount of clouds.

sky(40)

A background command overrides any sky command, even if the latter comes after the former in the program.
millimeter(x1, x2, ...) 
Displays square grid(s) with mesh sizes x1, x2, etc. The stroke for the grid at x1 assumes the settings for the ticks (see graphical settings). The stroke for the grid at x2 is twice thinner than the stroke for x1, and so on and so forth if more values are supplied.

millimeter(0.5, 0.1, 0.02)

toc
Marks and arrows
Marks refer to the additional small graphical items which may be required in geometrical figures. Examples of marks are: the little square to mark a right angle, or the two little transversal ticks to mark the endpoints of a line segment.

Marks use graphical settings of their own. For instance, by default, the stroke for a mark is twice thinner than the stroke for a regular draw. In addition to the usual graphical settings (stroke width, color, etc.) marks use a specific scaling parameter which specifies the size for all marks.

The commands for marking objects (actually, one mark command which supports a variety of inputs) and for making arrows follow.

mark(aPoint) 
draws a + cross in the location specified
mark(aSegment) 
draws short cross strokes at the endpoints of a line segment
mark(aRay) 
draws a short cross stroke at the endpoint of a semi-infinite line
mark(anArc) 
draws short cross strokes at the endpoints of an arc
mark(aPoint, aSymbol) 
draws in the location specified a symbol specified by aSymbol, which can be "+" to draw a + cross, "x" to draw an x cross, or "o" (or "circle") to draw a small circle.
mark(aPoint, aLineOrRayOrSegment) 
draws a tick in the location specified, orthogonal to a line, ray, or line segment.
mark(aPoint, aDiskOrCircle) 
draws a tick in the location specified, radially with respect to a disk or circle.
mark(aSegment, n) 
draws n ticks in the center of a line segment, at a small angle to the orthogonal to the line segment.
mark(p1, p2, p3) 
draws the two other sides of a small parallelogram built on the angle p1 p2 p3. If p1 p2 p3 is the right angle, the parallelogram is a square, the symbol for the right angle.
mark(p1, p2, p3, nArcs) 
draws nArcs arcs of circle in the angle p1 p2 p3.
mark(p1, p2, p3, nArcs, nTicks) 
draws nArcs arcs of circle in the angle p1 p2 p3, and marks them with nTicks ticks.
arrow(aPoint, anAngle) 
draws an arrow pointing the location specified, at the angle specified.
arrow(p1, p2) 
draws an arrow pointing p2, in the direction defined by p1, p2 (thus, the pole for the arrow might be segment(p1, p2)).
toc
Drawing text
The commands for drawing text are write and name.
write supports texts with multiple lines, and is for displaying a text.
name only accepts strings with one line. It is intended for naming objects, and provides specific features to that effect.

write(aPoint, aString) 
write(p1, p2, p3, ..., s1, s2, s3, ...) 
name(aPoint, aString) 
name(p1, p2, p3, ..., s1, s2, s3, ...) 
Draws the specified string(s) at the specified location(s).
For name, the location specifies the coordinates of the lower left corner of the rectangle enclosing strictly the text.
For write, the location specifies the center of that rectangle.
The string(s) that you pass to name must not contain a new line character.
The string(s) that you pass to write may consist in several lines of text. when you pass several lines of text to write, one instruction spans several lines.
When you pass several points, pass as many strings (except if you are using auto-naming.) You can pass the points first, then the strings, or you can interleave them: name and write sort out the points from the strings anyway.

The string(s) that you pass to name or to write support some conventions to perform specific features. The following options are available.
toc
subscripts and superscripts
To draw a character as a subscript, precede it with underscore ( _ ).
To draw a character as a superscript, precede it with hat ( ^ ).
To draw several characters as a subscript or superscript, repeat underscore or hat before each character, or group them with braces { }. For example the two lines below are equivalent, both print 123 as a subscript.

name(A, "A_1_2_3")
name(A, "A_{123}")

toc
greek letters
To draw a greek letter, precede its English name with backslash (\). Provide the greek letter's English name in lower case for the lower case greek letter, capitalize the English name to make an upper case greek letter.
Only those letters which are specific to the greek alphabet are implemented (for example, not the upper case alpha, which is the same as the Roman letter A.)

name(A, "\alpha")
name(A, "\Psi^2")

Four letters support a variant form: \varpi, \vartheta, \varphi, \varsigma, which print ϖ, ϑ, ϕ and ς.
toc
naming a vector
To draw a small horizontal arrow on top of a letter or a group of letters (like for naming a vector), pass the keyword vector in addition to the point(s) and string(s).

name((A + B)/2, vector, "AB")

toc
naming an angle
To draw a hat on top of a letter or a group of letters (like for naming an angle), pass the keyword hat in addition to the point(s) and string(s).

name(B, hat, "ABC")

toc
mathematical symbols
To draw a mathematical symbol, use LaTeX-like symbols: keywords preceded with backslash (\). To view all the symbols available, open the table of mathematical symbols.

name(A, "X \cup  Y")

toc
Unicode characters
To draw a Unicode character assuming you know its code, use the &#dddd; notation, where dddd is the character's code in decimal notation, or &#xhhhh; where hhhh is the character's code in hexadecimal notation.

name(A, "&#x263A;")
name(A, "&#9786;")

toc
writing formulae
To write a mathematical expression or a formula, you can use the LaTeX syntax with the name command. You pass a LaTeX-compatible string, and you pass the keyword latex as one of the parameters.
If and while you are not familiar with LaTeX, you may want to use the following minimal instructions.
  • encapsulate a string between two dollar signs $ to have LaTeX consider it a mathematical expression,
  • in a mathematical expression, use \vec{somechar} to make an arrow above one character,
  • in a mathematical expression, use \overrightarrow{somestring} to make an arrow above a string,
  • in a mathematical expression, use \frac{somenum}{somedenom} to make a fraction,
  • in a mathematical expression, use \sqrt{somenum} to make a square root,
  • in a mathematical expression, use _{somesubscript} or ^{somesuperscript} to make subscripts or superscripts,
  • in a mathematical expression, you can use all the symbols given in the table of the mathematical symbols.

name(M, latex, "$\frac{\vec{u_x}+\vec{u_y}}{2}$")

toc
placing text
The string that you pass to name may include a "positional hint". (Positional hints are not supported by write.) Providing a positional hint consists in prefixing the string with one or two letters, enclosed between brackets [ ], which specify where to print the string. The letters allowed are the following.
l 
(left) the text prints on the left of the point specified
r 
(right) the text prints on the right of the point specified
t 
(top) the text prints above the point specified
b 
(bottom) the text prints below the point specified
h 
(here) the text prints centered with respect to the point specified

name(O, "[tr]{x > 0} \cap {y > 0}")

toc
justifying text
The write command uses the current setting for the justification of the text. (Justification is not supported by name.) By default, text is justified left.


text around a figure
Instead of providing individually each string with a positional hint, you can have the strings shifted radially, in the direction opposite to the center of the figure. This is called "smart naming", and the corresponding command is smartname.
smartname(aPoint, aString) 
smartname(p1, p2, p3, ..., s1, s2, s3, ...) 

auto-naming
When you want to name several points, maybe your naming scheme is simple enough for the name command (or smartname, or write) to guess for you. If this is the case, pass all the points but do not pass all the names: pass only the first one, name will construct the missing names.

If you pass a string ending with a number, the subsequent names will be constructed by incrementing the number. For example the command below will name the three points E, F, and G after: "A_1", "A_2", "A_3".

name(E, F, G, "A_1")

If the string does not end with a number, the first character of the string is incremented alphabetically to generate the new names. For example the command below will name the three points E, F, and G after: "A_H", "B_H", "C_H".

name(E, F, G, "A_H")

toc
The graphical settings
When your program uses a command which produces a graphical result (fill, draw, drawfill/filldraw, shadow, mark, arrow, name, smartname), as well as when you use implicit drawing, the program uses the current values of the graphical settings as they are defined at that point in the program.
Thus, changing any of the graphical settings affects all the drawing commands which occur after that change.

You can choose to let all graphical settings as they are: the commands will use the default graphical settings.

There are three groups of graphical settings: the settings for drawing the geometrical objects, the settings for drawing text, and the settings for the "tick", that the program uses for the marks and arrows. In this section aColor stands for a color.
toc
settings for objects

fillcolor(aColor) 
sets the "fill color", the color for filling the inside of objects (fill command). Default: (1/2, 1/2, 1/2, 1/2), semi-transparent gray.
pencolor(aColor) 
sets the "pen color", the color for drawing the boundaries of objects (draw command). Default: black (or (0, 0, 0)).
penwidth(aNumber) 
sets the "pen width", the thickness of the stroke for the draw command. Default: 1, which produces a stroke of 1 pixel, or 1/72 inch (0.3 mm).
toc
settings for the text

textjustification(left or centered or right) 
sets the justification of the text used by the write command when drawing several lines of text. The possible values are left, centered, right.
textfont(aFont) 
sets the "text font", the font for drawing text. aFont is really a string, which may assume any of the following values.
"Arial" 
font with no seriph, proportional (the default),
"Times" 
font with seriph, proportional,
"Monaco" 
font with no seriph, fixed width. Does not support styles. Setting the font to "Monaco" resets the text style to none.
"Courier" 
font with seriph, fixed width.
"Script" 
script font, for ornemental usage, or for some mathematical notations. Does not support styles. Setting the font to "Script" resets the text style to none.
textsize(aNumber) 
sets the "text size", the size of the characters. The unit is approximately one pixel or 1/72 inch (0.3 mm). aNumber may have a fractional part. Default: 12.
textstyle(aStyle) 
sets the "text style", the style of the font. aStyle is really a string, which may assume any of the following values.
"" 
(the empty string) resets the font to no style (the default),
"bold" 
sets the font to a bold type,
"italic" 
sets the font to a italic type,
"bold italic" 
sets the font to a bold-italic type.
textcolor(aColor) 
sets the "text color", the color for the text. Default: black (or (0, 0, 0)).
texttilt(anAngle) 
sets the "text tilt angle", the angle (with respect to the horizontal) at which the text prints. The rotation occurs around the point that you pass to name or to write to set the text's location. anAngle is an angle: a real in radians or a real in degrees followed by °. Default: 0 (horizontal).
smartnamecenter(aPoint) 
sets the "smartname center". smartname prints a string shifted in the direction opposite to the smartname center. Default: the center of the figure.
smartnameoffset(aNumber) 
sets the "smartname offset". This specifies how far from the specified point smartname shifts the specified string. That offset scales with the font's size. Default: 1, provides the standard setting.
toc
settings for the tick

tickcolor(aColor) 
sets the "tick color", the color used by the mark and arrow commands. Default: black (or (0, 0, 0)).
tickpenwidth(aNumber) 
sets the "tick pen width", the thickness of the stroke used by the mark and arrow commands. Default: 0.5, the unit being 1 pixel, or 1/72 inch (0.3 mm).
ticksize(aNumber) 
sets the "tick size", the size of the short lines and other small elements made by the mark and arrow commands. Default: 1, which produces the standard setting.
toc
saving and restoring the settings
The following commands are for encapsulating a group of commands where you change some graphical setting. When you use this encapsulation your program recovers after the group the same settings than before.
savestate()
remembers the current graphical settings to be restored later,
restorestate() 
restores the graphical settings to what they were when savestate() was last called.
toc
Operations on numbers and on arrays
You can use the following operators on numbers as well as on arrays.
+ - * / ^ 
The hat ( ^ ) is the symbol for exponientation: the square of x is x ^ 2.
The minus sign ( - ) is also the unary negation operator. The unary negation really works like 0 - ... so its precedence may be lower than what you would expect. For instance, - 2 ^ 2 returns - 4.
cos, sin, tan, arccos, arcsin, arctan, arctan2 
cosh, sinh, tanh, arccosh, arcsinh, arctanh 
log, log10, exp, sqr, sqrt, hypot 
abs, ceil, floor, trunc, mod, div 
mod supports non-integers, for instance pi mod 1 returns 0.1415... 
abs supports the bar notation: instead of abs(x) you can write |x|.
erf, erfc, gamma, lgamma 

Points and colors, for example, are arrays, so they support those operators.

myColor = (orange + 5 * white) / 6
A = (ux + uy) / 2

The following comparison operators return a boolean (true or false.)
<, >, =, /= 
Note that the operator to test equality is simply =.
The < and > comparison operators work only on numbers (not on arrays.)
toc
Operations on points or vectors
In addition to the operators presented in the section above, which apply to arrays thus to points and vectors, a group of commands are specific to (2D) points or vectors.
||u||
norm(u) 
returns the norm of the vector (leave no space between the two bars),
scalar(u, v) 
returns the scalar product of the vectors,
determinant(u, v) 
returns the determinant (or, algebric value of the external product) of the vectors,
distance(P1, P2) 
returns the distance between two points.
toc
Operations on colors
You can use the lighter and darker commands to get a color lighter or darker than a given color.
The syntax is as follows.
lighter(aColor) 
returns a color lighter than the color specified.
darker(aColor) 
returns a color darker than the color specified.
toc
Operations on booleans
The operators on booleans are the binary operators or and and, and the unary operator not. not negates its argument: (not b).
The syntax for or and and is as follows.
b1 or b2 
returns true if any of b1 or b2 is true, false otherwise. b2 is evaluated only if b1 is false.
b1 and b2 
returns true if both b1 and b2 are true, false otherwise. b2 is evaluated only if b1 is true.
toc
Operations on strings
The operators on strings are the & operator, to concatenate two strings, and the letters command, to split a string into its letters.
The syntax is as follows.
s1 & s2 
returns the string made of s1 then s2. Sometimes it may be more legible to use s1 & return & s2 rather than inserting a new line in the program.
letters(s) 
returns the array of the letters of the string s. Thus, the length of s is size(letters(s)).
toc
Geometrical operations
The commands for moving an object are translate and rotate. These commands do not exactly move the original object, they return a moved copy of the object.

The syntax is as follows.
translate(anObject, aVector) 
returns a copy of the object, translated by the vector specified.
rotate(anObject, aPoint, anAngle) 
returns a copy of the object, rotated around the point by the angle specified.

The following geometrical operations are supported. They all return a geometrical object.
intersect(object1, object2) 
returns the intersection of the two objects. A line segment or a semi-infinite line is considered not differently from the infinite line where they belong, for instance intersect will return a point for two (non-parallel) segments even if they are at a distance one from the other.
The intersection returned may be or not an object of a simple geometrical kind.
If the intersection consists in two components or more (for example, the intersection of a line and a circle), intersect will return only one (in the example, one point.) intersect guesses what point you really want. If it does not guess right, use the next form of the command.
Disks and circles are treated differently: the intersection of a line and a circle is two points, the intersection of a line and a disk is a line segment.
(If the intersection is empty, intersect returns an object which essentially behaves like a point, but a very remote one.)
intersect(object1, object2, ...) 
intersect supports to compute the intersection of more than two objects.
intersect(object1, object2, n) 
like the first form of intersect, but if the intersection consists in several components (for example, the intersection of a line and a circle) intersect will return the nth component.
intersect(object1, object2, strict) 
like the first form of intersect, but the strict keyword specifies that segments and semi-infinite lines are treated strictly, for instance two non-parallel segments may or may not have an intersection in strict mode.
project(object1, aLine) 
return the projection of an object onto a line. aLine may be a segment or a semi-infinite line: it will be treated like the line to which it belongs.
parallel(aLine, aPoint) 
returns the object parallel to aLine which passes through aPoint. aLine may be a segment or a semi-infinite line: parallel will return an object translated in the direction orthogonal to the line.
symmetric(anObject, aPointOrLine) 
returns the object symmetric of anObject with respect to a point or with respect to a line (or a ray, or a segment.) The symmetry with respect to a point is the same as the rotation by 180° around the point.
bisector(aSegment) 
returns the line which bisects the line segment
bisector(ray1, ray2) 
returns the semi-infinite line which bisects the angle defined by two semi-infinite lines. You are supposed to pass two rays which share the same endpoint. You can pass lines or segments instead of semi-infinite lines: they will be treated like the semi-infinite line that you would have defined with the same two points.
bisector(ray1, ray2, n) (rotate by n x 90°) 
by default, bisector returns the interior bisector of an angle, considering the orientation that you have implicitly defined by the order of the two points you have once passed to define the line. To have it return another bisector (still, a semi-infinite line), specify a non-zero integer n. bisector will return the interior bisector, rotated n times by 90° around the vertex.
bisector(P1, P2) (bisect the line segment) 
returns a line, the bisector of the line segment whose endpoints are P1 and P2.
bisector(P1, P2, P3) (bisect the angle) 
returns a semi-infinite line, the bisector of the angle defined by the three points.

The following commands convert an object into the associated object of another kind.
line(anObject), ray(anObject), segment(anObject) 
convert any object whose kind is one of line, semi-infinite line, or line segment into what the command's name suggests.
disk(anObject), circle(anObject) 
convert any object whose kind is one of disk, circle, or arc into what the command's name suggests.

The following command computes the distance from a point to an object.
distance(aPoint, anObject) 
anObject may be a line, ray, or segment, a circle, or a disk. The object is considered strictly: for example, the distance to a segment may be different from the distance to the line to which it belongs.
toc
Program control
Some instructions specify how or when other instructions will be considered: the program control instructions. They include instructions for making loops (repeating several times the same group of instructions), for making tests (branching your program according to some boolean expression), for making subroutines (groups of instructions that you want to call from different places in your program), and for terminating the execution of your program.

Most of the program control instructions are wrappers which encapsulate a group of lines. To close such a wrapper, use the end instruction, alone.

loops
The instructions for repeating instructions are for and repeat.

for i = m to n do [instruction] 
This defines a variable i, which will assume the values from m to n. The instruction is repeated for each successive value of i. m and n must be integers. You can use any variable name instead of i for the counter.
The instruction to repeat may itself be a loop in one line - up to three nested loops in one line - but it may not be an instruction which would span several lines such as a multi-line if or for structure.
for i=-1 to 1 do for j=-1 to 1 do mark((i,j))

for i = m to n by p do [instruction]
Same as the first form, but each step increments i by p instead of 1.
for i = m to n
  [instructions]
end
Same as the first form, but to repeat a group of instructions rather than one instruction.
repeat while [boolean expression]
  [instructions]
end
Same as for, but the instructions are repeated as long as the boolean expression evaluates to true.
repeat until [boolean expression]
  [instructions]
end
Same as for, but the instructions are repeated as long as the boolean expression evaluates to false.

tests
Testing a boolean expression, you can program conditional execution, or conditional branching.
if [boolean expression] then [instruction] 
This is the one-liner form to program conditional execution of one instruction.
if [boolean expression] then
  [instructions]
end
Same as above, but for a group of instructions.
if [boolean expression] then
  [instructions]
else
  [instructions]
end
Conditional branching, binary.
if [boolean expression] then
  [instructions]
else if [boolean expression]
  [instructions]
...
end
Conditional branching, multiple.

subroutines and global variables
A subroutine is a new command that you define in your program by encapsulating a group of instructions, so as to provide it with a name. Anywhere in your program you can call the subroutine. The syntax for declaring a subroutine is the following.
function MyFunction(param1, param2, ...)
  [instructions]
  return MyValue
end
The declaration begins with the function keyword, and ends with an end line. You declare what parameters the subroutine requires between parentheses, separated with commas.
MyFunction is any name which conforms to the rules for a variable name. You use that name, followed by the parameters between parentheses, to call the subroutine in the program: MyFunction(1, A, red).
If you want to use the subroutine as a function, have it return some value: terminate the subroutine with the return keyword, followed by the value to return, which can be a quantity of any kind.

The variables that you define in the subroutine, and in particular its parameters, are local: the subroutine does not share variables with the main program.
To have your program share a variable with its subroutines, declare it as global with the global keyword, at the beginning of the program.
global var1, var2, ...
To declare several variables as global, separate them with commas.

termination
The terminate command ends the execution of the program. This command is mainly intended to help you test a program while you are writing it.
terminate()