research-beneath.rs
1.7 kB · rust · 59 lines
1use mrlycore::errors::Result;2use mrlyfig::{ink, save, Board, Frame};3use mrlynum::radix::koch;45const LEVEL: usize = 6;6const POINTS: usize = 4096;7const DIGITS: usize = 4;8const NORM: u64 = 9;9const PROBE: usize = 2;10const STEP: f64 = 1.0 / 9.0;11const TOL: f64 = 1e-12;12const MARGIN: f64 = 0.08;13const THIN: f64 = 1.4;1415fn main() -> Result<()> {16 let design = koch();17 assert_eq!(design.size(), DIGITS);18 assert_eq!(design.base().norm(), NORM);19 assert_eq!(design.fill(LEVEL), POINTS as u128);2021 let probe = design.plane(PROBE);22 assert_eq!(probe.len(), DIGITS * DIGITS);23 assert!(steps(&probe).iter().all(|s| (s - STEP).abs() < TOL));2425 let curve = design.plane(LEVEL);26 assert_eq!(curve.len(), POINTS);2728 let mut board = Board::square();29 let frame = board.frame(MARGIN);30 let laid = fit(&curve, frame);31 board.polyline(&laid, THIN, ink::blue());32 save("research-beneath", &board)?;33 Ok(())34}3536// THE STEP3738fn steps(pts: &[(f64, f64)]) -> Vec<f64> {39 pts.windows(2)40 .map(|w| ((w[1].0 - w[0].0).powi(2) + (w[1].1 - w[0].1).powi(2)).sqrt())41 .collect()42}4344// THE LAYOUT4546fn fit(pts: &[(f64, f64)], frame: Frame) -> Vec<(f64, f64)> {47 let (mut low, mut high) = ((f64::MAX, f64::MAX), (f64::MIN, f64::MIN));48 for &(x, y) in pts {49 low = (low.0.min(x), low.1.min(y));50 high = (high.0.max(x), high.1.max(y));51 }52 let span = (high.0 - low.0, high.1 - low.1);53 let scale = (frame.w / span.0).min(frame.h / span.1);54 let mid = ((low.0 + high.0) / 2.0, (low.1 + high.1) / 2.0);55 let (cx, cy) = frame.center();56 pts.iter()57 .map(|&(x, y)| (cx + (x - mid.0) * scale, cy - (y - mid.1) * scale))58 .collect()59}