# Macrostructure

a slanted piece of a Structure, enlarged until its borders read as lines, after Sýkora's Macrostructures (1972 to 1973).

Run it with `swift run --package-path Examples Example-Recreations-ZdenekSykora-Macrostructure`.

```swift
//  Recreation after Zdeněk Sýkora - the Macrostructures (Makrostruktury, 1972
//  to 1973), the black and white paintings that close his ten years of
//  Structures. Each is a small piece of a finished Structure, cut out at a
//  slant and painted again much larger. At that size the half-circles stop
//  reading as elements, and the borders between black and white read as
//  lines. Macrostructure IV (1972) is oil on canvas, 150 by 140 centimeters.
//  The Macrostructure of 1973 is oil on canvas, 150 centimeters square. A
//  homage, not a reproduction, and not affiliated with or endorsed by the
//  artist or his estate.
//  http://www.zdeneksykora.cz/
//
//  An original Ollin interpretation, written from what Sýkora said in three
//  interviews and from pictures of the two paintings. The interviews are with
//  Josef Hlaváček (1968), Vítek Čapek (1982 to 1985), and Vít Petrjanoš
//  (1991). Nothing was ported: the paintings were cut from finished canvases
//  and painted by hand. Three things were measured off the pictures: the
//  slant, the size of the elements, and which elements they hold. In both,
//  some cells hold a whole disc, as his Structure of 1964 does. In the
//  slanted Structure of 1968 such cells hold two half-discs. He cut his
//  pieces from paintings a program had filled from a chart, and he chose
//  each cut by eye. The Structure this sketch cuts from is dealt in a shorter
//  way of its own, with no chart. It keeps only the question the published
//  rules ask of neighbors. Its way of choosing the cut is its own too.

import Ollin

/// Macrostructure (Zdeněk Sýkora, Louny and Prague, 1972 to 1973). A
/// Structure, he said, is not bounded by the edge of the painting. It goes
/// on in every direction. In 1968 he began to lay the grid at a slant, so
/// the edge of the canvas would cut through elements. That showed the
/// painting as a piece cut out of something endless. Four years later he
/// made the pieces small and the elements large. He took cuts from finished
/// paintings and enlarged the half-circles until, as he put it, "the
/// elements flowed into the surface". What carried the picture then was the
/// lines their borders formed. Those lines became the rest of his work: the
/// first Lines date from 1973.
///
/// Two paintings were measured for this sketch. In both the grid leans about
/// twelve and a half degrees. Macrostructure IV (1972) is a little over four
/// elements wide, and the Macrostructure of 1973 is two and a half. Both
/// read, cell for cell, as elements of the Structures. An element is a
/// square, white on black or black on white. It holds a half-disc standing
/// on one side, or two on facing sides, or a whole disc in the middle. In
/// the painting of 1972 the straight sides of the cells still show. In the
/// one of 1973 every border is a curve.
///
/// The lap opens on a whole Structure, `far` elements across and already
/// slanted by `tilt` degrees. Its elements fall in patches of light and
/// dark. Each one was drawn among those that answer `rule`, the question his
/// program asked. Does the color continue across a shared side, and does the
/// shape, two half-discs closing a circle? Then the canvas enlarges, the way
/// he enlarged the elements, until only `cells` of them fit across.
///
/// He chose his cuts by eye. Here `places` stands in for the eye, with one
/// taste. The seed tries that many places, and the cut is made where the
/// least border runs straight along the grid. That is the look of 1973. At
/// 1 the first place is taken, straight sides and all, which is the look of
/// 1972.
///
/// When the piece is as large as it will get, the paint drains away and the
/// borders stay, as lines `line` points wide. An arc runs on into the next
/// arc wherever two meet at a corner of the grid. So a border is one line
/// built from arcs and straight runs. The lines hold, and the canvas clears
/// for the next lap. The seed deals the Structure and the places to try, and
/// a press deals the next one.
///
/// Try it: `--param cells=4.2 --param places=1 --param rule=neither` is the
/// size and the look of Macrostructure IV. With a frame in the last hold,
/// `--export-svg` gives the borders alone, every one a single stroke, cut
/// off at the canvas edge.
@main
final class Macrostructure: Sketch {
    /// The answers the dealing wants from a cell's neighbors.
    enum Relation: String, CaseIterable, ParamOption {
        case both, colorOnly, shapeOnly, neither

        var wantsColor: Bool { self == .both || self == .colorOnly }
        var wantsShape: Bool { self == .both || self == .shapeOnly }
    }

    /// How many elements fit across the canvas at the full enlargement. The
    /// painting of 1973 shows about 2.55, Macrostructure IV about 4.2.
    @Param(1.5 ... 6, icon: "plus.magnifyingglass") var cells = 2.55
    /// The slant of the grid, in degrees, counterclockwise. Both paintings
    /// measured lean about 12.5.
    @Param(-30 ... 30, icon: "rotate.left") var tilt = 12.5
    /// How many places the cut is chosen among. The one with the least
    /// straight border wins, so 1 leaves the place to chance.
    @Param(1 ... 400, icon: "viewfinder") var places = 120
    /// What an element is asked of the ones beside it: that the color
    /// continues across the shared side, the shape, both, or neither.
    @Param(icon: "arrow.triangle.branch") var rule = Relation.colorOnly
    /// How many elements across the lap opens on, before the enlargement.
    @Param(7 ... 16, icon: "square.grid.3x3") var far = 11.0
    /// The width of the border lines, in canvas points.
    @Param(1 ... 14, icon: "pencil.line") var line = 5.0
    /// The length of one lap, in seconds.
    @Param(10 ... 30, icon: "clock") var seconds = 28.0

    override var canvasSize: CanvasSize { .square(1080) }
    override var loopDuration: Double? { seconds }

    private enum Side: Int, CaseIterable {
        case top, right, bottom, left

        var opposite: Side { Side(rawValue: (rawValue + 2) % 4)! }
        /// The middle of the side, in a cell one unit wide.
        var middle: Vector2 {
            switch self {
            case .top: Vector2(0.5, 0)
            case .right: Vector2(1, 0.5)
            case .bottom: Vector2(0.5, 1)
            case .left: Vector2(0, 0.5)
            }
        }
        /// From the side toward the middle of the cell: the way a half-disc
        /// standing on this side bulges.
        var inward: Vector2 { Vector2(0.5, 0.5) - middle }
        /// The same direction as one of four numbers: right, down, left, up.
        var heading: Int {
            switch self {
            case .left: 0
            case .top: 1
            case .right: 2
            case .bottom: 3
            }
        }
        var step: (column: Int, row: Int) {
            switch self {
            case .top: (0, -1)
            case .right: (1, 0)
            case .bottom: (0, 1)
            case .left: (-1, 0)
            }
        }
    }

    /// One of the elements read off the two paintings: a square holding discs
    /// of half its width. They are white on black in groups 1 and 3, and
    /// black on white in groups 2 and 4. A single has one half-disc standing
    /// on a side. A facing pair has two, on opposite sides, meeting in the
    /// middle. The third kind has no half-disc, only a whole disc in the
    /// middle of the cell.
    private struct Element {
        let group: Int
        /// The sides a half-disc stands on. None, where the figure is a disc.
        let flats: [Side]

        var figureIsWhite: Bool { group == 1 || group == 3 }
        var isDisc: Bool { flats.isEmpty }
        func isFlat(_ side: Side) -> Bool { flats.contains(side) }
        /// The paint along a side: the figure's where a half-disc stands on
        /// it, the ground's elsewhere.
        func isWhite(along side: Side) -> Bool { isFlat(side) == figureIsWhite }
    }

    /// Fourteen elements in four groups, by how much of each is black. The
    /// discs and the pairs make the lightest and the darkest group, and the
    /// singles the two between.
    private static let elements: [Element] = {
        var out: [Element] = []
        for group in [1, 4] {
            out.append(Element(group: group, flats: []))
            out.append(Element(group: group, flats: [.left, .right]))
            out.append(Element(group: group, flats: [.top, .bottom]))
        }
        for group in [2, 3] {
            out += Side.allCases.map { Element(group: group, flats: [$0]) }
        }
        return out
    }()

    /// Points along a circle of half a cell's width, from one angle to
    /// another, never more than a 64th of a half-turn apart.
    private static func arc(center: Vector2, from: Double, to: Double) -> [Vector2] {
        let steps = max(1, Int((abs(to - from) * 64 / .pi - 1e-9).rounded(.up)))
        return (0 ... steps).map { k in
            let angle = from + (to - from) * Double(k) / Double(steps)
            return center + Vector2(cos(angle), sin(angle)) * 0.5
        }
    }

    /// The arc of the half-disc standing on a side, from one end of the side
    /// to the other, in 64 steps.
    private static func halfDisc(on side: Side) -> [Vector2] {
        let facing = Double(side.heading) * .pi / 2
        return arc(center: side.middle, from: facing - .pi / 2, to: facing + .pi / 2)
    }

    /// The disc in the middle of a cell as four quarter arcs. Each runs from
    /// the point where the disc touches one side to where it touches the
    /// next. They go around by the right, the bottom, and the left.
    private static let quarters: [[Vector2]] = (0 ..< 4).map { k in
        arc(center: Vector2(0.5, 0.5), from: Double(k - 1) * .pi / 2, to: Double(k) * .pi / 2)
    }

    /// The four corners of a cell, each named after the quarter arc it lies
    /// beyond.
    private static let corners = [Vector2(1, 0), Vector2(1, 1), Vector2(0, 1), Vector2(0, 0)]

    /// The black of every element as outlines, in a cell one unit wide. It
    /// is the figure where the figure is black, and the square less the
    /// figure where it is white. An edge along a side of the cell always
    /// stops at the middle of the side. So the outlines of two cells that
    /// share a side share its points too, and no crack opens between them.
    private static let blacks: [[[Vector2]]] = elements.map { element -> [[Vector2]] in
        if element.isDisc {
            // A black disc, or the four corners of ground a white one leaves.
            return element.figureIsWhite
                ? (0 ..< 4).map { quarters[$0] + [corners[$0]] }
                : [Array(quarters.map { $0.dropLast() }.joined())]
        }
        guard element.figureIsWhite else {
            return element.flats.map { halfDisc(on: $0) + [$0.middle] }
        }
        let first = element.flats[0]
        let a = halfDisc(on: first)
        if element.flats.count == 2 {
            let c = halfDisc(on: element.flats[1])
            return [Array(a[...32]) + Array(c[33...]) + [(c[64] + a[0]) * 0.5],
                    Array(a[32...]) + [(a[64] + c[0]) * 0.5] + Array(c[...32])]
        }
        // Across the cell, from the side the half-disc stands on to the far one.
        let over = first.inward * 2
        return [a + [a[64] + over * 0.5, a[64] + over, first.middle + over, a[0] + over, a[0] + over * 0.5]]
    }

    /// A stretch of border, an arc or a straight run, between two points of
    /// the grid. Each end remembers which way the border leaves it, so two
    /// stretches that leave a point in opposite ways can be joined smoothly.
    private struct Stretch {
        var points: [Vector2]
        var headings: (Int, Int)
        var isStraight: Bool
    }

    private struct Dealt: Equatable {
        var cells: Double, tilt: Double, places: Int, rule: Relation, far: Double, seed: Int
    }

    private let primed = Color(hex: 0xF1EEE7)
    private let oilBlack = Color(hex: 0x141313)

    /// The Structure the piece is cut from: an element for every cell.
    private var field: [[Int]] = []
    /// The point of the Structure the canvas is centered on, in cells.
    private var focus = Vector2(0, 0)
    /// The borders of the piece at its full enlargement, in canvas points.
    private var borders: [[Vector2]] = []
    /// The black of the whole Structure, drawn once and kept.
    private var inked: Batch?
    private var dealtFor: Dealt?
    private var pressed = false

    override func mousePressed() {
        pressed = true
    }

    override func draw() {
        if pressed {
            pressed = false
            seed(variation + 1)
        }
        let wanted = Dealt(cells: cells, tilt: tilt, places: places, rule: rule, far: far, seed: variation)
        if dealtFor != wanted {
            dealtFor = wanted
            deal()
            inked = nil
            borders = joined(stretches(around: focus))
        }

        background(primed)
        let phase = loopProgress(over: seconds)
        let shown = 0.05, opened = 0.12, enlarged = 0.52, painted = 0.62, drained = 0.72, held = 0.92

        // The enlargement runs at an even pace for the eye: the same factor
        // every second, eased at both ends.
        let grown = smoothstep(opened, enlarged, phase)
        let across = far * pow(cells / far, grown)

        // The paint, until it has drained. After that only the lines are
        // drawn, so a file exported from the last hold holds nothing else.
        if phase < drained {
            paint(across: across)
            if phase > painted { cover(smoothstep(painted, drained, phase)) }
        }
        if phase > painted {
            noFill()
            stroke(oilBlack.withAlpha(smoothstep(painted, painted + 0.02, phase)))
            strokeWeight(line)
            strokeJoin(.round)
            strokeCap(.round)
            for border in borders { drawPolyline(border) }
            noStroke()
        }

        // The canvas clears at the end of a lap, and the Structure comes up
        // out of it at the start. So the lap closes on itself.
        cover(phase < shown ? 1 - smoothstep(0, shown, phase) : smoothstep(held, 1, phase))
    }

    /// Lays the bare canvas over everything drawn so far, as thick as asked.
    /// The sheet reaches past the frame, so its soft edge never falls on the
    /// outermost pixels and a full cover hides everything.
    private func cover(_ amount: Double) {
        guard amount > 0 else { return }
        noStroke()
        fill(primed.withAlpha(amount))
        drawRect(-4, -4, width + 8, height + 8)
    }

    /// Where a point of the Structure, in cells, falls on the canvas when
    /// the canvas is centered on `center` and `across` cells fit across it.
    private func place(_ p: Vector2, around center: Vector2, across: Double) -> Vector2 {
        let turn = tilt * .pi / 180
        let d = (p - center) * (width / across)
        return Vector2(width / 2 + d.x * cos(turn) + d.y * sin(turn),
                       height / 2 - d.x * sin(turn) + d.y * cos(turn))
    }

    /// The Structure in oil: the black of every cell, kept as one drawing in
    /// cells and laid on the canvas at the enlargement asked for.
    private func paint(across: Double) {
        if inked == nil {
            inked = makeBatch {
                noStroke()
                fill(oilBlack)
                for r in field.indices {
                    for c in field[r].indices {
                        let corner = Vector2(Double(c), Double(r))
                        for outline in Self.blacks[field[r][c]] { drawPolygon(outline.map { corner + $0 }) }
                    }
                }
            }
        }
        guard let inked else { return }
        withState {
            translate(width / 2, height / 2)
            rotate(-tilt * .pi / 180)
            scale(width / across)
            translate(-focus.x, -focus.y)
            drawBatch(inked)
        }
    }

    /// Deals the Structure and chooses the place of the cut.
    private func deal() {
        randomSeed(variation)
        noiseSeed(variation)
        // Enough cells to fill the canvas at its widest, with room to spare
        // for the place of the cut.
        let count = 2 * Int((far / 2.0.squareRoot() + 4).rounded(.up))

        // The groups, in patches: the cells ranked by a smooth field and cut
        // into four shares, the lightest group to the darkest. The two
        // middle groups, the singles, take four cells in five. That is their
        // share in the two paintings, where 27 of the 35 cells read are
        // singles.
        var ranked: [(value: Double, row: Int, column: Int)] = []
        for r in 0 ..< count {
            for c in 0 ..< count {
                ranked.append((signedNoise(Double(c) * 0.21 + 0.5, Double(r) * 0.21 + 0.5), r, c))
            }
        }
        ranked.sort { ($0.value, $0.row, $0.column) < ($1.value, $1.row, $1.column) }
        var groups = Array(repeating: Array(repeating: 1, count: count), count: count)
        for (k, cell) in ranked.enumerated() {
            let share = Double(k) / Double(ranked.count)
            groups[cell.row][cell.column] = share < 0.1 ? 1 : share < 0.5 ? 2 : share < 0.9 ? 3 : 4
        }

        // The elements, row by row, one row left to right and the next back
        // right to left. Each is drawn among those of its group that give the
        // wanted answers to the cells already placed beside it. The color is
        // asked first, then the shape. A question no element can answer is
        // dropped.
        var placed = Array(repeating: [Int?](repeating: nil, count: count), count: count)
        for r in 0 ..< count {
            for c in r % 2 == 0 ? Array(0 ..< count) : Array((0 ..< count).reversed()) {
                let beside: [(Side, Element)] = Side.allCases.compactMap { side in
                    let (nc, nr) = (c + side.step.column, r + side.step.row)
                    guard nr >= 0, nr < count, nc >= 0, nc < count, let e = placed[nr][nc] else { return nil }
                    return (side, Self.elements[e])
                }
                func colorContinues(_ e: Element) -> Bool {
                    beside.contains { e.isWhite(along: $0.0) == $0.1.isWhite(along: $0.0.opposite) }
                }
                func shapeContinues(_ e: Element) -> Bool {
                    beside.contains { e.isFlat($0.0) && $0.1.isFlat($0.0.opposite) }
                }
                let candidates = Self.elements.indices.filter { Self.elements[$0].group == groups[r][c] }
                let byColor = candidates.filter { colorContinues(Self.elements[$0]) == rule.wantsColor }
                let kept = byColor.isEmpty ? candidates : byColor
                let byShape = kept.filter { shapeContinues(Self.elements[$0]) == rule.wantsShape }
                placed[r][c] = randomChoice(Array(byShape.isEmpty ? kept : byShape))
            }
        }
        field = placed.map { $0.map { $0! } }

        // The cut: of the places tried, all within two cells of the middle,
        // the one where the least border runs straight inside the canvas.
        let middle = Double(count) / 2
        var best = Double.infinity
        for _ in 0 ..< places {
            let spot = Vector2(middle + random(-2, 2), middle + random(-2, 2))
            let straight = stretches(around: spot).filter(\.isStraight).reduce(0.0) { sum, stretch in
                sum + lengthOnCanvas(place(stretch.points[0], around: spot, across: cells),
                                     place(stretch.points[1], around: spot, across: cells))
            }
            if straight < best {
                best = straight
                focus = spot
            }
        }
    }

    /// How much of the segment from `a` to `b` lies on the canvas.
    private func lengthOnCanvas(_ a: Vector2, _ b: Vector2) -> Double {
        let d = b - a
        var from = 0.0, to = 1.0
        for (step, room) in [(-d.x, a.x), (d.x, width - a.x), (-d.y, a.y), (d.y, height - a.y)] {
            if step == 0 {
                if room < 0 { return 0 }
            } else if step < 0 {
                from = max(from, room / step)
            } else {
                to = min(to, room / step)
            }
        }
        return to > from ? (to - from) * d.length : 0
    }

    /// Every stretch of border between black and white in the cells the
    /// canvas shows at its full enlargement, when it is centered on `center`.
    private func stretches(around center: Vector2) -> [Stretch] {
        // Right, down, left, up, as unit steps.
        let ways = [Vector2(1, 0), Vector2(0, 1), Vector2(-1, 0), Vector2(0, -1)]
        var out: [Stretch] = []
        func run(from a: Vector2, to b: Vector2) -> Stretch {
            let d = b - a
            let heading = abs(d.x) > abs(d.y) ? (d.x > 0 ? 0 : 2) : (d.y > 0 ? 1 : 3)
            return Stretch(points: [a, b], headings: (heading, (heading + 2) % 4), isStraight: true)
        }

        // The cells within the canvas's half diagonal of the center, and one
        // more ring of them.
        let reach = cells / 2.0.squareRoot() + 1.5
        for r in field.indices {
            for c in field[r].indices
            where (Vector2(Double(c) + 0.5, Double(r) + 0.5) - center).length <= reach {
                let element = Self.elements[field[r][c]]
                let corner = Vector2(Double(c), Double(r))
                // The arc of every half-disc is a border along its whole
                // length, figure inside it and ground outside. It leaves both
                // ends of its side heading into the cell.
                for side in element.flats {
                    out.append(Stretch(points: Self.halfDisc(on: side).map { corner + $0 },
                                       headings: (side.heading, side.heading), isStraight: false))
                }
                // A disc is a border all the way around. It is taken in its
                // four quarters, since a border along a side passes through
                // the point where the disc touches that side. A quarter
                // leaves each of its ends along the side it touches there.
                if element.isDisc {
                    for (k, quarter) in Self.quarters.enumerated() {
                        out.append(Stretch(points: quarter.map { corner + $0 },
                                           headings: (k, (k + 3) % 4), isStraight: false))
                    }
                }
                // A side of the grid is a border where the paints across it
                // differ. It is taken in two halves, as the outlines of the
                // black are.
                for side in [Side.right, .bottom] {
                    let (nc, nr) = (c + side.step.column, r + side.step.row)
                    guard nr >= 0, nr < field.count, nc >= 0, nc < field[nr].count else { continue }
                    let other = Self.elements[field[nr][nc]]
                    guard element.isWhite(along: side) != other.isWhite(along: side.opposite) else { continue }
                    let middle = corner + side.middle
                    let half = ways[(side.heading + 1) % 4] * 0.5
                    out.append(run(from: middle - half, to: middle))
                    out.append(run(from: middle, to: middle + half))
                }
            }
        }
        return out
    }

    /// Joins the stretches into lines and cuts the lines off at the canvas
    /// edge. The lines come back in canvas points.
    private func joined(_ stretches: [Stretch]) -> [[Vector2]] {
        // The ends that meet at each point of the grid, at whole and half
        // steps. An end is a stretch and which of its two ends.
        var meeting: [SIMD2<Int>: [Int]] = [:]
        var order: [SIMD2<Int>] = []
        for (k, stretch) in stretches.enumerated() {
            for (end, p) in [stretch.points.first!, stretch.points.last!].enumerated() {
                let key = SIMD2(Int((2 * p.x).rounded()), Int((2 * p.y).rounded()))
                if meeting[key] == nil { order.append(key) }
                meeting[key, default: []].append(2 * k + end)
            }
        }
        func heading(_ end: Int) -> Int {
            end % 2 == 0 ? stretches[end / 2].headings.0 : stretches[end / 2].headings.1
        }

        // At each point the ends are joined two by two. Two straight runs
        // that leave in opposite ways come first. Then come any two that
        // leave in opposite ways, an arc running on into the next arc. What
        // is left turns a corner.
        var partner: [Int: Int] = [:]
        for key in order {
            var open = meeting[key]!
            func join(_ fits: (Int, Int) -> Bool) {
                var i = 0
                while i < open.count {
                    if let j = open.indices.dropFirst(i + 1).first(where: { fits(open[i], open[$0]) }) {
                        partner[open[i]] = open[j]
                        partner[open[j]] = open[i]
                        open.remove(at: j)
                        open.remove(at: i)
                    } else {
                        i += 1
                    }
                }
            }
            join { a, b in
                stretches[a / 2].isStraight && stretches[b / 2].isStraight && heading(a) == (heading(b) + 2) % 4
            }
            join { a, b in heading(a) == (heading(b) + 2) % 4 }
            join { _, _ in true }
        }

        // Walk the joined stretches into lines, in cells.
        var used = Array(repeating: false, count: stretches.count)
        var lines: [[Vector2]] = []
        for k in stretches.indices where !used[k] {
            used[k] = true
            var points = stretches[k].points
            var closed = false
            var at = 2 * k + 1
            while let next = partner[at] {
                if next / 2 == k {
                    closed = true
                    break
                }
                guard !used[next / 2] else { break }
                used[next / 2] = true
                let more = stretches[next / 2].points
                points += (next % 2 == 0 ? more : more.reversed()).dropFirst()
                at = next ^ 1
            }
            at = 2 * k
            while !closed, let next = partner[at], !used[next / 2] {
                used[next / 2] = true
                let more = stretches[next / 2].points
                points = (next % 2 == 1 ? more : more.reversed()).dropLast() + points
                at = next ^ 1
            }
            lines.append(points)
        }

        // On the canvas at the full enlargement, cut off just past its edge,
        // far enough out that the widest line's end never shows.
        let margin = 8.0
        let edges = [
            HalfPlane(point: Vector2(-margin, 0), inward: Vector2(1, 0)),
            HalfPlane(point: Vector2(width + margin, 0), inward: Vector2(-1, 0)),
            HalfPlane(point: Vector2(0, -margin), inward: Vector2(0, 1)),
            HalfPlane(point: Vector2(0, height + margin), inward: Vector2(0, -1)),
        ]
        return lines.flatMap { points -> [[Vector2]] in
            var placed = points.map { place($0, around: focus, across: cells) }
            // A line that closes on itself is begun again at a point off the
            // canvas, where it has one. Otherwise the cut would also part it
            // at the point it happened to start from.
            if placed.count > 2, (placed[0] - placed[placed.count - 1]).length < 1e-6,
               let off = placed.firstIndex(where: { p in !edges.allSatisfy { $0.contains(p) } }) {
                let ring = placed.dropLast()
                placed = Array(ring[off...] + ring[..<off]) + [ring[off]]
            }
            var parts = [Contour(placed, closed: false)]
            for edge in edges { parts = parts.flatMap { $0.clipped(to: edge) } }
            return parts.map(\.points)
        }
    }
}
```
