Skip to content

Commit adfdaef

Browse files
committed
use references to hittables for hittablelist
1 parent 9e698da commit adfdaef

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

src/hittable.rs

-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ pub struct HitRecord {
99
pub front_face: bool,
1010
}
1111
impl HitRecord {
12-
// pub fn new() -> Self {
13-
// HitRecord{
14-
// p: Point3::new(0.0, 0.0, 0.0),
15-
// normal: Vec3::new(0.0, 0.0, 0.0),
16-
// t: 0.0,
17-
// front_face: false,
18-
// mat: None,
19-
// }
20-
// }
2112
pub fn set_face_normal(&mut self, r: &Ray, outward_normal: &Vec3) {
2213
self.front_face = dot(*r.dir(), *outward_normal) < 0.0;
2314
self.normal = if self.front_face {*outward_normal} else {-*outward_normal}

src/hittable_list.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use crate::{hittable::{HitRecord, Hittable}, interval::Interval};
22

3-
pub struct HittableList {
4-
objects: Vec<Box<dyn Hittable>>,
3+
pub struct HittableList<'a> {
4+
objects: Vec<&'a dyn Hittable>,
55
}
6-
impl HittableList {
6+
impl<'a> HittableList<'a> {
77
pub fn new() -> Self {
88
HittableList{objects: Vec::new()}
99
}
10-
pub fn add(&mut self, object: Box<dyn Hittable>) {
10+
pub fn add(&mut self, object: &'a dyn Hittable) {
1111
self.objects.push(object);
1212
}
1313
}
14-
impl Hittable for HittableList {
14+
impl<'a> Hittable for HittableList<'a> {
1515
fn hit(&self, r: &crate::ray::Ray, ray_t: Interval, rec: &mut HitRecord) -> bool {
1616
let mut temp_rec = HitRecord::default();
1717
let mut hit_anything = false;

src/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ fn main() {
2727
let left = Sphere::new(Point3::new(-1.0, 0.0, -1.0), 0.5, Box::new(left));
2828
let right = Sphere::new(Point3::new(1.0, 0.0, -1.0), 0.5, Box::new(right));
2929

30-
world.add(Box::new(ground));
31-
world.add(Box::new(center));
32-
world.add(Box::new(left));
33-
world.add(Box::new(right));
30+
world.add(&ground);
31+
world.add(&center);
32+
world.add(&left);
33+
world.add(&right);
3434

3535
let mut cam = Camera::new(16.0/9.0, 400);
3636
cam.samples_per_pixel = 100;

0 commit comments

Comments
 (0)