Polybench Logo - Turtle Graphics in Polybench
Overview
Some components in Polybench, notably the "Y-T Reviewer", offer the possibility to draw onto the component surface using the Logo-syntax.While looking for a compact script to draw lines, labels and markings, we realised that Logo may be a good choice, since there are already many resources on the internet that describe this programming language.
Logo, mostly known for its Turtle Graphics, was designed in 1967 by Seymour Papert, Cynthia Solomon, and Wally Feurzeig. Over 50 years later, there are several implementations of Logo. Among the most well known are Code.org and Scratch.
Polybench Logo was introduced in Polybench 1.34.1 using a subset of the general Logo-syntax. In the current version, Polybench Logo programs can be written on one line only.
Items
forward
forward 100 fd 100
Lets the turtle move N (in this case 100) units forwards, following the current heading.
back
back 200 bk 200
Lets the turtle move N (in this case 200) units backwards, following the current heading.
left
left 90 lt 90
Lets the turtle turn itself N (in this case 90) degrees to the left, thus changing its heading.
right
right 33 rt 33
Lets the turtle turn itself N (in this case 33) degrees to the right, thus changing its heading.
setheading
setheading 45 seth 45
Lets the turtle change its heading (angle) to a new absolute value of N (in this case 45) degrees clockwise from the positive Y-axis.
setxy
setxy 20 10
Lets the turtle move itself to the absolute coordinates (X (in this case 20), Y (in this case 10)).
Note that the Y-axis in Polybench Logo is upside down as shown in the image below:
setx
setx 20
Lets the turtle move itself to an absolute X-coordinate (in this case 20).
sety
sety 10
Lets the turtle move itself to an absolute Y-coordinate (in this case 10).
Note that the Y-axis in Polybench Logo is upside down, so the higher the Y value, the lower you go on the drawing surface.
home
home
Lets the turtle move itself to the center of the coordinate system (0, 0).
This has the same effect as setxy 0 0 seth 0
arc
arc 270 20
Draws an arc of a circle (in this case 270 degrees clockwise) around the current coordinates of the turtle with the specified radius (in this case 20 units).
So, the turtle is in the center of the arc.
repeat
repeat 4 [fd 20 rt 90]
Repeats the words inside the block quotes M times (in this case 4).
This example draws a square with sides of length 20 units.
label
label "Hi! label [Hello with so many words]
Writes text in the current font, where the turtle sits at the left-top of the first letter.
Note that when using a double-quote ", that only one word is allowed. If you need to write multiple words, then use the block quotes.
print
In Polybench Logo works equally like label.setpencolor
setpencolor [255 128 0] setpencolor Orange setpencolor ##FF8000
Changes the color of the pen. The color can be specified using values between 0 and 255 for the Red (in this case 255), the Green (128) and the Blue (0) components.
It can also be specified as a hexadecimal value (start with a #, in this case #FF8000), or just by the name of a color in the "Color Repository" (in this case Orange).
setpensize
setpensize 2 setpensize [4 4]
Changes the thickness of the pen to N units (in this case 2), or a list of two values meaning width and height, where only the width is used.
setpenpattern
setpenpattern [5 3 2 3]
Changes the way lines are drawn. In stead of a solid line, a pattern of alternating dashes and spaces can be defined.
In this example, lines are drawn with a dash of length 5, then a space of 3, a dash of 2 and a space of 3.
There should be at least two values, but there is no theoretical limit to the number of values in the list.
penup
penup pu
Lifts up the pen, so that it does not draw anymore. After penup the words setxy, setx, sety and forward do change the position of the turtle, but no line is drawn.
pendown
pendown pd
Activates the pen, so that it draws lines. After pendown the words setxy, setx, sety and forward change the position of the turtle, and while doing so draw lines.
clearscreen
clearscreen cs
Deletes all graphics previously drawn, so that you can start with a fresh drawing.
clean
clean
Deletes all graphics and procedures and resets the turtle graphics to a complete new start.
to
to writelabel :x :text pu setxy :x 0 pd label :text end
Defines a procedure that can later be called. In this example, the procedure is called writelabel and has two variables as arguments: :x and :text (these variable names are just made up by me).
The contents of the procedure (so, the script that is executed if the procedure is called) are listed until the word end. In this case, the turtle is moved to the X-coordinate :x (without drawing a line) and then a label is written with the text :text.
If you send the above sentence to the Logo parser, nothing happens. The procedure is simply stored in memory. It is only executed if someone calls it, for example:
writelabel 100 "Hello!
Details
Basic understanding of Logo Turtle Graphics
Imagine you are a turtle in a sandy desert, and that you can understand English. Someone else shouts instructions to you: "Move 20 steps forward", and after you have followed that order, the person shouts "Turn 90 degrees right. Move 20 steps forward". If that is repeated a few times, you are back on the place where you have started. In the sand you will have left a trace in the form of a square by that time.That is how Turtle Graphics can be understood. Using a few instructions, a turtle aka a pen is moved over the graphics surface, leaving a line where it goes.
The example above in a Logo script looks like this:
forward 20 right 90 forward 20 right 90 forward 20 right 90 forward 20 right 90
This can be written in a shorter form:
repeat 4 [forward 20 right 90]
Coordinates depend on object surface
In Polybench, a unit can have different measures. Without any further specification, a step unit is 1 pixel if the surface is zoomed to 100%.The component that offers a drawing surface may define other units. Even units in the X-direction and Y-direction can be different.
Such a component can specify a character, that is written before a step number, in order to indicate which unit is meant.
We take the "Y-T Reviewer" as an example. Because the purpose of the Turtle Graphics in this case is drawing markings relative to the signals, the viewer defines options to select a signal channel and values in time or per sample number. For example:
setxy 2.3s c2 arc 360 15
lets the turtle move to 2.3 seconds from the left border on the sample value of the second channel, and then draw a circle of 360 degrees with a radius of 15 pixels. This looks like this:
It is also possible to select a value on the Y-axis in the range of the sample values of a specific channel. This is written like:
sety C3:135meaning 135 in the range of the 3rd channel.