Ollin

CuadradosOlivaYNegro

Sixteen squares standing off a ground of fine lines, after Jesús Rafael Soto's Cuadrados oliva y negro (1966).

Run
swift run --package-path Examples Example-Recreations-JesusRafaelSoto-CuadradosOlivaYNegro
Source
Examples/Recreations/JesusRafaelSoto/CuadradosOlivaYNegro/Sketch.swift
//  Recreation after Jesús Rafael Soto - Cuadrados oliva y negro (Carrés olive
//  et noir) (1966; acrylic paint on wood and painted metal, 106 x 106 x 16 cm,
//  Centre Pompidou, Paris, AM 2012-108), with Ariel Jiménez's account of it
//  on the museum's page and Jean Clay's account of the system, quoted there
//  for Premier carré vibrant (1958). A homage, not a reproduction, and not
//  affiliated with or endorsed by the artist, his estate, or the museum.
//  https://www.centrepompidou.fr/en/ressources/oeuvre/ca9d7b
//  https://www.centrepompidou.fr/en/ressources/oeuvre/l40fA0L
//
//  An original Ollin interpretation, written from the record, the two
//  accounts, and the museum's two photographs of the work. Nothing was
//  ported: the work is painted wood and metal. The layout was measured off
//  the front view. How far the squares stand off the ground is read off the
//  side view, since the record gives only the whole depth.

import Foundation
import Ollin

/// *Cuadrados oliva y negro* (Jesús Rafael Soto, Paris, 1966). **Move the
/// pointer** to move where you stand.
///
/// A black panel carries two boards painted in fine black and white lines,
/// one above the other. Sixteen metal squares stand in front of them on
/// posts you cannot see from the front: two rows of black over two rows of
/// olive green. Jean Clay described the system Soto had worked out by 1957.
/// Squares stand a few centimeters in front of a panel of small regular
/// lines. Their mass is eaten away by the lines as soon as you move at all.
/// Stand still and each square is exact. Take a step and its edges start to
/// slip.
///
/// Why they slip is plain geometry, and this sketch draws nothing else. A
/// square stands `depth` centimeters in front of the ground. Seen from `v`,
/// it lands on the ground at `v + (p - v) * D / (D - d)`. So every square
/// slides over the lines as you walk. A black edge over a black line cannot
/// be seen. Over a white line it can. So while a black square's edge
/// crosses a black line nothing in the picture changes, and then it moves
/// on across the next white one: the edge is there half the time. An olive
/// edge shows over both lines, faintly over the black ones, so an olive
/// square's edge can always be found. Ariel Jiménez wrote that the
/// vibration is strongest among the black squares, where the contrast with
/// the white lines is greatest. Among the green ones it drops. `blackRows`
/// moves the line between the two.
///
/// While the pointer rests you walk on your own, slowly, `sway` centimeters
/// to each side of where you stand. You slow to a stop at each end. Nobody
/// stands quite still, so your head also drifts by `unrest` centimeters the
/// whole time, and that is enough to keep the black edges uncertain. The
/// lamp is overhead, so each square and its post leave a soft shadow below
/// them on the ground. The shadows stay put while the squares move.
///
/// Try it: `stripePitch` is one black and one white line in pixels, and the
/// boards always hold a whole number of them. `depth` and `distance` set how
/// far the squares slide. `standX` and `standY` are where you stand, and
/// `accent` is the second color. `tolerance` is how far off true a hand-set
/// square may sit, and `handSeed` deals those. `shadow` is the lamp.
///
/// `--export-svg` writes each line of the ground, each shadow, each post,
/// and each square as a polygon. The pitch and the slide can be read back
/// from the file.
@main
final class CuadradosOlivaYNegro: Sketch {
    /// One black and one white line of the ground, in pixels.
    @Param(4 ... 12, icon: "line.3.horizontal") var stripePitch = 4
    /// How far the squares stand in front of the ground, in centimeters.
    @Param(2 ... 14, icon: "square.3.layers.3d") var depth = 12.0
    /// How far from the panel you stand, in centimeters. From three meters
    /// the squares sit as they do in the museum's front view.
    @Param(80 ... 400, icon: "figure.stand") var distance = 300.0
    /// Where you stand, left or right of the middle, in centimeters.
    @Param(-50 ... 50, icon: "arrow.left.and.right") var standX = 0.0
    /// Where your eyes are, above or below the middle, in centimeters.
    @Param(-30 ... 30, icon: "arrow.up.and.down") var standY = 0.0
    /// How far you walk to each side while the pointer rests, in centimeters.
    @Param(0 ... 45, icon: "figure.walk") var sway = 24.0
    /// How far your head drifts when you think you are standing still, in
    /// centimeters.
    @Param(0 ... 3, icon: "waveform.path") var unrest = 0.8
    /// How many of the four rows are black. The rest take the accent color.
    @Param(0 ... 4, icon: "square.grid.2x2") var blackRows = 2
    /// The second color: olive green in 1966.
    @Param(icon: "paintpalette") var accent = Color(hex: 0x2B451D)
    /// How far off true a hand-set square may sit, in millimeters.
    @Param(0 ... 4, icon: "hand.point.up.left") var tolerance = 1.0
    /// Which way each square sits off true. Every number is another hand.
    @Param(1 ... 9999, icon: "dice") var handSeed = 1
    /// How dark the lamp's shadows are on the ground.
    @Param(0 ... 0.6, icon: "shadow") var shadow = 0.28

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

