*What Links Here?*

*Outbound Links*

# Sierpinski Triangle

Anything that's covered here is almost definitely covered in a very well known webpage, The Sierspinski Triangle Page to End Most Sierspinski Triangle Pages but regardless, I must press on.

Let's draw a triangle.

to triangle :length fd :length lt 120 fd :length lt 120 fd :length lt 120 end triangle 150

Woah. Holy repetition! Repetition is the bane of the programmer's existence.

Let's use the repeat command to remove that repetition.

to triangle :length repeat 3 [ fd :length lt 120 ] end triangle 150

Neat. Now what is a Sierpinski triangle?

A Sierpinski triangle is the shape that emerges when we say: 'how does one draw a triangle? SIMPLE! One draws three smaller triangles!

A partial implementation of that is as follows.

to triangle :length repeat 3 [ fd :length lt 120 ] end to sier :length repeat 3 [ triangle :length/2 fd :length/2 fd :length/2 lt 120 ] end sier 100

Actually I told a *fib*. When I said: "A Sierpinski triangle arises when we say: 'how does one draw a triangle? SIMPLE! One draws three smaller triangles!' "

I **should've** said:

How does one draw a Sierpinski triangle? SIMPLE! One draws three smaller *Sierpinski* triangles!

But this is an example of unbounded recursion. We need to specify some kind of boundary condition, to stop the recursion before the computer sets on fire. (Or before the program fails with a stack overflow error. Whichever is first.)

Here's a pretty good boundary condition: What if they are too small? Then one simply draws a *regular* triangle.

What we need is a 'stack counter' as described in the Koch curve article. A way to say 'if the stack depth is greater than our limit, just draw a triangle, otherwise, draw a Sierpinski!'

;export sierpinski to triangle :length repeat 3 [ fd :length lt 120 ] end to sier :length :limit ifelse :limit = 0 [ triangle :length ] [ repeat 3 [ sier :length/2 (:limit - 1) pu fd :length/2 fd :length/2 lt 120 pd ] ] end

;import sierpinski sier 250 5

That worked smashingly.

But it might be easier to see *how* it worked so smashingly if we look at the simplest version of the Sierpinski, followed by some more complex examples.

;import sierpinski sier 250 1 setxy 5 500 sier 250 2 setxy 5 750 sier 250 3 setxy 5 1000 sier 250 4 setxy 5 1250 sier 250 5 setxy 5 1500 sier 250 6

##Sierpinski Arrow-Head Curve

Another technique for drawing the Sierpinski triangle in logo is to use a curve, specifically, the Sierpinski Arrow-Head Curve, as shown below.

Technically this isn't a Sierpinski triangle. It's a curve that approximates a Sierpinski triangle. We go even further than that. We can say that at its limit it approaches a Sierpinski triangle. But that's hardly the same thing now is it?

Sierpinski, by the way, in case you were a little concerned, didn't just sit around all day thinking about triangles. In his time between thinking about triangles he managed to write some 50 books*, and he also created 'Sierpinski's Curve' which is a completely different thing and doesn't involve triangles. Okay, it is a *little* bit similar. He made a carpet too.

(*I like to imagine that all 50 books were just animated flip-books showing a deeper and deeper zoom into the Sierpinski triangle.)

to curve :count :length ifelse :count = 1 [fw :length] [ lt 60 pu fd :length/2 rt 180 pd curve :count-1 :length /2 pu rt 180 fd :length/2 pd rt 60 curve :count-1 :length /2 rt 60 pu fd :length/2 rt 180 pd curve :count-1 :length /2 pu rt 180 fd :length/2 pd lt 60 ] end curve 2 200 curve 3 200 curve 4 200 curve 5 200 setxy 20 520 curve 6 200 curve 7 200 curve 8 200

What will he get up to next!

(Nothing, he's dead.)