# HommageADurer

a sheet of small figures, each one line through the same sixteen points in another order, after Molnár's line of 1948 and her sheets of variations of 1990.

Run it with `swift run --package-path Examples Example-Recreations-VeraMolnar-HommageADurer`.

```swift
//  Recreation after Vera Molnár - "Hommage à Dürer", the line she drew in
//  1948 through the sixteen numbers of the magic square in Albrecht Dürer's
//  engraving "Melencolia I" (1514), and the sheets of variations she made
//  from it from 1982 on, among them the plotter drawings "Hommage à Dürer,
//  400 variations" and "Hommage à Dürer, 225 variations aléatoires,
//  direction chaos" (both 1990, ink on paper, 30 by 30 centimeters, shown by
//  the Digital Art Museum). A homage, not a reproduction, and not affiliated
//  with or endorsed by the artist's estate.
//  https://dam.org/museum/artists_ui/artists/molnar-vera/works_vera_molnar/hommage-a-durer/
//  http://www.veramolnar.com/xml/1990.xml (the list behind her own catalog)
//
//  An original Ollin interpretation. The rule for the line of 1948 is the
//  Digital Art Museum's statement of it: she joins the numbers with one line
//  in ascending order. It was checked against the picture of that drawing in
//  the artist's own catalog. What a variation is was read off pictures of
//  the sheets and their titles. In the sheet of 400, every figure touches
//  all sixteen points of an even lattice, a figure is three steps wide, and
//  the next one starts a step farther on. That the points are joined in an
//  order drawn by chance, each point once, is this sketch's reading of those
//  pictures. So is how far the points stray and how a line fades. No code
//  was read or ported.

import Foundation
import Ollin

/// "Hommage à Dürer" (Vera Molnár, from 1948). On a wall in the upper right
/// of Dürer's engraving is a square of the numbers 1 to 16, four to a row:
///
///     16  3  2 13
///      5 10 11  8
///      9  6  7 12
///      4 15 14  1
///
/// Every row, every column, and both diagonals add up to 34. In 1948 Molnár
/// joined the sixteen numbers with one line, from 1 to 2 to 3 and on to 16.
/// In this square a number and the one that makes 17 with it sit opposite
/// each other across the middle. So her line looks the same turned upside
/// down.
///
/// She came back to the figure for decades. From 1982 she made whole sheets
/// of it: 81, 100, 225, or 400 small figures, each a variation. A variation
/// keeps the sixteen points and the single line, and lets chance pick the
/// order the line visits them in. There are more than twenty million million
/// such orders, and Dürer's is one of them.
///
/// A lap opens on a finished sheet of variations. The lines are taken up
/// again, each from its first point. Then every figure draws Dürer's own
/// order, which is her line of 1948, and gives it up too. Last, each figure
/// draws its variation, all of them at once, from the first point to the
/// sixteenth.
///
/// Try it: `side` is how many figures stand along a side of the sheet. 9,
/// 10, 15, and 20 give her sheets, and 1 gives one figure large. `chaos`
/// pushes every point off its place, as in the sheets she titled "direction
/// chaos". `fading` shortens the lines row by row down the sheet, as in the
/// ones she titled "disparition". A click rolls the next `seed`. The lines
/// export as strokes, so `--export-svg` gives a plotter drawing back.
@main
final class HommageADurer: Sketch {

    /// How many figures stand along a side of the sheet. Her sheets hold 81,
    /// 100, 225, and 400 figures: 9, 10, 15, and 20 to a side.
    @Param(1 ... 20, icon: "square.grid.3x3") var side = 10
    /// How far a point may be pushed off its place, in steps of the square.
    /// At 0 every figure stands on the same sixteen points.
    @Param(0 ... 1, icon: "tornado") var chaos = 0.0
    /// How much of its line a figure in the bottom row has lost. Rows in
    /// between lose their share. At 1 the bottom row keeps one segment.
    @Param(0 ... 1, icon: "eraser") var fading = 0.0
    /// Seconds for one lap.
    @Param(8 ... 30, icon: "timer") var cycle = 24.0
    @Param(1 ... 9999, icon: "dice") var seed = 1514

    override var loopDuration: Double? { cycle }

    // MARK: The square

    /// The magic square of "Melencolia I", row by row from the top.
    static let square = [16, 3, 2, 13,
                         5, 10, 11, 8,
                         9, 6, 7, 12,
                         4, 15, 14, 1]

    /// Dürer's order: where in the square the numbers 1, 2, 3, up to 16
    /// stand, as places counted row by row from 0.
    static let theme: [Int] = (1 ... 16).map { number in square.firstIndex(of: number)! }

    /// One figure of the sheet: where its sixteen points are, in steps from
    /// its own top left point, and the order its variation joins them in.
    struct Figure {
        var points: [Vector2]
        var order: [Int]
    }

    /// The figure in one cell of the sheet. Its generator is rolled in one
    /// fixed order: fifteen rolls for the order, then two for each point.
    /// So a seed keeps its orders whatever `chaos` is.
    static func figure(cell: Int, chaos: Double, seed: UInt64) -> Figure {
        var generator = SplitMix64(seed: seed &* 0x9E37_79B9_7F4A_7C15 &+ UInt64(cell))
        func roll() -> Double { Double(generator.next() >> 11) * 0x1p-53 }

        // An order by chance: each place in turn takes one of the points
        // not yet taken, every one of them as likely as any other.
        var order = Array(0 ..< 16)
        for place in 0 ..< 15 {
            let pick = place + min(15 - place, Int(roll() * Double(16 - place)))
            order.swapAt(place, pick)
        }
        let points = (0 ..< 16).map { index in
            Vector2(Double(index % 4) + (roll() * 2 - 1) * chaos,
                    Double(index / 4) + (roll() * 2 - 1) * chaos)
        }
        return Figure(points: points, order: order)
    }

    /// How many of its fifteen segments a figure in `row` keeps.
    static func kept(row: Int, rows: Int, fading: Double) -> Int {
        guard rows > 1 else { return 15 }
        let gone = fading * Double(row) / Double(rows - 1)
        return max(1, Int((15 - 14 * gone).rounded()))
    }

    // MARK: The lap

    /// Which order the sheet is showing.
    enum Order { case theme, variations }

    /// Where the lap is: which order is on the sheet, and the part of each
    /// line that is drawn, as shares of its length from its first point.
    static func stage(at lap: Double) -> (order: Order, from: Double, to: Double) {
        func share(_ start: Double, _ end: Double) -> Double { (lap - start) / (end - start) }
        switch lap {
        case ..<0.20: return (.variations, 0, 1)                    // The finished sheet.
        case ..<0.30: return (.variations, share(0.20, 0.30), 1)     // Taken up.
        case ..<0.46: return (.theme, 0, share(0.30, 0.46))         // Dürer's order drawn.
        case ..<0.56: return (.theme, 0, 1)
        case ..<0.64: return (.theme, share(0.56, 0.64), 1)
        case ..<0.98: return (.variations, 0, share(0.64, 0.98))    // The variations drawn.
        default: return (.variations, 0, 1)
        }
    }

    let paper = Color(hex: 0xF6F5F1)
    let pen = Color(hex: 0x15130F)

    /// A click rolls the next sheet.
    override func mousePressed() {
        seed = seed >= 9999 ? 1 : seed + 1
    }

    override func draw() {
        background(paper)
        let stage = Self.stage(at: loopProgress(over: cycle))
        guard stage.to > stage.from else { return }

        // The whole sheet is one even lattice. A figure is three steps wide
        // and the next one starts a step farther on, as in her sheet of 400.
        let field = min(width, height) * 0.84
        let step = field / Double(4 * side - 1)
        let corner = Vector2(center.x - field / 2, center.y - field / 2)

        noFill()
        stroke(pen)
        // A pen about a fourteenth of a step wide, close to the weight of
        // her sheet of 400, and never a heavy one when a figure stands alone.
        strokeWeight(min(max(0.75, step * 0.07), min(width, height) * 0.0033))
        strokeCap(.round)
        strokeJoin(.round)

        for row in 0 ..< side {
            let segments = Self.kept(row: row, rows: side, fading: fading)
            for column in 0 ..< side {
                let figure = Self.figure(cell: row * side + column, chaos: chaos, seed: UInt64(seed))
                let order = stage.order == .theme ? Self.theme : figure.order
                let origin = corner + Vector2(Double(column * 4), Double(row * 4)) * step
                let path = order.prefix(segments + 1).map { origin + figure.points[$0] * step }
                drawPolyline(stage.from <= 0 && stage.to >= 1
                    ? path : Contour(path, closed: false).piece(from: stage.from, to: stage.to).points)
            }
        }
    }
}
```
