M src/main.rs => src/main.rs +7 -5
@@ 6,14 6,16 @@ use ray::*;
fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> f64 {
let oc = r.origin() - *center;
- let a = dot(&r.direction(), &r.direction());
- let b = 2.0 * dot(&oc, &r.direction());
- let c = dot(&oc, &oc) - radius*radius;
- let discriminant = b*b - 4f64*a*c;
+ let a = r.direction().length_squared();
+ let half_b = dot(&oc, &r.direction());
+ let c = oc.length_squared() - radius*radius;
+ let discriminant = half_b*half_b - a*c;
+
+
if discriminant < 0.0 {
-1.0
} else {
- (-b - discriminant.sqrt() ) / (2.0*a)
+ (-half_b - discriminant.sqrt() ) / a
}
}
M src/vec3.rs => src/vec3.rs +4 -4
@@ 21,11 21,11 @@ impl Vec3 {
self.e[2]
}
- pub fn lenght(&self) -> f64 {
- self.lenght_squared().sqrt()
+ pub fn length(&self) -> f64 {
+ self.length_squared().sqrt()
}
- pub fn lenght_squared(&self) -> f64 {
+ pub fn length_squared(&self) -> f64 {
self.e[0] * self.e[0] + self.e[1] * self.e[1] + self.e[2] * self.e[2]
}
}
@@ 160,7 160,7 @@ pub fn cross(u: &Vec3, v: &Vec3) -> Vec3 {
}
pub fn unit_vector(v: Vec3) -> Vec3 {
- v / v.lenght()
+ v / v.length()
}
pub fn write_color(out: &mut impl std::io::Write, pixel_color: Color) {