- Rule 90 in a square 100x100 image with a pseudorandom starting seed
- first saved as a
.pbm
file I later converted to.webp
using an image viewer application - I generated it with the following rust code …
use std::io::Write;
const WIDTH: usize = 100;
const HEIGHT: usize = 100;
type BITMAP = [[bool; WIDTH]; HEIGHT];
fn write_bitmap_file(filename: &str, bitmap: BITMAP) -> std::io::Result<()> {
let mut file: std::fs::File = std::fs::File::create(filename)?;
file.write_all(b"P1\n")?;
file.write_all(format!("{} {}\n", WIDTH, HEIGHT).as_bytes())?;
for y in 0..HEIGHT {
let mut one_row = String::with_capacity(WIDTH + 1);
for x in 0..WIDTH {
if bitmap[y][x] {
// one_row = format!("{}1", one_row);
one_row.push('1');
} else {
one_row.push('0');
}
}
one_row.push('\n');
file.write_all(one_row.as_bytes())?;
}
Ok(())
}
fn applyrule(rulenr: u32, bitmap: &mut BITMAP) {
for y in 1..HEIGHT {
for x in 0..WIDTH {
let mut rule_select = 0;
for xpm in 0..3 {
let addx = xpm as i32 - 1; // -1 to 1
let idx: i32 = x as i32 + addx;
if idx < 0 || idx == WIDTH as i32 {
continue;
}
let idx: usize = idx as usize;
let reading = bitmap[y - 1][idx];
if reading {
rule_select |= 1 << xpm;
}
}
let rule_index = 1 << rule_select;
if rulenr & rule_index != 0 {
bitmap[y][x] = true;
} else {
bitmap[y][x] = false;
}
}
}
}
fn main() {
let randomseed: u128 = 0x46f470db4dbf5ac29b6e572f51ecafe;
let mut bitmap: BITMAP = [[false; WIDTH]; HEIGHT];
// populate first row width random values
for x in 0..WIDTH {
let xmod = x % 128;
let curr = randomseed & (1 << xmod) == 0;
bitmap[0][x] = curr;
}
applyrule(90, &mut bitmap);
write_bitmap_file("rule90.bpm", bitmap).unwrap();
}
You must log in or register to comment.