diff --git a/src/image.rs b/src/image.rs index 6011551..d456b81 100644 --- a/src/image.rs +++ b/src/image.rs @@ -57,7 +57,7 @@ impl Image { } pub fn get_pixel(&mut self, x: i32, y: i32) -> Option<&mut Pixel> { - if x >= self.width || y >= self.height { + if x >= self.width || y >= self.height || x < 0 || y < 0{ return None; } diff --git a/src/object.rs b/src/object.rs index 68e0ea5..26cfeb4 100644 --- a/src/object.rs +++ b/src/object.rs @@ -60,9 +60,9 @@ impl Hittable for Sphere { let sqrtd = discriminant.sqrt(); let mut root = (h - sqrtd) / a; - if !interval.surounds(root) { + if !interval.surrounds(root) { root = (h + sqrtd) / a; - if !interval.surounds(root) { + if !interval.surrounds(root) { return None; } } diff --git a/src/vec3.rs b/src/vec3.rs index 95a4077..0b4eaeb 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -41,7 +41,7 @@ impl Interval { self.min <= x && x <= self.max } - pub fn surounds(self, x:f64) -> bool { + pub fn surrounds(self, x:f64) -> bool { self.min < x && x < self.max } } @@ -136,21 +136,21 @@ impl Vec3 { self.x * other.x + self.y * other.y + self.z * other.z } - pub fn x(self) -> f64 { + pub fn x(&self) -> f64 { self.x } - pub fn y(self) -> f64 { + pub fn y(&self) -> f64 { self.y } - pub fn z(self) -> f64 { + pub fn z(&self) -> f64 { self.z } pub fn cross(self, other: Vec3) -> Vec3 { let x = self.y * other.z - self.z * other.y; - let y = self.y * other.x - self.x * other.y; + let y = self.z * other.x - self.x * other.z; let z = self.x * other.y - self.y * other.x; Vec3 { x, y, z } } @@ -164,6 +164,10 @@ impl Vec3 { } pub fn unit(self) -> Vec3 { - self / self.length() + if self.length() == 0.0{ + self + } else { + self / self.length() + } } }