Programming Turtles

For the programmers and the more adventurous among Evolved Art users, it is possible to manually modify existing turtle programs, or to program your own turtle from scratch. This may seem like a pointless exercise considering the powerful evolutionary tool that you already have with Evolved Art, but there are those users who have their own reasons for wanting to work with turtle programs directly. This page will discuss how to go about working with turtle programs in Evolved Art.

Unfortunately, the current release of Evolved Art does not focus on supporting working with turtle programs directly. Therefore, the process of exporting and importing turtle programs is a little complicated. A future release of Evolved Art will make this process more straightforward. Most likely, and editor will be provided in the Evolved Art app that will provide for editing turtle programs directly within the app.

Exporting Turtle Programs

To begin working with turtle programs, you will first need to export a program to work with, unless you intend to write a program from scratch. Turtle programs are written in ASCII text, so any text editor should work with them. If you wish to work with an existing turtle program, the simplest approach is to export the turtle's definition file to iTunes. Once the turtle definition file is exported to iTunes, you can use iTunes to save the file to your disk. From there, you can open the definition file using any text editor. The definition file is an XML file with a file suffix of ".turtle". Some text editors will not open a file with a suffix of ".turtle", but you can simply change the suffix to ".txt" or whatever suffix your editor prefers. Once you export and open the definition file, you will see some rather ugly XML, and unfortunately it will all be one a single line. Some editors will clean this up for you, but most likely you will need to work with the text to clean it up yourself.

The turtle's actual program is located in an XML element named <code>. Search the text for <code> and you will see the turtle's code inside the XML element. You want to copy all of the text from <code><![CDATA[ up to but not including ]]></code>. Since the program is enclosed within parentheses, it should be easy to see where the code begins and ends. The remainder of the definition file's contents may be deleted. Once you have isolated the turtle's program, you can reformat it as you please and begin working with it.

Since this is a somewhat complicated process, you have the alternative of displaying the turtle in the Turtle Details view, selecting the Program subview, selecting all of the program text and copying it directly from the Evolved Art app. You can then paste the program into any iPad app that can edit text. However, getting the code from there to your Mac or PC is not exactly straightforward. I am not familiar with doing this, so Google may help.

Importing Turtle Programs

Fortunately, importing turtle programs into Evolved Art is somewhat easier. Once you have finished your turtle program modifications, save the text file and give the file name a suffix of ".txt" or ".tcode", then add it to the iTunes File Sharing area for the Evolved Art app. Then display a Gene Pool in the Population View and select the Import From iTunes action to import the turtle program, or display a turtle in the Turtle Details view and use the Replace button and select Import From iTunes action and import the turtle program to replace the program of the currently displayed turtle.

Turtle Programs

The Turtle Programming Language page provides and overview of the programming language used by Evolved Art. However, the details are left out. Primarily, the syntax of an overall program is not covered. The important thing to know is that the Evolved Art programming language looks a lot like Lisp. This is because everything is enclosed in parentheses. Terminal expressions, such as FWD_5 or LEFT_90 are written as (FWD_5) and (LEFT_90). Furthermore, programs begin with an open parenthesis and end with a closing parenthesis. Block expressions, such as BLOCK, REPEAT_3, and IF_PEN_UP are begun with an open parenthesis and ended with a closing parenthesis.

As an example, the following program will draw a perfect square:

( REPEAT_2 ( REPEAT_2 (FWD_100) (RIGHT_90) ) )

In this example, REPEAT_2, is a block expression, and FWD_100 and RIGHT_90 are terminal expressions.

Turtle State

When the turtle is drawing, it maintains a state. The turtle's state consists of three items: the up/down state of the turtle's pen, the direction in which the turtle is pointing, and the turtle's pen's color. When it begins drawing, the turtle is at the imaginary point of X = 0.0, Y = 0.0, although this is not really important as turtle drawings are always scaled and centered to exactly fit the area in which it is drawing. The turtle's direction is initially zero degrees, which is pointing directly right on the iPad screen. The turtle's pen starts in the down state, which means that it will begin drawing as soon as it gets a move forward instruction, unless it gets an intervening instruction to change it's pen state to up. The turtle's pen color is initially the RGB components Red-0, Green-0, Blue-221, which is commonly represented as the HEX code #0000DD. This color is deep blue.

Colors And Degrees

As the turtle gets instructions to change it's pen color, ultimately it can overflow the valid values for each of the three color components. RGB color components have values from 0 to 255. To deal with this problem, the instructions which increment the color components perform a modulus of 255 to ensure the values are always valid. In other words, if the turtle gets an instruction to increment the Red component by 10, and the current value is 250, the increment will move the value to 260. However, a modulus 255 is then performed, changing the value to 5. In other words, the new value is the remainder of dividing the value by 255.

Likewise with the turtles direction. It is always kept within 0.0 and 360.0 degrees. When the turtle is turned, if the new direction is less than 0.0 degrees, then 360.0 is added to its value, and if the direction is greater than 360.0 degrees, then 360.0 is subtracted from its value.

Turtle Expressions

