demo-radix.rs

1.3 kB · rust · 45 lines

1use mrlycore::errors::Result;2use mrlyfig::{ink, plot, save, Board, Frame};3use mrlynum::radix::flowsnake;45const LEVEL: usize = 6;6const WORDS: usize = 117649;7const DIGITS: usize = 7;8const NORM: u64 = 7;9const MARGIN: f64 = 0.08;10const DOT: f64 = 1.2;1112fn main() -> Result<()> {13    let design = flowsnake();14    assert_eq!(design.size(), DIGITS);15    assert_eq!(design.base().norm(), NORM);16    assert_eq!(design.fill(LEVEL), WORDS as u128);17    assert_eq!(design.distinct(LEVEL), WORDS);1819    let cloud = design.plane(LEVEL);20    assert_eq!(cloud.len(), WORDS);2122    let mut board = Board::square();23    let frame = board.frame(MARGIN);24    let laid = fit(&cloud, frame);25    plot::dots(&mut board, &laid, DOT, ink::blue());26    save("demo-radix", &board)?;27    Ok(())28}2930// THE LAYOUT3132fn fit(pts: &[(f64, f64)], frame: Frame) -> Vec<(f64, f64)> {33    let (mut low, mut high) = ((f64::MAX, f64::MAX), (f64::MIN, f64::MIN));34    for &(x, y) in pts {35        low = (low.0.min(x), low.1.min(y));36        high = (high.0.max(x), high.1.max(y));37    }38    let span = (high.0 - low.0, high.1 - low.1);39    let scale = (frame.w / span.0).min(frame.h / span.1);40    let mid = ((low.0 + high.0) / 2.0, (low.1 + high.1) / 2.0);41    let (cx, cy) = frame.center();42    pts.iter()43        .map(|&(x, y)| (cx + (x - mid.0) * scale, cy - (y - mid.1) * scale))44        .collect()45}