applets.

Turtle Drawing

← All apps

About Turtle Drawing

You write a small program; a robot with a pen follows it across a big sheet of paper. Press RUN to watch it draw from an empty sheet, or STEP to do it one move at a time with the line it's on lit up. The Drive by hand buttons move the turtle and add the line to the program for you, so you can start with no typing (the arrow keys do the same when you're not in the program box). CLEAR empties the sheet and the program, and then offers to bring them back.

THE ONE BIG IDEA

Walk round any closed path, back to where you began and facing the way you began, adding up every turn as you go (right as plus, left as minus). The total is always a whole number of 360s. Harold Abelson and Andrea diSessa call this the Total Turtle Trip Theorem in their book Turtle Geometry (MIT Press, 1981). It's why a square's corners are 90: four equal turns must make 360. And why a five-pointed star's are 144: five equal turns make 720, twice round. If a path never crosses itself the total is exactly one 360 (the Heart shows the catch: turn left instead of right at its dip and the same-looking picture counts 720).

The dial on the box winds up a ring for every 360, and the counter keeps the running total. Total trip turns the theorem into a program: it stops when the total is a multiple of 360, which is enough to close the path whatever the angle, as long as every step and turn are the same and the turn isn't a whole number of turns (with 360 the turtle just goes straight on). The trip starts when the pen first touches the paper, so travel with the pen up before the first line doesn't count. HOME, SETXY and SETHEADING teleport rather than turn, so each starts a new trip.

THE STORY

The first robots of this kind were W. Grey Walter's "tortoises" in Bristol in 1948 and 1949, Elmer and Elsie, which wandered about by themselves using a light sensor and a touch switch. They were a study of how simple a nervous system could be, and nothing told them where to go.

Logo began in 1967 at the research firm Bolt, Beranek and Newman in Massachusetts, by Wally Feurzeig, Seymour Papert and Cynthia Solomon, as a programming language for children. The turtle came soon after, a dome on wheels with a pen that could be lowered to the floor. Papert's point, made in his book Mindstorms (1980), was that a child can work out geometry by pretending to be the turtle: to draw a circle, go forward a bit and turn a bit. Maths from inside the shape rather than from above it.

The examples grew from the Python scripts this app came from: turtle.py (the Koch snowflake, there done by rewriting a string), tur_Heart.py (the Heart, without its fill), Turtle_Circle.py (the Sunburst, and the commented-out Spiky star), Turtle_Random.py (the Random example), polygon.py (the Triangle) and Turle.py (the Wheel). They're scaled to fit the sheet.

THE LANGUAGE

Mind the minus: :N - 1 and :N-1 subtract, but :N -1 is two separate values, as in other Logos, which is how TREE :N -1 passes two inputs. Words like RANDOM take in sums to their right, so FORWARD RANDOM 50 + 10 is RANDOM 60; write (RANDOM 50) + 10 for the other meaning.

HOW IT WORKS

The program is read into a little stack machine, written for this page. That way it can pause after any step and carry on in the next frame however deep its recursion has gone, so the page never freezes. A call that is the last thing a procedure does reuses its place, so TO SPIN FD 1 RT 1 SPIN END goes on for ever without using up the depth limit. Limits: 5,000,000 steps, 100,000 moves, 5,000 procedures waiting inside each other. Past any of them the program stops and says why.

The challenges walk along the faint shape every 4 units and ask whether ink comes within 4 units, then walk along your ink and ask how much strays more than 8 units from the shape. 98% covered with at most 3% stray earns the gold star. It doesn't care what order you draw in, or how many moves it took.

WHAT'S LEFT OUT

Real Logo has words and lists to work on, and this has only numbers and pen names. Real floor turtles also drift: wheels slip and paper rucks, so the square never quite closed, which was part of the lesson. This turtle is exact, and goes off the sheet without falling off anything. The sheet here shows 250 units each way from the middle, and lines beyond it are kept but can't be seen. A link holds up to about 4,000 characters of program.

FURTHER WATCHING AND READING

EXAMPLES

simplest first

CHALLENGES

draw over the faint shape, starting from the middle

PROGRAM Ctrl + Enter runs it

X0
Y0
HEAD0°
PENDOWN
MOVES0
TOTAL TURTLE TRIP

The rule: a trip that ends where it began, facing the way it began, turns a whole number of 360s. Right turns count plus, left turns minus.

DRIVE BY HANDor the arrow keys