我的bezier曲线出了什么问题?

时间:2017-05-05 20:28:27

标签: c++ opengl graphics

我正在画一条二次贝塞尔曲线,我不确定它为什么表现得像它那样。它几乎看起来好像有另一个控制点影响曲线,但只有3个控制点,公式仅适用于二次曲线。

以下是它正在做的事情的一些图像:http://imgur.com/a/wOWSe

代码:

Point Bezier::evaluate(float t)
{
    //  [x,y]=(1–t)^2*2P0+2(1–t)t*P1+t^2*P2

    Point P;
    P.x = (1 - t)*(1 - t)*points[0].x +
          2*(1 - t)*(1 - t)*t*points[1].x +
          t*t*points[2].x;

    P.y = (1 - t)*(1 - t)*points[0].y +
          2*(1 - t)*(1 - t)*t*points[1].y +
          t*t*points[2].y;

    return P;
}

void Bezier::drawCurve()
{
    glColor3d(red, green, blue);
    Point lastPoint = points[0];

    for (float t = 0.0; t <= 1.0; t += 0.01)
    {
        Point currentPoint = evaluate(t);
        drawLine(lastPoint.x, lastPoint.y, currentPoint.x, currentPoint.y);
        lastPoint = currentPoint;
    }
}

void Bezier::drawHandles()
{
    glColor3d(red, green, blue);

    for (int i = 0; i < 3; i++)
    {
        drawCircle(points[i].x, points[i].y, points[i].radius);
    }
}

标题

class Point
{
    public:
        float x;
        float y;
        float radius = 5;
};

class Bezier
{
    public:
        Bezier(float startX, float startY, float endX, float endY, float red, float green, float blue);
        Point evaluate(float time);
        void drawCurve();
        void drawHandles();

        Point points[3];

        float red;
        float green;
        float blue;
};

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:6)

您使用的公式是错误的

P.x = (1 - t)*(1 - t)*points[0].x +
      2*(1 - t)*t*points[1].x +
      t*t*points[2].x;

P.y = (1 - t)*(1 - t)*points[0].y +
      2*(1 - t)*t*points[1].y +
      t*t*points[2].y;

第二个学期你有一个额外的(1-t)