summaryrefslogtreecommitdiffstats
path: root/gfx/skia/skia/src/base/SkCubics.h
blob: 7e3cbbb567411f77817b77581eb04840c996dcd5 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Copyright 2023 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef SkCubics_DEFINED
#define SkCubics_DEFINED

/**
 * Utilities for dealing with cubic formulas with one variable:
 *   f(t) = A*t^3 + B*t^2 + C*t + d
 */
class SkCubics {
public:
    /**
     * Puts up to 3 real solutions to the equation
     *   A*t^3 + B*t^2 + C*t + d = 0
     * in the provided array and returns how many roots that was.
     */
    static int RootsReal(double A, double B, double C, double D,
                         double solution[3]);

    /**
     * Puts up to 3 real solutions to the equation
     *   A*t^3 + B*t^2 + C*t + D = 0
     * in the provided array, with the constraint that t is in the range [0.0, 1.0],
     * and returns how many roots that was.
     */
    static int RootsValidT(double A, double B, double C, double D,
                           double solution[3]);


    /**
     * Puts up to 3 real solutions to the equation
     *   A*t^3 + B*t^2 + C*t + D = 0
     * in the provided array, with the constraint that t is in the range [0.0, 1.0],
     * and returns how many roots that was.
     * This is a slower method than RootsValidT, but more accurate in circumstances
     * where floating point error gets too big.
     */
    static int BinarySearchRootsValidT(double A, double B, double C, double D,
                                       double solution[3]);

    /**
     * Evaluates the cubic function with the 4 provided coefficients and the
     * provided variable.
     */
    static double EvalAt(double A, double B, double C, double D, double t) {
        return A * t * t * t +
               B * t * t +
               C * t +
               D;
    }

    static double EvalAt(double coefficients[4], double t) {
        return EvalAt(coefficients[0], coefficients[1], coefficients[2], coefficients[3], t);
    }
};

#endif