Koch Curve

A Koch curve is a well-known fractal, first described by a Swedish Mathematician, Helge Von Koch, in his paper "Sur une courbe continue sans tangente, obtenue par une construction géométrique élémentaire".

Put three Koch curves together in a triangular configuration and you have a Koch Snowflake.

Let's build up a Koch curve with Logo. It's a variation on a straight line, so we'll start with just a line. Here's a line.

Okay, that is definitely a line. Now let's make a slightly bumpy line.

That's all well and good. But looking at the code, I see this 'forward 40' bit is very repetitive. Programmer's hate repetitive code.

Let's make a function that does that for us.

Okay, that didn't achieve very much, but trust me, I'm going somewhere with this. It did demonstrate how we can make a function that accepts parameters.

Now let's neaten it up a little by making a function out of bumpy line. That way we can reason about bumpy-line all on it's own, without having to think about its constituent parts.

Bumpyline is very easy to use.

Now get ready for the giant leap.

It's time to turn our bumpy line into a fractal, by exploring the idea of 'self-similarity'. 'Self-similarity' is the idea of making a shape where the entire shape is similar (or exactly the same) to a part of itself.

What if each line in the shape was in fact a bumpy line?

Here's a naive version of that idea. (Hint: this won't actually work).

The error message says "Too much recursion!". How much recursion do we have? We have an infinite amount. Bumpyline calls bumpyline calls bumpyline on and on for ever.

Sometimes, too much recursion is too much recursion.

How can we limit the amount of recursion?

We can use a technique called 'stack counting'. Each time we call bumpyline we pass in a number. If the number is bigger than a certain limit, we don't call bumpy line, we do something else instead (e.g. draw a straight line instead of a bumpy line).

And that's a Koch curve in anyone's book.

We could change the limit, so that instead of 'greater than 2' it says 'greater than 3' or 'greater than 5'... But the right way to do it is to encapsulate this information in a parameter. Let's do that.

Refactoring to create a better program

Now there's a couple of refactorings we can make to improve our code. First up, let's call 'bumpyline' what it really is: a koch curve. Second, we can simplify the way we handle count and limit. Instead of counting up from 1, we can count down from :limit, and stop when we hit zero.

That's our best Koch curve, but what's a Koch snowflake? Well that's another article altogether. (It builds on this one)

Alterations

Let's dig into our curve here, and see if we can make some little alterations with surprising results.

Now the fundamental 'self similar' piece in the koch curve, is what we called the "bumpy line".

What if we change it up a bit and alter the shape of the bumpy line.

For example, we can change some of the angles. I'll change one of the 60 degree angles to 61 degrees. What happens to our curve then?

The bumpy line is no longer straight. It bends upward. In each image I've increased the limit from 1 to 3, and then up to 5. It goes from 'not quite straight' to 'crooked' to 'completely twisted'. That tiny alteration, just a single degree has destroyed the Koch Curve.

What I've done is broken the 'self-similarity'. The outer bumpy-line no longer resembles the inner bumpy line.

What is the property of the bumpy line that is no longer self-similar? Straightness. I've taken away straight-ness. If we persist in the modifications without restoring self-similarity we'll be able to make some very pretty patterns, but they won't be fractals. I can try to put self-similarity back by adjusting one of the other angles to compensate for the one I changed.

There were originally two 60 degree angles inside the koch kurve. Now I've changed one to 61 and the other to 59, so they still approximate to a straight line.

Now let's generalize this, by adding a new parameter, skew. Skew is the amount of change we want to add/subtract from our 60 degree angles. And we'll experiment with large amounts of skew.

This is still a bit broken. The higher the :limit, the smaller the curve. Why is that?

Here's a different variation. A koch curve is built from 4 straight sections. What if every second section was reversed?

External Links

See Also

 

Logo + Turtle graphics via Papert.