Finish PPM conversions and plot the projectile
This commit is contained in:
parent
15d87fbde6
commit
2569a48792
|
@ -1,4 +1,5 @@
|
||||||
use ray_tracer::types::*;
|
use ray_tracer::{types::*, PPM};
|
||||||
|
use std::{fs::File, io::Write};
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
struct Projectile {
|
struct Projectile {
|
||||||
|
@ -18,26 +19,35 @@ fn tick(env: &Environment, proj: &Projectile) -> Projectile {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let mut canvas = Canvas::new(900, 550);
|
||||||
|
|
||||||
let start = Projectile {
|
let start = Projectile {
|
||||||
position: Point::new(0., 1., 0.),
|
position: Point::new(0., 1., 0.),
|
||||||
velocity: Vector::new(1., 1., 0.).normalize() * 5.,
|
velocity: Vector::new(1., 5., 0.).normalize() * 5.,
|
||||||
};
|
};
|
||||||
|
|
||||||
let e = Environment {
|
let e = Environment {
|
||||||
gravity: Vector::new(0., -0.1, 0.),
|
gravity: Vector::new(0., -0.1, 0.),
|
||||||
wind: Vector::new(-0.1, 0., 0.),
|
wind: Vector::new(-0.01, 0., 0.),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut p = start;
|
let mut p = start;
|
||||||
while p.position.y() > 0. {
|
while p.position.y() > 0. {
|
||||||
p = tick(&e, &p);
|
p = tick(&e, &p);
|
||||||
|
|
||||||
|
let x = p.position.x().round() as usize;
|
||||||
|
let y = p.position.y().round() as usize;
|
||||||
|
if x > 1 && x < 900 && y > 1 && y < 550 {
|
||||||
|
*canvas.pixel_mut(x, 550 - y - 1) = Color::new(1., 1., 0.);
|
||||||
|
*canvas.pixel_mut(x+1, 550 - y - 1) = Color::new(1., 1., 0.);
|
||||||
|
*canvas.pixel_mut(x-1, 550 - y - 1) = Color::new(1., 1., 0.);
|
||||||
|
*canvas.pixel_mut(x, 550 - y) = Color::new(1., 1., 0.);
|
||||||
|
*canvas.pixel_mut(x, 550 - y - 2) = Color::new(1., 1., 0.);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(
|
let ppm = PPM::from(canvas);
|
||||||
"distance travelled: [{}] {} {} {}",
|
|
||||||
(p.position - start.position).magnitude(),
|
let mut file = File::create("projectile.ppm").unwrap();
|
||||||
p.position.x(),
|
file.write(ppm.as_bytes()).unwrap();
|
||||||
p.position.y(),
|
|
||||||
p.position.z(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,5 @@
|
||||||
pub mod ppm;
|
mod ppm;
|
||||||
|
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
pub use ppm::PPM;
|
||||||
|
|
|
@ -24,13 +24,11 @@ fn join_to_line_limit(data: impl IntoIterator<Item = String>) -> Vec<String> {
|
||||||
lines.push(line);
|
lines.push(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{:?}", lines);
|
|
||||||
|
|
||||||
lines
|
lines
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct PPM(String);
|
pub struct PPM(String);
|
||||||
|
|
||||||
impl From<Canvas> for PPM {
|
impl From<Canvas> for PPM {
|
||||||
fn from(c: Canvas) -> Self {
|
fn from(c: Canvas) -> Self {
|
||||||
|
|
|
@ -36,7 +36,11 @@ impl Canvas {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn addr(&self, y: usize, x: usize) -> usize {
|
fn addr(&self, y: usize, x: usize) -> usize {
|
||||||
y * self.width() + x
|
let val = y * self.width() + x;
|
||||||
|
if val >= self.pixels.len() {
|
||||||
|
eprintln!("[{val}] out of range: [{x}, {y}]");
|
||||||
|
}
|
||||||
|
val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue