A simple fractal
2026-09-10
The Sierpiński triangle might be the simplest fractal there is. Take a triangle, cut out the triangle in its middle, and do the same to the three triangles that are left. Forever.
Want to try it? Grab sierpinski.py and run python3 sierpinski.py. It's plain Python with nothing to install, and it draws both pictures in this post.
The middle triangle's corners are the midpoints of the sides, so the whole thing is a few lines of recursion:
def midpoint(p, q):
return ((p[0] + q[0]) / 2, (p[1] + q[1]) / 2)
def sierpinski(a, b, c, depth):
"""The triangles left after cutting out the middle, depth times."""
if depth == 0:
return [(a, b, c)]
ab, bc, ca = midpoint(a, b), midpoint(b, c), midpoint(c, a)
return (sierpinski(a, ab, ca, depth - 1)
+ sierpinski(ab, b, bc, depth - 1)
+ sierpinski(ca, bc, c, depth - 1))
And to see it, turn every triangle into an SVG polygon:
def svg(triangles, size=300):
shapes = ''.join(
'<polygon points="' + ' '.join(f'{x:.1f},{y:.1f}' for x, y in t) + '" fill="#c9738f"/>'
for t in triangles)
return f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {size} {size}">{shapes}</svg>'
with open('sierpinski.svg', 'w') as f:
f.write(svg(sierpinski((150, 20), (10, 262), (290, 262), 7)))
Why it's strange
Every step keeps 3 triangles out of 4, so after n steps there are 3ⁿ triangles covering (3/4)ⁿ of the area. The number of triangles goes to infinity while the area goes to zero.
It isn't quite a line and it isn't quite a surface. Its dimension is log 3 / log 2 ≈ 1.585: shrink it by 2 and you get 3 copies of itself.
Something that looks the same no matter how close you get.