Home

My attempt at the

Sierpiński triangle

Today I decided to make an attempt at the Sierpiński triangle fractal using the Chaos Game algorithm.

The idea behind this algorithm is extraordinarily simple! Starting from a random point inside the triangle, repeatedly jump halfway to a random vertex. Despite the apparent randomness, a beautiful fractal pattern emerges.

How it works

  1. Take three points in a plane to form a triangle
  2. Randomly select any point inside the triangle (your starting position)
  3. Randomly select one of the three vertices
  4. Move halfway from your current position to the selected vertex
  5. Plot the new position
  6. Repeat from step 3

Implementation challenges

The trickiest part was determining if a randomly generated point falls inside the triangle. I used barycentric coordinates for this:

Given a point P and triangle vertices A, B, C, we calculate three weights (λ₁, λ₂, λ₃). If all three are non-negative, the point is inside!