demo-sumset.rs

1.4 kB · rust · 45 lines

1use figures::{ink, save, Board};2use mrlyrs::core::error::Result;3use mrlyrs::num::sumset::Sumset;45const LEVEL: u32 = 16;6const ROWS: usize = 21;7const CELLS: usize = 860;89fn main() -> Result<()> {10    let sumset = Sumset::new(LEVEL)?;11    assert_eq!(sumset.count(3u64.pow(10)), Some(45968));12    assert_eq!(sumset.count(14348906), Some(10953840));1314    let mut board = Board::square();15    let area = board.frame(0.08);16    let band = area.h / ROWS as f64;17    let gap = (band * 0.22).round().max(2.0);1819    for row in 0..ROWS {20        let x = 3f64.powf(6.0 + row as f64 / 2.0).round() as u64;21        let cells = CELLS.min(x as usize + 1);22        let fills = sumset.fills(0, x + 1, cells)?;23        assert_eq!(fills.len(), cells);24        let y = (area.y + row as f64 * band).round();25        let h = (band - gap).round();26        let step = area.w / cells as f64;27        for (i, &share) in fills.iter().enumerate() {28            if share == 0.0 {29                continue;30            }31            let left = (area.x + i as f64 * step).round();32            let right = (area.x + (i + 1) as f64 * step).round();33            board.rect(34                left,35                y,36                (right - left).max(1.0),37                h,38                ink::mix(ink::ground(), ink::blue(), share),39            );40        }41    }4243    save("demo-sumset", &board)?;44    Ok(())45}