Tutorial - Shapes

In this tutorial we are going to look at shapes and how to build your own.

System Shapes

The GraRLS system comes with a number of shapes built in. The Line routine called in the introduction was actually a shape. Others include Circle and Rectangle. To draw a shape in the output file just use the name of the shape with its parameters.

Program( )
    Begin
	Circle(100);
	Rectangle(100, 10);
	Ellipse(200, 50);
    End;
basic_sh_1.grs Output of basic_sh_1.grs basic_sh_1.png

OK, that drew all the shapes, but at the same location. For the circle and ellipse the centre was at the origin, {0,0}, and for the rectangle the top left corner was at the origin. To draw shapes at a position of your choice use the syntax as shown in the following example.

Program( )

    Begin

	Circle(50) => {100, 100};
	Rectangle(60, 10) => {110, 110};
	Ellipse(50, 20) => {150, 100};

    End;
basic_sh_2.grs Output of basic_sh_2.grs basic_sh_2.png

For this basic example this may seem cumbersome, but once you start writing more complex programs the power of the mechanism will become clearer.

Creating your own shapes

the basic syntax for defining your own shape is as follows :-

Shape shapename ( parameters )
	local variables
    Begin
	
	some code

    End;

Lets take a simple example - the astronomical symbol for the Earth.

Program( )
    Begin
	EarthSymbol(20) => {100, 100};
	EarthSymbol(30) => {160, 100};
    End;

Shape EarthSymbol(Number size)
    Begin
	Circle(size);
	Line({-size, 0}, {size, 0});
	Line({0, -size}, {0, size});
    End;
basic_sh_3.grs Output of basic_sh_3.grs basic_sh_3.png

You will see that we do not need to specify any location information. The whole shape is drawn relative to the coordinates specified after the call. If we used a more conventional approach our code would have to be like :-

Program( )
    Begin
	EarthSymbol(20, 100, 100);
	EarthSymbol(30, 160, 100);
    End;

Shape EarthSymbol(Number size, Number xorg, Number yorg)
    Begin
	[= Circle(size, xorg, yorg); - would need to redefine =]
	Line({xorg - size, yorg}, {xorg + size, yorg});
	Line({xorg, yorg - size}, {xorg, yorg + size});
    End;

The items following the '=>' symbol are known collectively as the "graphics context". So far we have only seen the origin element; there are also the pen ( for drawing lines ), the brush ( for filling regions ) and the Font ( for drawing text ). All these items are handled in the same automatic manner by the system and we will cover each in more detail later.