first sphere

This commit is contained in:
2023-01-27 00:45:00 +01:00
parent bdda31935e
commit 7761307d54
2 changed files with 245 additions and 231 deletions

View File

@@ -9,7 +9,21 @@ use crate::{
ray::*
};
fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> bool {
let oc = r.origin() - *center;
let a = r.direction().dot(&r.direction());
let b = 2.0 * oc.dot(&r.direction());
let c = oc.dot(&oc) - radius*radius;
let discriminant = b*b - 4.0*a*c;
discriminant > 0.0
}
fn ray_color(r: &Ray) -> Color {
if hit_sphere(&Point3::new(0.0, 0.0, -1.0), 0.5, r) {
return Color::new(1.0, 0.0, 0.0);
}
let unit_direction: Vec3 = r.direction().unit_vector();
let t = 0.5 * (unit_direction.y() + 1.0);
//println!("{}", t);