# The Coordinate System of CindyScript

There is an important feature of CindyScript that immediately changes the appearance of drawings treated by the script in a controlled and global way. Usually the coordinate system of CindyScript is the same as the coordinate system of the geometric construction. However it is possible to transform the coordinate system by special operators. Then all drawing is performed with respect to this modified reference frame. Care has to be taken when using the transformations: if many of them are applied it may be difficult to determine, where an actual drawing is performed. Still make an easy use of the transformation operators CindyScript provides two operators `gsave` and `grestore`. Similar to PostScript like languages these operators push/pop the actual state of the drawing engine to a stack. This state contains (besides the graphical default appearance information) the present coordinate transformation. So a temporal use of coordinate systems may be enclosed by a `gsave()`....`grestore()` construction. We first introduce the operators and than give a combined example.

### Translating the coordinate system `translate(<list>)`:

Description: This operator assumes that `<list>` is of the form -+[<real>,<real>] and translates the drawing coordinate system by this vector.

### Rotating the coordinate system `rotate(<real>)`:

Description: This operator takes a real number `<real>` and rotates the current drawing coordinate system by an angle determined by this number. The anlge is given in rad. If one wants to use angles in degree one can do this by the `°` operator (this operator multiplies a number by `pi/180`. So `rotate(30°)` rotates the coordinate system by 30°.

### Scaling the coordinate system `scale(<real>)`:

Description: This operator takes a real number `<real>` and scales the current drawing coordinate system by this factor.
Example: The following table shows the effect of applying diverse transformations before invoking a code that draws of a square with vertex coordiantes `[0,0],[0,1],[1,1],[1,0]`.

    translate([1,0]);
    rotate(30°);
    scale(2));

The following table shows different combinations of transformations. The order of the operations may seem a little bit counterintuitive. It results from the fact that the operators transforms the coordinate system rather than the objects that are drawn.

    translate([1,0]);
    rotate(30°);
    rotate(30°);
    translate([-0.5,-0.5]);
    rotate(30°);
    scale(2));
    translate([-0.5,-0.5]);

Recursive or iterated application of transformations can lead to surprising effects. The picture below was generated by the following picture (assuming that `square` is a list of objects that draw the unit square).

    repeat(90,
    drawall(square);
    translate((1,1));
    scale(0.92);
    rotate(30°);
    )

### Relating to a projective basis `setbasis(<basis>)`:

Description: The geometric part of Cinderella admits to fix a basis to which all drawing is related. These bases can be just translational bases, similarity bases, affine bases or even projective bases. These basis operations can also be used in CindyScript. The `setbasis(<basis>)` operator sets the drawing basis to a basis defined in Cinderella. The argument has to be the label of the basis in Cinderella. After applying the setbasis operator all prior coordinate transformations in CindyScript are obsoleted. However, they can be stored by `gsave` and restored by `grestore`.
Example: In the following example a basis `Bas0` was defined to be a projective basis related to the points A, B, C and D. This means that with respect to this basis the the corners `[0,0],[0,1],[1,1],[1,0]` of the unit quadrangle are the points A, B, C and D. The first line of the code applies the basis transformation. The next three lines draw a grid within the unit square. The resulting image is shown below.

    setbasis(Bas0);
    x=(0..10)/10;
    drawall(apply(x,([#,0],[#,1])));
    drawall(apply(x,([0,#],[1,#])));

See also: Bases in Cinderella

### Relating to a translation basis `setbasis(<vec1>)`,

### Relating to a similarity basis `setbasis(<vec1>,<vec2>)`,

### Relating to an affine basis `setbasis(<vec1>,<vec2>,<vec3>)`,

### Relating to a projective basis `setbasis(<vec1>,<vec2>,<vec3>,<vec4>)`:

Description: it is also possible to relate the internal drawing basis of CindyScript directly to a basis specified by points. This avoids the explicit creation of a basis in Cinderella. This can be easily done by providing points of the base frame of the basis.
Example: The following piece of code together with pictue below demonstrates this feature. in the code `sq` is nfirst defined as a macro to draw a square grid. The grid is drawn with respect to several bases.

    sq:=(draw([0,0],[1,0]);
         draw([0,0.25],[1,0.25]);
         draw([0,0.5],[1,0.5]);
         draw([0,0.75],[1,0.75]);
         draw([0,1],[1,1]);
         draw([0,0],[0,1]);
         draw([0.25,0],[0.25,1]);
         draw([0.5,0],[0.5,1]);
         draw([0.75,0],[0.75,1]);
         draw([1,0],[1,1]));

    setbasis(A);
    sq;
    setbasis(B,C);
    sq;
    setbasis(D,E,F);
    sq;
    setbasis(G,H,L,K);
    sq;

## The appearance and basis stack

The `gsave` operator stores all informations in the graphic state (coordinate transformations, sizes, colors, opazity) to a stack. While the `grestore` operator reverses this effect by popping the information from the stack. Thus within a sequence `gsave() ..... grestore()` any coordinate transformations can be made without having effect to the remaining code. In the Examples section the `gsave`, `grestore` operators will be explained in an elaborate example in which a "turtle graphics" is implemented.
See also: Appearance of Objects

Page last modified on Wednesday 24 of May, 2006 [23:18:32 UTC].
The original document is available at http://doc.cinderella.de/tiki-index.php?page=Script%20Coordinate%20System