Skip to content

Recursive Shapes

A function that draws something, then calls itself with smaller values: trees, Mondrian-like blocks, spirals of squares.

How it works

A recursive drawing function does a little drawing and then calls itself, once or more, with a smaller size. It must be given a depth that counts down to 0 and stops there, or it never ends (and in C, crashes when the stack runs out).

A branch that draws a line and then two smaller branches at its end, turned a little left and right, gives a tree. Depth 9 with two calls each is 2⁹ = 512 twigs; every extra level doubles the work.

Cutting a rectangle in two at a random point, then cutting each half, gives blocks like a Mondrian painting. Stopping early at random leaves some large blocks among the small ones; the first two cuts always happen, so the picture is never one block.

The same idea with one call per level and a turn each time gives nested squares, each smaller and turned a little more.

The C is for the raylib shapes starter; a function the sketch calls goes above draw().

A tree

static void branch(float x, float y, float angle, float len, int depth)
{
    if (depth == 0) return;
    float x2 = x + len * cosf(angle), y2 = y + len * sinf(angle);
    DrawLineEx((Vector2){ x, y }, (Vector2){ x2, y2 }, depth * 1.2f, INK);
    branch(x2, y2, angle - 0.45f, len * 0.72f, depth - 1);
    branch(x2, y2, angle + 0.45f, len * 0.72f, depth - 1);
}

static void draw(float t)
{
    clear(PAPER);
    branch(W / 2, H - 20, -PI / 2, 140, 9);
}
function branch(x, y, angle, len, depth) {
  if (depth === 0) return;
  let x2 = x + len * cosf(angle), y2 = y + len * sinf(angle);
  DrawLineEx(Vector2(x, y), Vector2(x2, y2), depth * 1.2, INK);
  branch(x2, y2, angle - 0.45, len * 0.72, depth - 1);
  branch(x2, y2, angle + 0.45, len * 0.72, depth - 1);
}

function draw(t) {
  clear(PAPER);
  branch(W / 2, H - 20, -PI / 2, 140, 9);
}

Cutting a rectangle again and again

static void divide(float x, float y, float w, float h, int depth)
{
    if (depth == 0 || (depth < 4 && frand(0, 1) < 0.15f)) {
        Color fills[] = { PAPER, PAPER, PAPER, { 220, 50, 40, 255 }, { 30, 60, 160, 255 }, { 240, 200, 40, 255 } };
        DrawRectangleRec((Rectangle){ x, y, w, h }, fills[GetRandomValue(0, 5)]);
        DrawRectangleLinesEx((Rectangle){ x, y, w, h }, 6, INK);
        return;
    }
    if (w > h) {
        float k = frand(0.3f, 0.7f) * w;
        divide(x, y, k, h, depth - 1);
        divide(x + k, y, w - k, h, depth - 1);
    } else {
        float k = frand(0.3f, 0.7f) * h;
        divide(x, y, w, k, depth - 1);
        divide(x, y + k, w, h - k, depth - 1);
    }
}

static void draw(float t)
{
    divide(0, 0, W, H, 5);
}
function divide(x, y, w, h, depth) {
  if (depth === 0 || (depth < 4 && frand(0, 1) < 0.15)) {
    const fills = [PAPER, PAPER, PAPER, Color(220, 50, 40, 255), Color(30, 60, 160, 255), Color(240, 200, 40, 255)];
    DrawRectangleRec(Rectangle(x, y, w, h), fills[GetRandomValue(0, 5)]);
    DrawRectangleLinesEx(Rectangle(x, y, w, h), 6, INK);
    return;
  }
  if (w > h) {
    let k = frand(0.3, 0.7) * w;
    divide(x, y, k, h, depth - 1);
    divide(x + k, y, w - k, h, depth - 1);
  } else {
    let k = frand(0.3, 0.7) * h;
    divide(x, y, w, k, depth - 1);
    divide(x, y + k, w, h - k, depth - 1);
  }
}

function draw(t) {
  divide(0, 0, W, H, 5);
}

Nested turning squares

A square's corners are a radius of size / √2 from its centre, so DrawPolyLines with 4 sides draws it turned by any angle.

static void nest(float size, float turn, int depth)
{
    if (depth == 0) return;
    DrawPolyLinesEx((Vector2){ W / 2, H / 2 }, 4, size / sqrtf(2), 45 + turn, 2, INK);
    nest(size * 0.92f, turn + 0.1f * RAD2DEG, depth - 1);
}

static void draw(float t)
{
    clear(PAPER);
    nest(520, 0, 40);
}
function nest(size, turn, depth) {
  if (depth === 0) return;
  DrawPolyLinesEx(Vector2(W / 2, H / 2), 4, size / sqrtf(2), 45 + turn, 2, INK);
  nest(size * 0.92, turn + 0.1 * RAD2DEG, depth - 1);
}

function draw(t) {
  clear(PAPER);
  nest(520, 0, 40);
}