    /// The walk comes back to its start after this many seconds.
    static let loop = 24.0

    // The work in centimeters from the middle of the panel, y down, measured
    // off the museum's front view against the record's 106 cm.
    static let panelHalf = 53.0
    static let boardHalfWidth = 50.3
    /// The two boards, above and below a narrow gap.
    static let boards: [(top: Double, bottom: Double)] = [(-50.3, -0.5), (0.5, 50.3)]
    static let squaresPerSide = 4
    static let squareSpacing = 25.0
    static let squareSide = 14.5
    static let postSide = 3.5
    /// Where the lamp throws a shadow: this far across the ground for every
    /// centimeter a square stands in front of it. The lamp is overhead.
    static let lamp = Vector2(0.03, 0.34)

    static let center = Vector2(540, 524)
    static let pxPerCm = 930.0 / 106.0

    private static let wall = Color(hex: 0x57585A)
    private static let paper = Color(hex: 0xECEEEE)
    /// One black for the frame, the lines, the posts, and the black squares.
    /// This is the sketch's choice, taken from the accounts of the work: the
    /// photographs do not settle whether the two blacks match. With one
    /// black, nothing marks where a black square ends over a black line.
    private static let black = Color(hex: 0x19191A)

    /// One metal square, at rest: where its middle is, how it is turned, how
    /// far out it stands as a share of `depth`, and its color.
    struct Plate {
        var middle: Vector2
        var turn: Double
        var depthShare: Double
        var isBlack: Bool
    }

    private var plates: [Plate] = []
    private var built: [Double] = []
    /// How far the pointer has pulled you from the walk, in centimeters.
    private var pull = Vector2(0, 0)
    private var lastMoved = -100.0

    // MARK: - Drawing

    override func draw() {
        let signature = [Double(handSeed), Double(blackRows), tolerance]
        if signature != built {
            plates = makePlates()
            built = signature
        }
        let viewer = whereYouStand()

        background(CuadradosOlivaYNegro.wall)
        noStroke()
        drawPanel()
        drawShadows()
        drawSquares(from: viewer)
        if !isVectorExporting {
            drawText("move the pointer: where you stand", width / 2, height - 22, size: 14,
                     color: Color(white: 1, alpha: 0.4), align: .center, .bottom)
        }
    }

