Soliving Quadratic Equations

In order to render shapes that can be describe as quadratic equations, we need to be able to solve them first. The following function shall be added to Math.H:

// Solve quadratic equaltion
inline bool Quadratic(Float A, Float B, Float C, Float *t0, Float *t1)
{
    // Find quadratic discriminant
    Float discrim = B * B - 4.0f * A * C;
    if (discrim <= 0.0f)
    {
        return false;
    }
    Float rootDiscrim = sqrtf(discrim);

    // Compute quadratic t_ values
    Float q;
    if (B < 0)
    {
        q = -0.5f * (B - rootDiscrim);
    }
    else
    {
        q = -0.5f * (B + rootDiscrim);
    }

    *t0 = q / A;
    *t1 = C / q;

    if (*t0 > *t1)
    {
        std::swap(*t0, *t1);
    }

    return true;
}

If you recall high scrool math, these equations have two roots. If the roots are real numbers they are the ones stored in the arguments t0 and t0, otherwise the function returns false.

The ideas for the function has been "borrowed" from the the Physically Based Rendering Techniques Renderer, a.k.a pbrt, enjoy!

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License