Ollin

Chromointerference

A panel of fine colored lines, and a sheet of black lines that a motor slides across it, after Cruz-Diez's Chromointerférence mécanique (1967).

Run
swift run --package-path Examples Example-Recreations-CarlosCruzDiez-Chromointerference
Source
Examples/Recreations/CarlosCruzDiez/Chromointerference/Sketch.swift
//  Recreation after Carlos Cruz-Diez - the Chromointerférences (from 1964):
//  a ground printed with fine lines of color, and a transparent sheet of
//  lines moved over it. One was looked at closely: "Chromointerférence
//  mécanique" (Paris, 1967; screenprint on paper and plastic, motor, wood,
//  metal, 66 x 140 cm; Centre Pompidou, Paris). A homage, not a
//  reproduction, and not affiliated with or endorsed by the artist, his
//  family, the Cruz-Diez Foundation, the Atelier Cruz-Diez, Articruz, or the
//  museum that holds the work.
//  https://cruz-diez.com/works/chromointerference/
//  https://www.centrepompidou.fr/en/ressources/oeuvre/c5nbr7e
//
//  An original Ollin interpretation, written from the foundation's account of
//  how the series began and from the museum's photographs of the work.
//  Nothing was ported. Four things were measured off those photographs: the
//  line every centimeter, the three rows of modules, the four inks, and how
//  far the lines lean. What the sheet carries is read from the foundation's
//  sentence, "the same arrangement of lines", and from the photographs, as
//  the panel's own black lines. The layouts are this sketch's own, dealt by
//  seed, and the motor's speed is a guess.

import Foundation
import Ollin

/// A *Chromointerférence mécanique* (Carlos Cruz-Diez, Paris). **Move the
/// pointer** to slide the sheet across the panel.
///
/// In 1964 he was screen printing the fine colored lines of his *Couleur
/// Additive* pictures. He laid over them a transparent plastic sheet
/// "bearing the same arrangement of lines", in the words of the foundation
/// that keeps his work. As he moved the sheet, colors came and went where
/// the two sets of lines crossed. The foundation calls them colors "that
/// had no chemical counterpart in the support". Waves ran through them as
/// the sheet moved, and he called what he saw "false prisms". In the
/// mechanical works that followed, a motor moves the sheet.
///
/// The panel here follows the one in Paris. It has a line every `pitch`
/// centimeters and three rows of modules, a short row above and below a
/// tall one. In each module up to four sets of lines are printed on the
/// paper at the same spacing. One to three are colors, out of red, green,
/// and blue, and the black goes over them. Each set leans a little, by no
/// more than `lean`, and the colors lean up to `drift` more or less than
/// the black. So along a module's height each color slips out from behind
/// the black on one side and back under it on the other.
///
/// The sheet is 60 centimeters wide and carries the black lines of the part
/// of the panel it covers at rest. Set `position` and `travel` to 0 and it
/// sits in register. Every one of its lines then lies on a printed black
/// line, and the panel looks as if the sheet were not there. Slide it and
/// its lines cover the paper, or one color, or another. Over its own module
/// the change comes as one wide wash. Over a module that leans another way
/// the two sets of black cross again and again. Bands run up or down the
/// module, many times faster than the sheet itself moves.
///
/// What is on the canvas is the paper and four inks, and the sheet adds
/// only black. Every other color you see is mixed in your eye from lines
/// too fine to tell apart, as in the *Couleur Additive*. The sheet changes
/// the mix by hiding one ink here and another there, so the colors come in
/// bands, and the bands move.
///
/// While the pointer rests, the motor sways the sheet `travel` centimeters
/// around `position`. Try it: `showsSheet` lifts the sheet off, and
/// `panelSeed` or a click deals another panel. `--export-svg` writes every
/// line as a polygon, the sheet's last. The spacing, the leans, and the
/// sheet's place can be read back from the file.
@main
final class Chromointerference: Sketch {
    /// The distance from one line to the next, in centimeters. The panel in
    /// Paris has a line every centimeter.
    @Param(0.6 ... 2, icon: "line.3.horizontal") var pitch = 1.0
    /// The most a module's lines lean: how far a line moves sideways for
    /// each unit it runs down.
    @Param(0 ... 0.2, icon: "arrow.up.right") var lean = 0.12
    /// The most a color's lean differs from the black's in its module. This
    /// sets how soon the colors cross behind the black.
    @Param(0 ... 0.05, icon: "arrow.triangle.branch") var drift = 0.03
    /// Where the sheet rests, in centimeters from the panel's left end. At 0
    /// it is in register with the lines under it.
    @Param(0 ... 80, icon: "arrow.left.and.right") var position = 34.0
    /// How far the motor sways the sheet while the pointer rests, in
    /// centimeters; 0 stops the motor. The motor is held to two and a half
    /// lines a second at its fastest, so a long sway is cut short.
    @Param(0 ... 40, icon: "gearshape") var travel = 6.0
    /// Whether the sheet is on the panel.
    @Param(icon: "square.on.square") var showsSheet = true
    /// Deals the panel: its modules, their inks, and their leans.
    @Param(1 ... 999, icon: "dice") var panelSeed = 3

