float OpenNaturalCubicSpline::inverse( float x, float tGuess, float epsilon, int maxIterations ) { float result = tGuess; float xResult = evaluateAt( result ); float error = x - xResult; float absError = fabs( error ); int n = 0; while( ( absError > epsilon ) && ( n < maxIterations ) ) { float dxdt = derivativeAt( result ); result += error / dxdt; xResult = evaluateAt( result ); error = x - xResult; absError = fabs( error ); ++n; } #if _DEBUG if( n == maxIterations ) { printf( "max iterations reached! " ); } printf( "error = %f\n", absError ); #endif return result; }
float Spline2f::computeHalfSpace( const Vector2f& p, float* closestT, float* closestDistance ) { float t; Vector2f closestPoint = closestPointOnSpline( p, &t, closestDistance ); Vector2f tangent = derivativeAt( t ); Vector2f pointToClosestPoint = p - closestPoint; if( closestT != NULL ) { *closestT = t; } return Vector2f::cross( tangent, pointToClosestPoint ).z; }
Vector2f Spline2f::normalAt( float t ) { return derivativeAt( t ).normal(); }