# MovementInSquares

a checkerboard whose columns narrow to a fold and widen again, after Riley's Movement in Squares (1961).

Run it with `swift run --package-path Examples Example-Recreations-BridgetRiley-MovementInSquares`.

```swift
//  Recreation after Bridget Riley - Movement in Squares (1961), paint on
//  board, 123.2 x 121.2 cm, Arts Council Collection, Southbank Centre, London.
//  A homage, not a reproduction, and not affiliated with or endorsed by the
//  artist or the collection.
//  https://artscouncilcollection.org.uk/artwork/movement-squares
//  https://artsandculture.google.com/asset/movement-in-squares-bridget-riley/5gGo7raKbm-NtQ
//  https://www.lrb.co.uk/the-paper/v31/n19/bridget-riley/at-the-end-of-my-pencil
//
//  An original Ollin interpretation, written from her own account of drawing
//  it and from the collection's picture of the painting, read at two sizes.
//  The collection's record says the squares keep their height and only their
//  widths change. The thirty-one widths are measured off that picture, in
//  sixteenths of a square; no published account lists them. Nothing was
//  ported: the work is paint on board.

import Foundation
import Ollin

/// *Movement in Squares* (Bridget Riley, 1961): a checkerboard of black and
/// white, twelve rows high, whose columns narrow to a fold.
///
/// She wrote that one evening, on her way to the studio, she thought of
/// drawing a square. The first few held no discoveries. Then, as she went
/// on, the squares began to lose their original form. She drew the whole of
/// it without a pause and then painted each alternate space black, to see
/// more clearly what was there.
///
/// Every square keeps its height. Only the width changes, column by column.
/// The first three columns from the left are whole squares. The next ones
/// narrow, a little more each time, down to two slivers one sixteenth of a
/// square wide. Past them the columns widen again, and the board ends before
/// they are whole: the last is three quarters of a square. The slivers sit
/// about two thirds of the way across, so the flat board seems to fold there.
///
/// The painting is still and seems to move. Here the squares do move. They
/// come in whole at the left edge, travel to the right, are pressed thin at
/// the fold, and open again on the far side. The fold itself stays where it
/// is. One lap of `seconds` carries two columns past, a black one and a
/// white one, so the last frame meets the first.
///
/// Try it: `squeeze` at 0 is the plain checkerboard she started from, and
/// at 1 it is the painting's own widths. `fold` moves the fold across the
/// board, and `rows` changes how many rows share the height.
/// `--export-svg` writes one rectangle for every black space.
@main
final class MovementInSquares: Sketch {
    /// How many rows of squares share the height.
    @Param(2 ... 24, icon: "square.grid.3x3") var rows = 12
    /// How hard the columns are pressed: 0 is a plain checkerboard, and 1 is
    /// the painting's widths.
    @Param(0 ... 1, icon: "arrow.right.and.line.vertical.and.arrow.left") var squeeze = 1.0
    /// Where the fold sits across the board: 0 is the left edge, 1 the right.
    @Param(0.2 ... 0.8, icon: "arrow.left.and.right") var fold = 0.6492
    /// Seconds for one lap: two columns pass any place on the board.
    @Param(2 ... 30, icon: "clock") var seconds = 8.0

    override var loopDuration: Double? { seconds }

    /// The painting's black and its white.
    private let ink = Color(hex: 0x161616)
    private let ground = Color(hex: 0xFEFEFE)

    /// The widths of the painting's thirty-one columns from left to right, in
    /// sixteenths of a square's side, measured off the collection's picture.
    /// They add up to 191, one sixteenth short of twelve squares.
    nonisolated static let widths: [Double] = [
        16, 16, 16, 14, 13, 11, 8, 6, 5, 4, 3.5, 3, 2.5, 2, 1.75, 1.25, 1,
        1, 1.25, 1.75, 2, 2.5, 3, 3.5, 4, 5, 6, 7, 8, 10, 12,
    ]
    /// How many of those columns lie left of the fold, the line between the
    /// two narrowest.
    nonisolated static let leftOfFold = 17
    /// Whole squares that wait off the board on each side, so that the
    /// squares have somewhere to come from and somewhere to go.
    nonisolated static let spare = 24

    /// The edges of every column in the stream, in sixteenths of a square,
    /// counted from the fold. The painting's columns sit in the middle of the
    /// list. Before them come whole squares. After them the columns go on
    /// widening to whole squares, which the painting does not show.
    private func streamEdges() -> [Double] {
        let whole = [Double](repeating: 16, count: Self.spare)
        let all = whole + Self.widths + [14] + whole
        var edges: [Double] = [0]
        for width in all {
            edges.append(edges[edges.count - 1] + 16 + (width - 16) * squeeze)
        }
        let atFold = edges[Self.spare + Self.leftOfFold]
        return edges.map { $0 - atFold }
    }

    /// Where an edge sits when it is part of the way from its own place to
    /// the next edge's place. The path through the places is a smooth curve,
    /// so an edge slows down evenly as the columns ahead of it narrow.
    private func place(_ edges: [Double], at index: Double) -> Double {
        let i = min(max(Int(index.rounded(.down)), 1), edges.count - 3)
        let t = index - Double(i)
        let a = edges[i], b = edges[i + 1]
        let slopeA = (edges[i + 1] - edges[i - 1]) / 2
        let slopeB = (edges[i + 2] - edges[i]) / 2
        let t2 = t * t, t3 = t2 * t
        return (2 * t3 - 3 * t2 + 1) * a + (t3 - 2 * t2 + t) * slopeA
            + (-2 * t3 + 3 * t2) * b + (t3 - t2) * slopeB
    }

    override func setup() {
        noStroke()
    }

    override func draw() {
        background(ground)
        fill(ink)

        let side = height / Double(rows)
        // The painting's columns add up to 191 sixteenths against 192 for its
        // twelve rows, so the board is a hair narrower than it is tall. This
        // canvas is square. The columns are stretched by that half percent,
        // so the thirty-one of them fill it from edge to edge.
        let unit = side / 16 * (width / height) * 192 / 191
        let foldX = fold * width

        // Two columns pass in a lap, so every space gets its own color back.
        let travel = loopProgress(over: seconds) * 2

        let edges = streamEdges()
        let first = Self.spare      // the painting's first column in the list
        var x: [Double] = []
        x.reserveCapacity(edges.count)
        for j in 0 ..< edges.count {
            // An edge at rest sits on its own place. As the lap runs it moves
            // toward the place of the edge to its right.
            x.append(foldX + unit * place(edges, at: Double(j) + travel))
        }

        for row in 0 ..< rows {
            let top = Double(row) * side
            for j in 2 ..< x.count - 3 {
                // The top left square of the painting is white, and black
                // and white take turns along a row and down a column.
                guard (row + j - first) % 2 != 0 else { continue }
                // A column that shows less than a tenth of a pixel at an
                // edge of the canvas is left out.
                let left = max(0, x[j]), right = min(width, x[j + 1])
                guard right - left > 0.1 else { continue }
                drawRect(left, top, right - left, side)
            }
        }
    }
}
```
