
S23 ended with a shape that closes: a petal that puts the turtle back where it started. Once you have one of those, you have a pattern generator, because you can draw it again from a slightly different angle. And again.
to square :size
repeat 4 [fd :size rt 90]
end
Nothing new. Now draw it twelve times, turning thirty degrees between each:
repeat 12 [square 40 rt 30]

Twelve thirties are 360, so the last copy lands back on the first and the pattern closes.
Writing 12 and 30 separately means keeping them in step by hand. Better to say what you actually mean - go all the way round, turning this much each time:
to rosette :size :turn
repeat 360 / :turn [square :size rt :turn]
end
repeat works out 360 / :turn and uses it as the count; the square bracket is
where the sum stops and the instruction list begins.
Now rosette 40 30 gives twelve squares, rosette 40 20 gives eighteen, and
rosette 40 45 gives eight - and you never have to divide anything yourself.
to square :size
repeat 4 [fd :size rt 90]
end
to rosette :size :turn
repeat 360 / :turn [square :size rt :turn]
end
cs
ht
rosette 40 30
Starting from
A fresh boot, at the ? prompt.
What you should see
Twelve squares in a ring, meeting at the centre. It takes a few seconds.
repeat cannot tell you which time round it is, so a pattern that changes as it
goes has to count for itself - which means recursion, and a number that goes up:
to spin :size :turn :colour
if :colour > 15 [stop]
setpc :colour
square :size
rt :turn
spin :size :turn :colour + 1
end
Run it with spin 40 30 2. It stops when it runs out of colours rather than
when the circle closes, so at thirty degrees a turn you get fourteen squares -
two more than a full revolution, and the last two land on top of the first two.

Colours 0 and 1 are the background and the ordinary pen, which is why this starts at 2.
fill paints outwards from the dot underneath the turtle, in the current pen
colour, until it meets a line:
cs ht setpc 4
repeat 4 [fd 60 rt 90]
pu rt 45 fd 25 pd
fill

Two things will catch you.
Get inside with the pen up. If you draw your way to the middle, the turtle ends up standing on its own line - and since that pixel is already the pen colour, there is nothing for the flood to change. It does nothing and says nothing.
Make sure the shape is closed. If there is any gap, or if you fill on an empty screen, the paint gets out and covers everything.
It is not fast: a square sixty across takes about ten seconds.
rosette 40 20, then rosette 40 45. Which turns give a pattern that closes
neatly, and what do they have in common?rosette 40 7. Seven does not divide 360. What does repeat do with the
count it gets?spin, change rt :turn to rt :turn + 1. A pattern that never quite
closes is usually more interesting than one that does.setpc a different one, move into one of
the small shapes where two squares overlap with the pen up, and fill it.| Symptom | Cause |
|---|---|
| The pattern does not close | The turn does not divide 360. Not a fault - often the point. |
| Only one shape appears | The turn is outside the repeat list, so it happens once at the end. |
fill does nothing |
The turtle is standing on a line. Move to the fill point with the pen up. |
fill covers the screen |
The shape has a gap in it, or there was no shape. |
| It is taking a long time | fill is slow, and so is a rosette with a small turn. Count the shapes before you run it. |
A shape that closes can be repeated with a turn between the copies to make a
pattern, and writing the count as 360 / :turn keeps the two numbers in step.
repeat cannot count for you, so use recursion when each copy needs to differ.
fill floods from under the turtle to the nearest lines - get there with the
pen up, and close your shape first.
S25, a quiz - the last of the applied sections, and the one that uses nearly everything.