Sierpinski triangle

A triangle made of three half-size triangles, each made the same way: a subgraph that calls itself, stopped by a Switch.

A triangle split into three smaller triangles, each of them split the same way, five times over: 243 triangles. One subgraph does it, by calling itself on each third, with a depth that goes down by one on every call. At depth 0 it stops and draws a single triangle.

A Sierpinski triangle of 243 small indigo trianglesOpen in Playground

You'll use: a subgraph, "Sierpinski" (Graph Input, Make Array, Make Polygon, Lerp Point, Subtract, three Sierpinski nodes, Concat, Lower or Equals, Switch, Graph Output), Output.

Steps

1. The subgraph

Open the Subgraphs tab next to the inspector and click New subgraph. Rename it Sierpinski (double-click its name, type, press Enter). Its body opens: the breadcrumb reads Root › Sierpinski.

Select Graph Input and click Add param four times. Name the params and set their types:

  • depth: number;
  • a, b, c: Point (the triangle's corners).

Select Graph Output. Click Add result, name it triangles, and set its type to Polygon with Array<…>: the subgraph gives back all the triangles it made.

2. The base case: one triangle

At depth 0, the subgraph draws the triangle a, b, c.

  • Add a Make Array node (Array) and connect Graph Input's a, b and c to its items, in that order.
  • Add a Make Polygon node (Objects → Polygon), connect the array to its vertices, and set its fill to r 79, g 70, b 229.
  • Add a second Make Array and connect the polygon's object to its items: an array of one triangle, the same type as triangles.

3. Three smaller triangles

The midpoints of the sides split the triangle into three corner triangles, half as big.

  • Add three Lerp Point nodes (Objects → Point), each with t at 0.5: one from a to b, one from b to c and one from c to a (Graph Input's points to their a and b). Rename them ab, bc and ca (F2) to keep them apart.
  • Add a Subtract node (Math), connect depth to its a and set b to 1.
  • Add three Sierpinski nodes (Subgraph): the subgraph's own node, offered inside its body. Connect Subtract's result to each one's depth, and their corners:
    • the top one: a, ab, ca;
    • the right one: ab, b, bc;
    • the left one: ca, bc, c.
  • Add a Concat node (Array) and connect the three nodes' triangles to its arrays.

4. When to stop

  • Add a Lower or Equals node (Logic) and connect depth to its a (b stays 0).
  • Add a Switch node (Logic). Connect Lower or Equals' result to its condition, the one-triangle array to then, and Concat's array to else.
  • Connect Switch's result to Graph Output's triangles.

At depth 0, Switch takes then and draws one triangle. Above it, Switch takes else: three Sierpinski calls with depth − 1. Switch evaluates only the branch it picks, so at depth 0 the three calls aren't made.

5. Drawing it

Click Root in the breadcrumb. Add a Sierpinski node and set:

  • depth: 5;
  • a: x 500, y 40;
  • b: x 960, y 760;
  • c: x 40, y 760.

Connect its triangles to the Output node's objects.

How it works

The Sierpinski node in the main graph calls the subgraph with depth 5. That call makes three calls with depth 4, each of which makes three with depth 3, and so on, down to depth 0: 3⁵ = 243 calls draw a triangle each, and every level joins the triangles of its three calls with Concat.

Each call gets its own corners, half the size of its caller's, so the triangles at the bottom are 1/32 of the big one.

Nodes used

Variations

  • Deeper or shallower. Each level triples the number of calls: depth 8 makes 9 841 calls, the most that fits in the limit of 10 000; depth 9 fails with "More than 10 000 calls". See Limits.
  • No base case. Connect Concat's array straight to Graph Output, without the Switch: the calls never stop, and the Sierpinski node in the main graph fails with "Recursion deeper than 64 calls: check the base case".
  • A colour per triangle. Add a Random Color node to the body and connect it to the polygon's fill. Every call draws its own colour, so each of the 243 triangles gets its own.

See also

© 2026 Wirepaint