Skip to content

Patterns that Control Shapes

Every pixel pattern can drive shapes too: work out its value at the centre of each grid cell and use it for a size, an angle or a choice.

How it works

Instead of a colour for every pixel, loop over a grid and ask the pattern for one value per cell, at the cell's centre. Then use that value for something about a shape: a circle's size (a newspaper halftone), a line's angle (iron filings, hair), or which of two tiles to draw.

The patterns are the same functions as in pixel sketches. The pixel examples are worked out on a 300 × 300 picture and the shapes starter is 600 × 600, so plasma(x / 2, y / 2, ...) asks for the same place in the pattern.

The C is for the raylib shapes starter.

Halftone: plasma as circle size

static void draw(float t)
{
    clear(PAPER);
    for (int y = 10; y < H; y += 20)
        for (int x = 10; x < W; x += 20) {
            float v = 0.5f + 0.5f * plasma(x / 2.0f, y / 2.0f, 150, 150);   /* 0..1 */
            DrawCircle(x, y, v * 11, INK);
        }
}
function draw(t) {
  clear(PAPER);
  for (let y = 10; y < H; y += 20)
    for (let x = 10; x < W; x += 20) {
      let v = 0.5 + 0.5 * plasma(x / 2, y / 2, 150, 150);   // 0..1
      DrawCircle(x, y, v * 11, INK);
    }
}

Noise as the angle of each line

static void draw(float t)
{
    clear(PAPER);
    for (int y = 20; y < H; y += 30)
        for (int x = 20; x < W; x += 30) {
            float a = noise2(x * 0.005f, y * 0.005f) * 4 * PI;
            Vector2 d = { 12 * cosf(a), 12 * sinf(a) };
            DrawLineEx((Vector2){ x - d.x, y - d.y }, (Vector2){ x + d.x, y + d.y }, 4, INK);
        }
}
function draw(t) {
  clear(PAPER);
  for (let y = 20; y < H; y += 30)
    for (let x = 20; x < W; x += 30) {
      let a = noise2(x * 0.005, y * 0.005) * 4 * PI;
      let d = Vector2(12 * cosf(a), 12 * sinf(a));
      DrawLineEx(Vector2(x - d.x, y - d.y), Vector2(x + d.x, y + d.y), 4, INK);
    }
}

10 PRINT chosen by XOR

The XOR of the cell's column and row numbers chooses the direction, so the maze becomes a regular weave.

static void draw(float t)
{
    const int s = 30;
    clear(PAPER);
    for (int j = 0; j < H / s; j++)
        for (int i = 0; i < W / s; i++) {
            float x = i * s, y = j * s;
            if ((i ^ j) % 3 == 0) DrawLineEx((Vector2){ x, y }, (Vector2){ x + s, y + s }, 6, INK);
            else DrawLineEx((Vector2){ x + s, y }, (Vector2){ x, y + s }, 6, INK);
        }
}
function draw(t) {
  const s = 30;
  clear(PAPER);
  for (let j = 0; j < H / s; j++)
    for (let i = 0; i < W / s; i++) {
      let x = i * s, y = j * s;
      if ((i ^ j) % 3 === 0) DrawLineEx(Vector2(x, y), Vector2(x + s, y + s), 6, INK);
      else DrawLineEx(Vector2(x + s, y), Vector2(x, y + s), 6, INK);
    }
}

A moving halftone

Moving the plasma's centre with time makes the dots swell and shrink in waves.

static void draw(float t)
{
    clear(PAPER);
    for (int y = 10; y < H; y += 20)
        for (int x = 10; x < W; x += 20) {
            float v = 0.5f + 0.5f * plasma(x / 2.0f + t * 10, y / 2.0f, 150 + 60 * sinf(t * 0.5f), 150);
            DrawCircle(x, y, v * 11, toColor(sea(v)));
        }
}
function draw(t) {
  clear(PAPER);
  for (let y = 10; y < H; y += 20)
    for (let x = 10; x < W; x += 20) {
      let v = 0.5 + 0.5 * plasma(x / 2 + t * 10, y / 2, 150 + 60 * sinf(t * 0.5), 150);
      DrawCircle(x, y, v * 11, toColor(sea(v)));
    }
}