Online this kind of curve to draw a lot of articles, but most of the code is a paste on the finished, and even the parameters of how to tune it did not say clearly. I read a lot of information, here to do a summary, the main introduction of a few types of simple curve drawing, such as Hermite curve, Bezier curve and so on. Today, the first Hermite curve , basically the most common is 2.3 times the Hermite curve.
by convention, let's introduce the principle of the Hermite curve first. the Hermite curve is the curve described by the two endpoint coordinates of a given curve segment and the tangent vectors at the ends. A three-time parametric curve on a plane can be expressed as:
Figure 1
The spatial Hermite curve is similar to the above, just adding a Z component. According to the above formula, the vector expression of the curve is:
Figure 2
These variables with arrows are vectors. According to the geometry definition, whent=0 , the value of the curve expression is P0 point,t=1 , the value of the curve expression is the P1 point. We get the P0,P1,P0 ',P1 ' relationships, which solve the various algebraic coefficients:
Figure 3
Thus, the matrix expression is:
Figure 4
The middle matrix M is a matrix determined by the initial conditions. We have a basic knowledge of the writing here. This 2.3-time Hermite curve has four parameters, andP0 and P1 respectively represent the starting and ending positions, andP0 ' and P1 ' respectively represent the starting and ending directions (though slightly somewhat different, You can according to the program to debug their own feelings under the below, I posted the code, we can combine the code to understand more.
#include <math.h> #include <gl/glut.h> #include <iostream>using namespace std; #define COUNT 20struct Point2{float x;float y;};/ * global variable */float hermitemp[4][2]; Point2 vec[count];/* Q Key and a key can be adjusted derp0x * W key and S key can be adjusted DERP0Y * e key and D key can be adjusted DERP1X * R key and F key can be adjusted derp1y */int derp0x = 100;int DerP 0y = 500;int derp1x = 50;int derp1y = -50;/* Matrix M*/float Hermitem[4][4] = {{1, 0, 0, 0},{0, 0, 1, 0},{-3, 3,-2, -1},{2, 2 , 1, 1}};/* matrix m* matrix p*/void createmp (float hermitep[][2]) {memset (hermitemp, 0, sizeof (hermitemp));//initialize for (int i = 0; i < 4; i++) {for (int j = 0; J < 2; J + +) {for (int k = 0; k < 4; k++) {Hermitemp[i][j] + = (hermitem[i][k] * hermitep[k][j]);} ; }}}/* solution P (t) */void creatpt (float hermitep[][2]) {vec[0].x=hermitep[0][0];vec[0].y=hermitep[0][1];float t = 0.0; float DeltaT = 1.0/count; for (int i = 1; i < COUNT; i++) {T + = deltat;vec[i].x = Hermitemp[0][0] + hermitemp[1][0]*t + hermitemp[2][0]*t*t+ Hermi TEMP[3][0]*T*T*T;VEC[I].Y = hermitemp[0][1] + hermitemp[1][1]*t + hermitemP[2][1]*t*t + hermitemp[3][1]*t*t*t;} vec[count-1].x = HERMITEP[1][0];VEC[COUNT-1].Y = Hermitep[1][1];} void Init () {Glclearcolor (1.0, 1.0, 1.0, 0.0); Glshademodel (Gl_flat);} void reshape (int w, int h) {glviewport (0, 0, (Glsizei) W, (Glsizei) h); Glmatrixmode (gl_projection); glloadidentity (); Gluortho2d (-500, 500,-500, 500); }void display () {glclear (gl_color_buffer_bit);/*p0,p1,p0 ', P1 ' composition matrix p*/float hermitep[4][2] = {{0, 0},{200, 0},{derp0x, Derp0y},{derp1x, Derp1y}};createmp (Hermitep); creatpt (Hermitep); glcolor3f (1,0,0); Gllinewidth (3); Glbegin (Gl_line_strip); for (int i = 0; i < COUNT; i++) {glvertex2f (vec[i].x, vec[i].y);} Glend (); Glflush (); Glutswapbuffers ();} void keyboard (unsigned char key, int x, int y) {if (key = = ' Q ') derp0x + = 10;if (key = = ' a ') derp0x-= 10;if (Key = = ' W ') DerP 0y + = 10;if (key = = ' s ') derp0y-= 10;if (key = = ' E ') derp1x + = 10;if (key = = ' d ') derp1x-= 10;if (key = = ' R ') derp1y + = 10;i F (Key = = ' F ') derp1y-= 10;glutpostredisplay ();} int main (int argc, char** argv) {Glutinit (&ARGC, argv); Glutinitdisplaymode (Glut_rgb | glut_double); glutinitwindowposition (400,200); glutinitwindowsize (500,500); Glutcreatewindow ("Hermite curve"); init (); Glutdisplayfunc (display); Glutreshapefunc (reshape); Glutkeyboardfunc (keyboard); Glutmainloop (); return 0;}
OpenGL draw Simple parametric curve--2.3 times Hermite curve (i.)