2

I'm trying to make Bezier curve by control point. I have some examples and followed it. But it did not work. It shows the line is going to (0, 0) points during working. I still don't get it why. screenshot

Here's the code in the C++ language, using OpenGL.

#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;
    }

}
Melebius
  • 6,183
  • 4
  • 39
  • 52
garden lee
  • 21
  • 7

1 Answers1

4

For a bezier curve like this you have to calculate 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;
}

The code to create the curve may look like this:

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


An other solution is provided in the answer to the Stack Overflow question 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] };
}


But may be the result is not what you expect it to be, because you create a Higher-order Bézier curve which results in a very flat curve:

enter image description here


Maybe you want to connect any number of points with square bezier curves: Quadratic Bézier Curve: Calculate Points

Rabbid76
  • 202,892
  • 27
  • 131
  • 174