# Simbiosis

a hot wall, a wall of bricks, and a cool plane with lines ruled in it, each line with its halo, after Gunther Gerzso's Simbiosis (1996).

Run it with `swift run --package-path Examples Example-Recreations-GuntherGerzso-Simbiosis`.

```swift
//  Recreation after Gunther Gerzso - Simbiosis (Symbiosis), 1996, oil on
//  pressed wood panel, 64 x 53.4 cm, Princeton University Art Museum, bequest
//  of David L. Meginnity, Class of 1958. A homage, not a reproduction, and
//  not affiliated with or endorsed by the artist's estate or the museum.
//  https://artmuseum.princeton.edu/art/collections/objects/40092
//  https://bombmagazine.org/articles/gunther-gerzso/
//
//  An original Ollin interpretation written from the museum's picture of the
//  painting and from Gerzso's conversation with José Antonio Aldrete-Haas,
//  where he describes the ruled lines, their halos, and how a painting
//  begins. Nothing was ported: the work is oil on a wood panel.

import Foundation
import Ollin

/// Simbiosis (Gunther Gerzso, 1996). A late painting, and a hot one. Two
/// thirds of the panel is one orange wall, worked so that a yellow lies
/// broken over a red. Below it is a wall of another kind: bricks of red,
/// ocher, and yellow with a few thin blue ones, each darker along its top.
/// A cool plane is set into the bricks, blue at its rim and brown in the
/// middle. A pale square hangs in the orange, with a slit down it and a
/// wedge below. A smaller square sits inside the cool plane on a ruled
/// line.
///
/// The lines are what he talked about. He traced one with a ruler and India
/// ink, felt something was missing, and put a halo around it to make it
/// easier to see. He did not call the halo light, and he did not call his
/// large surfaces atmospheres: for him they were a flat wall. So here every
/// plane is one flat coat, its color broken by a second and lighter at its
/// rim. Every ruled line is dark and thin, with a soft band of color beside
/// it.
///
/// He also said how a painting begins. He draws many lines on the canvas,
/// and the next day he chooses what stays. Aldrete-Haas adds that the lines
/// follow the golden section, and Gerzso that he traces a rectangle with
/// its two diagonals. The cycle here starts from that drawing. The panel
/// is ruled with the golden cuts and the diagonals, and the outlines that
/// stay are drawn. Then the paint goes on in thin coats: the orange first,
/// the bricks one by one, the planes, and the ruled lines last. The places
/// of the parts are read off the picture and moved to the nearest cut. The
/// painting's own lines do not all sit on such cuts.
///
/// Try it: `lines` is how many lines are ruled in the cool plane, and
/// `halo` how strong their halos and the planes' rims are. `courses` is how
/// finely the brick wall is divided. `hue` turns the whole palette around
/// the color wheel, `grain` is how broken the paint is, and `guides` rules
/// the cuts over the finished picture. The seed deals the painting, and a
/// press deals the next.
@main
final class Simbiosis: Sketch {
    /// How many lines are ruled inside the cool plane.
    @Param(0 ... 8, icon: "line.diagonal") var lines = 5
    /// How strong the halos beside the lines and the rims of the planes are.
    @Param(0 ... 1, icon: "sun.max") var halo = 0.7
    /// How many times the brick wall is cut, each cut on a golden section.
    @Param(2 ... 7, icon: "square.grid.3x2") var courses = 5
    /// The whole palette turned around the color wheel, in degrees.
    @Param(-180 ... 180, icon: "paintpalette") var hue = 0.0
    /// How broken and streaked the paint is.
    @Param(0 ... 1, icon: "circle.dotted") var grain = 0.7
    /// The golden cuts and the two diagonals, ruled over the picture.
    @Param(icon: "grid") var guides = false
    /// One cycle: the painting, taken back to its drawing, and painted again.
    @Param(8 ... 30, icon: "clock") var seconds = 24.0

    // The panel is 53.4 by 64 centimeters.
    override var canvasSize: CanvasSize { .size(900, 1080) }
    override var loopDuration: Double? { seconds }

    // MARK: - The golden cuts

    /// The golden cut of a length, and the cuts of the cuts: 0.618, 0.382,
    /// 0.236, 0.146, 0.090, 0.056, 0.034.
    nonisolated static let cuts: [Double] = (1 ... 7).map { pow((5.0.squareRoot() - 1) / 2, Double($0)) }

    /// The lines ruled on the bare panel: each side cut at 0.382 and 0.618,
    /// and each of the three parts cut the same way again.
    nonisolated static let lattice: [Double] = {
        let a = cuts[1], b = cuts[0]
        var marks: [Double] = []
        for (from, to) in [(0.0, a), (a, b), (b, 1.0)] {
            marks += [from + (to - from) * a, from + (to - from) * b, to]
        }
        return marks.dropLast().sorted()
    }()

    // MARK: - The parts

    /// One painted plane. It has an outline, a color for its middle, a
    /// color its rim turns to, and a second color broken over it. It also
    /// has a darkness along its top, a soft dark it throws around itself,
    /// and a place in the order of painting.
    private struct Part {
        var outline: [Vector2]
        var inner: Color
        var rim: Color
        var second: Color
        var rimAmount: Double
        var rimWidth: Double
        var shade: Double
        var shadow: Double
        var order: Double
        var seed: Double
        var outlined = true
    }

    /// One ruled line: where it runs, its ink, the color of its halo, which
    /// side the halo lies on, and when it is ruled.
    private struct Rule {
        var from: Vector2
        var to: Vector2
        var ink: Color
        var glow: Color
        /// 1 puts the halo on the line's right-hand side, -1 on its left, 0
        /// on both.
        var side: Double
        var order: Double
    }

    private struct Painting {
        var parts: [Part] = []
        var rules: [Rule] = []
    }

    private let ground = Color(hex: 0x1F100D)
    private let chalk = Color(hex: 0xE2CDA6)

    private var painting = Painting()
    private var built = (seed: Int.min, lines: -1, courses: -1, hue: 0.0, width: 0.0, height: 0.0)
    private var pressed = 0

    override func mousePressed() {
        pressed += 1
    }

    // MARK: - Dealing a painting

    /// Deal one painting. Positions are parts of the width and the height
    /// until `at` turns them into pixels, mirrored for half the seeds.
    private func deal(seed: Int) -> Painting {
        var rng = SplitMix64(seed: UInt64(bitPattern: Int64(seed &* 1_000_003 &+ 1996)))
        func roll(_ range: ClosedRange<Double>) -> Double { Double.random(in: range, using: &rng) }
        func pick<T>(_ options: [T]) -> T { options[Int.random(in: 0 ..< options.count, using: &rng)] }
        func coin(_ p: Double) -> Bool { Double.random(in: 0 ..< 1, using: &rng) < p }
        let w = width, h = height
        let c = Simbiosis.cuts
        let mirrored = coin(0.5)
        func at(_ u: Double, _ v: Double) -> Vector2 { Vector2((mirrored ? 1 - u : u) * w, v * h) }
        let turn = hue
        func tone(_ hex: UInt32) -> Color {
            guard turn != 0 else { return Color(hex: hex) }
            var polar = OKLCH(Color(hex: hex))
            polar.h += turn
            return Color(polar)
        }
        var result = Painting()
        var grainSeed = roll(0 ... 40)
        func add(_ points: [(Double, Double)], inner: UInt32, rim: UInt32, second: UInt32, rimAmount: Double,
                 rimWidth: Double, shade: Double = 0, shadow: Double = 0, order: Double, outlined: Bool = true) {
            grainSeed += 1.37
            result.parts.append(Part(outline: points.map { at($0.0, $0.1) }, inner: tone(inner), rim: tone(rim),
                                     second: tone(second), rimAmount: rimAmount, rimWidth: rimWidth, shade: shade,
                                     shadow: shadow, order: order, seed: grainSeed, outlined: outlined))
        }
        func rule(_ from: (Double, Double), _ to: (Double, Double), ink: UInt32, glow: UInt32, side: Double,
                  order: Double) {
            // A mirrored line has its sides the other way round.
            result.rules.append(Rule(from: at(from.0, from.1), to: at(to.0, to.1), ink: tone(ink), glow: tone(glow),
                                     side: mirrored ? -side : side, order: order))
        }

        // The orange wall ends on a long line that bends once.
        let low = pick([0.652, 0.66, 0.668])
        let bend = pick([0.472, 0.528, c[0]])
        let bent = low + 0.026, far = bent - 0.003

        // The brick wall: the lower third, cut again and again on a golden
        // section, across whichever way a piece is longer.
        let reds: [(UInt32, UInt32)] = [(0x8A2A1C, 0xA8452A), (0x7B2013, 0x9C3A22), (0xA04428, 0xB85A34),
                                        (0x8D5620, 0xA9702C), (0xB47E3A, 0xC79646), (0x9C7E37, 0xB79848)]
        let rare: [(UInt32, UInt32)] = [(0x4E6995, 0x7391B0), (0x40567A, 0x6A88AC), (0xA58C5B, 0xC2AC7A),
                                        (0x541C19, 0x6E2A22)]
        var bricks: [(Double, Double, Double, Double)] = []
        func cut(_ x: Double, _ y: Double, _ bw: Double, _ bh: Double, _ depth: Int) {
            let wide = bw * w, tall = bh * h
            if depth == 0 || min(wide, tall) < 30 || (depth <= courses - 2 && coin(0.16)) {
                bricks.append((x, y, bw, bh))
                return
            }
            let part = coin(0.5) ? c[1] : c[0]
            var across = wide > 1.5 * tall
            if coin(0.2) { across.toggle() }
            if across, wide * min(part, 1 - part) >= 26 {
                cut(x, y, bw * part, bh, depth - 1)
                cut(x + bw * part, y, bw * (1 - part), bh, depth - 1)
            } else if tall * min(part, 1 - part) >= 16 {
                cut(x, y, bw, bh * part, depth - 1)
                cut(x, y + bh * part, bw, bh * (1 - part), depth - 1)
            } else {
                bricks.append((x, y, bw, bh))
            }
        }
        // One dark red course runs the whole width right under the orange,
        // and the cutting starts below it.
        let course = bent + 0.012
        bricks.append((0, low - 0.012, 1, course - low + 0.012))
        cut(0, course, 1, 1 - course, courses)
        for (index, brick) in bricks.enumerated() {
            let thin = brick.3 * h < 34 || brick.2 * w < 40
            let color = index == 0 ? reds[1] : coin(thin ? 0.5 : 0.1) ? pick(rare) : pick(reds)
            // A pixel is left between bricks, and the dark ground shows there.
            let gx = 1 / w, gy = 1 / h
            add([(brick.0 + gx, brick.1 + gy), (brick.0 + brick.2 - gx, brick.1 + gy),
                 (brick.0 + brick.2 - gx, brick.1 + brick.3 - gy), (brick.0 + gx, brick.1 + brick.3 - gy)],
                inner: color.0, rim: color.1, second: color.1, rimAmount: 0.35, rimWidth: 9, shade: 0.42,
                order: 0.2 + 0.36 * Double(index) / Double(max(1, bricks.count)), outlined: false)
        }

        // The orange wall, over the top of the bricks.
        add([(0, 0), (1, 0), (1, far), (bend, bent), (0, low)],
            inner: 0x9C401A, rim: 0xB8742E, second: 0xB26A26, rimAmount: 0, rimWidth: 60, shade: 0.16, shadow: 13,
            order: 0.02, outlined: false)
        // The long line that starts inside the orange and runs down to its
        // lower edge at the far side.
        let start = bend - pick([c[4], c[3], 0.118])
        rule((start, low + 0.026 * start / bend - 0.02), (0.985, far - 0.002), ink: 0x2A0E08, glow: 0xD79A40, side: 1,
             order: 0.9)

        // The stepped block at the far edge, standing on a pale bar.
        let barTop = far - 0.052
        let blockTop = pick([0.472, 0.49, 0.528])
        let steps = [blockTop, blockTop + (barTop - blockTop) * c[0], blockTop + (barTop - blockTop) * 0.86, barTop]
        for index in 0 ..< 3 {
            let lean = 0.012 * Double(index)
            add([(0.905 - lean, steps[index]), (1, steps[index] - 0.004), (1, steps[index + 1]),
                 (0.893 - lean, steps[index + 1])],
                inner: [0x8A2C22, 0x7A251D, 0x93342A][index], rim: 0xA84A36, second: 0x9A3A2A, rimAmount: 0.3,
                rimWidth: 10, shade: 0.35, shadow: 6, order: 0.6 + 0.02 * Double(index))
        }
        let barLeft = pick([0.708, 0.747, c[0] + c[3]])
        add([(barLeft, barTop), (1, barTop), (1, barTop + 0.033), (barLeft, barTop + 0.033)],
            inner: 0xA99A89, rim: 0xC4B9A2, second: 0xB3A28A, rimAmount: 0.6, rimWidth: 9, shadow: 7, order: 0.66)

        // The cool plane set into the bricks, with a notch cut from its top.
        let planeLeft = pick([c[4], c[4], c[3]]), planeRight = pick([c[0] + c[3], 0.708, c[0] + c[2]])
        let planeTop = bent + pick([0.016, 0.022, c[6]]), planeBottom = pick([1 - c[4], 0.9, 0.887])
        let pw = planeRight - planeLeft, ph = planeBottom - planeTop
        var outline: [(Double, Double)] = [(planeLeft, planeTop)]
        if coin(0.8) {
            let notch = planeLeft + pw * pick([0.13, 0.2, c[2]])
            outline += [(notch, planeTop), (notch + 0.095, planeTop + ph * 0.15), (notch + 0.095, planeTop)]
        }
        outline += [(planeRight, planeTop), (planeRight, planeBottom)]
        if coin(0.5) {
            let step = planeRight - pw * pick([c[2], c[1]])
            outline += [(step, planeBottom), (step, planeBottom - ph * 0.07), (planeLeft + pw * 0.3, planeBottom - ph * 0.07),
                        (planeLeft + pw * 0.3, planeBottom)]
        }
        outline.append((planeLeft, planeBottom))
        add(outline, inner: 0x7D502B, rim: 0x7391B0, second: 0x96693A, rimAmount: 1, rimWidth: 30, shadow: 16,
            order: 0.7)
        // Thin bars of another color lie along some of its edges.
        let bar = 0.0145
        if coin(0.75) {
            add([(planeLeft, planeTop), (planeLeft + bar, planeTop), (planeLeft + bar, planeBottom),
                 (planeLeft, planeBottom)],
                inner: 0x7791AF, rim: 0x9AB4CC, second: 0x7F9AB8, rimAmount: 0.4, rimWidth: 5, order: 0.76)
        }
        if coin(0.7) {
            add([(planeRight - pw * 0.25, planeTop), (planeRight, planeTop), (planeRight, planeTop + bar),
                 (planeRight - pw * 0.25, planeTop + bar)],
                inner: 0x618165, rim: 0x86A486, second: 0x6C8C6E, rimAmount: 0.4, rimWidth: 5, order: 0.77)
        }
        if coin(0.7) {
            add([(planeRight - pw * 0.25, planeBottom - bar), (planeRight, planeBottom - bar), (planeRight, planeBottom),
                 (planeRight - pw * 0.25, planeBottom)],
                inner: 0x84671E, rim: 0xB0902E, second: 0x94752A, rimAmount: 0.4, rimWidth: 5, order: 0.78)
        }

        // The small square in the cool plane, and the lines ruled around it.
        let seat = planeLeft + pw * pick([c[1], 0.5, c[0]])
        let seatTop = planeTop + ph * 0.33
        add([(seat, seatTop), (seat + 0.078, seatTop), (seat + 0.078, seatTop + 0.0585), (seat, seatTop + 0.0585)],
            inner: 0xA6937A, rim: 0xC0B499, second: 0xB09C80, rimAmount: 0.7, rimWidth: 8, shadow: 7, order: 0.97)
        let seatLine = seatTop + 0.0585 * 0.8
        var ruled: [((Double, Double), (Double, Double), UInt32, Double)] = [
            ((planeLeft + pw * 0.25, seatLine), (planeLeft + pw * 0.87, seatLine), 0xB9A23A, 1),
            ((planeLeft + pw * 0.15, planeTop + ph * 0.25), (planeLeft + pw * 0.12, planeTop + ph * 0.66), 0x9DB8D6, 0),
            ((planeLeft + pw * 0.42, planeTop + ph * 0.8), (planeLeft + pw * 0.62, planeTop + ph * 0.8), 0xB9C4CC, 1),
            ((planeLeft + pw * 0.8, planeTop + ph * 0.67), (planeLeft + pw * 0.95, planeTop + ph * 0.67), 0xB9A23A, 1),
            ((planeLeft + pw * 0.87, planeTop + ph * 0.36), (planeLeft + pw * 0.84, planeTop + ph * 0.64), 0x9DB8D6, 0),
        ]
        // Past the five read off the painting, more are ruled on the cuts.
        while ruled.count < 8 {
            let v = planeTop + ph * pick([c[2], c[1], c[0], 0.72])
            let from = planeLeft + pw * roll(0.06 ... 0.6)
            ruled.append(((from, v), (from + pw * roll(0.1 ... 0.3), v), pick([0xB9A23A, 0x9DB8D6, 0xB9C4CC]), 1))
        }
        for (index, line) in ruled.prefix(lines).enumerated() {
            let nudge = roll(-0.012 ... 0.012)
            rule((line.0.0 + nudge, line.0.1), (line.1.0 + nudge, line.1.1), ink: 0x16161E, glow: line.2, side: line.3,
                 order: 0.92 + 0.008 * Double(index))
        }

        // The pale square in the orange, its slit, and the wedge under it.
        let squareLeft = pick([c[2] - c[6], c[3], 0.326, 0.44])
        let squareTop = pick([c[3], c[3], c[2]]), squareBottom = squareTop + 0.163
        let slit = squareLeft + 0.2173 * pick([0.46, 0.5, c[0]])
        add([(squareLeft, squareTop), (squareLeft + 0.2173, squareTop), (squareLeft + 0.2173, squareBottom),
             (squareLeft, squareBottom)],
            inner: 0xB48F5D, rim: 0xCFC49A, second: 0xC2A473, rimAmount: 0.95, rimWidth: 24, shadow: 15, order: 0.84)
        add([(slit - 0.03, squareBottom - 0.002), (slit, squareBottom - 0.002), (slit - 0.013, squareBottom + 0.078)],
            inner: 0xC2AE82, rim: 0xCFC49A, second: 0xC8B68C, rimAmount: 0.5, rimWidth: 8, shadow: 5, order: 0.87,
            outlined: false)
        rule((slit, squareTop + 0.163 * 0.46), (slit - 0.004, squareBottom + 0.045), ink: 0x2A0E08, glow: 0xE6DCB4,
             side: 1, order: 0.9)
        return result
    }

    // MARK: - The paint

    /// One plane's paint, put on as a coat that can be thin. The second
    /// color is broken over the first in small patches. Toward the plane's
    /// edge the color turns to the rim's, by an uneven margin. The top of the
    /// plane is darker, where the plane above it throws its shade. The
    /// outline rides along as numbers so the paint can measure the distance
    /// to the nearest edge.
    private static let paint = """
    float4 shade(float2 uv, ShaderInfo info) {
        float3 inner = float3(param(info, 0), param(info, 1), param(info, 2));
        float3 rim = float3(param(info, 3), param(info, 4), param(info, 5));
        float3 second = float3(param(info, 6), param(info, 7), param(info, 8));
        float top = param(info, 9);
        float bottom = param(info, 10);
        float seed = param(info, 11);
        float grain = param(info, 12);
        float rimAmount = param(info, 13);
        float rimWidth = param(info, 14);
        float shadeAmount = param(info, 15);
        float coat = param(info, 16);
        int count = int(param(info, 17));
        float2 p = info.position;
        float edge = 1.0e6;
        for (int i = 0; i < count; i++) {
            int j = (i + 1) % count;
            float2 a = float2(param(info, 18 + 2 * i), param(info, 19 + 2 * i));
            float2 b = float2(param(info, 18 + 2 * j), param(info, 19 + 2 * j));
            float2 ab = b - a;
            float along = clamp(dot(p - a, ab) / max(dot(ab, ab), 1.0e-6), 0.0, 1.0);
            edge = min(edge, length(p - a - ab * along));
        }
        float broken = 0.46 * fbm(p / 2.6 + seed * 3.7) + 0.30 * fbm(p / 13.0 + seed * 8.9)
            + 0.24 * fbm(p / 120.0 + seed * 1.3);
        float3 color = mix(inner, second, grain * smoothstep(0.36, 0.66, broken));
        float uneven = 1.0 + 0.9 * (fbm(p / 24.0 + seed * 5.1) - 0.5);
        color = mix(color, rim, clamp(rimAmount * exp(-edge / max(rimWidth * uneven, 1.0)), 0.0, 1.0));
        float run = clamp((p.y - top) / max(bottom - top, 1.0), 0.0, 1.0);
        color *= mix(1.0 - shadeAmount, 1.0, smoothstep(0.0, 0.8, run));
        float streak = valueNoise(float2(p.x / 1.9 + seed * 3.1, p.y / 60.0)) - 0.5;
        float drag = fbm(float2(p.x / 9.0, p.y / 210.0) + seed * 6.3) - 0.5;
        float fine = hash12(floor(p) + seed * 17.0) - 0.5;
        color *= 1.0 + grain * (0.12 * streak + 0.16 * drag + 0.11 * fine);
        return float4(color, coat);
    }
    """

    private func paint(for part: Part, coat: Double, glow: Double) -> Shader {
        let ys = part.outline.map { $0.y }
        var numbers: [Float] = [Float(part.inner.red), Float(part.inner.green), Float(part.inner.blue),
                                Float(part.rim.red), Float(part.rim.green), Float(part.rim.blue),
                                Float(part.second.red), Float(part.second.green), Float(part.second.blue),
                                Float(ys.min() ?? 0), Float(ys.max() ?? 1), Float(part.seed), Float(grain),
                                Float(part.rimAmount * glow), Float(part.rimWidth), Float(part.shade),
                                Float(coat), Float(part.outline.count)]
        for point in part.outline {
            numbers += [Float(point.x), Float(point.y)]
        }
        return Shader(Simbiosis.paint, params: numbers, using: [.noise, .hash])
    }

    // MARK: - Drawing

    override func draw() {
        let seed = variation &+ pressed
        if built.seed != seed || built.lines != lines || built.courses != courses || built.hue != hue
            || built.width != width || built.height != height {
            built = (seed, lines, courses, hue, width, height)
            painting = deal(seed: seed)
        }
        let cycle = max(8, seconds)
        let turn = (time / cycle).truncatingRemainder(dividingBy: 1)
        func ramp(_ from: Double, _ to: Double) -> Double {
            let t = clamp((turn - from) / (to - from), 0, 1)
            return t * t * (3 - 2 * t)
        }
        // The cycle. The painting stands whole until 0.25. By 0.34 the
        // paint is off and the drawing shows: the cuts, the diagonals, and
        // every outline. From 0.42 the paint goes back on in its order, and
        // by 0.9 the last line is ruled.
        let off = ramp(0.25, 0.34)
        let painted = turn < 0.4 ? -1 : (turn - 0.42) / 0.48
        let drawing = min(ramp(0.27, 0.34), 1 - ramp(0.5, 0.74))
        let ruling = min(ramp(0.27, 0.34), 1 - ramp(0.42, 0.5))
        // The halos breathe twice in a cycle.
        let glow = halo / 0.7 * (1 + 0.22 * sin(2 * .pi * 2 * turn))

        background(ground)
        if ruling > 0.001 { rule(alpha: 0.4 * ruling) }
        if drawing > 0.001 {
            noFill()
            stroke(chalk.withAlpha(0.55 * drawing))
            strokeWeight(1)
            for part in painting.parts {
                drawShape(Shape(part.outline, closed: true))
            }
            for line in painting.rules {
                drawLine(line.from, line.to)
            }
            noStroke()
        }
        // The planes go on first, then the ruled lines. The small square
        // goes on after them, so it covers the line it sits on.
        func coat(_ order: Double, over span: Double) -> Double {
            turn < 0.4 ? 1 - off : clamp((painted - order) / span, 0, 1)
        }
        for part in painting.parts where part.order < 0.95 && coat(part.order, over: 0.07) > 0 {
            lay(part, coat: coat(part.order, over: 0.07), glow: glow)
        }
        for line in painting.rules where coat(line.order, over: 0.06) > 0 {
            draw(line, amount: coat(line.order, over: 0.06), fading: turn < 0.4, glow: glow)
        }
        for part in painting.parts where part.order >= 0.95 && coat(part.order, over: 0.03) > 0 {
            lay(part, coat: coat(part.order, over: 0.03), glow: glow)
        }
        if guides { rule(alpha: 0.55) }
    }

    /// Lay one plane as a coat `coat` thick: the soft dark around it, its
    /// paint, and its thin dark outline.
    private func lay(_ part: Part, coat: Double, glow: Double) {
        let shape = Shape(part.outline, closed: true)
        noStroke()
        if part.shadow > 0 {
            // The dark around a plane is its own outline again, slid a
            // little down and toward the far side, in thin coats.
            let copies = 7
            for k in 1 ... copies {
                let reach = part.shadow * Double(k) / Double(copies)
                fill(Color.black.withAlpha(0.075 * coat))
                for corner in [Vector2(0.5, 1), Vector2(-0.45, 0.6), Vector2(0.9, -0.1), Vector2(-0.2, -0.45)] {
                    withState {
                        translate(corner * reach)
                        drawShape(shape)
                    }
                }
            }
        }
        fill(paint(for: part, coat: coat, glow: glow))
        drawShape(shape)
        if part.outlined {
            noFill()
            stroke(ground.withAlpha(0.8 * coat))
            strokeWeight(1.3)
            drawShape(shape)
            noStroke()
        }
    }

    /// Rule one line as far as it has got, with its halo. While the painting
    /// is being taken back, the whole line fades instead.
    private func draw(_ line: Rule, amount: Double, fading: Bool, glow: Double) {
        let grown = fading ? 1 : min(1, amount * 1.6)
        let presence = fading ? amount : clamp((amount - 0.4) / 0.6, 0, 1)
        let end = line.from + (line.to - line.from) * grown
        let along = (line.to - line.from).normalized
        let across = Vector2(-along.y, along.x)
        noStroke()
        // The halo is a band beside the line, full at the line and gone a
        // few pixels out. A line with no side has a thinner band on each.
        let sides: [(Double, Double)] = line.side == 0 ? [(1, 7), (-1, 7)] : [(line.side, 15), (-line.side, 4)]
        for (side, reach) in sides {
            let out = across * (side * reach)
            let color = line.glow.withAlpha(min(1, 0.62 * glow) * presence * (reach < 8 ? 0.6 : 1))
            fill(.linear(from: line.from, to: line.from + out, [color, color.withAlpha(0)]))
            drawPolygon([line.from, end, end + out, line.from + out])
        }
        stroke(line.ink.withAlpha(fading ? amount : 1))
        strokeWeight(1.7)
        drawLine(line.from, end)
        noStroke()
    }

    /// Rule the golden cuts across the panel, both ways, and its two
    /// diagonals.
    private func rule(alpha: Double) {
        stroke(chalk.withAlpha(alpha))
        strokeWeight(1)
        for mark in Simbiosis.lattice {
            drawLine(mark * width, 0, mark * width, height)
            drawLine(0, mark * height, width, mark * height)
        }
        drawLine(0, 0, width, height)
        drawLine(width, 0, 0, height)
        noStroke()
    }
}
```
