Turtle LibreLogo
Tilbake til startsida
 

 

Plotting charts

This is an example of how to make charts in LibreLogo. In this case the sine of angles from 0 to 90 is used, but it is easy to change conditions. The program is divided into smaller functions to make it more comprehensible.

 1 TO axis ; Render the x- and y-axis
 2   HOME
 3   PENCOLOR 'green'
 4   FORWARD 300 BACK 600 HOME ; y-axis
 5   LEFT 90 BACK 300 FORWARD 600 HOME ; x-axis
 6 END

 8 TO reset ; Resets the program and variables
 9   HOME
10   CLEARSCREEN
11   GLOBAL :angle
12   :angle = 0 ; The angle to be plotted. Increased by 1 each step
13   GLOBAL :x_value
14   :x_value = 0 ; Steps on x-axis
15   GLOBAL :height
16   :height = 50 ; Determines the vertical size of the chart (y-axis)
17   GLOBAL :width
18   :width = 2 ; Determines the horizontal size of the chart (x-axis)
19 END

21 TO find_origin ; Find x- and y-values for the center of the screen
22   HOME
23   GLOBAL :x_null
24   GLOBAL :y_null
25   :p = POSITION ; Returns position as [x, y]
26   :x_null = :p[0] ; x-position
27   :y_null = :p[1] ; y-position
28 END

30 TO step ; Increase the value for :angle and :x_value with 1
31   GLOBAL :angle
32   :angle = :angle +1
33   GLOBAL :x_value
34   :x_value = :x_value + 1
35 END

37 TO plot ; Rendering
38   :y = ((SIN :angle )* :height) + :y_null ; calculates y-position
39   :x =((:x_value * :width) + :x_null) ; calculates x-position
40   POSITION [:x, :y] ; Moves the turtle to new position
41 END

Here is the program:

43 reset
44 axis
45 find_origin
46 FOR :n IN RANGE 90 [ ; Counts from 0 to 89, i.e. 90 steps
47   step
48   plot
49 ]

Some comments on the program

The function “axis” draws the two axes with the crossing point, the origin, in the center of the screen.

In “reset” the screen is cleared and the turtle is set to its starting point. The global variables are set to zero. The variables :height and :width are used to set parameters concerning the height of the graph and how much the curves will be stretched in width. You may want to experiment with other values ​​here. This is also the place to change the value of :angle if you want to start with another angle than 0 degrees.

Libre Logo calculates the screen coordinates starting from the upper left corner that has coordinates (0, 0). In a graphical representation the coordinates (0, 0) is the crossing point (origin) between the axes. To get the turtle to perceived origo as (0, 0), I have to add the x and y coordinates for this point to the calculated coordinates. In “find_origin” the variable: x_null is set to the distance along the x-axis from the edge of the page to its center. : y_null is set as the distance along the y-axis from the top of the page to the middle of the page.

The function “step” increases the angle and the x-value by 1. Also here you can make changes.

Most of the mathematics happens in the function “plot”. In line 38 :y is first set to the sine of the angle. The sinus value ranges from -1 to 1. Therefore this value is multiplied by a constant :hight, to be a little more visible on the screen. Finally, the distance from the top of the page down to the x-axis is added to get fluctuations around the x axis. In line 39 is :x set to the value of :x_value and then multiplied by the width-constant and then added to the distance from the left side out to the y-axis.

The last part is the actual program. First, the function “reset” is called, then the axes are drawn. After the data for origin is fetched, the counting starts. FOR :n IN RANGE 90 in line 46, is LibreLogo’s way of writing FOR n = 1 TO 90 which you might know from other programming languages. The first time the loop is executed, the value of :n = 0. In the next iteration, the value increased by one to 1. This adding is repeated as long as the value of n is less than 90.

A sensible thought is plotting the angles between 90 and 180. Thus FOR :n IN RANGE 90 180. The program is set up in such a way that the angles will still be counted from 0. To display the angles 90 to 180, we must change the program slightly. In function “step” you have to comment out lines 31 and 32 by writing a semicolon in front of the commands. The function is therefore as follows:

30 TO step ; Increase the value for :angle and :x_value by 1.
31   ; GLOBAL :angle ; not read by LibreLogo
32   ; :angle = :angle +1 ; not read by LibreLogo
33   GLOBAL :x_value
34   :x_value = :x_value + 1
35 END

A semicolon makes the following commands not to be executed. Naturally you can instead remove these two lines. The next change occurs in the program itself. The value of :angle must be set to the same value as :n in the loop. In other programs we could have done this with: :value = :n, but not in Libre Logo. Here we have to use the function REPCOUNT. We must also tell LibreLogo that the variable :angle is global. Therefore these two new lines must be added to the the main program:

43 reset
44 axis
45 find_origin
46 FOR :n IN RANGE 90 180 [ ; Count from 90 to 179
47   step
48   GLOBAL :angle
49   :angle = REPCOUNT
50   plot
51 ]

Of course it is possible to write a code in such a way that you could write the angle range directly instead of changing the code.

I hope my little experiment will show you that LibreLogo can more than create geometrical figures. Happy experimenting.


© Context and design: Kolbjørn StuestølStuestøl homesite (in Norwegian)Modified February 11 2016