Dr Logo for the Tatung Einstein, showing a rosette drawn by the turtle
← Back to Courses
module
24

Patterns

Introduction

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.

A shape, turned

Nothing new. Now draw it twelve times, turning thirty degrees between each:

twelve squares turned thirty degrees apart

Twelve thirties are 360, so the last copy lands back on the first and the pattern closes.

Letting Dr Logo do that sum

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:

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.

The code

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.

A colour for each copy

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:

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.

fourteen squares, each a different colour

Colours 0 and 1 are the background and the ordinary pen, which is why this starts at 2.

Filling a shape

fill paints outwards from the dot underneath the turtle, in the current pen colour, until it meets a line:

a solid blue square

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.

Change one thing

  • 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?
  • In spin, change rt :turn to rt :turn + 1. A pattern that never quite closes is usually more interesting than one that does.
  • Draw a rosette in one colour, then setpc a different one, move into one of the small shapes where two squares overlap with the pen up, and fill it.

When it goes wrong

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.

Summary

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.

Next

S25, a quiz - the last of the applied sections, and the one that uses nearly everything.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.