Sierpinski didn't just mess around with triangles, he also wrote 50 books (all of them flip-books of Sierpinski triangles, I assume), created a curve and finally — the highlight of his storied career — he released this: his Sierpinski Signature Edition Carpet design.
A short warning before we embark on the creation of Sierpinski's renowned carpet: It takes a while to weave one properly. Fortunately, what you waste on time you make up for on materials. This carpet has an infinite edge and no area!
Before we begin, we'll need a function for drawing a square. (And we will
export this function so we can use it in the example below.)
; export square to square :length repeat 4 [ fd :length rt 90 ] end
And we're going to need a function for putting a square inside a larger area. It will build on the idea of a square, so first we needs to import our
square from above.
; import square ; export fsquare to fsquare :length pu fd :length rt 90 fd :length lt 90 pd square :length pu lt 90 fd :length lt 90 fd :length rt 180 pd end
Now with those building blocks under our belt, we can build the carpet itself.
Are you ready? I am ready. That means we are both ready. Here we go!
We'll define our carpet function. The code still won't do anything because we don't call the function yet, we're only writing it, and exporting it, for use later. Soon, soon we will call it!
; import fsquare ; export carpet to carpet :length :depth fsquare :length if :depth > 0 [ ;fsquare :length repeat 4 [ repeat 2 [ carpet :length/3 (:depth - 1) pu fd :length pd ] pu fd :length pd rt 90 ] ] end
Finally it's time to call the
carpet function and weave our mystical weave.
This time we will stop at a recursive depth of 4. If we took it to 5, it would take 8 times as long to build.
;import carpet square 600 carpet 200 4
If we stopped at a recursive depth of 3... it would be even faster, but far simpler:
;import carpet square 600 carpet 200 3