gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:30:33 24 4
gpt4 key购买 nike

我正在尝试通过控制点制作贝塞尔曲线。我有一些例子并遵循它。但它没有用。它显示该线在工作期间将走向 (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;
}

}

最佳答案

对于这样的贝塞尔曲线,您必须计算 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

关于c++ - 控制点的贝塞尔曲线不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47696179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com