demo-tour.rs
1.9 kB · rust · 59 lines
1use mrlycore::errors::Result;2use mrlyfig::{ink, save, Board, Grid};3use mrlymath::two;4use mrlymath::two::census;56const CODE: u128 = 7;7const NUMBER: usize = 3;8const BASE: usize = 2;9const LEVEL: usize = 4;10const SIDE: usize = 81;1112fn main() -> Result<()> {13 let mut board = Board::square();14 let frame = board.frame(0.08);15 let carpet = two::create(CODE, NUMBER, LEVEL, 0, BASE)?;16 assert_eq!((carpet.width(), census::fills(&carpet)), (SIDE, 4096));17 assert_eq!(census::perimeter(&carpet), 3536);18 let types = carpet.types();19 let on = |row: i64, col: i64| {20 (0..SIDE as i64).contains(&row)21 && (0..SIDE as i64).contains(&col)22 && types.get(&[row as usize, col as usize]) != 023 };24 let grid = Grid::new(frame, SIDE, SIDE, 0.0);25 let body = ink::mix(ink::line(), ink::dim(), 0.35);26 for row in 0..SIDE {27 for col in 0..SIDE {28 if on(row as i64, col as i64) {29 grid.fill(&mut board, col, row, body);30 }31 }32 }33 let thick = frame.cell(SIDE) * 0.22;34 let mut edges = 0usize;35 for row in 0..SIDE {36 for col in 0..SIDE {37 if !on(row as i64, col as i64) {38 continue;39 }40 let (x, y, w, h) = grid.cell(col, row);41 let (r, c) = (row as i64, col as i64);42 let sides = [43 (!on(r - 1, c), (x, y), (x + w, y)),44 (!on(r + 1, c), (x, y + h), (x + w, y + h)),45 (!on(r, c - 1), (x, y), (x, y + h)),46 (!on(r, c + 1), (x + w, y), (x + w, y + h)),47 ];48 for (open, a, b) in sides {49 if open {50 board.segment(a, b, thick, ink::yellow());51 edges += 1;52 }53 }54 }55 }56 assert_eq!(edges as u128, census::perimeter(&carpet));57 save("demo-tour", &board)?;58 Ok(())59}