Sierpinski triangle
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.
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,bandcto itsitems, in that order. - Add a Make Polygon node (Objects → Polygon), connect the array to
its
vertices, and set itsfillto r 79, g 70, b 229. - Add a second Make Array and connect the polygon's
objectto itsitems: an array of one triangle, the same type astriangles.
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
tat 0.5: one fromatob, one frombtocand one fromctoa(Graph Input's points to theiraandb). Rename them ab, bc and ca (F2) to keep them apart. - Add a Subtract node (Math), connect
depthto itsaand setbto 1. - Add three Sierpinski nodes (Subgraph): the subgraph's own node,
offered inside its body. Connect Subtract's
resultto each one'sdepth, and their corners:- the top one:
a, ab, ca; - the right one: ab,
b, bc; - the left one: ca, bc,
c.
- the top one:
- Add a Concat node (Array) and connect the three nodes'
trianglesto itsarrays.

4. When to stop
- Add a Lower or Equals node (Logic) and connect
depthto itsa(bstays 0). - Add a Switch node (Logic). Connect Lower or Equals'
resultto itscondition, the one-triangle array tothen, and Concat'sarraytoelse. - Connect Switch's
resultto Graph Output'striangles.
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
- Make Array and Concat.
- Make Polygon and Lerp Point, on their types' pages.
- Subtract.
- Lower or Equals and Switch.
- Output.
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
arraystraight 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
- Subgraphs: recursion, base cases and limits.
- Subgraphs and functions.
- Triangles on a grid: a subgraph called by Map.