paper-spin-harmonics.rs

2.2 kB · rust · 79 lines

1use mrlycore::errors::Result;2use mrlyfig::{ink, save, Board, Grid};34const SIDE: usize = 3;5const CODE: u32 = 45;67type Cell = (usize, usize);8type Chord = (Cell, Cell, i64);910fn cells() -> Vec<Cell> {11    (0..SIDE * SIDE)12        .filter(|j| CODE >> j & 1 == 1)13        .map(|j| (j / SIDE, j % SIDE))14        .collect()15}1617fn chords(cells: &[Cell]) -> Vec<Chord> {18    let mut out = Vec::new();19    for (a, p) in cells.iter().enumerate() {20        for q in &cells[a + 1..] {21            let dr = p.0 as i64 - q.0 as i64;22            let dc = p.1 as i64 - q.1 as i64;23            out.push((*p, *q, dr * dr + dc * dc));24        }25    }26    out27}2829fn main() -> Result<()> {30    let cells = cells();31    let chords = chords(&cells);32    let long: Vec<_> = chords.iter().filter(|c| c.2 == 4).collect();33    assert_eq!(cells.len(), 4, "code 45 fills four cells");34    assert_eq!(chords.len(), 6, "four centres carry six chords");35    assert_eq!(long.len(), 2, "two chords have squared length 4/9");3637    let mut board = Board::square();38    let frame = board.frame(0.08);39    let step = frame.w / SIDE as f64;40    let lattice = Grid::new(frame, SIDE, SIDE, 0.10);41    let centre = |cell: Cell| {42        (43            frame.x + (cell.1 as f64 + 0.5) * step,44            frame.y + (cell.0 as f64 + 0.5) * step,45        )46    };4748    let plate = ink::fade(ink::dim(), 0.16);49    for row in 0..SIDE {50        for col in 0..SIDE {51            lattice.fill(&mut board, col, row, plate);52        }53    }54    for cell in &cells {55        lattice.fill(&mut board, cell.1, cell.0, ink::blue());56    }5758    let thin = step * 0.017;59    let thick = step * 0.038;60    let weight = |d2: i64| if d2 == 4 { thick } else { thin };61    let casing = |d2: i64| weight(d2) + step * if d2 == 4 { 0.016 } else { 0.008 };62    for chord in &chords {63        board.segment(64            centre(chord.0),65            centre(chord.1),66            casing(chord.2),67            ink::ground(),68        );69    }70    for chord in chords.iter().filter(|c| c.2 != 4) {71        board.segment(centre(chord.0), centre(chord.1), thin, ink::dim());72    }73    for chord in &long {74        board.segment(centre(chord.0), centre(chord.1), thick, ink::pink());75    }7677    save("paper-spin-harmonics", &board)?;78    Ok(())79}