c++ - 我的贝塞尔曲线有什么问题?

标签 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;
};

在此先感谢您的帮助!

最佳答案

你使用的公式是错误的

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)

关于c++ - 我的贝塞尔曲线有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43813290/

相关文章:

c++ - 如何获得忘记算术运算的警告?

c++ - 在使用opencv和eclipse的facedetect程序中使用 'CvHaarClassifierCascade*'

c++ - CMake 找不到源文件(add_executable)

c++ - OpenGL 代码适用于 Windows 但不适用于 Mac

c# - 如何不断从内部目录获取相同的目录路径

java - Graphis2d drawString 生成德语变音符号

c++ - 回文之谜: Why an array of size 3 ends up being printed with 5 elements?

c++ - 我在实现 Prim 最小生成树算法时的逻辑错误是什么?

c++ - 使用 opengl 和 c++ 加载纹理

c# - 获取特定窗口的边界