    override var canvasSize: CanvasSize { .fhd1080 }
    override var loopDuration: Double? { travel > 0 && showsSheet ? Chromointerference.loop : nil }

    /// The motor brings the sheet back to its start after this many seconds.
    nonisolated static let loop = 24.0
    /// The most lines the motor carries the sheet past in a second. Every
    /// line it passes turns the whole area under the sheet darker and
    /// lighter once, and wide areas should not flash fast.
    nonisolated static let fastest = 2.5
    /// The panel and the sheet, in centimeters.
    nonisolated static let panelWidth = 140.0
    nonisolated static let panelHeight = 66.0
    nonisolated static let sheetWidth = 60.0
    /// The three rows of modules, from the top: where each begins and ends.
    nonisolated static let rows: [(Double, Double)] = [(0, 11), (11, 55), (55, 66)]
    /// Pixels per centimeter, and the panel's corner on the canvas.
    nonisolated static let scale = 12.0
    nonisolated static let corner = Vector2(120, 144)

    // MARK: - The inks

    /// The paper, then the inks: black, blue, red, and green. They are close
    /// to the ones in the museum's photographs.
    nonisolated static let paper = Color(hex: 0xEDE9DC)
    nonisolated static let inks = [Color(hex: 0x1C1D1C), Color(hex: 0x2B4FB3),
                                   Color(hex: 0xE8502A), Color(hex: 0x3FC455)]
    /// A line's width as a share of the pitch: the black, then a color.
    nonisolated static let blackWidth = 0.30
    nonisolated static let colorWidth = 0.27
    nonisolated static let wall = Color(hex: 0xD6D6D6)

    // MARK: - The panel

    /// One set of parallel lines in a module. `ink` is its ink, and 0 is
    /// black. `slope` is how far a line moves sideways for each unit down.
    /// `phase` is where its lines stand at the module's middle height, as a
    /// share of the pitch.
    struct LineSet: Equatable {
        var ink: Int
        var slope: Double
        var phase: Double
    }

    /// A module, in centimeters from the panel's corner, and its sets in
    /// the order they are printed: the colors, then the black over them.
    struct Module: Equatable {
        var x0, x1, y0, y1: Double
        var sets: [LineSet]
    }

    /// One printed line, or one line of the sheet: its ink and its outline
    /// in centimeters.
    struct Piece {
        var ink: Int
        var outline: [Vector2]
    }

    /// The modules of one panel, dealt by `seed`. Each row is cut into
    /// modules 10 to 26 centimeters wide. Each module gets a lean for its
    /// black lines, and one to three colors that lean a little more or a
    /// little less.
    nonisolated static func deal(seed: Int, lean: Double, drift: Double) -> [Module] {
        var rng = SplitMix64(seed: UInt64(bitPattern: Int64(seed &* 1_000_003 &+ 67)))
        var modules: [Module] = []
        for (y0, y1) in rows {
            var x = 0.0
            while x < panelWidth {
                // What is left after a module is nothing, or room for another.
                var width = Double(Int.random(in: 10 ... 26, using: &rng))
                let rest = panelWidth - x
                if rest <= 26 { width = rest } else if rest - width < 10 { width = rest - 10 }
                let slope = Double.random(in: -1 ... 1, using: &rng) * lean
                // Which colors, and in which order they are printed.
                var colors = [1, 2, 3]
                colors.shuffle(using: &rng)
                let count = [1, 2, 2, 3, 3, 3][Int.random(in: 0 ..< 6, using: &rng)]
                var sets: [LineSet] = []
                for (i, ink) in colors.prefix(count).enumerated() {
                    // At the module's middle the colors stand side by side
                    // in the space the black leaves. Their own leans carry
                    // them under the black toward the top or the bottom.
                    let place = (Double(i) + 0.5 + Double.random(in: -0.2 ... 0.2, using: &rng)) / Double(count)
                    let more = Double.random(in: 0.3 ... 1, using: &rng) * (Bool.random(using: &rng) ? 1 : -1)
                    sets.append(LineSet(ink: ink, slope: slope + more * drift, phase: 0.3 + 0.7 * place))
                }
                sets.append(LineSet(ink: 0, slope: slope, phase: Double.random(in: 0 ..< 1, using: &rng)))
                // The black's place moves every other set with it, so the
                // colors keep their places beside it.
                let black = sets[sets.count - 1].phase
                for i in 0 ..< sets.count - 1 { sets[i].phase += black }
                modules.append(Module(x0: x, x1: x + width, y0: y0, y1: y1, sets: sets))
                x += width
            }
        }
        return modules
    }

