wiki-thue-morse-sequence.rs

1.4 kB · rust · 41 lines

1use mrlycore::errors::Result;2use mrlyfig::{ink, save, Board};3use mrlynum::morse::{digits, lift, Lift};45const STRIP: usize = 64;6const SIDE: usize = 32;78fn main() -> Result<()> {9    let mut board = Board::square();10    let frame = board.frame(0.08);11    let word = digits(STRIP);12    assert_eq!(word.iter().filter(|&&bit| bit == 1).count(), STRIP / 2);13    let signs = lift(Lift::Parity, SIDE);14    assert_eq!(lift(Lift::Xor, SIDE), signs);15    assert_eq!(signs.iter().filter(|&&bit| bit == 1).count(), SIDE * SIDE / 2);16    let tone = |bit: u8| if bit == 0 { ink::orange() } else { ink::blue() };17    let cell = (frame.h / (2.0 * SIDE as f64 + 5.0)).floor();18    let block = 2.0 * cell;19    let band = 3.0 * cell;20    let gap = 2.0 * cell;21    let run = block * SIDE as f64;22    let left = ((board.width as f64 - run) / 2.0).round();23    let top = ((board.height as f64 - run - band - gap) / 2.0).round();24    for (place, &bit) in word.iter().enumerate() {25        board.rect(left + place as f64 * cell, top, cell, band, tone(bit));26    }27    let head = top + band + gap;28    for row in 0..SIDE {29        for col in 0..SIDE {30            board.rect(31                left + col as f64 * block,32                head + row as f64 * block,33                block,34                block,35                tone(signs[row * SIDE + col]),36            );37        }38    }39    save("wiki-thue-morse-sequence", &board)?;40    Ok(())41}