1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
use std::{fs::File, io::Write, path::PathBuf};
use crate::particle_system::{Particle, Vector, N, Scalar};
pub struct PPM {
pub prefix: PathBuf,
pub width: usize,
pub height: usize,
// pixels_per_unit: usize,
}
impl PPM {
fn render_particles(&self, particles: &Vec<Particle>) -> String {
let mut s = format!("P3\n{} {}\n255\n", self.width, self.height);
let white = "255 255 255 ";
let black = "0 0 0 ";
for pixel_row in 0..self.height {
for pixel_col in 0..self.width {
let point = Vector::new(pixel_col as Scalar, (pixel_row as Scalar) * -1.0)
+ Vector::new(self.width as Scalar / -2.0, self.height as Scalar / 2.0);
let color = match particles.iter().any(|p| {
(p.position - point).coords.norm() <= (p.mass).powf(1.0 / (N as f64))
}) {
true => black,
false => white,
};
s += color;
}
}
s
}
pub fn save_frame(&self, particles: &Vec<Particle>, time: Scalar) {
let file_name = format!("frame-{:08.3}", time);
let path = self.prefix.join(file_name);
let mut file = File::create(path).unwrap();
let data = self.render_particles(particles);
file.write(data.as_bytes()).unwrap();
}
}
|