aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/QuadraticUtilities.cpp
blob: 51a7569609941c4dca5156434cd183cf0c76a14b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "QuadraticUtilities.h"
#include <math.h>

int quadraticRoots(double A, double B, double C, double t[2]) {
    B *= 2;
    double square = B * B - 4 * A * C;
    if (square < 0) {
        return 0;
    }
    double squareRt = sqrt(square);
    double Q = (B + (B < 0 ? -squareRt : squareRt)) / -2;
    int foundRoots = 0;
    if ((Q <= A) ^ (Q < 0)) {
        t[foundRoots++] = Q / A;
    }
    if ((C <= Q) ^ (C < 0)) {
        t[foundRoots++] = C / Q;
    }
    return foundRoots;
}