|
|
@ -24,16 +24,16 @@ public record Sphere(@NotNull Vec3 center, double radius) implements Shape {
|
|
|
|
var oc = ray.origin().minus(center());
|
|
|
|
var oc = ray.origin().minus(center());
|
|
|
|
|
|
|
|
|
|
|
|
var a = ray.direction().squared();
|
|
|
|
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 c = oc.squared() - radius * radius;
|
|
|
|
|
|
|
|
|
|
|
|
var discriminant = b * b - 4 * a * c;
|
|
|
|
var discriminant = h * h - a * c;
|
|
|
|
if (discriminant < 0) return Optional.empty();
|
|
|
|
if (discriminant < 0) return Optional.empty();
|
|
|
|
|
|
|
|
|
|
|
|
var sd = Math.sqrt(discriminant);
|
|
|
|
var sd = Math.sqrt(discriminant);
|
|
|
|
|
|
|
|
|
|
|
|
double t = (- b - sd) / (2 * a);
|
|
|
|
double t = (- h - sd) / a;
|
|
|
|
if (t < 0) t = (-b + sd) / (2 * a);
|
|
|
|
if (t < 0) t = (- h + sd) / a;
|
|
|
|
if (t < 0) return Optional.empty();
|
|
|
|
if (t < 0) return Optional.empty();
|
|
|
|
return Optional.of(new HitResult(t, ray.at(t).minus(center)));
|
|
|
|
return Optional.of(new HitResult(t, ray.at(t).minus(center)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|