Fixes wrong formular at one place and reduces duplicates of the discriminant

This commit is contained in:
Maximilian Giller 2022-11-10 12:47:31 +01:00
parent f98e578f94
commit c7f6974b58

View file

@ -28,16 +28,17 @@ bool Sphere::intersect(Ray &ray) const {
float t = INFINITY;
if (B * B - 4 * A * C < 0) {
float discriminant = B * B - 4 * A * C;
if (discriminant < 0) {
return false;
} else if (B * B - 4 + A * C == 0) {
} else if (discriminant == 0) {
t = -B / (2 * A);
} else {
float q {static_cast<float>([A, B, C](){
} else { // discriminant > 0
float q {static_cast<float>([B, discriminant](){
if (B < 0) {
return -0.5f * (B - sqrt(B * B - 4 * A * C));
return -0.5f * (B - sqrt(discriminant));
} else {
return -0.5f * (B + sqrt(B * B - 4 * A * C));
return -0.5f * (B + sqrt(discriminant));
}
}())};
float t0 = q / A;