Ever heard of a fractal?
The Sierpinski Sieve, also known as the Sierpinski Triangle, is one of the most well-known fractals. A fractal is essentially a pattern that repeats itself forever, and will look the same, if not similar, no matter how much you zoom in or out of the shape!
Here’s what the Sierpinski Sieve looks like (source: Google Images):


How It Works
The sieve starts with an equilateral triangle and in each iteration, every new triangle is divided into four more equilateral triangles. You can also view it as connecting the midpoints of every new triangle, which creates an equilateral triangle half the size of the original (and three more triangles are automatically in the remaining space)
Number of Triangles: 1 -> 4 -> 13 -> 40

Interesting, right?
…
Well, if not, then I challenge whoever is reading this to see how many patterns they can find within this sieve (hint: can you see the pattern in the change in area and perimeter?).
I can guarantee there’s so much to discover! Which is why I decided to attempt to code my own version…
How It Started
It all started as an Object Oriented Programming (OOP) Assignment for my Grade 12 Computer Science course. Since I had already experimented with OOP ahead of time for my previous assignment (See my Box Capture Game post!), I wanted to push my boundaries again for this project and do something beyond the regular expectations.
However, what to do, what to do? It was time to use the best method of finding inspiration: looking around.
And behold! My eyes caught a poster of a portion of the Mandelbrot Set, a famous fractal for it’s aesthetic appeal and usefulness in calculations for coastlines (fun fact alert: coastlines an example of fractals found in nature!).
Now, I had already peaked at our course outline and knew we would learn further about the Mandelbrot set, so I didn’t want to code it for my project. Also because I wasn’t sure how to incorporate OOP because my brief research (read: scroll through Wikipedia) told me it required an equation with imaginary numbers.
So in short, Mandelbrot set was a no-no. However, any other fractal was still on the table…And you guessed, I chose to code the Koch Snowflake!
…
Just Kidding.
As you may have guessed, I chose the Sierpinski Sieve. Why? I’m not sure. I guess I like triangles. I felt the self-contained aspect would be easier to code, giving me more experience to better tackle harder fractals I would potentially code in the future. Also, I thought I could nicely incorporate the OOP requirement in by making each triangle an object.
So…Did You Use Recursion?
NO.
When I first explained the idea to my mother, she said it could only be done through recursion. I disagreed. We continued to debate, and even other people I asked told me it would be very difficult to do without recursion.
Now, I don’t mind using recursion, except for the fact that I am fuelled by spite and telling me something isn’t possible only encourages all the more.
So, no recursion it was.
The Process
I’ll admit, there was no long multi-step process that usually occurs in my other projects. There was no set of features I would implement one-by-one. It was just figuring out the logic, and implementing it. I love logic problems and making things “click” in my mind. This was going to be easy…
Days went by.
No code in progress.
I was stuck.
I felt like a mad scientist without the science as my CS notebook slowly filled up more and more diagrams of triangles. No, I am not a conspiracy theorist. I just couldn’t figure out how to implement my code. I fully understood the process of each iteration would work and the potential pseudo-code, but every time I tried implementing it, I always came across a roadblock.
So what did I do? Ignore it.
I know, it sounds pretty counterproductive. That’s what I did though: stepped away for a few days, focusing on finishing up any other schoolwork. When I came back, with water bottles filled and a fresh mind, I was able to do it all in an evening. Coding work in mysterious way, huh?
The Results
Take a look my friends…

I did it! As you can see on the left side of the image, I coded it on the Processing language, and had variables users could change and play around with. See the variable that says, “Animation Speed” ? Check this out:
I also had a colourful version, with randomly generated colours in each iteration:

An A-M-A-Z-I-N-G Discovery!
I know I made the Process seem like everything magically went right that evening I did the project. Which isn’t true. I obviously faced many bugs, but the motivation and knowledge I had the entire evening (and night) cleared up to focus on it made me excited enough to push past the obstacles.
However, one bug was particularly special. You see, the code worked in three chunks, one for each ‘line’ in a triangle. When I was testing, I accidentally left the third chunk commented and excluded from the running code. This resulted in something truly amazing: A Diamond Sierpinski.

What’s Next?
So much! There’s so many other fractals and fractal variations I am eager to potentially explore and code in the future.
- Koch Snowflake. Remember how I mentioned a “Koch Snowflake” earlier? That’s actually a really neat fractal that’s very similar to the Sierpinski sieve. Instead of creating smaller triangles on the inside of an equilateral triangle, it expands outward by adding triangles on the outside! I actually seriously considered doing this for the project instead so I’d love to do it for fun.
- Sierpinski Square. It’s basically where you divide up a square into nine smaller squares in every iteration instead triangles. It looks really cool.
- 3D Simulation. I saw some really cool 3D diagrams of the Sierpinski triangle (and square) and am curious how the math and logic would differ from the 2D versions.
- General Variations. If I can stumble upon a Diamond Variation by accident, imagine would I can create by actively playing around! The possibilities are endless…
Looking forward for what’s to come,
Cheers : )
Leave a comment