Remix.run Logo
Animats 9 hours ago

You know, stuff like this, which I had to write today, for turning a 2D floating point height map into a 2D greyscale image.

        let max = self
            .heights
            .elements_row_major_iter()
            .max_by(|a, b| a.total_cmp(b))
            .unwrap();
        let min = self
            .heights
            .elements_row_major_iter()
            .min_by(|a, b| a.total_cmp(b))
            .unwrap();
        //  Scale into 0..255
        let range = (max - min).max(0.001);
        let height_array = self
            .heights
            .as_rows()
            .into_iter()
            .map(|r| {
                r.into_iter()
                    .map(|v| ((((v - min) / range) / 256.0).round() as usize).clamp(0, 255) as u8)
                    .collect()
            })
            .collect();
After Rust formats it, it's probably more lines than doing it with FOR statements. Every parenthesis matters. So much of the typing is implicit that it's not at all clear what's going on.