Nilo

Examples10

frost

A still plate. Sixteen thousand specks fall one at a time, drifting a square to either side as they go, and each stops where it first touches the growth. A short twig joins it to the square it caught on. The early twigs are heavy and dark and the late ones fine and pale, so the growth thins as it climbs.

A branching growth of short right-angled twigs rising from the foot of a pale sheet, heavy and dark green at the base and thinning to fine grey at the top

SourceShips asGzippedTarget
41 lines1,345 bytes890 bytessvg
// frost: specks fall, drifting a little to either side, and stick where they first touch the growth
plate(1920, 2400)
use colour v2
random layout, fall, ink

let cell = 9                                    // one square of the grid, in plate units
let cols = 213, rows = 266
let taken = int[cols * rows]                    // 1 where the growth is
let paper = #efe9dd
let deep = [#1d1a18, #1b2a4e, #5a1a1f, #24382e][ink.int(0 ..< 4)]
let ramp = [deep, colour.mix(deep, toward: paper, amount: 0.4), colour.mix(deep, toward: paper, amount: 0.7)]
let lean = layout.float(-0.12 ..< 0.12)         // the same sideways lean for every speck
// the ground that catches the first specks: a short stretch of the foot of the plate
let from = layout.int(70 ..< 120), to = from + layout.int(20 ..< 60)
for x in from ..< to { taken[(rows - 1) * cols + x] = 1 }

var top = rows - 2
let count = 16000
background(paper)
for n in 0 ..< count {
  var x = fall.int(from - 50 ..< to + 50), y = max(top - 10, 1)
  while y < rows - 1 {
    // the squares it could catch on: right, left, and below, which wins
    let below = (y + 1) * cols + x, left = y * cols + x - 1, right = y * cols + x + 1
    var hit = -1
    if taken[right] == 1 { hit = right }
    if taken[left] == 1 { hit = left }
    if taken[below] == 1 { hit = below }
    if hit >= 0 {
      taken[y * cols + x] = 1
      top = min(top, y)
      // a twig from this square to the one it caught on, heavier and darker near the ground
      let at = vec(x, y) * cell, onto = vec(hit % cols, floor(hit / cols)) * cell
      let age = n / count
      polyline([at, onto], stroke: colour.at(ramp, along: age), width: 4.5 - 3 * age, caps: "round")
      break
    }
    // one row down, and perhaps a square to the side
    let side = fall.unit() + lean
    x = clamp(x + (side < 0.33 ? -1 : 0) + (side > 0.67 ? 1 : 0), 1, cols - 2)
    y += 1
  }
}