simplify Sphere#hit
This commit is contained in:
parent
14fd1d73fc
commit
e3a8b08381
@ -24,16 +24,16 @@ public record Sphere(@NotNull Vec3 center, double radius) implements Shape {
|
||||
var oc = ray.origin().minus(center());
|
||||
|
||||
var a = ray.direction().squared();
|
||||
var b = 2 * ray.direction().times(oc);
|
||||
var h = ray.direction().times(oc);
|
||||
var c = oc.squared() - radius * radius;
|
||||
|
||||
var discriminant = b * b - 4 * a * c;
|
||||
var discriminant = h * h - a * c;
|
||||
if (discriminant < 0) return Optional.empty();
|
||||
|
||||
var sd = Math.sqrt(discriminant);
|
||||
|
||||
double t = (- b - sd) / (2 * a);
|
||||
if (t < 0) t = (-b + sd) / (2 * a);
|
||||
double t = (- h - sd) / a;
|
||||
if (t < 0) t = (- h + sd) / a;
|
||||
if (t < 0) return Optional.empty();
|
||||
return Optional.of(new HitResult(t, ray.at(t).minus(center)));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user