Bezier曲线

目的:了解曲线的生成原理,掌握几种常见的曲线生成算法,利用VC+OpenGL实现Bezier曲线生成算法。
要求:1、分析空间曲线生成算法的原理,绘制其程序流程图;
2、结合示范代码了解曲线生成原理与算法实现,尤其是Bezier曲线;

#include <GL/glut.h>    
#include <stdio.h>  
#include <stdlib.h> 
#include <vector>
using namespace std;
struct Point {
       int x, y;
};
Point pt[4], bz[11];    
vector<Point> vpt;
bool bDraw; 
int nInput;
void CalcBZPoints(){
       float a0,a1,a2,a3,b0,b1,b2,b3;
       a0=pt[0].x;
       a1=-3*pt[0].x+3*pt[1].x;
       a2=3*pt[0].x-6*pt[1].x+3*pt[2].x;
       a3=-pt[0].x+3*pt[1].x-3*pt[2].x+pt[3].x;
       b0=pt[0].y;
       b1=-3*pt[0].y+3*pt[1].y;
       b2=3*pt[0].y-6*pt[1].y+3*pt[2].y;
       b3=-pt[0].y+3*pt[1].y-3*pt[2].y+pt[3].y;
       float t = 0; 
       float dt = 0.01;
       for(int i = 0; t<1.1; t+=0.1, i++){
              bz[i].x = a0+a1*t+a2*t*t+a3*t*t*t;
              bz[i].y = b0+b1*t+b2*t*t+b3*t*t*t;           
       }
}
void ControlPoint(vector<Point> vpt){
       glPointSize(9);
       for(int i=0; i<vpt.size(); i++){
              glBegin (GL_POINTS);
              glColor3f (1.0f, 0.0f, 0.0f);   
              glVertex2i (vpt[i].x,vpt[i].y);
              glEnd ();
       }
}
void PolylineGL1(Point *pt, int num){
       glBegin (GL_LINE_STRIP);
       for(int i=0;i<num;i++){           
              glColor3f (1.0f, 0.0f, 1.0f);  
              glVertex2i (pt[i].x,pt[i].y);           
       }
       glEnd ();
}
void PolylineGL2(Point *pt, int num){
       glBegin (GL_LINE_STRIP);
       for(int i=0;i<num;i++){           
              glColor3f (1.0f, 1.0f, 1.0f);  
              glVertex2i (pt[i].x,pt[i].y);           
       }
       glEnd ();
}
void myDisplay(){
       glClear(GL_COLOR_BUFFER_BIT);
       glColor3f (1.0f, 1.0f, 1.0f);
       if (vpt.size() > 0) {
              ControlPoint(vpt);
       }
       if(bDraw){
              PolylineGL1(pt, 4);
              CalcBZPoints();
              PolylineGL2(bz, 11);
       }
       glFlush();
}

void Init(){
       glClearColor(0.0, 0.0, 0.0, 0.0);
       glShadeModel(GL_SMOOTH);
       printf("Please Click left button of mouse to input control point of Bezier Curve!\n");
}
void Reshape(int w, int h){
       glViewport(0, 0, (GLsizei) w, (GLsizei) h);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

void mouse(int button, int state, int x, int y){
       switch (button){
       case GLUT_LEFT_BUTTON:
              if (state == GLUT_DOWN){
                     if (nInput == 0){
                            pt[0].x = x;
                            pt[0].y = 480 - y;
                            nInput = 1;
                            vpt.clear();
                            vpt.push_back(pt[0]);
                            bDraw = false;
                            glutPostRedisplay();
                     }
                     else if (nInput == 1){
                            pt[1].x = x;
                            pt[1].y = 480 - y;
                            vpt.push_back(pt[1]);
                            nInput = 2;
                            glutPostRedisplay();
                     }    
                     else if (nInput == 2){
                            pt[2].x = x;
                            pt[2].y = 480 - y;
                            vpt.push_back(pt[2]);
                            nInput = 3;
                            glutPostRedisplay();
                     }
                     else if (nInput == 3){
                            pt[3].x = x;
                            pt[3].y = 480 - y;
                            bDraw = true;
                            vpt.push_back(pt[3]);
                            nInput = 0;
                            glutPostRedisplay();
                     }
              }
              break;
       default:
              break;
       }
}
int main(int argc, char *argv[]){
       glutInit(&argc, argv);
       glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
       glutInitWindowPosition(100, 100);
       glutInitWindowSize(640, 480);
       glutCreateWindow("Hello World!");
       Init();
       glutDisplayFunc(myDisplay);
       glutReshapeFunc(Reshape);
       glutMouseFunc(mouse);
       glutMainLoop();
       return 0;
}

这里写图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bezier曲线是一种数学曲线,它可以用来创建光滑的曲线Bezier曲线算法是通过一系列控制点来定义曲线的形状。以下是Bezier曲线算法的基本步骤: 1.定义控制点:定义一组控制点,这些点将决定曲线的形状。 2.计算Bezier曲线上的点:通过递归地计算相邻线段的同等比例点处的连线,再取同等比例的点再连线,一直取到最后那条线段的同等比例处,该点就是Bezier曲线上的点。 3.绘制Bezier曲线:将计算出的Bezier曲线上的点连接起来,就可以绘制出Bezier曲线。 下面是一个使用Python实现的例子: ```python import matplotlib.pyplot as plt import numpy as np def bezier_curve(control_points, num=1000): t = np.linspace(0, 1, num=num) n = len(control_points) result = [] for i in range(num): point = np.zeros((2,)) for j in range(n): point += control_points[j] * bernstein_poly(j, n-1, t[i]) result.append(point) return result def bernstein_poly(i, n, t): return comb(n, i) * t**i * (1-t)**(n-i) def comb(n, i): return np.math.factorial(n) / (np.math.factorial(i) * np.math.factorial(n-i)) control_points = np.array([[0, 0], [1, 2], [3, 1], [4, 3]]) curve_points = bezier_curve(control_points) plt.plot(control_points[:,0], control_points[:,1], 'ro--') plt.plot([p[0] for p in curve_points], [p[1] for p in curve_points], 'b-') plt.show() ``` 该例子中,我们定义了四个控制点,然后使用`bezier_curve`函数计算出Bezier曲线上的点,最后使用Matplotlib库绘制出Bezier曲线。你可以根据自己的需要修改控制点的坐标来创建不同形状的Bezier曲线
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值