3

图表

我需要一种方法来以编程方式找到源自原点的线 f(x) 与由 4 个点定义的三次样条曲线之间的交点。保证该线与样条的中心段相交,在 X1 和 X2 之间。

我尝试了多种方法,但似乎无法获得预期的结果。我怀疑我的问题出在我处理复数的某个地方。

谁能找到我的代码的问题,或者提出不同的方法?

    private Vector2 CubicInterpolatedIntersection(float y0, float y1, 
                            float y2, float y3, float lineSlope, lineYOffset)
    {
        // f(x) = lineSlope * x + lineYOffset
        // g(x) = (a0 * x^3) + (a1 * x^2) + (a2 * x) + a3

        // Calculate Catmull-Rom coefficients for g(x) equation as found 
        // in reference (1). These 
        double a0, a1, a2, a3;
        a0 = -0.5 * y0 + 1.5 * y1 - 1.5 * y2 + 0.5 * y3;
        a1 = y0 - 2.5 * y1 + 2 * y2 - 0.5 * y3;
        a2 = -0.5 * y0 + 0.5 * y2;
        a3 = y1;

        // To find POI, let 'g(x) - f(x) = 0'. Simplified equation is:
        // (a0 * x^3) + (a1 * x^2) + ((a2 - lineSlope) * x) 
        //                         + (a3 - lineYOffset) = 0

        // Put in standard form 'ax^3 + bx^2 + cx + d = 0'
        double a, b, c, d;
        a = a0;
        b = a1;
        c = a2 - lineSlope;
        d = a3 - lineYOffset;


        // Solve for roots using cubic equation as found in reference (2).
        //  x = {q + [q^2 + (r-p^2)^3]^(1/2)}^(1/3) 
        //         + {q - [q^2 + (r-p^2)^3]^(1/2)}^(1/3) + p
        //  Where...
        double p, q, r;
        p = -b / (3 * a);
        q = p * p * p + (b * c - 3 * a * d) / (6 * a * a);
        r = c / (3 * a);

        //Solve the equation starting from the center.
        double x, x2;
        x = r - p * p;
        x = x * x * x + q * q;
        // Here's where I'm not sure. The cubic equation contains a square 
        // root. So if x is negative at this point, then we need to proceed
        // with complex numbers.
        if (x >= 0)
        {
            x = Math.Sqrt(x);
            x = CubicRoot(q + x) + CubicRoot(q - x) + p;
        }
        else
        {
            x = Math.Sqrt(Math.Abs(x)); 
            // x now represents the imaginary component of 
            //                       a complex number 'a + b*i'
            // We need to take the cubic root of 'q + x' and 'q - x'
            // Since x is imaginary, we have two complex numbers in 
            // standard form. Therefore, we take the cubic root of 
            // the magnitude of these complex numbers
            x = CubicRoot(Math.Sqrt(q * q + x * x)) + 
                 CubicRoot(Math.Sqrt(q * q + -x * -x)) + p;
        }

        // At this point, x should hold the x-value of 
        //      the point of intersection.
        // Now use it to solve g(x).

        x2 = x * x;
        return new Vector2((float)Math.Abs(x), 
           (float)Math.Abs(a0 * x * x2 + a1 * x2 + a2 * x + a3));
    }

参考:

  1. http://paulbourke.net/miscellaneous/interpolation/
  2. http://www.math.vanderbilt.edu/~schectex/courses/cubic/
4

2 回答 2

1

编码

if (x >= 0)
{  // One real root and two imaginaries.
    x = Math.Sqrt(x);
    x = CubicRoot(q + x) + CubicRoot(q - x) + p;
}
else
{  // Three real roots.
    x = Math.Sqrt(Math.Abs(x)); 


    x_1 = Math.Sign(q)*2*(q*q + x*x)^(1/6)*Math.Cos(Math.Atan(x/q)/3) + p;
    x_2 = Math.Sign(q)*2*(q*q + x*x)^(1/6)*Math.Cos(Math.Atan(x/q)/3 + Math.PI*2/3) + p;
    x_3 = Math.Sign(q)*2*(q*q + x*x)^(1/6)*Math.Cos(Math.Atan(x/q)/3 + Math.PI*4/3) + p;
}

您可以( )^(1/6)使用Math.Pow( , 1/6)or your Math.Sqrt(CubicRoot( ))or计算Math.Sqrt(Cbrt( ))。请参阅Microsoft 论坛上的以下主题

小心q = 0( Atan(x/0) = pi/2 radians. Cos(Atan(x/0)/3) = Sqrt(3)/2 )

于 2013-04-04T01:33:46.537 回答
0

对于二次方程,至少存在 1 个单实根。使用此方法查找根http://en.wikipedia.org/wiki/Cubic_function#General_formula_of_roots

于 2013-04-04T18:49:07.523 回答