我有一组要使用 B 样条曲线平滑的点。
我的问题是如何实现 B 样条曲线来平滑这些点集?
我想使用 C++ 来实现这一点。

最佳答案

以下是任意给定点数的函数:

void Spline(double x[N+1],double y[N+1], // input
            double A[N],double B[N],     // output
            double C[N],double D[N])     // output
{
    double w[N];
    double h[N];
    double ftt[N+1];

    for (int i=0; i<N; i++)
    {
        w[i] = (x[i+1]-x[i]);
        h[i] = (y[i+1]-y[i])/w[i];
    }

    ftt[0] = 0;
    for (int i=0; i<N-1; i++)
        ftt[i+1] = 3*(h[i+1]-h[i])/(w[i+1]+w[i]);
    ftt[N] = 0;

    for (int i=0; i<N; i++)
    {
        A[i] = (ftt[i+1]-ftt[i])/(6*w[i]);
        B[i] = ftt[i]/2;
        C[i] = h[i]-w[i]*(ftt[i+1]+2*ftt[i])/6;
        D[i] = y[i];
    }
}

以下是打印此函数结果的方法:
void PrintSpline(double x[N+1],            // input
                 double A[N], double B[N], // input
                 double C[N], double D[N]) // input
{
    for (int i=0; i<N; i++)
    {
        cout << x[i] << " <= x <= " << x[i+1] << " : f(x) = ";
        cout << A[i] << "(x-" << x[i] << ")^3 + ";
        cout << B[i] << "(x-" << x[i] << ")^2 + ";
        cout << C[i] << "(x-" << x[i] << ")^1 + ";
        cout << D[i] << "(x-" << x[i] << ")^0";
        cout << endl;
    }
}

请注意,这两个函数都假定 x[0] < x[1] < ... < x[N]

关于c++ - B样条曲线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25379422/

10-12 17:29