ft: 5,6.1: sphere class + normals

This commit is contained in:
djaidjai committed 2026-04-15 13:32:42 +02:00
1 parent 67c1eb861d
commit ebf39d014b
8 files changed
+126 -12

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
use core::f32::math::sqrt;
use crate::objects::hit::Hit;
use crate::objects::traits::{Hittable, Normal};
use crate::Vec3;
pub struct Sphere {
center: Vec3,
radius: f32,
}
impl Sphere {
pub fn new(center: Vec3, radius: f32) -> Sphere {
Sphere {
center: center,
radius: radius,
}
}
}
impl Hittable for Sphere {
fn hit(&self, r: &crate::ray::Ray) -> Option<Hit> {
let oc = self.center - r.origin();
let a = r.dir().dot(r.dir());
let b = -2. * r.dir().dot(&oc);
let c = oc.dot(&oc) - self.radius * self.radius;
let d = b * b - 4. * a * c;
if d < 0. {
None
} else {
let t = (-b - sqrt(d)) / (2.0 * a);
let p = r.at(t);
Some(Hit::new(t, p, self.normal_at(&p)))
}
}
}
impl Normal for Sphere {
fn normal_at(&self, p: &Vec3) -> Vec3 {
(*p - self.center).get_unit()
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.