osu.Framework/MathUtils/Interpolation.cs
/// <summary>
/// Calculates the Lagrange basis polynomial for a given set of x coordinates based on previously computed barycentric weights.
/// </summary>
/// <param name="points">An array of coordinates. No two x should be the same.</param>
/// <param name="weights">An array of precomputed barycentric weights.</param>
/// <param name="time">The x coordinate to calculate the basis polynomial for.</param>
Calculates the Lagrange basis polynomial for a given set of x coordinates based on previously computed barycentric weights.
Calculates the Lagrange basis polynomial for a given set of x coordinates based on previously computed barycentric weights .
public static double BarycentricLagrange(ReadOnlySpan<Vector2> points, double[] weights, double time)
        {
            if (points == null || points.Length == 0)
                throw new ArgumentException($"{nameof(points)} must contain at least one point");
            if (points.Length != weights.Length)
                throw new ArgumentException($"{nameof(points)} must contain exactly as many items as {nameof(weights)}");
            double numerator = 0;
            double denominator = 0;
            for (int i = 0; i < points.Length; i++)
            {
                if (time == points[i].X)
                    return points[i].Y;
                double li = weights[i] / (time - points[i].X);
                numerator += li * points[i].Y;
                denominator += li;
            }
            return numerator / denominator;
        }
public static double BarycentricLagrange ( ReadOnlySpan < Vector2 > points , double [ ] weights , double time ) { if ( points == null || points . Length == 0 ) throw new ArgumentException ( $" { nameof ( points ) }  must contain at least one point " ) ; if ( points . Length != weights . Length ) throw new ArgumentException ( $" { nameof ( points ) }  must contain exactly as many items as  { nameof ( weights ) } " ) ; double numerator = 0 ; double denominator = 0 ; for ( int i = 0 ; i < points . Length ; i ++ ) { if ( time == points [ i ] . X ) return points [ i ] . Y ; double li = weights [ i ] / ( time - points [ i ] . X ) ; numerator += li * points [ i ] . Y ; denominator += li ; } return numerator / denominator ; }
Calculates the Lagrange basis polynomial for a given set of x coordinates based on previously computed barycentric weights.
Calculates the Lagrange basis polynomial for a given set of x coordinates based on previously computed barycentric weights .
// while this is not great with branch prediction, it prevents NaN at control point X coordinates
points
ReadOnlySpan<Vector2>
An array of coordinates. No two x should be the same.
An array of coordinates . No two x should be the same .
weights
double[]
An array of precomputed barycentric weights.
An array of precomputed barycentric weights .
time
double
The x coordinate to calculate the basis polynomial for.
The x coordinate to calculate the basis polynomial for .