demo-arcs.rs

2.0 kB · rust · 65 lines

1use figures::{ink, save, Board, Grid};2use mrlyrs::core::error::Result;3use mrlyrs::math::arcs;4use mrlyrs::math::bang::Code;5use std::f64::consts::{FRAC_PI_2, PI};67const LEVEL: u32 = 5;8const SIDE: usize = 32;910type Sweep = ((f64, f64), (f64, f64));1112fn sweeps(filled: bool, left: f64, top: f64, cell: f64) -> [Sweep; 2] {13    if filled {14        [15            ((left, top + cell), (-FRAC_PI_2, 0.0)),16            ((left + cell, top), (FRAC_PI_2, PI)),17        ]18    } else {19        [20            ((left + cell, top + cell), (PI, PI + FRAC_PI_2)),21            ((left, top), (0.0, FRAC_PI_2)),22        ]23    }24}2526fn main() -> Result<()> {27    let drawn = arcs::draw(Code::from(7u128), 2, LEVEL as usize)?;28    assert_eq!(drawn.side, SIDE);29    assert_eq!(drawn.loops, 3u64.pow(LEVEL - 1) - 2u64.pow(LEVEL) + 1);30    assert_eq!(drawn.loops, 50);31    assert_eq!(drawn.strands, 2 * SIDE as u64);32    assert_eq!(drawn.cells.iter().filter(|&&b| b & 1 == 1).count(), 243);3334    let mut board = Board::square();35    let frame = board.frame(0.08);36    let cell = frame.w / SIDE as f64;37    let grid = Grid::new(frame, SIDE, SIDE, 0.0);38    for y in 0..SIDE {39        for x in 0..SIDE {40            if drawn.cells[y * SIDE + x] & 1 == 1 {41                grid.fill(&mut board, x, SIDE - 1 - y, ink::line());42            }43        }44    }45    for y in 0..SIDE {46        for x in 0..SIDE {47            let byte = drawn.cells[y * SIDE + x];48            let left = frame.x + x as f64 * cell;49            let top = frame.y + (SIDE - 1 - y) as f64 * cell;50            for ((centre, angles), bit) in sweeps(byte & 1 == 1, left, top, cell)51                .into_iter()52                .zip([2, 4])53            {54                let color = if byte & bit == 0 {55                    ink::blue()56                } else {57                    ink::orange()58                };59                board.arc(centre, cell / 2.0, angles, cell * 0.18, color);60            }61        }62    }63    save("demo-arcs", &board)?;64    Ok(())65}