|
|
|
< how simple |
Turtles |
lines > |
|
|
The Tymless Turtle is a mostly abstract beast that we use to do our drawing for us. He has evolved from a robotic turtle that would sit on a large piece of paper holding a pencil and tracing lines as it ran to and fro. Evolution is a powerful thing, and the Tymless turtle no longer even has a physical presence. Nor, for that matter, does the large piece of paper. We are now drawing onto the computer screen, and you will just have to imagine there is a turtle sitting in the middle of it! A Tymless program is in large part a series of commands that we send to the Turtle telling it to walk forwards, turn left or right, draw lines etc. The Turtle does give us some help in these matters. We don't have to tell it which direction to walk because it knows which direction it is pointing in. This is a held in a system variable, and can be changed. For example turning the turtle right will increase the direction value. You can also change the system variable directly, setting it to whatever value you choose. Similarly we don't need to tell the Turtle how far to turn, because it holds the angle-of-turn in a system variable. By default this is a right-angle, so if you tell the turtle to turn left the direction will decrease by pi/2 (90 degrees). This system variable can also be changed by various means. For example the command >= $pi/3 tells the turtle to change the angle-of-turn to pi/3 before turning right. After this, all left and right commands will turn the Turtle through 60 degrees. The Tymless turtle is not alone. It can breed! You can create a child of the current turtle, and program this to do its own side drawing while the parent stays where it is. When the side drawing is finished you carry on programming the parent turtle. This is very useful for drawings that involve branching, for instance. The branch can be drawn by a 'baby' turtle while the parent continues up the main trunk of the image. 'Baby' turtle's programs are written inside square brackets: // a sine wave Here the main turtle keeps walking forward in a straight line. After
every step a baby is born which turns left and draws the curve. The connect
command \ tells the baby-turtle to
walk forwards sine x, and draw a line from there to the end of the last
line. As the baby turns and walks, the value of some system variables such as the direction will change. But the important point is that each turtle holds its own versions of these values. They are called 'private' variables because the baby takes a copy of its parent's values, and then any changes it makes do not affect the parent. When the square brackets close the variables revert to their previous values. User-defined variables are also private by default. By nesting square brackets, baby turtles can also breed. |
|
< how simple |
lines > |