控制点的贝塞尔曲线不起作用

时间:2017-12-07 13:26:14

标签: c++ opengl bezier

我正试图通过控制点制作贝塞尔曲线。 我有一些例子,并遵循它。但它没有用。 它表示该线在工作期间将进入(0,0)点。 我仍然不明白为什么。 screenshot

以下是使用OpenGL的C ++语言代码。

#define _CRT_SECURE_NO_WARNINGS

#include <Windows.h>

#include <gl/glut.h>
#include <gl/GLU.h>
#include <gl/GL.h>

#include <math.h>

#define CTRL_COUNT 100

void display();
void init();
float getNextBezierPointX(float t);
float getNextBezierPointY(float t);
void drawline();

int ctrlPointsCount;
int ctrlPointsX[CTRL_COUNT], ctrlPointsY[CTRL_COUNT];
int X1[20] = { 10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105 };
int Y1[20] = { 50,60,40,70,40,60,35,80,45,55,30,60,40,60,40,55,35,70,40,50 };

void main(int argc, char *argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(500, 300);
    glutCreateWindow("Bezier");

    glutDisplayFunc(display);
    init();
    glutMainLoop();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);

    ctrlPointsCount = 20;

    for (int i = 0; i < 20; i++) {
        ctrlPointsX[i] = X1[i];
        ctrlPointsY[i] = Y1[i];
    }

    drawline();

    glFlush();
}

void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glColor3f(1.0, 0.0, 0.0);
    glPointSize(8.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 128.0, 0.0, 96.0);
}

float getNextBezierPointX(float t) {
    float x = 0.0;
    int c;

    for (int i = 0; i < ctrlPointsCount; i ++) {
        if (i == 0 || i == ctrlPointsCount - 1)
            c = 1;
        else
            c = ctrlPointsCount - 1;

        x = x + c * pow(t, i) * pow(1 - t, ctrlPointsCount - 1 - i) * ctrlPointsX[i];
    }

    return x;
}

float getNextBezierPointY(float t) {
    float y = 0.0;
    int c;

    for (int i = 0; i < ctrlPointsCount; i ++) {
        if (i == 0 || i == ctrlPointsCount - 1)
            c = 1;
        else
            c = ctrlPointsCount - 1;
        y = y + c * pow(t, i) * pow(1 - t, ctrlPointsCount - 1 - i) * ctrlPointsY[i];
    }

    return y;
}

void drawline() {
    float x, y;

    for (int i = 0; i < 20; i++) {
        glBegin(GL_POINTS);
        glVertex2i(ctrlPointsX[i], ctrlPointsY[i]);
        glEnd();
        glFlush();
    }

    float oldX = ctrlPointsX[0], oldY = ctrlPointsY[0];

    for (double t = 0.0; t <= 1.0; t += 0.01) {
        x = getNextBezierPointX(t);
        y = getNextBezierPointY(t);

        glColor3f(1.0, 1.0, 1.0);

        glBegin(GL_LINES);
        glVertex2f(oldX, oldY);
        glVertex2f(x, y);
        glEnd();

        glFlush();

        oldX = x;
        oldY = y;
    }

}

1 个答案:

答案 0 :(得分:3)

对于像这样的贝塞尔曲线,您必须计算Bernstein polynomials

double factorial(int n)
{
    double x = 1.0;
    for (int i = 1; i <= n; i++)
      x *= (double)i;
    return x;
}

double Ni(int n, int i)
{
    double a1 = factorial(n);
    double a2 = factorial(i);
    double a3 = factorial(n - i);
    double ni =  a1/ (a2 * a3);
    return ni;
}

double Bernstein(int n, int i, double t)
{
    double ti = (t == 0.0 && i == 0) ? 1.0 : pow(t, i); /* t^i */
    double tni = (n == i && t == 1.0) ? 1.0 : pow((1 - t), (n - i)); /* (1 - t)^i */
    double basis = Ni(n, i) * ti * tni; //Bernstein basis 
    return basis;
}

创建曲线的代码可能如下所示:

struct vec2
{
  double x, y;
};

vec2 getBezierPoint(float t, int n, int x[], int y[] )
{
    vec2 pt{ 0.0, 0.0 };
    for (int i = 0; i < n; i ++) {
        double b = Bernstein( n - 1, i, t );
        pt.x += b * x[i];
        pt.y += b * y[i];
    }
    return pt;
}

float oldX = ctrlPointsX[0], oldY = ctrlPointsY[0];
for (double t = 0.0; t <= 1.0; t += 0.01)
{
    vec2 pt = getBezierPoint( t, ctrlPointsCount, ctrlPointsX, ctrlPointsY );

    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINES);
    glVertex2f(oldX, oldY);
    glVertex2f((float)pt.x, (float)pt.y);
    glEnd();
    glFlush();

    oldX = (float)pt.x;  oldY = (float)pt.y;
}


Stack Overflow问题How do I implement a Bézier curve in C++?的答案中提供了另一种解决方案:

#include <vector>

vec2 getBezierPoint2( float t, int n, int x[], int y[] )
{
    std::vector<double> tmpx( n ), tmpy( n );
    for ( int i=0; i<n; ++ i )
    {
      tmpx[i] = x[i];
      tmpy[i] = y[i];
    }
    int i = n - 1;
    while (i > 0)
    {
        for (int k = 0; k < i; k++)
        {
            tmpx[k] = tmpx[k] + t * ( tmpx[k+1] - tmpx[k] );
            tmpy[k] = tmpy[k] + t * ( tmpy[k+1] - tmpy[k] );
        }
        i--;
    }
    return { tmpx[0], tmpy[0] };
}


但结果可能不是您所期望的结果,因为您创建的Higher-order Bézier curve会产生非常平坦的曲线:

enter image description here


也许你想用方形贝塞尔曲线连接任意数量的点:Quadratic Bézier Curve: Calculate Points