Return to site

Sierpinski Carpet - Coding Challenge

A nice fractal coding challenge in Blender

September 20, 2020

I've recently started looking into how to create fractals. I started with the Sierpinski Carpet which looks like the image below.

I had to try a few approaches before I got it right but now I have a functioning script that will generate a Sierpinski carpet for however many n iterations that you specify (as long as your computer has the juice). I got stuck at the beginning until I did the math and started analysing various features of the result. It was a good lesson in sitting down an thinking instead of being lazy and searching up answers or beating your head against a wall trying random things out.

I have a finished version of the script ready at the bottom of this page but I encourage you to try coding this in Blender yourself. I'll describe the Sierpinski Carpet in a little detail first, so that you have some idea about how to approach coding it. Try to plan out your script first and don't peek at the answer. Very likely, there's more than one way to approach this and none of those approaches are necessarily more correct than another.

The Sierpinski Carpet is a recursive fractal first created by Waclaw Sierpinski. It's basic algorithm of construction is as follows:

  1. Start with a square
  2. Subdivide that square into a 3x3 grid
  3. Remove the central square

Sounds simple right? In practice, I couldn't get the subdivision method to work (cause Blender appears to assign indices to faces inconsistently) so I changed my approach to placing down squares instead of removing them. If I were to write this as an algorithm, the steps would be as follows.

  1. Start with a square
  2. Calculate 1/3 scale of the square
  3. Place 8 squares of 1/3 scale around the initial square
  4. Repeat for new squares

Here are the generalised co-ordinates for generating the 8 squares:

 

C1: (xi + 2si, yi)

C2: (xi, yi + 2si)

C3: (xi - 2si, yi)

C4: (xi, yi - 2si)

C5: (xi + 2si, yi + 2si)

C6: (xi - 2si, yi - 2si)

C7: (xi + 2si, yi - 2si)

C8: (xi - 2si, yi + 2si)

 

where, xi and yi are the co-ordinates of the current square and si is the scale which you can take either from the x or y since they're the same, cause the object is a square.

If any of you can find a way of getting the subdivision method to work, I'd be interested in seeing it. If you really want a single plane with the squares cut out, you could probably still use the code I've attached and just Bool Tool the holes into a plane. Might need to extrude the planes out a bit first, and you could probably write up a script to make the holes instead of individually selecting objects and applying a Difference operation to them manually.

But, there you go, a fun little coding challenge and I've done the math for you so you just have to code it up. Note that the more iterations you specify, the more work Blender has to do so start off small in case your computer can't handle so much at once.

If you run into any issues or if you've made a mad little render with this, send me an email at the following address: steambeanblog@gmail.com