The turtle language consists of terminal expressions and block expressions. These names are not appropriate terminology for those familiar with the science of writing programming languages, but they will suffice for our discussion (and I am not an expert in this area). Terminal expressions are expressions that perform an action and do not contain any other expressions (hence the name "terminal", as they "terminate" the current branch of the expression tree). Examples of terminal expressions are FWD_100, RED_32, and RIGHT_90. Block expressions perform no specific action with the turtle - they contain other expressions. Examples of block expressions are REPEAT_2, and BLOCK. Terminal expressions are always enclosed within parentheses, for example ( FWD_100 ). Block expressions are enclosed within parentheses, with the block expression name after the opening parenthesis, following by the block's subexpressions, followed by a closing parenthesis. For example, ( REPEAT_2 (FWD_100) (RED_32) (RIGHT_90) ).

LIMITATION Block expressions cannot contain more than 10 subexpressions.

The two unique block expressions in the turtle programming language are IF_PEN_UP and IF_PEN_DOWN. These block expressions must contain exactly two subexpressions. The first subexpression is executed if the expression is true, otherwise the second subexpression is executed. In other words, for IF_PEN_UP, the first subexpression is executed if the turtle's pen is currently in the up state, and the second subexpression is executed if the turtle's pen is currently in the down state. Likewise for the IF_PEN_DOWN expression, except the logic is reversed - the first expression is executed if the pen is currently in the down state, and the second subexpression is executed if the pen is currently in the up state.

Block Expressions

The following table itemizes the Evolved Art programming language block expressions.

Name Description
BLOCK Execute all subexpressions 1 time.
REPEAT_2 Repeat all subexpressions 2 times.
REPEAT_3 Repeat all subexpressions 3 times.
REPEAT_5 Repeat all subexpressions 5 times.
REPEAT_10 Repeat all subexpressions 10 times.
REPEAT_50 Repeat all subexpressions 50 times.
REPEAT_100 Repeat all subexpressions 100 times.
IF_PEN_UP If the turtle's current pen state is UP, execute the first subexpression, otherwise execute the second subexpression. This block expression must contain exactly two subexpressions.
IF_PEN_DOWN If the turtle's current pen state is DOWN, execute the first subexpression, otherwise execute the second subexpression. This block expression must contain exactly two subexpressions.

Terminal Expressions

The following table itemizes the Evolved Art programming language terminal expressions.

Name Description
PEN_UP Change the turtle's pen state to UP. The turtle does not draw lines, but continues to execute all instructions such as moving, turning, and changing color.
PEN_DOWN Change the turtle's pen state to DOWN. The turtle draws lines using the current pen color and direction.
FWD_1 Move the turtle forward 1 unit, drawing with the current color in the current direction.
FWD_2 Move the turtle forward 2 units, drawing with the current color in the current direction.
FWD_3 Move the turtle forward 3 units, drawing with the current color in the current direction.
FWD_5 Move the turtle forward 5 units, drawing with the current color in the current direction.
FWD_10 Move the turtle forward 10 units, drawing with the current color in the current direction.
FWD_50 Move the turtle forward 50 units, drawing with the current color in the current direction.
FWD_100 Move the turtle forward 100 units, drawing with the current color in the current direction.
RIGHT_1 Turn the turtle's direction right 1 degree.
RIGHT_2 Turn the turtle's direction right 2 degrees.
RIGHT_3 Turn the turtle's direction right 3 degrees.
RIGHT_5 Turn the turtle's direction right 5 degrees.
RIGHT_10 Turn the turtle's direction right 10 degrees.
RIGHT_15 Turn the turtle's direction right 15 degrees.
RIGHT_45 Turn the turtle's direction right 45 degrees.
RIGHT_90 Turn the turtle's direction right 90 degrees.
LEFT_1 Turn the turtle's direction left 1 degree.
LEFT_2 Turn the turtle's direction left 2 degrees.
LEFT_3 Turn the turtle's direction left 3 degrees.
LEFT_5 Turn the turtle's direction left 5 degrees.
LEFT_10 Turn the turtle's direction left 10 degrees.
LEFT_15 Turn the turtle's direction left 15 degrees.
LEFT_45 Turn the turtle's direction left 45 degrees.
LEFT_90 Turn the turtle's direction left 90 degrees.
RED_1 Increment the Red component of the turtle's pen by 1.
RED_2 Increment the Red component of the turtle's pen by 2.
RED_3 Increment the Red component of the turtle's pen by 3.
RED_8 Increment the Red component of the turtle's pen by 8.
RED_16 Increment the Red component of the turtle's pen by 16.
RED_32 Increment the Red component of the turtle's pen by 32.
RED_64 Increment the Red component of the turtle's pen by 64.
GRN_1 Increment the Green component of the turtle's pen by 1.
GRN_2 Increment the Green component of the turtle's pen by 2.
GRN_3 Increment the Green component of the turtle's pen by 3.
GRN_8 Increment the Green component of the turtle's pen by 8.
GRN_16 Increment the Green component of the turtle's pen by 16.
GRN_32 Increment the Green component of the turtle's pen by 32.
GRN_64 Increment the Green component of the turtle's pen by 64.
BLU_1 Increment the Blue component of the turtle's pen by 1.
BLU_2 Increment the Blue component of the turtle's pen by 2.
BLU_3 Increment the Blue component of the turtle's pen by 3.
BLU_8 Increment the Blue component of the turtle's pen by 8.
BLU_16 Increment the Blue component of the turtle's pen by 16.
BLU_32 Increment the Blue component of the turtle's pen by 32.
BLU_64 Increment the Blue component of the turtle's pen by 64.