use image::{ImageBuffer, Rgb, RgbImage};
use std::f64::consts::PI;

fn chladni(x: f64, y: f64, n: f64, m: f64) -> f64 {
    (n * PI * x).cos() * (m * PI * y).cos() - (m * PI * x).cos() * (n * PI * y).cos()
}

fn generate_png(n: u32, m: u32, width: u32, height: u32) {
    let mut img: RgbImage = ImageBuffer::new(width, height);

    for (x, y, pixel) in img.enumerate_pixels_mut() {
        let xf = (x as f64 / width as f64) * 2.0 - 1.0;
        let yf = (y as f64 / height as f64) * 2.0 - 1.0;

        let val = chladni(xf, yf, n as f64, m as f64);
        let intensity = val.abs();

        let brigh = (((0.08 - intensity).max(0.0) / 0.08) * 255.0) as u8;
        *pixel = Rgb([brigh, brigh, brigh]);
    }

    let filename = format!("out/chladni_{}_{}.png", n, m);
    img.save(&filename).unwrap();
    println!("{}", filename);
}

fn main() {
    let width = 1000;
    let height = 1000;
    let max_n = 6;
    let max_m = 6;

    for n in 1..max_n {
        for m in 1..max_m {
            generate_png(n, m, width, height);
        }
    }
}
