Space Filling Curves

Space-filling curves!

Amazing things.

Peano sat around thinking and thinking, What was he thinking? He was thinking:

I wonder, I wonder, I wonder! Can I draw a continuous line that touches every single point of the unit square, exactly once? (i.e. it never crosses itself!)

Not only did Peano discover that there was a way to do it, he was thrilled to find that the way to do it was called "The Peano curve!" (Mathematics seems to be full of these awesome coincidences)

Peano described this curve in a mathematical paper which did not include any pictures. Think about that for a minute. Let it really sink in.

I'll demonstrate a Peano curve soon, but you might be interested in the famed Hilbert Curve which is fairly similar, published one year after Peano's and achieved greater fame because he was clever enough to include pictures in his paper.

To make the Peano curve look interesting, you need to draw it with rounded corners. Otherwise it just creates a lattice-like grid. So instead of performing a sharp 90 degree lt or rt (left turn or right) I've created function called ilt and irt which perform a bevelled 90 degree turn to the left or right.

'Flipping' to create space-filling curves

Here's an excellent curve I first saw in the book 'Brain Filling Curves'

The 'trick' is that part of the curve is drawn 'flipped'.

Flipped means that instead of just drawing a segment, you instead: pick up the pen, move to the end of the segment, turn around 180 degrees, put the pen down.... then draw the segment, then turn around, pick up the pen, move back to the end of the segment and put the pen back down.

Flipping, it turns out, is very useful in creating space-filling curves!

Here's our basic program, with extra comments around the 'flipping' -- run with a limit of just 2.

Now here's the same program with less comments, running this time with limits of 2, then 3, then 4, then 5. For exponentially more space-filling fun.

That flipping technique is not very satisfying, for a couple of reasons. First: the code is ugly. The "pen up, run, spin around, pen down" eventually followed by "pen up spin around, run, pen down" is just confusing.

We could hoist that into its own function, reverse_curve. But it's still awkward.

But worst is that the curve is no longer drawn as a single continuous line.

And the whole idea of space filling curves is that you never lift the pen.

'Flipping' by replacing Left Turns with Right Turns etc.

A better way of "flipping" a section is to reverse all of the angles: replace "right turns" with "left turns" and vice-versa (avv). This only works if the sections being flipped as symmetric length-ways: the first half is a mirror equivalent of the second half.

The easy way to replace "right turns" with "left turns", avv, is to turn right by a negative amount, avv.

Implementing this in the flavor of logo that i'm currently using turned out to be a little tricker than I expected. Trying to multiply by negative 1 is not something that the logo parser was familiar with. The only way I found to reliably create the number "-1" was with the expression "(0-1)". Fair enough.

So in the code below, the term ":angle" is a number that sometimes equals 1 and sometimes equals -1. Thus it can transform left turns into right, avv.

This trick does not always work. As I mentioned, this only works if the sections being flipped are symmetric length-ways, that is: if the first half is a mirror equivalent of the second half.

The 'flow snake' is an example of a fractal curve that requires 'flipping' but where the unit-section is not symmetric length-ways.

There is a third technique we can use, which requires more code but does not involve lifting the pen.

'Flipping' by Mutual Recursion.

In this scenario we have two methods (flowsnake and flowsnake_r) which are "mutually recursive" and where one is the "reverse" of the other. (The curve we're drawing this time is the famous flowsnake (or 'Peano-Gosper curve')).

It would be good if, instead of meticulously composing the second method to be a reverse of the first method, we could simply wrap the first function in a special 'reverse' function. It would reverse the sequence of instructions. And if they are function calls, it would wrap them in 'reverse' functions. And if they are atomic methods (such as 'left turn') it would replace them with their reversed function (e.g. convert left turns into right turns). And if they are already reversed, it would unreverse them!

At this point we're starting to wish for "meta-programming" features: instructions that act on instructions themselves, rather than just on data.

I'm sure it can be done, but I don't know enough logo to know quite how. Do you?

External Links

See Also

 

Logo + Turtle graphics via Papert.