This commit is contained in:
2023-01-31 01:20:07 +01:00
parent b764470c77
commit 0a70a1e1de
4 changed files with 50 additions and 6 deletions

View File

@@ -12,6 +12,10 @@ impl Vec3 {
Vec3 { elements: [x, y, z] }
}
pub fn new_empty() -> Self {
Vec3::new(0.0, 0.0, 0.0)
}
pub fn x(&self) -> f64 {
self.elements[0]
}
@@ -59,6 +63,11 @@ impl Vec3 {
pub fn random_range(min: f64, max: f64) -> Self {
Vec3::new(random_range_f64(min, max), random_range_f64(min, max), random_range_f64(min, max))
}
pub fn near_zero(&self) -> bool {
let s = 1e-8;
(self.x().abs() < s) && (self.y().abs() < s) && (self.z().abs() < s)
}
}
impl Add for Vec3 {
@@ -212,4 +221,8 @@ pub fn random_in_hemisphere(normal: &Vec3) -> Vec3 {
} else {
return -in_unit_sphere;
}
}
pub fn reflect(v: &Vec3, n: &Vec3) -> Vec3 {
*v - 2.0 * v.dot(n) * *n
}