Computer Graphics: Drawing of Bezier Curves

1. Experiment purpose
: Master the definition principle and drawing process of Bezier curve

Definition:
Bezier curve, also known as Bezier curve or Bezier curve, is a mathematical curve used in two-dimensional graphics applications. General vector graphics software uses it to accurately draw curves. Bezier curves are composed of line segments and nodes. The nodes are draggable fulcrums, and the line segments are like stretchable rubber bands. The pen tool we see on the drawing tool is used to draw curves. This kind of vector curve.

2. Experimental requirements:
Use OpenGL to implement a cubic Bezier curve, input any four control points, and draw the corresponding Bezier curve; and adjust the control points with the mouse (change randomly or along a certain pattern) to dynamically display the curve.

3. The experimental code (part)
needs to install the glut library first

#include<GL/glut.h>
#include<GL/GL.h>
#include<math.h>
void mouseClick(int button, int state, int x, int y)
{
    
    
	if (button == GLUT_LEFT_BUTTON)  
	{
    
    
		if (state == GLUT_DOWN) {
    
    
			if (mask == -1) {
    
    
				for (int i = 0; i < 4; i++)
				{
    
    
					float mouse_x = (x - 200) / 200.0f, mouse_y = (200 - y) / 200.0f;
					float dis = sqrt((mouse_x - ctrl_points[i][0]) * (mouse_x - ctrl_points[i][0]) + (mouse_y - ctrl_points[i][1]) * (mouse_y - ctrl_points[i][1]));
					if (dis < 0.1f) mask = i;
				}

			}

		}
		else if (state == GLUT_UP)
			mask = -1;
	}
}

deCateljau algorithm:
In C++, Bezier curves are often generated using the deCateljau algorithm. The idea is to traverse adjacent control points each time and select a new control point on the line segment formed by two adjacent control points according to the given parameter t. In this way, the new control points generated after each traversal of the previous control points will be closer to the Bezier curve, and the number of control points is reduced by 1. When there is only one control point left, this control point can be regarded as a point on the Bezier curve. .

void de_Cateljau(int n, GLfloat list[][2])
{
    
    
	float R_x[4];
	float R_y[4];

	int k = 0;
	for (double t = 0.0; t <= 1; t += 0.1)
	{
    
    
		for (int i = 0; i < n; i++) {
    
    
			R_x[i] = ctrl_points[i][0];
			R_y[i] = ctrl_points[i][1];

		}
		
		for (int i = 1; i < n; i++)
		{
    
    
			for (int j = 0; j < n - i; j++)
			{
    
    
				R_x[j] = (1 - t) * R_x[j] + t * R_x[j + 1];
				R_y[j] = (1 - t) * R_y[j] + t * R_y[j + 1];
			}
			
		}
		list[k][0] = R_x[0];
		list[k][1] = R_y[0];
		k++;
	}
}

Plot function:

void display() {
    
    
	glClearColor(1.0, 1.0, 1.0, 1.0);//(R,G,B,alpha)自己设置
	glClear(GL_COLOR_BUFFER_BIT);//当前可写的颜色缓冲

	glPointSize(3);  
	glColor3f(0.530.510.78);
	for (int i = 0; i < 4; i++) {
    
    
		glBegin(GL_POINTS);//把每个顶点作为一个点进行处理,顶点n定义了点n,绘制N个点。
		glVertex2fv(&ctrl_points[i][0]);
		glEnd();

	}

	glColor3f(0.530.510.78);
	glBegin(GL_LINE_STRIP);//绘制从第一个顶点到最后一个顶点依次相连的一组线段,第n和n+1个顶点定义了线段n,绘制n-1条线段
	for (int i = 0; i < 4; i++) {
    
    
		glVertex2fv(&ctrl_points[i][0]);

	}
	glEnd();

	GLfloat pos_list[11][2] = {
    
     0 };
	de_Cateljau(4, pos_list);

	glColor3f(1.0, 1.0, 0.0);
	glBegin(GL_LINE_STRIP);
	for (int i = 0; i < 11; i++) {
    
    
		glVertex2fv(pos_list[i]);//指定顶点

	}
	glEnd();
	glFlush();//强制刷新缓冲,保证绘图命令将被执行

}

4. Result sample diagram
Insert image description here
You can drag any point to change the shape of the bezier curve. Click here to get the complete source code .

Guess you like

Origin blog.csdn.net/weixin_52049271/article/details/127936088