WIP: add climate animation

This commit is contained in:
Juergen Stuber
2024-01-14 20:08:42 +01:00
parent 7cc48f17f2
commit e28a299d0d

77
src/bin/climate/main.rs Normal file
View File

@@ -0,0 +1,77 @@
use std::env::args;
use std::io::stdout;
use std::io::Write;
use std::iter::repeat_with;
use std::thread::sleep;
use std::time::Duration;
use rand::thread_rng;
use rand::Rng;
use pixelfoo::color::Color;
type Frame = Vec<Vec<Color>>;
fn send<T: Write>(w: &mut T, f: &Frame) -> std::io::Result<()> {
for l in f {
for c in l {
w.write_all(&c.rgb())?;
}
}
w.flush()
}
/// Mean annual temperature in NRW from 1881 to 2023
/// Source: Deutscher Wetterdienst
const data: &[f64] = &[
8.14, 9.03, 8.71, 9.39, 8.31, 8.71, 7.60, 7.44, 8.05, // 1881
7.78, 8.07, 8.17, 8.82, 8.86, 7.91, 8.25, 8.59, 9.10, 8.77, // 1890
8.88, 8.27, 7.73, 8.89, 8.79, 8.46, 8.81, 8.42, 8.15, 7.94, // 1900
9.09, 9.74, 8.76, 9.22, 9.23, 8.54, 8.90, 7.99, 9.13, 7.85, // 1910
9.22, 9.62, 7.80, 8.46, 8.15, 8.84, 9.31, 8.59, 8.89, 8.16, // 1920
9.43, 8.28, 8.99, 8.43, 10.05, 9.14, 8.95, 9.22, 9.21, 9.08, // 1930
7.52, 8.11, 8.00, 9.47, 8.90, 9.63, 8.87, 9.31, 9.73, 9.77, // 1940
9.17, 9.32, 8.53, 9.51, 8.53, 8.26, 7.58, 9.38, 8.93, 9.93, // 1950
9.16, 9.59, 7.63, 7.65, 8.90, 8.16, 9.20, 9.51, 8.82, 8.73, // 1960
8.55, 9.16, 8.52, 9.00, 9.43, 9.59, 9.41, 9.31, 8.48, 8.22, // 1970
8.62, 8.86, 9.69, 9.70, 8.86, 8.03, 8.60, 8.23, 9.75, 10.26, // 1980
10.20, 9.01, 9.97, 9.08, 10.26, 9.77, 7.93, 9.65, 9.69, 10.35, // 1990
10.50, 9.85, 10.28, 10.14, 9.58, 9.88, 10.35, 10.48, 9.87, 9.81, // 2000
8.43, 10.38, 9.66, 9.20, 10.95, 10.35, 10.11, 10.31, 11.02, 10.74, // 2010
11.13, 9.79, 11.21, 11.23, // 2020
];
const DEFAULT_ARG: usize = 0;
fn main() -> std::io::Result<()> {
let args = args().collect::<Vec<_>>();
eprintln!("executing {}", args[0]);
let x_size = args[1].parse::<usize>().unwrap();
let y_size = args[2].parse::<usize>().unwrap();
let arg = args[3].parse::<usize>().unwrap_or(DEFAULT_ARG);
eprintln!("screen size {}x{}, arg {}", x_size, y_size, arg);
let t_frame = 0.040; // s
let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
let mut frame = repeat_with(|| {
repeat_with(|| palette.pick(&mut rng))
.take(x_size)
.collect::<Vec<_>>()
})
.take(y_size)
.collect::<Vec<_>>();
loop {
let x = rng.gen_range(0..x_size);
let y = rng.gen_range(0..y_size);
let c = palette.pick(&mut rng);
frame[y][x] = c;
let mut buf = Vec::with_capacity(x_size * y_size * 3);
send(&mut buf, &frame)?;
stdout().write_all(&buf)?;
stdout().flush()?;
sleep(delay);
}
}