Compare commits

..

No commits in common. "bd899e3a2ef3f1e2ae995efb032900b875e81241" and "a2aa13288691961e5ebced4941997ffa2213a8da" have entirely different histories.

6 changed files with 27 additions and 46 deletions

View File

@ -49,5 +49,5 @@ fn main() {
let ppm = PPM::from(canvas);
let mut file = File::create("projectile.ppm").unwrap();
let _ = file.write(ppm.as_bytes());
file.write(ppm.as_bytes()).unwrap();
}

View File

@ -8,7 +8,8 @@ fn join_to_line_limit(data: impl IntoIterator<Item = String>) -> Vec<String> {
let mut lines = vec![];
let mut line = String::new();
for element in data {
let mut iter = data.into_iter();
while let Some(element) = iter.next() {
if line.is_empty() {
line = line + &element;
} else if line.len() + 1 + element.len() < 70 {

View File

@ -189,31 +189,4 @@ mod tests {
let p = Point::new(2., 3., 4.);
assert_eq!(transform * p, Point::new(2., 3., 7.));
}
#[test]
fn individual_transformations_are_applied_in_sequence() {
let p = Point::new(1., 0., 1.);
let rotation = rotation_x(PI / 2.);
let scale = scaling(5., 5., 5.);
let translate = translation(10., 5., 7.);
let p2 = rotation * p;
assert_eq!(p2, Point::new(1., -1., 0.));
let p3 = scale * p2;
assert_eq!(p3, Point::new(5., -5., 0.));
let p4 = translate * p3;
assert_eq!(p4, Point::new(15., 0., 7.));
}
#[test]
fn chained_translations() {
let p = Point::new(1., 0., 1.);
let rotation = rotation_x(PI / 2.);
let scale = scaling(5., 5., 5.);
let translate = translation(10., 5., 7.);
let tr = translate * scale * rotation;
assert_eq!(tr * p, Point::new(15., 0., 7.));
}
}

View File

@ -29,7 +29,7 @@ impl Canvas {
&self.pixels[self.addr(y, x)]
}
pub fn pixel_mut(&mut self, x: usize, y: usize) -> &mut Color {
pub fn pixel_mut<'a>(&'a mut self, x: usize, y: usize) -> &'a mut Color {
let addr = self.addr(y, x);
&mut self.pixels[addr]
}

View File

@ -1,6 +1,4 @@
use std::cmp::Ordering;
use crate::types::{eq_f64, Point, Tuple, Vector};
use crate::types::{eq_f64, Tuple, Point, Vector};
#[derive(Clone, Debug)]
pub struct Matrix {
@ -29,7 +27,7 @@ impl Matrix {
self.values[self.addr(row, column)]
}
pub fn cell_mut(&mut self, row: usize, column: usize) -> &mut f64 {
pub fn cell_mut<'a>(&'a mut self, row: usize, column: usize) -> &'a mut f64 {
let addr = self.addr(row, column);
&mut self.values[addr]
}
@ -94,15 +92,19 @@ impl Matrix {
for r in 0..self.size {
for c in 0..self.size {
let dest_r = match r.cmp(&row) {
Ordering::Less => r,
Ordering::Greater => r - 1,
Ordering::Equal => continue,
let dest_r = if r < row {
r
} else if r > row {
r - 1
} else {
continue;
};
let dest_c = match c.cmp(&column) {
Ordering::Less => c,
Ordering::Greater => c - 1,
Ordering::Equal => continue,
let dest_c = if c < column {
c
} else if c > column {
c - 1
} else {
continue;
};
*m.cell_mut(dest_r, dest_c) = self.cell(r, c);
}
@ -196,8 +198,8 @@ impl std::ops::Mul<Tuple> for Matrix {
let mut t = [0.; 4];
for (row, item) in t.iter_mut().enumerate() {
*item = self.cell(row, 0) * rside.0
for row in 0..4 {
t[row] = self.cell(row, 0) * rside.0
+ self.cell(row, 1) * rside.1
+ self.cell(row, 2) * rside.2
+ self.cell(row, 3) * rside.3;

View File

@ -6,7 +6,7 @@ pub struct Tuple(
pub f64, // y or green
pub f64, // z or blue
pub f64, // w, the flag which
// indicates point vs vec, or alpha
// indicates point vs vec, or alpha
);
impl Tuple {
@ -44,7 +44,12 @@ impl std::ops::Sub for Tuple {
impl std::ops::Neg for Tuple {
type Output = Tuple;
fn neg(self) -> Self::Output {
Tuple(-self.0, -self.1, -self.2, -self.3)
return Self::Output {
0: -self.0,
1: -self.1,
2: -self.2,
3: -self.3,
};
}
}