【问题标题】:What's wrong with my bezier curve?我的贝塞尔曲线有什么问题?
【发布时间】:2017-05-05 20:28:26
【问题描述】:

我正在绘制二次贝塞尔曲线,但我不确定它为什么会这样。几乎看起来好像还有另一个控制点影响曲线,但只有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;
};

提前感谢您的帮助!

【问题讨论】:

    标签: c++ opengl graphics


    【解决方案1】:

    你使用的公式是错误的

    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)

    【讨论】:

    • agh 我认为它在公式中并在发布之前阅读了 3 次以确保确定。不知怎的,就这样溜走了。谢谢!
    • 别担心!这种情况一直在发生。
    猜你喜欢
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    相关资源
    最近更新 更多