Bezier曲線生成【計算機圖形學】
阿新 • • 發佈:2018-12-16
原理:
Bezier曲線是通過一組多邊形折線的頂點來定義的。如果折線的頂點固定不變,則由其定義的Bezier曲線是唯一的。在折線的各頂點中,只有第一點和最後一點在曲線上且作為曲線的起始處和終止處,其他的點用於控制曲線的形狀及階次。曲線的形狀趨向於多邊形折線的形狀,要修改曲線,只要修改折線的各頂點就可以了。因此,多邊形折線又稱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(5);//點的粗細 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 PolylineGL(Point *pt, int num)//連線函式 { glBegin (GL_LINE_STRIP); for(int i=0;i<num;i++) { glColor3f (1.0f, 1.0f, 0.0f);//線的顏色 glVertex2i (pt[i].x,pt[i].y); } glEnd (); } void myDisplay() { glClear(GL_COLOR_BUFFER_BIT);//清屏 glColor3f (1.0f, 1.0f, 1.0f);//線的顏色為白色 glPointSize(5);//點的寬度 if (vpt.size() > 0) { ControlPoint(vpt); } if(bDraw) { PolylineGL(pt, 4);//畫連線點的三條直線 CalcBZPoints(); PolylineGL(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; }