## Turtle Graphics [2/12]

### Turtle Graphics

Let's imagine that the given program creates a pen for drawing lines on the screen.

This pen is at the middle of the screen has an initial direction to the right. When it is given the command FW(100), it moves forward by 100 pixels, and leaves a white trail on the screen.

The next command, LT(60), changes the direction of the pen by 60 degrees to the left. The command FW(250) makes the pen move 250 pixels forward and leave a trail.

The next command, RT(90), changes the direction of the pen by 90 degrees to the right. The command FW(150) makes the pen move 150 pixels forward and leave a trail.

The next command, SETCOLOR(/orange), changes the color of the pen. The command RT(90) changes the direction of the pen by 90 degrees to the right. The final command, FW(500) makes the pen move 500 pixels forward, but this time the trail is orange.

This way of drawing images is known as **turtle graphics**,
in association with the turtle cursor of the Logo programming language, which was used as
an educational programming language in the past.

The function-procedures FW, LT, RT and SETCOLOR are defined in the second part of the program. We will explore them in more detail later on.

The command SKIP(20) moves the pen forward by 20 pixels, but without leaving a trail.

The command SKIP is implemented as a user-defined procedure. The signature of this procedure is:

- void SKIP(
- num distance
- )

The signature of user-defined procedure SETCOLOR is:

- void SETCOLOR(
- color cl
- )

Procedures don't return a value, which is indicated by the keyword
void. This allows procedures to be used as statements,
and in this usage they can be called **commands**.

Drawing a square seems to be easy enough.

The command FW is implemented as a user-defined procedure. The signature of this procedure is:

- void FW(
- num distance
- )

Let's try to draw a house.

The first three lines are akin to drawing a square. Now the roof. First, the command RT(180) turns the pen into the opposite direction.

A shape that makes up the roof is a triangle. If the lines of the roof are to be the same length as the other lines, then the triangle is equilateral. Equailateral triangles have all sides of equal lenght and inner angles equal to 60 degrees. Therefore, we turn the pen by 60 degrees and draw a line of length 200.

For the second part of the roof, we have to make another turn. The inner angle of an equilateral triangle is 60 degrees, which makes the outer angle equal to 120 degrees, because inner angle and outer angle must sum up to 180 degrees. So, we turn 120 degrees to the right.

Unfortunately, the drawing is out of bounds. To correct this, uncomment the SETPOS command at the top.

Let's now add a diagonal line. The first issue is the direction of the pen. To make it go straight downwards, it first needs to be rotated by 30 degrees. Then, to change into diagonal direction, it needs to be changed by another 45 degrees.

What's the length of the diagonal line? The diagonal line is longer than the sides of the house, so the lenght must be bigger than 200, but how much exactly? The mathematics tells that the length of a diagonal of a square equals the side length multiplied by the square root of 2. The procedure sqrt calculates square roots, so the length of the diagonal equals 200*sqrt(2).

After that, the direction changes again and the line of length 200 is drawn.

Exercise: draw the other diagonal of the house.

The user-defined procedure RSKIP makes the pen go backwards, without leaving a trail.

Here is another simple shape.

Here is an even simpler shape, which uses a rotation of 50 degrees. Try out different values for a rotation angle, for example 60, 70, 80, 83.3 degrees.

If the angle of 120 degrees was used, the result would be a triangle.

What if the angle is slightly greater than 120 degrees?

This program answers the question.

This program answers the question: What if the line color changes in a gradient?

The user-defined procedure SETPAUSEMS changes the drawing speed. The parameter is sleep time in milliseconds.

This program answers the question: What if the line length increases and the color changes?

* * *

The state of the pen consists of the following properties: color, width, position and direction. These properties are implemented as variables penColor, penWidth, penPos and penDir. The variable penPos is a point of type point2D and variable penDir is a vector of type vector2D. There are two limitations on the possible values of these variables, namely: penWidth must be positive and penDir must be a unit vector (a vector of length 1).

The user-defined procedure InitPen procedure sets the pen's initial state. The starting state of the pen is: white color, width of 4, position at the screen center and direction to the right.

The definition of this procedure is given at the beginning of the second part of the source code.

This program answers the question: What if a bigger angle and random colors were used?

* * *

The procedure FW has a parameter named distance. It is defined at the end of the second part of the source code. It first calculates the motion vector v. The variable penPos contains the current position. Then a disc is drawn at the current position, and a line is drawn from current position to motion end position. Lastly, the current position is set to equal the motion end position.

The procedures LT and RT change the pen's direction. The direction is rotated according to the parameter angle. The pen's direction, stored in the variable penDir, must be a unit vector. Since rotating a vector doesn't change its length, the new direction vector should remain to be approximately a unit vector.

<< F2:Prev - - F4:Next >>