ChessmenOne
Four rows of chessmen stacked from the square and the circle, each row taller than the last, after Shemza's Chessmen One (1961).
- Run
swift run --package-path Examples Example-Recreations-AnwarJalalShemza-ChessmenOne- Source
- Examples/Recreations/AnwarJalalShemza/ChessmenOne/Sketch.swift
// Recreation after Anwar Jalal Shemza - Chessmen One (1961, oil on canvas,
// 92 x 71 cm, Tate T13333). A homage, not a reproduction, and not affiliated
// with or endorsed by the artist or his estate.
// https://www.tate.org.uk/art/artworks/shemza-chessmen-one-t13333
// https://artuk.org/discover/artworks/chessmen-one-201808
// https://www.anwarshemza.com/
//
// An original Ollin interpretation, written from the published description
// of the painting and from looking at pictures of it. The description gives
// the rows, their colors, and the ground. How a man is stacked from tiers,
// and how many tiers a row has, are read off pictures of the work. The order
// of setting up is this sketch's own. There is no code to port.
import Ollin
/// Chessmen One (Anwar Jalal Shemza, 1961). Four rows of black chess pieces
/// stand on a pale blue ground, and each row is taller than the one above
/// it. Every row has a color of its own for the thin line that decorates its
/// men: red on the top row, then blue, cream, and green. The line each row
/// stands on, which divides it from the next, is painted in that color too.
/// The men are seen from the front and flat, like cut paper. Shemza painted
/// it the year he settled in Stafford, and he came back to chessmen through
/// the decade.
///
/// He said his pictures were built from two forms, the square and the
/// circle. Read off pictures of the painting, every man is a stack of tiers.
/// Every tier is a rectangle that a half circle, stretched to fit, has
/// changed. A half circle cut from each side leaves a waist. One swelling
/// from each side gives a rounded collar. One above makes a dome, one below
/// a bowl, and bites out of the top edge make a crown. The same half circles
/// are the detail, drawn in the row's color inside a tier. One arc standing
/// on a line is the letter D lying down, and two side by side are a B.
///
/// In pictures of the painting the four rows measure close to 5, 6, 7, and 8
/// parts in height, within about one part in twenty. So this sketch gives
/// each row one more tier than the row above it.
///
/// Try it: `rows` and `men` set how many rows there are and how many men
/// stand in each. `tiers` is how many tiers the top row's men have. `growth`
/// is how many tiers each row adds, so 0 makes every row the same height.
/// `detail` thins the arcs inside the men down to the outline and the tier
/// lines. The seed deals the men, and a press deals the next set.
///
/// Over one cycle of `seconds` the rows are set up from the top down. In
/// each row the line is drawn first, from the right. Then the men are
/// stacked on it one tier at a time, right to left, and the colored line
/// follows each man up. Then the picture holds and clears, which is also the
/// export loop.
///
/// `--export-svg` with a frame in the hold writes every man as one black
/// outline. His rim, his tier lines, and his arcs are strokes in the row's
/// color. So the file gives the rule back: the count of men, the heights of
/// the rows, and one color to a row.
@main
final class ChessmenOne: Sketch {
/// How many rows of chessmen the picture has.
@Param(2 ... 6, icon: "rectangle.grid.1x2") var rows = 4
/// How many chessmen stand in each row.
@Param(3 ... 8, icon: "person.3") var men = 5
/// How many tiers the men of the top row are stacked from.
@Param(3 ... 7, icon: "square.stack") var tiers = 5
/// How many tiers each row adds to the row above it.
@Param(0 ... 2, icon: "arrow.up.right") var growth = 1
/// How much of the arc detail inside the men is drawn.
@Param(0 ... 1, icon: "scribble") var detail = 0.85
/// One cycle: the rows are set up, held, and cleared.
@Param(8 ... 30, icon: "clock") var seconds = 24.0
// The painting is 92 cm tall and 71 cm wide.
override var canvasSize: CanvasSize { .size(834, 1080) }
override var loopDuration: Double? { seconds }
private enum Kind: Equatable {
/// A plain rectangle.
case block
/// A rectangle with a half circle cut from each side.
case waist
/// A rectangle with this many half circles swelling from each side.
case collar(Int)
/// The lower part of a disc: flat on top, round below.
case bowl
/// This many half discs side by side, round side up.
case dome(Int)
/// A rectangle with this many bites out of its top edge.
case crown(Int)
}
private enum Decor {
/// Nothing but the tier's own outline.
case plain
/// One arc standing on the tier's lower line.
case cap
/// One arc hanging from the tier's upper line.
case hang
/// Two arcs standing side by side.
case twinCaps
/// Two arcs hanging side by side.
case twinHangs
/// A standing arc and a hanging arc that close into an eye.
case eye
/// An arc on each side, facing in.
case sides
}
private struct Tier {
var kind: Kind
/// Half the tier's width.
var half: Double
/// The tier's lower edge and its height. Up is toward smaller y.
var bottom: Double
var height: Double
/// How far a side arc reaches, or how deep a crown's bites go.
var reach: Double
var decor: Decor
/// The arcs are drawn when this is under `detail`.
var chance: Double
}
/// One stroke of the colored line: a straight piece or an arc.
private enum Mark {
case line(Vector2, Vector2)
case arc(center: Vector2, radiusX: Double, radiusY: Double, start: Double, stop: Double)
}
private struct Man {
var axis: Double
var base: Double
var height: Double
var tiers: [Tier]
var silhouette: Shape
/// The outline again, a little inside the edge, where the colored
/// line runs.
var rim: Shape
var tierLines: [Mark]
/// The arcs, each with its tier's chance.
var arcs: [(mark: Mark, chance: Double)]
/// Where each tier ends, measured up from the base, for stacking.
var levels: [Double]
}
private struct Row {
var line: [Vector2]
/// The men in the order they are set up, right to left.
var men: [Man]
var paint: Int
var tiers: Int
}
private struct Dealt: Equatable {
var rows: Int, men: Int, tiers: Int, growth: Int, seed: Int
}
// The paints, read off pictures of the painting.
private let ground = Color(hex: 0x93ACBD)
private let fleck = Color(hex: 0x414A51)
private let bodies = [Color(hex: 0x33211F), Color(hex: 0x171B22), Color(hex: 0x1D1E1C), Color(hex: 0x1C211F)]
private let lines = [Color(hex: 0x9A3F31), Color(hex: 0x2F5BAB), Color(hex: 0xD9CA98), Color(hex: 0x86A796)]
private var picture: [Row] = []
private var dealtFor: Dealt?
private var pressed = false
/// The side of one tier, the unit everything else is measured in.
private var unit = 36.0
/// How far inside the black edge the colored line runs.
private var inset: Double { 0.0042 * height }
private var lineWidth: Double { 0.0026 * height }
private var bandWidth: Double { 0.009 * height }
/// The ground: pale blue brushed thin over a dark canvas, so the paint
/// skips threads of the weave in patches and the dark shows through as
/// short flecks.
private static let groundPaint = """
float4 shade(float2 uv, ShaderInfo info) {
float2 p = info.position;
float seed = param(info, 0);
float3 blue = float3(param(info, 1), param(info, 2), param(info, 3));
float3 dark = float3(param(info, 4), param(info, 5), param(info, 6));
// Where the brush ran dry, in soft patches, for threads each way.
float dryAcross = smoothstep(0.4, 0.8, fbm(p * 0.006 + seed));
float dryDown = smoothstep(0.4, 0.8, fbm(p * 0.006 + seed + 37.0));
// A thread shows as a short dash, broken by the grain of the paint.
float grain = smoothstep(0.3, 0.55, valueNoise(p * 0.7 + seed));
float across = step(hash12(floor(p / float2(5.0, 1.5)) + seed), 0.015 + dryAcross * 0.32);
float down = step(hash12(floor(p / float2(1.5, 5.0)) + seed + 91.0), 0.015 + dryDown * 0.32);
float skipped = max(across, down) * grain;
float tone = 0.97 + 0.06 * fbm(p * 0.02 + seed);
float3 color = mix(blue * tone, dark, skipped * 0.7);
return float4(color, param(info, 7));
}
"""
override func mousePressed() {
pressed = true
}
override func draw() {
if pressed {
pressed = false
seed(variation + 1)
}
let wanted = Dealt(rows: rows, men: men, tiers: tiers, growth: growth, seed: variation)
if dealtFor != wanted {
dealtFor = wanted
picture = deal()
}
background(ground)
drawGround(alpha: 1)
let phase = loopProgress(over: seconds)
let begun = 0.03, built = 0.8, held = 0.93
let building = min(1, max(0, (phase - begun) / (built - begun)))
// Each row takes a share of the building time in proportion to its
// tiers, so the stacking keeps one pace from the top row to the last.
let total = Double(picture.reduce(0) { $0 + $1.tiers })
var before = 0.0
for row in picture {
let share = Double(row.tiers) / total
// A hair over, so rounding never leaves the last row a hair short.
let progress = min(1, max(0, (building - before) / share + 1e-9))
before += share
if progress > 0 {
drawRow(row, progress: progress)
}
}
if phase > held {
drawGround(alpha: smoothstep(held, 1, phase))
}
}
/// The textured ground, which is also laid over the finished picture to
/// clear it. A vector file takes the flat color instead.
private func drawGround(alpha: Double) {
noStroke()
if isVectorExporting {
fill(ground.withAlpha(alpha))
} else {
fill(Shader(ChessmenOne.groundPaint, params: [
Float(variation % 97),
Float(ground.red), Float(ground.green), Float(ground.blue),
Float(fleck.red), Float(fleck.green), Float(fleck.blue),
Float(alpha),
]))
}
drawRect(0, 0, width, height)
}
/// One row: its line first, drawn from the right, then its men.
private func drawRow(_ row: Row, progress: Double) {
let body = bodies[row.paint % bodies.count]
let line = lines[row.paint % lines.count]
let ruled = 0.1
let drawn = min(1, progress / ruled)
func rule() {
noFill()
strokeJoin(.round)
strokeCap(.butt)
stroke(body)
strokeWeight(bandWidth)
drawPolyline(row.line)
stroke(line)
strokeWeight(lineWidth)
drawPolyline(row.line)
noStroke()
}
if drawn >= 1 {
rule()
} else {
withClip(Rectangle(x: width * (1 - drawn), y: 0, width: width * drawn, height: height)) {
rule()
}
}
// The men overlap in time, so one is still taking its line while the
// next is being stacked.
let count = Double(row.men.count)
let stacking = min(1, max(0, (progress - ruled) / (1 - ruled)))
let span = 1.7
for (index, man) in row.men.enumerated() {
let start = Double(index) / (count + span - 1)
let share = min(1, max(0, (stacking - start) * (count + span - 1) / span + 1e-9))
guard share > 0 else { continue }
drawMan(man, body: body, line: line, share: share)
}
}
/// One man. The black rises a tier at a time, and the colored line
/// climbs after it.
private func drawMan(_ man: Man, body: Color, line: Color, share: Double) {
let stacked = min(1, share / 0.62)
let lined = min(1, max(0, (share - 0.45) / 0.55))
let reach = man.tiers.map(\.half).max() ?? 0
func black() {
noStroke()
fill(body)
drawShape(man.silhouette)
}
func colored() {
noFill()
stroke(line)
strokeWeight(lineWidth)
strokeJoin(.round)
strokeCap(.round)
for contour in man.rim.contours {
drawPolyline(contour.points, closed: true)
}
for mark in man.tierLines {
drawMark(mark)
}
for arc in man.arcs where arc.chance < detail {
drawMark(arc.mark)
}
noStroke()
}
/// Everything of the man from the base up to a height.
func upTo(_ level: Double, _ body: () -> Void) {
withClip(Rectangle(x: man.axis - reach - 4, y: man.base - level,
width: 2 * reach + 8, height: level + 1)) {
body()
}
}
if stacked >= 1 {
black()
} else {
// Which tier is being set down, and how far it has come. A tier
// rises quickly and then rests before the next one starts.
let at = stacked * Double(man.tiers.count)
let tier = min(man.tiers.count - 1, Int(at))
let low = tier == 0 ? 0 : man.levels[tier - 1]
let level = low + (man.levels[tier] - low) * smoothstep(0, 0.7, at - Double(tier))
upTo(level, black)
}
guard lined > 0 else { return }
if lined >= 1 {
colored()
} else {
upTo(lined * man.height, colored)
}
}
private func drawMark(_ mark: Mark) {
switch mark {
case .line(let from, let to):
drawLine(from, to)
case .arc(let center, let radiusX, let radiusY, let start, let stop):
drawArc(center: center, radiusX: radiusX, radiusY: radiusY, start: start, stop: stop, mode: .open)
}
}
// MARK: - Dealing
/// Lays out the rows for the seed and stacks every man.
private func deal() -> [Row] {
randomSeed(variation)
let counts = (0 ..< rows).map { tiers + $0 * growth }
// A row's line and the air under it take a third of a tier.
let between = 0.34
let top = 0.058 * height, bottom = 0.052 * height
unit = (height - top - bottom) / (Double(counts.reduce(0, +)) + between * Double(rows - 1))
var result: [Row] = []
var y = top
for (r, count) in counts.enumerated() {
let level = y + Double(count) * unit
y = level + between * unit
// The rows widen a little toward the bottom, as the painting's do.
let depth = rows > 1 ? Double(r) / Double(rows - 1) : 0
let span = (0.75 + 0.08 * depth) * width
let pitch = span / Double(men)
let left = 0.485 * width - span / 2
// The line tilts a little, and each man stands at its own level.
let tilt = random(-0.012, 0.012)
var rowMen: [Man] = []
var line: [Vector2] = []
for m in 0 ..< men {
let axis = left + (Double(m) + 0.5) * pitch
let full = min(pitch * random(0.82, 0.9), 4 * unit)
let base = level + tilt * (axis - width / 2) + random(-0.07, 0.07) * unit
let reach = Double(count) * unit * random(0.93, 1.0)
let man = stack(tiers: count, axis: axis, base: base, width: full, height: reach)
rowMen.append(man)
let foot = (man.tiers.first?.half ?? full / 2) + 0.1 * unit
line.append(Vector2(axis - foot, base + bandWidth / 2))
line.append(Vector2(axis + foot, base + bandWidth / 2))
}
let first = line[0], last = line[line.count - 1]
line.insert(Vector2(-4, first.y + tilt * (-4 - first.x) + random(-0.1, 0.1) * unit), at: 0)
line.append(Vector2(width + 4, last.y + tilt * (width + 4 - last.x) + random(-0.1, 0.1) * unit))
result.append(Row(line: line, men: rowMen.reversed(), paint: r, tiers: count))
}
return result
}
/// Stacks one man: a base, a body of blocks with a waist here and there,
/// and a head. Most tiers are plain blocks, as in the painting, so the
/// men read as turned columns and not as vases.
private func stack(tiers count: Int, axis: Double, base: Double, width full: Double, height reach: Double) -> Man {
let half = full / 2
// One width runs through the body, a little narrower than the base.
let body = half * random(0.86, 1.0)
var kinds: [Kind] = []
// The base: a plinth, or now and then a foot that flares into the line.
kinds.append(random() < 0.75 ? .block : .waist)
for _ in 1 ..< max(1, count - 1) {
let below = kinds[kinds.count - 1]
var choices: [Kind] = [.block, .block, .block, .block, .waist, .waist, .collar(1), .collar(2), .bowl]
if below == .waist { choices = [.block, .block, .block, .collar(1)] }
if below == .bowl { choices = [.block, .waist] }
kinds.append(randomChoice(choices))
}
if count > 1 {
// The head: a crown more often than not.
kinds.append(randomChoice([.crown(1), .crown(1), .crown(2), .crown(2), .crown(3),
.dome(1), .dome(1), .dome(2), .block, .bowl]))
}
// Heights as shares, then scaled so the man is as tall as asked.
var shares: [Double] = kinds.enumerated().map { index, kind in
switch kind {
case .block: index == 0 ? random(0.6, 0.85) : random(0.75, 1.3)
case .waist: random(0.9, 1.25)
case .collar(let lobes): lobes == 1 ? random(0.7, 1.0) : random(1.0, 1.3)
case .bowl: random(0.85, 1.1)
case .dome(let discs): discs == 1 ? random(0.95, 1.25) : random(0.6, 0.8)
case .crown: random(0.9, 1.2)
}
}
let scale = reach / shares.reduce(0, +)
shares = shares.map { $0 * scale }
var stacked: [Tier] = []
var bottom = base
var levels: [Double] = []
for (index, kind) in kinds.enumerated() {
let tall = shares[index]
var wide = body
var reach = 0.0
var decor = Decor.plain
switch kind {
case .block:
// A thin tier is a slab, a disc seen from its edge, and may
// stand as wide as the base.
wide = index == 0 ? half : (tall < 0.9 * unit && random() < 0.4 ? half : body * random(0.94, 1.06))
decor = tall > 1.05 * unit
? randomChoice([.cap, .hang, .twinCaps, .twinHangs, .eye, .eye, .sides, .sides])
: randomChoice([.cap, .hang, .twinCaps, .twinHangs])
case .waist:
wide = index == 0 ? half : body
// The cut is close to a half circle and leaves a neck.
reach = min(wide * random(0.34, 0.56), tall * 0.6)
case .collar(let lobes):
wide = min(half, body * random(1.0, 1.12))
reach = min(wide * 0.28, tall / Double(2 * lobes))
decor = randomChoice([.cap, .hang, .eye])
case .bowl:
wide = min(half, body * random(1.0, 1.15))
decor = randomChoice([.hang, .twinHangs])
case .dome(let discs):
wide = body
decor = discs == 1 ? randomChoice([.cap, .twinCaps]) : .plain
case .crown(let bites):
wide = min(half, body * random(1.0, 1.15))
reach = tall * (bites == 1 ? random(0.5, 0.7) : random(0.38, 0.52))
decor = randomChoice([.cap, .twinCaps])
}
stacked.append(Tier(kind: kind, half: wide, bottom: bottom, height: tall, reach: reach,
decor: decor, chance: random()))
bottom -= tall
levels.append(base - bottom)
}
// A waist is never wider than what it meets above or below, so its
// corners end on its neighbors and leave no splinter standing out.
for index in stacked.indices where stacked[index].kind == .waist {
var wide = stacked[index].half
if index > 0 { wide = min(wide, topHalf(of: stacked[index - 1])) }
if index + 1 < stacked.count { wide = min(wide, bottomHalf(of: stacked[index + 1])) }
stacked[index].half = wide
stacked[index].reach = min(stacked[index].reach, wide * 0.56)
}
var silhouette = Shape(contours: [], winding: .nonZero)
for tier in stacked {
silhouette = silhouette.union(outline(of: tier, axis: axis))
}
let rim = silhouette.offset(by: -inset, join: .round)
// A line where two tiers meet, as wide as the narrower of them there.
var tierLines: [Mark] = []
for index in 1 ..< stacked.count {
let lower = stacked[index - 1], upper = stacked[index]
let reach = min(topHalf(of: lower), bottomHalf(of: upper)) - inset
guard reach > 2 else { continue }
tierLines.append(.line(Vector2(axis - reach, upper.bottom), Vector2(axis + reach, upper.bottom)))
}
var arcs: [(mark: Mark, chance: Double)] = []
for (index, tier) in stacked.enumerated() {
for mark in marks(in: tier, axis: axis, isBase: index == 0, isHead: index == stacked.count - 1) {
arcs.append((mark, tier.chance))
}
}
return Man(axis: axis, base: base, height: base - bottom, tiers: stacked, silhouette: silhouette,
rim: rim, tierLines: tierLines, arcs: arcs, levels: levels)
}
/// How wide a tier is where it meets the tier above it, as a half width.
private func topHalf(of tier: Tier) -> Double {
switch tier.kind {
case .collar: tier.half - tier.reach
default: tier.half
}
}
/// How wide a tier is where it meets the tier below it.
private func bottomHalf(of tier: Tier) -> Double {
switch tier.kind {
case .collar: tier.half - tier.reach
case .bowl: tier.half * (1 - 1 / (ChessmenOne.bowlDepth * ChessmenOne.bowlDepth)).squareRoot()
default: tier.half
}
}
/// A bowl is cut from a disc this much deeper than the tier, so it
/// stands on a flat foot and not on a point.
private static let bowlDepth = 1.16
/// A tier's own outline: its rectangle, and what the half circles add
/// to it or take from it.
private func outline(of tier: Tier, axis: Double) -> Shape {
let left = axis - tier.half, right = axis + tier.half
let top = tier.bottom - tier.height, middle = tier.bottom - tier.height / 2
let box = rectangle(left, top, right, tier.bottom)
switch tier.kind {
case .block:
return box
case .waist:
return box
.subtracting(ellipse(Vector2(left, middle), tier.reach, tier.height / 2))
.subtracting(ellipse(Vector2(right, middle), tier.reach, tier.height / 2))
case .collar(let lobes):
var collar = rectangle(left + tier.reach, top, right - tier.reach, tier.bottom)
let each = tier.height / Double(lobes)
for lobe in 0 ..< lobes {
let y = top + (Double(lobe) + 0.5) * each
collar = collar
.union(ellipse(Vector2(left + tier.reach, y), tier.reach, each / 2))
.union(ellipse(Vector2(right - tier.reach, y), tier.reach, each / 2))
}
return collar
case .bowl:
return ellipse(Vector2(axis, top), tier.half, tier.height * ChessmenOne.bowlDepth).intersection(box)
case .dome(let discs):
var dome = Shape(contours: [], winding: .nonZero)
let each = tier.half / Double(discs)
for disc in 0 ..< discs {
let center = Vector2(left + (2 * Double(disc) + 1) * each, tier.bottom)
dome = dome.union(ellipse(center, each, tier.height).intersection(box))
}
return dome
case .crown(let bites):
var crown = box
let each = tier.half / Double(bites)
for bite in 0 ..< bites {
let center = Vector2(left + (2 * Double(bite) + 1) * each, top)
crown = crown.subtracting(ellipse(center, each * 0.93, tier.reach))
}
return crown
}
}
/// The arcs drawn inside a tier. Each stands on one of the tier's lines
/// or hangs from it, so each is a D lying down, and a pair is a B.
private func marks(in tier: Tier, axis: Double, isBase: Bool, isHead: Bool) -> [Mark] {
let pad = 1.6 * lineWidth
// The lines the arcs sit on: the tier's own edges, or the rim where
// the tier is the man's base or head.
let floor = tier.bottom - (isBase ? inset : 0)
let ceiling = tier.bottom - tier.height + (isHead ? inset : 0)
var room = floor - ceiling - pad
var span = tier.half - inset - pad
switch tier.kind {
case .waist: span = tier.half - tier.reach - inset - pad
case .collar: span = tier.half - tier.reach - pad
case .crown: room -= tier.reach
case .dome: span *= 0.78; room *= 0.7
case .bowl: span *= 0.78; room *= 0.7
case .block: break
}
guard span > 3 * lineWidth, room > 3 * lineWidth else { return [] }
func cap(_ x: Double, _ radius: Double, _ rise: Double) -> Mark {
.arc(center: Vector2(x, floor), radiusX: radius, radiusY: rise, start: .pi, stop: 2 * .pi)
}
func hang(_ x: Double, _ radius: Double, _ drop: Double) -> Mark {
.arc(center: Vector2(x, ceiling), radiusX: radius, radiusY: drop, start: 0, stop: .pi)
}
switch tier.decor {
case .plain:
return []
case .cap:
return [cap(axis, span, room)]
case .hang:
return [hang(axis, span, room)]
case .twinCaps:
let rise = min(room, span * 0.8)
return [cap(axis - span / 2, span / 2, rise), cap(axis + span / 2, span / 2, rise)]
case .twinHangs:
let drop = min(room, span * 0.8)
return [hang(axis - span / 2, span / 2, drop), hang(axis + span / 2, span / 2, drop)]
case .eye:
return [cap(axis, span, room), hang(axis, span, room)]
case .sides:
let middle = (floor + ceiling) / 2, tall = (floor - ceiling) / 2
let deep = min(span * 0.62, tall)
return [
.arc(center: Vector2(axis - span - pad, middle), radiusX: deep, radiusY: tall,
start: -.pi / 2, stop: .pi / 2),
.arc(center: Vector2(axis + span + pad, middle), radiusX: deep, radiusY: tall,
start: .pi / 2, stop: 3 * .pi / 2),
]
}
}
private func rectangle(_ left: Double, _ top: Double, _ right: Double, _ bottom: Double) -> Shape {
Shape([Vector2(left, top), Vector2(right, top), Vector2(right, bottom), Vector2(left, bottom)])
}
private func ellipse(_ center: Vector2, _ radiusX: Double, _ radiusY: Double) -> Shape {
let count = 64
return Shape((0 ..< count).map { step in
let angle = Double(step) / Double(count) * 2 * .pi
return Vector2(center.x + cos(angle) * radiusX, center.y + sin(angle) * radiusY)
})
}
}