    /// The pointer stands for you while it moves. When it rests you go back
    /// to the walk. The walk is a function of the clock alone, so a frame of
    /// an export does not depend on the frames before it.
    private func whereYouStand() -> Vector2 {
        let walk = walkAt(time)
        if mouse != previousMouse { lastMoved = time }
        var target = Vector2(0, 0)
        if time - lastMoved < 4 {
            let reach = Vector2(48, 30)
            let pointed = Vector2((mouseX / width - 0.5) * 2 * reach.x, (mouseY / height - 0.5) * 2 * reach.y)
            target = pointed - walk
        }
        pull = pull + (target - pull) * (1 - exp(-deltaTime * 5))
        return walk + pull
    }

    /// The walk: slowly side to side past the panel and back, with the small
    /// drift of a body that is never quite still. Every term turns a whole
    /// number of times in one loop, so the loop closes.
    private func walkAt(_ t: Double) -> Vector2 {
        let turn = 2 * Double.pi * t / CuadradosOlivaYNegro.loop
        return Vector2(standX + sway * sin(turn) + unrest * sin(9 * turn + 1.3),
                       standY + sway * 0.12 * sin(2 * turn) + unrest * 0.6 * sin(14 * turn + 0.4))
    }

    private func toCanvas(_ p: Vector2) -> Vector2 {
        CuadradosOlivaYNegro.center + p * CuadradosOlivaYNegro.pxPerCm
    }

    /// Where a point standing `d` centimeters in front of the ground lands on
    /// the ground, seen from where you stand.
    private func seen(_ p: Vector2, standing d: Double, from viewer: Vector2) -> Vector2 {
        let k = distance / (distance - d)
        return toCanvas(viewer + (p - viewer) * k)
    }

    /// Where the lamp throws that point's shadow on the ground.
    private func shadowOf(_ p: Vector2, standing d: Double) -> Vector2 {
        toCanvas(p + CuadradosOlivaYNegro.lamp * d)
    }

    /// The corners of a square of side `side` about `middle`, turned.
    private func corners(_ middle: Vector2, side: Double, turn: Double, grow: Double = 0) -> [Vector2] {
        let h = side / 2 + grow
        return [Vector2(-h, -h), Vector2(h, -h), Vector2(h, h), Vector2(-h, h)].map { middle + $0.rotated(by: turn) }
    }

    // MARK: - The panel

    /// The left edge of the lines and how many there are: a whole number of
    /// pitches, the first edge on a whole pixel, so every line is crisp.
    private var lines: (left: Double, count: Int) {
        let pitch = Double(stripePitch)
        let c = CuadradosOlivaYNegro.center.x, s = CuadradosOlivaYNegro.pxPerCm
        let span = 2 * CuadradosOlivaYNegro.boardHalfWidth * s
        let count = Int((span / pitch).rounded(.down))
        let left = (c - CuadradosOlivaYNegro.boardHalfWidth * s + (span - Double(count) * pitch) / 2).rounded()
        return (left, count)
    }

    private func drawPanel() {
        let half = CuadradosOlivaYNegro.panelHalf
        // The panel stands off the wall, and its shadow falls below it.
        for (i, spread) in [1.8, 1.0, 0.3].enumerated() {
            fill(Color(white: 0, alpha: 0.07 + 0.05 * Double(i)))
            let a = toCanvas(Vector2(-half + 0.4 - spread, -half + 2.2 - spread))
            let b = toCanvas(Vector2(half + 0.4 + spread, half + 2.2 + spread))
            drawRect(a.x, a.y, b.x - a.x, b.y - a.y)
        }
        fill(CuadradosOlivaYNegro.black)
        let a = toCanvas(Vector2(-half, -half)), b = toCanvas(Vector2(half, half))
        drawRect(a.x, a.y, b.x - a.x, b.y - a.y)

        // Each board: the white first, then every black line at its own
        // multiple of the pitch. The lines are drawn as polygons, which keep
        // a two-pixel white line white between two black ones.
        let pitch = Double(stripePitch)
        let (left, count) = lines
        let lead = (pitch / 4).rounded(.down)
        for board in CuadradosOlivaYNegro.boards {
            let top = toCanvas(Vector2(0, board.top)).y.rounded()
            let bottom = toCanvas(Vector2(0, board.bottom)).y.rounded()
            fill(CuadradosOlivaYNegro.paper)
            drawPolygon([Vector2(left, top), Vector2(left + Double(count) * pitch, top),
                         Vector2(left + Double(count) * pitch, bottom), Vector2(left, bottom)])
            fill(CuadradosOlivaYNegro.black)
            for i in 0 ..< count {
                let x = left + Double(i) * pitch + lead
                drawPolygon([Vector2(x, top), Vector2(x + pitch / 2, top),
                             Vector2(x + pitch / 2, bottom), Vector2(x, bottom)])
            }
        }
    }

