feat: replace the sprite with a full-width pixel strip that thins out
The strip is four lines of 12-dot pixels across the paper: the first line is solid and each line below it has fewer pixels in random places. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,30 +1,36 @@
|
||||
//! A small random pixel-art sprite for the bottom of every printed slip.
|
||||
//! A strip of random pixel art for the bottom of every printed slip.
|
||||
//!
|
||||
//! The sprite is a few chunky cells high and mirrored left to right, which
|
||||
//! makes random bits read as a little creature or emblem instead of noise.
|
||||
//! The first line is solid; every line below it has fewer pixels, so the
|
||||
//! strip dissolves towards the bottom edge of the slip.
|
||||
|
||||
pub const COLUMNS: usize = 8;
|
||||
/// Lines of pixels in the strip.
|
||||
pub const ROWS: usize = 4;
|
||||
const HALF: usize = COLUMNS / 2;
|
||||
/// How many of the 16 cells of the left half may be filled: enough to read as
|
||||
/// a shape, not so many that it turns into a block.
|
||||
const FILLED: std::ops::RangeInclusive<u32> = 6..=11;
|
||||
/// Pixels across the paper that the strip aims for; the printer picks the
|
||||
/// pixel size that comes closest (12 dots on 80 mm paper, 8 on 58 mm).
|
||||
pub const CELLS_ACROSS: usize = 48;
|
||||
/// Percent of the pixels that are set in each line, from the solid one down.
|
||||
const DENSITY: [usize; ROWS] = [100, 65, 35, 12];
|
||||
|
||||
pub type Sprite = [[bool; COLUMNS]; ROWS];
|
||||
|
||||
/// The sprite for `seed`; the same seed always gives the same sprite.
|
||||
pub fn sprite(seed: u64) -> Sprite {
|
||||
/// The pixels of each line for `seed`; the same seed always gives the same
|
||||
/// strip. Every line has exactly `columns * DENSITY / 100` pixels set, in
|
||||
/// random places.
|
||||
pub fn pattern(seed: u64, columns: usize) -> [Vec<bool>; ROWS] {
|
||||
let mut state = seed;
|
||||
loop {
|
||||
let bits = next(&mut state) & 0xFFFF;
|
||||
let row = |index: usize| (bits >> (index * HALF)) & 0xF;
|
||||
if !FILLED.contains(&bits.count_ones()) || (0..ROWS).any(|index| row(index) == 0) {
|
||||
continue;
|
||||
std::array::from_fn(|row| {
|
||||
let filled = columns * DENSITY[row] / 100;
|
||||
// Partial Fisher-Yates shuffle: the first `filled` entries are a
|
||||
// uniform random choice of columns.
|
||||
let mut order: Vec<usize> = (0..columns).collect();
|
||||
for index in 0..filled {
|
||||
let other = index + (next(&mut state) % (columns - index) as u64) as usize;
|
||||
order.swap(index, other);
|
||||
}
|
||||
return std::array::from_fn(|y| {
|
||||
std::array::from_fn(|x| row(y) >> x.min(COLUMNS - 1 - x) & 1 == 1)
|
||||
});
|
||||
}
|
||||
let mut line = vec![false; columns];
|
||||
for &column in &order[..filled] {
|
||||
line[column] = true;
|
||||
}
|
||||
line
|
||||
})
|
||||
}
|
||||
|
||||
/// SplitMix64: a few lines, well mixed, and good enough for decoration.
|
||||
@@ -41,25 +47,41 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sprites_are_mirrored_filled_in_every_row_and_not_a_block() {
|
||||
for seed in 0..2000 {
|
||||
let sprite = sprite(seed);
|
||||
let filled = sprite.iter().flatten().filter(|cell| **cell).count();
|
||||
assert!((12..=22).contains(&filled), "seed {seed}: {filled} cells");
|
||||
for row in sprite {
|
||||
assert!(row.iter().any(|cell| *cell), "seed {seed}: empty row");
|
||||
fn the_first_line_is_solid_and_each_line_is_sparser() {
|
||||
for columns in [8, 16, 48, 50] {
|
||||
for seed in 0..200 {
|
||||
let lines = pattern(seed, columns);
|
||||
let counts: Vec<usize> = lines
|
||||
.iter()
|
||||
.map(|line| line.iter().filter(|cell| **cell).count())
|
||||
.collect();
|
||||
assert_eq!(counts[0], columns, "{columns} columns, seed {seed}");
|
||||
assert!(
|
||||
(0..COLUMNS).all(|x| row[x] == row[COLUMNS - 1 - x]),
|
||||
"seed {seed}: not mirrored"
|
||||
counts.windows(2).all(|pair| pair[0] > pair[1]),
|
||||
"{columns} columns, seed {seed}: {counts:?}"
|
||||
);
|
||||
assert!(lines.iter().all(|line| line.len() == columns));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_seed_gives_one_sprite_and_seeds_give_many() {
|
||||
assert_eq!(sprite(7), sprite(7));
|
||||
let distinct: std::collections::HashSet<Sprite> = (0..1000).map(sprite).collect();
|
||||
assert!(distinct.len() > 200, "{} sprites", distinct.len());
|
||||
fn a_seed_gives_one_pattern_and_seeds_give_many() {
|
||||
assert_eq!(pattern(7, 48), pattern(7, 48));
|
||||
let distinct: std::collections::HashSet<_> =
|
||||
(0..1000).map(|seed| pattern(seed, 48)).collect();
|
||||
assert_eq!(distinct.len(), 1000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparse_pixels_land_everywhere_across_the_strip() {
|
||||
// Not clumped on one side: over many seeds each column gets used.
|
||||
let mut seen = [false; 48];
|
||||
for seed in 0..200 {
|
||||
for (column, set) in pattern(seed, 48)[3].iter().enumerate() {
|
||||
seen[column] |= *set;
|
||||
}
|
||||
}
|
||||
assert!(seen.iter().all(|used| *used));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user