/**
* @brief Interpolates a parabolic segment and finds its vertex and roots within a specific range.
*
* This function fits a quadratic polynomial y = ax^2 + bx + c to three points,
* implicitly defined by the y-values ym, y0, yp (presumably at x = -1, 0, +1).
* It calculates the vertex (xe, ye) of this parabola and its roots (where y = 0).
* It specifically counts how many roots fall within the interval [-1, 1]
* and adjusts one of the roots if it's less than -1.
*
* @param ym The y-value at the point before the central point (e.g., y[-1]).
* @param y0 The y-value at the central point (e.g., y[0]).
* @param yp The y-value at the point after the central point (e.g., y[+1]).
* @param xe Pointer to a double where the x-coordinate of the parabola's vertex will be stored.
* @param ye Pointer to a double where the y-coordinate of the parabola's vertex will be stored.
* @param z1 Pointer to a double where the first root (or adjusted root) will be stored.
* @param z2 Pointer to a double where the second root will be stored.
* @param nz Pointer to an integer where the count of roots within [-1, 1] will be stored.
*/
void Interp( double ym, double y0, double yp, double* xe, double* ye, double* z1, double* z2, int* nz )
{
// Local variables for calculations
double a, b, c; // Coefficients of the quadratic equation y = ax^2 + bx + c
double d; // Discriminant (b^2 - 4ac)
double dx; // Half the distance between the roots
// Initialize the root counter to zero.
*nz = 0;
// Calculate the coefficients of the quadratic parabola passing through
// (assumed) points (-1, ym), (0, y0), and (1, yp).
// The general form is y = ax^2 + bx + c.
// Using the points:
// For (0, y0): y0 = a(0)^2 + b(0) + c => c = y0
// For (1, yp): yp = a(1)^2 + b(1) + c => yp = a + b + y0
// For (-1, ym): ym = a(-1)^2 + b(-1) + c => ym = a - b + y0
//
// Solving the system for a and b:
// (yp - y0) = a + b
// (ym - y0) = a - b
// Adding the two equations: (yp - y0) + (ym - y0) = 2a => a = 0.5 * (ym + yp - 2*y0)
// Subtracting the second from the first: (yp - y0) - (ym - y0) = 2b => b = 0.5 * (yp - ym)
// The code uses a slightly rearranged form for 'a':
a = 0.5 * (ym + yp) - y0; // Equivalent to 0.5 * (ym + yp - 2*y0)
b = 0.5 * (yp - ym); // Coefficient for the linear term
c = y0; // Coefficient for the constant term (y-value at x=0)
// Calculate the vertex (xe, ye) of the parabola y = ax^2 + bx + c.
// The x-coordinate of the vertex is at -b / (2a).
// Handle the case a = 0 separately if necessary, though it's not explicitly done here.
// If a is very close to zero, this might lead to division by zero or large values.
*xe = -b / (2.0 * a);
// Calculate the y-coordinate of the vertex by substituting xe back into the parabola equation.
*ye = a * (*xe) * (*xe) + b * (*xe) + c;
// Alternative calculation: *ye = c - b*b / (4.0*a); (derived from vertex formula)
// The code uses: *ye = ( a * (*xe) + b ) * (*xe) + c; which is equivalent.
// Calculate the discriminant of the quadratic equation ax^2 + bx + c = 0.
d = b * b - 4.0 * a * c;
// Check if the parabola has real roots (i.e., intersects the x-axis).
if (d >= 0) {
// Calculate the distance from the vertex x-coordinate (xe) to the roots.
// The roots are given by (-b +/- sqrt(d)) / (2a).
// dx = sqrt(d) / (2*|a|) represents half the distance between the roots.
dx = 0.5 * sqrt(d) / fabs(a);
// Calculate the two potential roots, z1 and z2.
*z1 = *xe - dx; // First root
*z2 = *xe + dx; // Second root
// Count how many roots lie within the interval [-1, 1] based on their absolute value.
// Check if the first root is within the range [-1, 1].
if (fabs(*z1) <= 1.0) {
*nz += 1; // Increment count if it is.
}
// Check if the second root is within the range [-1, 1].
if (fabs(*z2) <= 1.0) {
*nz += 1; // Increment count if it is.
}
// Special adjustment: If the first root (z1) is strictly less than -1.0,
// replace it with the second root (z2).
// This might be intended to discard roots outside the [-1, 1] range on the
// negative side, or to ensure z1 is closer to the [-1, 1] interval if possible.
// Note: The count *nz is NOT updated after this potential replacement.
// It reflects the count based on the original z1 and z2 values.
if (*z1 < -1.0) {
*z1 = *z2;
}
// Note: There's no corresponding check for z2 < -1.0 or for values > 1.0.
// Also, if both z1 and z2 are <= -1.0, z1 becomes z2, but *nz would still be 0.
}
// If d < 0, the parabola does not cross the x-axis, so z1, z2 are not calculated/updated,
// and *nz remains 0. The values of *z1 and *z2 might be uninitialized or hold
// previous values if not handled carefully by the caller.
}