Minor fix

This commit is contained in:
k 2026-04-10 09:05:20 -04:00
parent 00a1fb16bd
commit 0f4840a09b
3 changed files with 13 additions and 9 deletions

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -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()
}
}
}