    // MARK: - The squares

    /// A square's shadow and its post's, soft at the edge: a few layers, each
    /// a little wider and fainter. They lie on the ground, so they do not
    /// move when you do.
    private func drawShadows() {
        guard shadow > 0 else { return }
        let layers = isVectorExporting ? 1 : 5
        for plate in plates {
            let d = depth * plate.depthShare
            for layer in 0 ..< layers {
                let grow = layers == 1 ? 0 : -1.0 + 0.5 * Double(layer)
                fill(Color(white: 0, alpha: shadow / Double(layers)))
                drawPolygon(corners(plate.middle, side: CuadradosOlivaYNegro.squareSide, turn: plate.turn, grow: grow)
                    .map { shadowOf($0, standing: d) })
                // The post's shadow runs from its foot to the square's.
                let foot = corners(plate.middle, side: CuadradosOlivaYNegro.postSide, turn: plate.turn, grow: grow * 0.5)
                drawPolygon(convexHull(of: foot.map { toCanvas($0) } + foot.map { shadowOf($0, standing: d) }))
            }
        }
    }

    private func drawSquares(from viewer: Vector2) {
        // Every post first, so no post is drawn over a neighbor's square.
        fill(CuadradosOlivaYNegro.black)
        for plate in plates {
            let d = depth * plate.depthShare
            let foot = corners(plate.middle, side: CuadradosOlivaYNegro.postSide, turn: plate.turn)
            drawPolygon(convexHull(of: foot.map { toCanvas($0) } + foot.map { seen($0, standing: d, from: viewer) }))
        }
        for plate in plates {
            let d = depth * plate.depthShare
            fill(plate.isBlack ? CuadradosOlivaYNegro.black : accent)
            drawPolygon(corners(plate.middle, side: CuadradosOlivaYNegro.squareSide, turn: plate.turn)
                .map { seen($0, standing: d, from: viewer) })
        }
    }

    /// The sixteen squares on their lattice, each a hair off true as
    /// `handSeed` deals it, the way plates set by hand are.
    private func makePlates() -> [Plate] {
        var rng = SplitMix64(seed: UInt64(handSeed) &* 1_000_003 &+ 66)
        let n = CuadradosOlivaYNegro.squaresPerSide
        let off = tolerance / 10
        var out: [Plate] = []
        for row in 0 ..< n {
            for column in 0 ..< n {
                let lattice = Vector2((Double(column) - Double(n - 1) / 2) * CuadradosOlivaYNegro.squareSpacing,
                                      (Double(row) - Double(n - 1) / 2) * CuadradosOlivaYNegro.squareSpacing)
                let slip = Vector2(Double.random(in: -1 ... 1, using: &rng), Double.random(in: -1 ... 1, using: &rng)) * off
                let turn = Double.random(in: -1 ... 1, using: &rng) * tolerance * 0.2 * Double.pi / 180
                let share = 1 - Double.random(in: 0 ... 1, using: &rng) * off * 0.2
                out.append(Plate(middle: lattice + slip, turn: turn, depthShare: share, isBlack: row < blackRows))
            }
        }
        return out
    }
}

↑↓ move ↩ open