public static float Spline ( float x , int numKnots , float [ ] knots ) { int span ; int numSpans = numKnots - 3 ; float k0 , k1 , k2 , k3 ; float c0 , c1 , c2 , c3 ; if ( numSpans < 1 ) throw new IllegalArgumentException ( "Too few knots in spline" ) ; x = x > 1 ? 1 : x ; x = x < 0 ? 0 : x ; x *= numSpans ; span = ( int ) x ; if ( span > numKnots - 4 ) span = numKnots - 4 ; x -= span ; k0 = knots [ span ] ; k1 = knots [ span + 1 ] ; k2 = knots [ span + 2 ] ; k3 = knots [ span + 3 ] ; c3 = - 0.5f * k0 + 1.5f * k1 + - 1.5f * k2 + 0.5f * k3 ; c2 = 1f * k0 + - 2.5f * k1 + 2f * k2 + - 0.5f * k3 ; c1 = - 0.5f * k0 + 0f * k1 + 0.5f * k2 + 0f * k3 ; c0 = 0f * k0 + 1f * k1 + 0f * k2 + 0f * k3 ; return ( ( c3 * x + c2 ) * x + c1 ) * x + c0 ; }
compute a Catmull - Rom spline .