    /// The lines of one set inside the part of its module from `from` to
    /// `to` across. They are slim leaning bars, cut off straight at the two
    /// sides.
    nonisolated static func lines(of set: LineSet, in module: Module, from: Double, to: Double, pitch: Double) -> [[Vector2]] {
        let width = (set.ink == 0 ? blackWidth : colorWidth) * pitch
        let middle = (module.y0 + module.y1) / 2
        let reach = abs(set.slope) * (module.y1 - module.y0) / 2 + width
        let first = Int(((from - module.x0 - reach) / pitch - set.phase).rounded(.down))
        let last = Int(((to - module.x0 + reach) / pitch - set.phase).rounded(.up))
        var out: [[Vector2]] = []
        guard last >= first else { return out }
        for n in first ... last {
            let center = module.x0 + (Double(n) + set.phase) * pitch
            let top = center + set.slope * (module.y0 - middle)
            let bottom = center + set.slope * (module.y1 - middle)
            let bar = [Vector2(top - width / 2, module.y0), Vector2(top + width / 2, module.y0),
                       Vector2(bottom + width / 2, module.y1), Vector2(bottom - width / 2, module.y1)]
            let cut = clip(bar, from: from, to: to)
            if cut.count >= 3 { out.append(cut) }
        }
        return out
    }

    /// The part of a convex outline that lies between `from` and `to`
    /// across.
    nonisolated static func clip(_ outline: [Vector2], from: Double, to: Double) -> [Vector2] {
        func cut(_ points: [Vector2], at x: Double, keepingRight: Bool) -> [Vector2] {
            var out: [Vector2] = []
            for i in points.indices {
                let p = points[i], q = points[(i + 1) % points.count]
                let pIn = keepingRight ? p.x >= x : p.x <= x
                let qIn = keepingRight ? q.x >= x : q.x <= x
                if pIn { out.append(p) }
                if pIn != qIn {
                    let t = (x - p.x) / (q.x - p.x)
                    out.append(Vector2(x, p.y + (q.y - p.y) * t))
                }
            }
            return out
        }
        let right = cut(outline, at: from, keepingRight: true)
        return right.count < 3 ? [] : cut(right, at: to, keepingRight: false)
    }

    private var modules: [Module] = []
    private var printed: [Piece] = []
    private var sheet: [Piece] = []
    private var builtFor: [Double] = []
    private var shown = 0.0
    private var shownReady = false
    private var nudge = 0.0
    private var lastMoved = -100.0

    /// A click deals the next panel.
    override func mousePressed() {
        panelSeed = panelSeed >= 999 ? 1 : panelSeed + 1
    }

    /// Deals the panel and lays out every line once; the sheet's lines are
    /// kept where they stand when it is in register.
    private func build() {
        modules = Chromointerference.deal(seed: panelSeed, lean: lean, drift: drift)
        printed = []
        sheet = []
        for module in modules {
            for set in module.sets {
                for outline in Chromointerference.lines(of: set, in: module, from: module.x0, to: module.x1, pitch: pitch) {
                    printed.append(Piece(ink: set.ink, outline: outline))
                }
            }
            // The sheet carries the black lines of what it covers at rest.
            let to = min(module.x1, Chromointerference.sheetWidth)
            if module.x0 < to, let black = module.sets.last {
                for outline in Chromointerference.lines(of: black, in: module, from: module.x0, to: to, pitch: pitch) {
                    sheet.append(Piece(ink: 0, outline: outline))
                }
            }
        }
    }

