|
|
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 graphProgramming with g2d is intended for defining a graphic by representing geometrical objects.Programming instructions Programming with g2d consists in writing a program. A program is a set of lines, or instructions (possibly separated with blank lines).for repeat if function end terminate() ends the program - even if followed by more instructions.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.Data types numbers To make a number, specify the usual decimal notation. Use period (.) as the decimal separator.pi (π)For very small or very large numbers, use the scientific notation with e as the prefix for the exponent. golden (the golden number) 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.O the origin (0,0)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. ux the horizontal unit vector (1,0) uy the vertical unit vector (0,1) 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.return the character to make a new line booleanThe following string is preset: 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.angle By default, you provide an angle as a real number, in radians.colorAlternately you can provide an angle in degrees, as a real number immediately followed by °. You can use any of the following descriptions for a color.none or transparent haze (a semi-transparent gray) red blue green magenta cyan yellow black gray or grey white orange purple brown |
|
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. |
|
arrayAn 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.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.size(A) 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.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.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.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.ellipse(c, u1, u2) The ellipse with center c and principal axes 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.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.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.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 linedots 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 interiorfilldraw 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.).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.sky(percent) Sets the background to display a cloudy sky. percent, a number in the range 0 .. 100, is the amount of clouds.millimeter(x1, x2, ...)A background command overrides any sky command, even if the latter comes after the former in the program. 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.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. mark(aPoint) draws a + cross in the location specifiedmark(aSegment) draws short cross strokes at the endpoints of a line segmentmark(aRay) draws a short cross stroke at the endpoint of a semi-infinite linemark(anArc) draws short cross strokes at the endpoints of an arcmark(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)).Drawing text The commands for drawing text are write and name. 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).subscripts and superscripts To draw a character as a subscript, precede it with underscore ( _ ).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.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).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).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.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.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. |
|
placing textThe 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 specifiedr (right) the text prints on the right of the point specifiedt (top) the text prints above the point specifiedb (bottom) the text prints below the point specifiedh (here) the text prints centered with respect to the point specified
|
|
justifying textThe 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.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.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).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.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.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.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.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...erf, erfc, gamma, lgamma Points and colors, for example, are arrays, so they support those operators. The following comparison operators return a boolean (true or false.)<, >, =, /= Note that the operator to test equality is simply =.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.Operations on colors You can use the lighter and darker commands to get a color lighter or darker than a given color.lighter(aColor) returns a color lighter than the color specified.darker(aColor) returns a color darker than the color specified.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).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.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.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)).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.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.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 segmentbisector(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.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. 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.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. 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.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() |
|
|
|