1 /* 2 * Copyright 2023 Google LLC 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 #ifndef SkCubics_DEFINED 8 #define SkCubics_DEFINED 9 10 /** 11 * Utilities for dealing with cubic formulas with one variable: 12 * f(t) = A*t^3 + B*t^2 + C*t + d 13 */ 14 class SkCubics { 15 public: 16 /** 17 * Puts up to 3 real solutions to the equation 18 * A*t^3 + B*t^2 + C*t + d = 0 19 * in the provided array and returns how many roots that was. 20 */ 21 static int RootsReal(double A, double B, double C, double D, 22 double solution[3]); 23 24 /** 25 * Puts up to 3 real solutions to the equation 26 * A*t^3 + B*t^2 + C*t + D = 0 27 * in the provided array, with the constraint that t is in the range [0.0, 1.0], 28 * and returns how many roots that was. 29 */ 30 static int RootsValidT(double A, double B, double C, double D, 31 double solution[3]); 32 33 34 /** 35 * Puts up to 3 real solutions to the equation 36 * A*t^3 + B*t^2 + C*t + D = 0 37 * in the provided array, with the constraint that t is in the range [0.0, 1.0], 38 * and returns how many roots that was. 39 * This is a slower method than RootsValidT, but more accurate in circumstances 40 * where floating point error gets too big. 41 */ 42 static int BinarySearchRootsValidT(double A, double B, double C, double D, 43 double solution[3]); 44 45 /** 46 * Evaluates the cubic function with the 4 provided coefficients and the 47 * provided variable. 48 */ EvalAt(double A,double B,double C,double D,double t)49 static double EvalAt(double A, double B, double C, double D, double t) { 50 return A * t * t * t + 51 B * t * t + 52 C * t + 53 D; 54 } 55 EvalAt(double coefficients[4],double t)56 static double EvalAt(double coefficients[4], double t) { 57 return EvalAt(coefficients[0], coefficients[1], coefficients[2], coefficients[3], t); 58 } 59 }; 60 61 #endif 62