    // MARK: - The motor and the hand

    /// Where the sheet's left edge is, in centimeters from the panel's. The
    /// pointer leads it while it moves; when it has rested a while the motor
    /// sways the sheet around the place it was left.
    private func sheetPosition() -> Double {
        let limit = Chromointerference.panelWidth - Chromointerference.sheetWidth
        // The sway is `travel`, held to what the fastest speed allows over
        // one lap.
        let reach = min(travel, Chromointerference.fastest * pitch * Chromointerference.loop / Double.pi)
        let sway = reach / 2 * sin(2 * Double.pi * time / Chromointerference.loop)
        if mouse != previousMouse { lastMoved = time }
        if time - lastMoved < 4 {
            let pointer = (mouseX - Chromointerference.corner.x) / Chromointerference.scale
            let target = min(limit, max(0, pointer - Chromointerference.sheetWidth / 2))
            if !shownReady {
                shown = target
                shownReady = true
            } else {
                shown += (target - shown) * (1 - exp(-deltaTime * 8))
            }
            nudge = shown - sway - position
            return shown
        }
        // At rest the place is a function of the clock alone, so an exported
        // frame does not depend on the frames before it.
        shown = min(limit, max(0, position + nudge + sway))
        shownReady = true
        return shown
    }

    // MARK: - Drawing

    override func draw() {
        let key = [Double(panelSeed), pitch, lean, drift]
        if key != builtFor {
            build()
            builtFor = key
        }
        let at = sheetPosition()
        let scale = Chromointerference.scale
        let corner = Chromointerference.corner
        func point(_ p: Vector2, shifted dx: Double = 0) -> Vector2 {
            corner + Vector2(p.x + dx, p.y) * scale
        }
        func box(_ x0: Double, _ y0: Double, _ x1: Double, _ y1: Double) {
            drawPolygon([point(Vector2(x0, y0)), point(Vector2(x1, y0)), point(Vector2(x1, y1)), point(Vector2(x0, y1))])
        }

        background(Chromointerference.wall)
        noStroke()
        let w = Chromointerference.panelWidth, h = Chromointerference.panelHeight

        // The panel hangs a little off the wall on four dark blocks; its
        // shadow falls down and right.
        for (i, spread) in [0.8, 0.45, 0.15].enumerated() {
            fill(Color(white: 0, alpha: 0.04 + 0.03 * Double(i)))
            box(0.35 - spread, 0.5 - spread, w + 0.35 + spread, h + 0.5 + spread)
        }
        fill(Color(hex: 0x1A1A1A))
        for (x, y) in [(-1.6, -1.2), (w - 0.4, -1.2), (-1.6, h - 1.6), (w - 0.4, h - 1.6)] {
            box(x, y, x + 2, y + 2.8)
        }

        // The paper, then every printed line, module by module, each
        // module's colors first and its black last.
        fill(Chromointerference.paper)
        box(0, 0, w, h)
        var ink = -1
        for piece in printed {
            if piece.ink != ink {
                ink = piece.ink
                fill(Chromointerference.inks[ink])
            }
            drawPolygon(piece.outline.map { point($0) })
        }

        // The sheet: its black lines, moved along with it, and the two
        // metal rails it hangs from and runs in.
        if showsSheet {
            fill(Chromointerference.inks[0])
            for piece in sheet {
                drawPolygon(piece.outline.map { point($0, shifted: at) })
            }
            let x0 = at - 0.6, x1 = at + Chromointerference.sheetWidth + 0.6
            for (y0, y1) in [(-1.5, -0.1), (h + 0.1, h + 1.5)] {
                fill(Color(hex: 0x8D9296))
                box(x0, y0, x1, y1)
                fill(Color(hex: 0xB9BDC0))
                box(x0, y0, x1, y0 + 0.35)
            }
            // The sheet's two cut edges catch a little light.
            fill(Color(white: 1, alpha: 0.35))
            box(at - 0.05, 0, at + 0.05, h)
            box(at + Chromointerference.sheetWidth - 0.05, 0, at + Chromointerference.sheetWidth + 0.05, h)
        }

        if !isExporting {
            drawText("move the pointer to slide the sheet, click for another panel", width / 2, height - 30, size: 16,
                     color: Color(white: 0, alpha: 0.4), align: .center, .bottom)
        }
    }
}

↑↓ move ↩ open