Agile Coder Logo
AgileCoder
Intermediate

Sierpiński Triangle: Recursive Fractals in Code

Visualise the Sierpiński triangle through recursive geometry. A simple rule — divide, remove the centre — repeated until you can't see the individual pieces anymore.

Smruti Ranjan
May 20, 2026
3 min read
#fractals#generative-art#recursion#p5js
Sierpiński Triangle: Recursive Fractals in Code
p5.js sketch
Advertisement

The Sierpiensky Triangle, a captivating mathematical construct, has enthralled mathematicians, artists, and enthusiasts for over a century. Named after the Polish mathematician Wacław Sierpiński, this mesmerizing fractal showcases the mesmerizing beauty of self-similarity and infinite complexity.

At its core, the Sierpiensky Triangle is an equilateral triangle divided into four smaller congruent equilateral triangles. The process continues as each of these smaller triangles is further divided into four even tinier triangles, ad infinitum. The central triangle from each iteration is removed, leaving behind a pattern of triangles nested within triangles.

Algorithm to reproduce this pattern

  • Take a Square at the center
  • Then, add more square half the length of the previously taken square and place on any three sides of the squares.
  • Repeat this step over and over to get this pattern

We can use p5.js to code the pattern. p5 is a javascript library for drawing and animation. Here is the following code.

js
const setup = (canvasParentRef) => {
  createCanvas(500, 500).parent(canvasParentRef);
  background(255);
};

const drawRect = (x,y,d) => {
  noFill();
  rectMode(CENTER)
  rect(x, y, d, d);
  strokeWeight(0.5);
  
  if(d < 1) return;

  setTimeout(() => {
    drawRect(x, y - d/2, d/2);
  }, 10)

  setTimeout(() => {
    drawRect(x + d/2, y, d/2);
  }, 20)

  setTimeout(() => {
    drawRect(x - d/2, y, d/2);
  }, 30)
}

const draw = (p5) => {
  drawRect(width/2, height/2, 200)
}
↕ click to expand

Some of the facts about Sierpiensky Triangle

  1. Fractal Construction: The Sierpiński Triangle is a classic example of a fractal, a geometric shape that displays self-similarity at different scales. It is formed through an iterative process of subdividing an equilateral triangle into smaller triangles and removing the central triangle repeatedly.

  2. Named After Wacław Sierpiński: The Sierpiński Triangle is named after the Polish mathematician Wacław Sierpiński, who studied its properties and introduced it in 1915. Sierpiński made significant contributions to various areas of mathematics, and the Sierpiński Triangle remains one of his most famous works.

  3. Infinite Self-Similarity: At each iteration, the Sierpiński Triangle generates smaller copies of itself, and the pattern continues infinitely. No matter how much you zoom in or out, the structure looks similar, revealing an infinite level of detail.

  4. Fractal Dimension: The Sierpiński Triangle has a fractal dimension of approximately 1.585, which is a non-integer value between 1 and 2. Fractal dimensions represent the "degree of crinkliness" or "space-filling capacity" of a fractal object.

  5. Mathematical Representation: The Sierpiński Triangle can be represented using various mathematical notations, including recursive formulas and algorithms. One common approach is through the use of a chaotic dynamical system called the chaos game, which creates the fractal by randomly choosing points and repeatedly applying specific rules.

  6. Simple Construction Rules: Despite its complex and mesmerizing appearance, the Sierpiński Triangle is constructed through straightforward rules. Starting with an equilateral triangle, removing the central triangle at each iteration generates the intricate pattern.

  7. Popularity in Education and Art: The Sierpiński Triangle's aesthetic appeal and simplicity in construction have made it a popular subject in educational settings. It serves as an excellent introduction to fractals and recursive algorithms for students of mathematics and computer science. Moreover, the triangle's mesmerizing patterns have inspired artists to create captivating artworks based on fractal geometry.

  8. Variations and Extensions: The Sierpiński Triangle has several variations and extensions, such as the Sierpiński Arrowhead Curve and the Sierpiński Carpet. These variations involve similar principles of self-replication and showcase the versatility of fractal geometry.

  9. Connections to Chaos Theory: The Sierpiński Triangle is connected to chaos theory, a branch of mathematics that studies complex and unpredictable systems. The chaotic nature of the Sierpiński Triangle's construction using the chaos game makes it an intriguing subject in this field.

  10. Real-World Fractals: Fractal patterns similar to the Sierpiński Triangle are found in various natural and artificial phenomena, such as the branching of trees, coastlines, and certain types of antennas. These real-world examples highlight the ubiquity and significance of fractals in nature and human creations.

Overall, the Sierpiński Triangle is an enchanting and fundamental example of fractal geometry, displaying infinite self-similarity and offering insights into the complexity and beauty of mathematics and the natural world.

Advertisement

Behind-the-build, in your inbox.

New tutorials, book updates, and behind-the-scenes notes from the studio. No schedule, no spam.

Comments

Leave a comment

Comments are moderated before appearing.