demo-shell.rs
2.5 kB · rust · 77 lines
1use mrlycore::errors::Result;2use mrlycore::Color;3use mrlyfig::{ink, save, Board, Grid};4use mrlymath::bang::factory;5use mrlymath::shape::crossing_tree;67const RADIUS: u64 = 242;8const LEVEL: usize = 5;9const SIDE: usize = 243;1011fn outline(board: &mut Board, grid: &Grid, x: u64, y: u64, step: usize, thick: f64, color: Color) {12 let (left, top, _, _) = grid.cell(y as usize * step, SIDE - (x as usize + 1) * step);13 let (right, foot, w, h) = grid.cell((y as usize + 1) * step - 1, SIDE - 1 - x as usize * step);14 let (wide, tall) = (right + w - left, foot + h - top);15 board.rect(left, top, wide, thick, color);16 board.rect(left, top + tall - thick, wide, thick, color);17 board.rect(left, top, thick, tall, color);18 board.rect(left + wide - thick, top, thick, tall, color);19}2021fn main() -> Result<()> {22 let tile = factory::create(7, 3, 2, 2, 1)?;23 let keep: Vec<bool> = tile.bytes().iter().map(|&byte| byte != 0).collect();24 let tree = crossing_tree(RADIUS, 3, &keep);25 assert_eq!(tree.orphans, 0);26 assert_eq!(tree.levels.len(), LEVEL + 1);27 for (level, boxes) in tree.levels.iter().enumerate() {28 assert_eq!(29 boxes.len() as u64,30 2 * (RADIUS / 3u64.pow(level as u32)) + 131 );32 }33 assert_eq!(tree.levels[0].len(), 485);3435 let design = factory::create(7, 3, 2, 2, LEVEL)?;36 assert_eq!(design.shape, vec![SIDE, SIDE]);37 assert_eq!(design.sum(), 32768);3839 let mut board = Board::square();40 let grid = Grid::new(board.frame(0.08), SIDE, SIDE, 0.0);41 for x in 0..SIDE {42 for y in 0..SIDE {43 if design.bytes()[x * SIDE + y] != 0 {44 grid.fill(&mut board, y, SIDE - 1 - x, ink::line());45 }46 }47 }48 let unit = grid.cell(0, 0).2;49 for (level, thick, color) in [50 (2usize, 0.5 * unit, ink::dim()),51 (3, unit, ink::dim()),52 (4, 2.0 * unit, ink::dim()),53 ] {54 let step = 3usize.pow(level as u32);55 for cell in &tree.levels[level] {56 outline(&mut board, &grid, cell.x, cell.y, step, thick, color);57 }58 }59 let mut kept = 0u64;60 for cell in &tree.levels[0] {61 let color = if cell.live {62 kept += 1;63 ink::yellow()64 } else {65 ink::blue()66 };67 grid.fill(68 &mut board,69 cell.y as usize,70 SIDE - 1 - cell.x as usize,71 color,72 );73 }74 assert_eq!(kept, 296);75 save("demo-shell", &board)?;76 Ok(())77}