GrayStructure
The first Structure, placed by hand under one rule about neighbors, after Sýkora's Gray Structure (1962 to 1963).
- Run
swift run --package-path Examples Example-Recreations-ZdenekSykora-GrayStructure- Source
- Examples/Recreations/ZdenekSykora/GrayStructure/Sketch.swift
// Recreation after Zdeněk Sýkora - Gray Structure (Šedá struktura, 1962 to
// 1963), oil on canvas, 150 by 120 centimeters. It is the first of his
// Structures: a grid of rectangles, each cut into five fields of black,
// white, and two grays. He placed them by hand, before he ever used a
// computer. 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 of the
// painting in two interviews and from the estate's picture of it. The
// interviews are with Josef Hlaváček (1968) and with Vítek Čapek (1982 to
// 1985). Nothing was ported: there was no program, and he said he built it
// by instinct. The grid, the shape of the element, the four tones, and how
// often a tone meets itself were read off that picture. It is small, 361 by
// 450 pixels, so the counts are close and not exact. The order the hand
// works in and the way it draws among its choices are this sketch's own.
import Ollin
/// Gray Structure (Zdeněk Sýkora, Louny and Prague, 1962 to 1963). Sýkora
/// was reading Paul Klee on how a structure is built by repeating one unit.
/// He divided a canvas by a grid of rectangles and cut every rectangle into
/// the same five fields. Then he let the fields alternate between black,
/// white, and two grays, and tried to have a division repeat as little as
/// possible. Twenty years later he gave the basic rule this way. Two
/// elements that are the same should not have the same elements bordering
/// them. He kept it by eye. The wish for more rules than an eye can keep
/// took him to a mathematician and a computer the next year.
///
/// The estate's picture shows 18 by 18 rectangles, each four wide and five
/// high. In each, two triangles stand on the left and right sides and point
/// at each other. Their tips reach three quarters of the way across, so they
/// overlap in a diamond, and a field is left above and below. The diamond is
/// white in 289 of the 324 rectangles. In the others it takes the tone of
/// one of its triangles, and that triangle lies whole over the other. The
/// four outer fields are black, dark gray, or light gray, never white.
///
/// This sketch places the elements row by row. Inside an element no triangle
/// shares a tone with the field above or below it. That leaves eighteen ways
/// to divide the three tones. The hand leans toward the ones the painting
/// uses most: two triangles in different tones, with one tone above and
/// below. `touching` is how readily it lets a tone meet itself across the
/// side two rectangles share. At 0 it never does where it can help it, and
/// at 1 it does not mind. In the painting that happens at about one shared
/// side in seven. `whites` is the share of diamonds left white. His rule is
/// kept exactly: a division is passed over if it would give two equal
/// elements the same four neighbors, in any order. `columns` and `rows` are
/// the grid. The seed deals the painting, and a press deals the next one.
///
/// Over one cycle of `seconds` the grid and the triangles are ruled in
/// pencil. Then the canvas is painted one tone at a time, black first and
/// white last. A tone on its own shows how the hand keeps it apart, and
/// where it lets two fields of it join into a larger shape. Then the
/// painting holds and fades back, which is also the export loop.
///
/// Try it: `--param touching=0` keeps every tone apart, and `--param
/// touching=1` lets the tones pool into diamonds and bow ties. Every field
/// exports as one polygon. So `--export-svg` with a frame in the hold gives
/// the whole painting back, and the rule can be checked from the file.
@main
final class GrayStructure: Sketch {
/// How many rectangles across. The painting has 18.
@Param(4 ... 30, icon: "square.grid.3x3") var columns = 18
/// How many rectangles down. The painting has 18.
@Param(4 ... 30, icon: "line.3.horizontal") var rows = 18
/// The share of diamonds left white. In the painting, 289 of 324.
@Param(0 ... 1, icon: "diamond") var whites = 0.89
/// How readily a tone may meet itself across a shared side. 0 is never
/// where it can be helped, and 1 is as often as chance brings it.
@Param(0 ... 1, icon: "square.split.2x1") var touching = 0.33
/// The length of one cycle, in seconds.
@Param(8 ... 30, icon: "clock") var seconds = 24.0
override var canvasSize: CanvasSize { .square(1080) }
override var loopDuration: Double? { seconds }
/// The four paints, darkest first, which is also the order they go on.
private enum Tone: Int, CaseIterable {
case black, dark, light, white
}
/// The five fields of an element, in the order one rectangle is painted.
private enum Field: CaseIterable {
case center, left, right, top, bottom
}
/// One rectangle: the tone of each of its five fields.
private struct Element: Equatable {
var left: Tone, right: Tone, top: Tone, bottom: Tone, center: Tone
/// A number that is the same for two elements painted the same.
var code: Int {
[left, right, top, bottom, center].reduce(0) { $0 * 4 + $1.rawValue }
}
func tone(of field: Field) -> Tone {
switch field {
case .center: center
case .left: left
case .right: right
case .top: top
case .bottom: bottom
}
}
}
/// A way to give the four outer fields their tones, and how much the hand
/// leans toward it.
private struct Division {
var left: Tone, right: Tone, top: Tone, bottom: Tone
var leaning: Double
}
/// The eighteen divisions in which no triangle shares a tone with the
/// field above or below it. The leanings are the painting's own. Of its
/// rectangles, 270 were read as one of these. In 147 the two triangles
/// differ and one tone lies above and below. In 70 the triangles are
/// alike, and in 53 the tones above and below differ.
private static let divisions: [Division] = {
let grays: [Tone] = [.black, .dark, .light]
var out: [Division] = []
for top in grays {
for bottom in grays {
for left in grays where left != top && left != bottom {
for right in grays where right != top && right != bottom {
let leaning = top != bottom ? 1.0 : left != right ? 2.8 : 1.3
out.append(Division(left: left, right: right, top: top, bottom: bottom,
leaning: leaning))
}
}
}
}
return out
}()
private struct Dealt: Equatable {
var columns: Int, rows: Int, whites: Double, touching: Double, seed: Int
}
// The wall, the canvas, the pencil, and the four oil paints, the paints
// sampled from the estate's picture.
private let wall = Color(hex: 0xA8A39B)
private let primed = Color(hex: 0xE9E3D6)
private let pencil = Color(hex: 0x8E8A84)
private static let paints: [Color] = [0x27212A, 0x3D3B48, 0x807E8B, 0xEDEBE7].map { Color(hex: $0) }
/// How far across its rectangle a triangle's tip reaches.
private let reach = 0.75
private var canvas = Rectangle(x: 0, y: 0, width: 1, height: 1)
/// The ruled lines, in the order they are drawn.
private var ruled: [[Vector2]] = []
/// Every field as a polygon with its paint, in the order it is painted.
private var fields: [(paint: Color, outline: [Vector2])] = []
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(columns: columns, rows: rows, whites: whites, touching: touching,
seed: variation)
if dealtFor != wanted {
dealtFor = wanted
construct(deal())
}
background(wall)
noStroke()
fill(primed)
drawRect(canvas)
let phase = loopProgress(over: seconds)
let constructed = 0.16, painted = 0.8, held = 0.93
// The pencil, line after line, until the paint has covered it.
if phase < painted {
let drawn = Double(ruled.count) * min(1, phase / constructed)
noFill()
stroke(pencil)
strokeWeight(1)
for (i, line) in ruled.enumerated() where Double(i) < drawn {
drawPolyline(start(of: line, min(1, drawn - Double(i))))
}
noStroke()
}
// The paint, one field at a time, the newest still thin. After the
// hold every field thins again, down to the bare canvas, so the end
// of a cycle is the start of the next.
let laid = phase < constructed ? 0
: Double(fields.count) * min(1, (phase - constructed) / (painted - constructed))
let left = 1 - smoothstep(held, 1, phase)
for (i, field) in fields.enumerated() where Double(i) < laid {
fill(field.paint.withAlpha(min(1, laid - Double(i)) * left))
drawPolygon(field.outline)
}
}
/// The first part of a polyline, by its share of the whole length.
private func start(of line: [Vector2], _ share: Double) -> [Vector2] {
guard share < 1, line.count > 1 else { return line }
let lengths = zip(line, line.dropFirst()).map { ($1 - $0).length }
var left = lengths.reduce(0, +) * share
var out = [line[0]]
for (i, length) in lengths.enumerated() {
if left >= length {
out.append(line[i + 1])
left -= length
} else {
out.append(line[i] + (line[i + 1] - line[i]) * (length > 0 ? left / length : 0))
break
}
}
return out
}
/// Places an element in every rectangle, row by row.
private func deal() -> [[Element]] {
randomSeed(variation)
var cells: [[Element]] = []
// Every finished neighborhood: an element's code, then the codes of
// the elements that border it, sorted, so their order does not count.
var kept = Set<[Int]>()
func neighborhood(_ r: Int, _ c: Int, in grid: [[Element]]) -> [Int] {
var around: [Int] = []
for (dr, dc) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
let (nr, nc) = (r + dr, c + dc)
if nr >= 0, nr < grid.count, nc >= 0, nc < grid[nr].count {
around.append(grid[nr][nc].code)
}
}
return [grid[r][c].code] + around.sorted()
}
for r in 0 ..< rows {
cells.append([])
for c in 0 ..< columns {
let beside = c > 0 ? cells[r][c - 1] : nil
let above = r > 0 ? cells[r - 1][c] : nil
var weights = Self.divisions.map { division -> Double in
var weight = division.leaning
if let beside, division.left == beside.right { weight *= touching }
if let above, division.top == above.bottom { weight *= touching }
return weight
}
let white = random() < whites
let wholeLeft = random() < 0.5
// The rectangles whose surroundings this one completes. That
// is the one above it. Along the last row it is also the one
// before it, and at the very end the rectangle itself.
var completed: [(Int, Int)] = []
if r > 0 { completed.append((r - 1, c)) }
if r == rows - 1 {
if c > 0 { completed.append((r, c - 1)) }
if c == columns - 1 { completed.append((r, c)) }
}
// The hand tries the divisions in the order of a weighted
// draw, and takes the first that keeps the rule.
var left = Array(Self.divisions.indices)
var chosen: Element?
var fresh: [[Int]] = []
while chosen == nil, !left.isEmpty {
let open = left.map { weights[$0] }
let pick = open.contains { $0 > 0 } ? randomChoice(left, weights: open) : left[0]
left.removeAll { $0 == pick }
weights[pick] = 0
let division = Self.divisions[pick]
let element = Element(left: division.left, right: division.right,
top: division.top, bottom: division.bottom,
center: white ? .white : wholeLeft ? division.left : division.right)
var grid = cells
grid[r].append(element)
fresh = completed.map { neighborhood($0.0, $0.1, in: grid) }
if Set(fresh).count == fresh.count, fresh.allSatisfy({ !kept.contains($0) }) {
chosen = element
} else if left.isEmpty {
// Eighteen divisions all repeat a neighborhood: it
// has not happened in any seed tried. The last one
// tried stands, so the canvas is never left open.
chosen = element
}
}
cells[r].append(chosen!)
kept.formUnion(fresh)
}
}
return cells
}
/// Lays the dealt elements out on the canvas: the lines to rule, and the
/// fields to paint, tone after tone.
private func construct(_ cells: [[Element]]) {
// A rectangle is four wide and five high, and the canvas takes the
// shape that many of them make.
let across = 4 * Double(columns), down = 5 * Double(rows)
let unit = 0.86 * min(width / across, height / down)
canvas = Rectangle(x: (width - across * unit) / 2, y: (height - down * unit) / 2,
width: across * unit, height: down * unit)
let w = 4 * unit, h = 5 * unit
// Where the two triangles' sides cross, above and below the middle.
let rise = h / (4 * reach)
func corner(_ r: Int, _ c: Int) -> Vector2 {
Vector2(canvas.x + Double(c) * w, canvas.y + Double(r) * h)
}
// The pencil: the grid, then down every column the sides of the
// triangles on its left edge and of those on its right.
ruled = (0 ... rows).map { [corner($0, 0), corner($0, columns)] }
+ (0 ... columns).map { [corner(0, $0), corner(rows, $0)] }
for c in 0 ..< columns {
var fromLeft: [Vector2] = [], fromRight: [Vector2] = []
for r in 0 ..< rows {
fromLeft += [corner(r, c), corner(r, c) + Vector2(reach * w, h / 2)]
fromRight += [corner(r, c + 1), corner(r, c + 1) + Vector2(-reach * w, h / 2)]
}
ruled += [fromLeft + [corner(rows, c)], fromRight + [corner(rows, c + 1)]]
}
// The paint: every field of one tone, in reading order, then the next.
fields = []
for tone in Tone.allCases {
for r in 0 ..< rows {
for c in 0 ..< columns {
let o = corner(r, c)
for field in Field.allCases where cells[r][c].tone(of: field) == tone {
let outline: [Vector2] = switch field {
case .center:
[Vector2((1 - reach) * w, h / 2), Vector2(w / 2, rise),
Vector2(reach * w, h / 2), Vector2(w / 2, h - rise)]
case .left:
[Vector2(0, 0), Vector2(w / 2, rise), Vector2((1 - reach) * w, h / 2),
Vector2(w / 2, h - rise), Vector2(0, h)]
case .right:
[Vector2(w, 0), Vector2(w, h), Vector2(w / 2, h - rise),
Vector2(reach * w, h / 2), Vector2(w / 2, rise)]
case .top:
[Vector2(0, 0), Vector2(w, 0), Vector2(w / 2, rise)]
case .bottom:
[Vector2(0, h), Vector2(w / 2, h - rise), Vector2(w, h)]
}
fields.append((Self.paints[tone.rawValue], outline.map { o + $0 }))
}
}
}
}
}
}