有人可以帮我解决B样条曲线错误吗?

我想在c++中绘制B样条曲线,但是即使所有坐标均为正,该线段的坐标也为负。

这是B样条曲线代码。

void BSplineCurve(Dot &ControlPoint1, Dot &ControlPoint2,
                  Dot &ControlPoint3,Dot &ControlPoint4,
                  Dot &DrawCurve, double &t){

    double t2 = t * t;
    double t3 = t2 * t;
    double mt3 = (1 - t) * (1 - t) * (1 - t);

    double bi3 = mt3 / 6;
    double bi2 = ((3 * t3) - (6 * t2) + 4) / 6;
    double bi1 = ((-3 * t3) + (3 * t2) + (3 * t) + 1) / 6;
    double bi  = mt3 / 6;

    DrawCurve.x = ControlPoint1.x * bi3;
    DrawCurve.x += ControlPoint2.x * bi2;
    DrawCurve.x += ControlPoint3.x * bi1;
    DrawCurve.x += ControlPoint4.x * bi;

    DrawCurve.y = ControlPoint1.y * bi3;
    DrawCurve.y += ControlPoint2.y * bi2;
    DrawCurve.y += ControlPoint3.y * bi1;
    DrawCurve.y += ControlPoint4.y * bi;
}

这是绘图代码。
double t = 3.f;
do{

    if ((3 < t) && (t <= 4)) {
    BSplineCurve(ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, DrawCurve, t);
    Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
    }
    else if ((4 < t) && (t <= 5)) {
    BSplineCurve(ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, DrawCurve, t);
    Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
    }
    else if ((5 < t) && (t <= 6)) {
    BSplineCurve(ControlPoint3, ControlPoint4, ControlPoint5, ControlPoint6, DrawCurve, t);
    Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
    }
    t += 0.001;
} while(t < 6.001);

这是控制点的坐标。

ion1:50,50

ion2:50,100

Poiont3:200、100

点四:200,50

Poiont5:350、50

六点:350,100

但这是第一段的坐标。

Q3:-1543,-349

最佳答案

您的绘图代码看起来错误。
BSplineCurve函数中,t参数应采用[0,1]范围内的值。通过将t从0更改为1,将在点ControlPoint2ControlPoint3之间建立三次B样条。
您可以尝试类似:

Dot points[6] = {ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, ControlPoint6};
for(double t = 3.0; t < 6.0; t += 0.001)
{
    const int start = static_cast<int>(t);
    BSplineCurve(points[start - 3],
                 points[start - 2],
                 points[start - 1],
                 points[start],
                 DrawCurve,
                 t - start);
    Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}

更新资料
您的B样条计算代码也看起来不正确:-)bi应该是t3/6.0而不是mt3/6.0。参见here(幻灯片25)。

固定功能可能看起来像这样(我没有测试过):
void BSplineCurve(const Dot &point1,
                  const Dot &point2,
                  const Dot &point3,
                  const Dot &point4,
                  const double t,
                  Dot &result)
{
    const double t2 = t * t;
    const double t3 = t2 * t;
    const double mt = 1.0 - t;
    const double mt3 = mt * mt * mt;

    const double bi3 = mt3;
    const double bi2 = 3 * t3 - 6 * t2 + 4;
    const double bi1 =-3 * t3 + 3 * t2 + 3 * t + 1;
    const double bi  = t3;

    result.x = point1.x * bi3 +
               point2.x * bi2 +
               point3.x * bi1 +
               point4.x * bi;
    result.x /= 6.0;

    result.y = point1.y * bi3 +
               point2.y * bi2 +
               point3.y * bi1 +
               point4.y * bi;
    result.y /= 6.0;
}

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

10-11 22:42