aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/QuadraticUtilities.cpp
blob: 4b4d79417e14c9a0d0fa9c22be965ce6aecf306e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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;
    double ratio;
    int foundRoots = 0;
    if ((Q <= A) ^ (Q < 0)) {
        ratio = Q / A;
        if (!isnan(ratio)) {
            t[foundRoots++] = ratio;
        }
    }
    if ((C <= Q) ^ (C < 0)) {
        ratio = C / Q;
        if (!isnan(ratio)) {
            t[foundRoots++] = ratio;
        }
    }
    return foundRoots;
}