OpenGL draws a simple parametric curve--three-order Bezier Curve (ii)

Source: Internet
Author: User

Today we introduce three times Bezier curve, this curve online information is very much, I just briefly introduce the principle.

In a two-dimensional space (three-dimensional also similar), given n+1 points P0, P1 、... 、 Pn. The Bezier curve for n times of the parameter T is:

Figure 1

We can introduce the first order, second and Sanche Besel curves according to the above formula, the following is the first order Bezier curve:

Figure 2

The following is the quadratic Bezier curve, which represents the Q1 of the Q0,P1P2 segments taken from the P0P1 segment, each of which is a tangent vector of the curve:

Figure 3

The following is the Sanche Besel curve, that is, take the Q0,P1P2 segment from the P0P1 segment take the Q1,P2P3 segment take Q2, and then take q0q1 from R0,Q1Q2, each R1 is a tangent vector of the curve:

Figure 4

This gives the formula, the following paste the third-order Beizer curve code, the same can be manually adjusted parameters, we refer to.

#include <math.h> #include <gl/glut.h> #include <iostream>using namespace std;  Glfloat xcoord[4], ycoord[4];  int num = 0;  /* Calculate Bezier curve */void Bézier (int n) {Float f1, F2, F3, F4; float DeltaT = 1.0/n;  Float T;glbegin (Gl_line_strip);  for (int i = 0; I <= N; i++) {T = i * deltat;f1 = (1-t) * (1-t) * (1-T);  F2 = 3 * T * (1-t) * (1-T);  F3 = 3 * t * t * (1-T);  F4 = T * t; GLVERTEX2F (F1*xcoord[0] + f2*xcoord[1] + f3*xcoord[2] + f4*xcoord[3], f1*ycoord[0] + f2*ycoord[1] + f3*yCoord[2] + F4*yC  OORD[3]);  } glend ();  */* With the mouse to draw, after completion can change the control point, Wsad control of the second point, IJKL control the third Point */void display () {glclear (gl_color_buffer_bit);  Gllinewidth (1.5); glcolor3f (1.0, 0.0, 0.0);  Glbegin (Gl_line_strip);  for (int i = 0; i < num; i++) glvertex3f (Xcoord[i], ycoord[i], 0.0);  Glend ();  glcolor3f (0.0, 0.0, 1.0);  if (num = = 4) Bézier (20);  Glflush (); Glutswapbuffers ();}  void Init () {Glclearcolor (1.0, 1.0, 1.0, 0.0); Glshademodel (Gl_flat); } void Myreshape (int w, int h) {glviewport (0, 0, (Glsizei) W, (Glsizei) h);  Glmatrixmode (gl_projection);  Glloadidentity ();  gluortho2d (0.0, (Glsizei) W, (Glsizei) H, 0.0);  Glmatrixmode (Gl_modelview);  Glloadidentity ();  } void Mouse (int button, int state, int x, int y) {if (Button = = Glut_left_button && state = = Glut_down) {if  (num = = 4) num = 0;  Xcoord[num] = x;  Ycoord[num] = y;  num++;  Glutpostredisplay (); }} void keyboard (unsigned char key, int x, int y) {if (key = = ' W ' && num = = 4) ycoord[1] = 5.0;if (key = = ' s ' &A mp;& num = = 4) ycoord[1] + = 5.0;if (key = = ' a ' && num = 4) xcoord[1]-= 5.0;if (key = = ' d ' && num = = 4) XCOORD[1] + = 5.0;if (key = = ' I ' && num = = 4) ycoord[2] = 5.0;if (key = = ' k ' && num = = 4) ycoord[2] + = 5.0;i F (Key = = ' J ' && num = = 4) xcoord[2] = 5.0;if (key = = ' L ' && num = 4) xcoord[2] + = 5.0;glutpostredisplay () ;}  int main (int argc, char** argv) {glutinit (&AMP;ARGC, argv); Glutinitdisplaymode (glut_double | Glut_rgb);  Glutinitwindowsize (450, 450);  Glutinitwindowposition (200, 200);  Glutcreatewindow ("Hello");  Init ();  Glutdisplayfunc (display);  Glutreshapefunc (Myreshape);  Glutkeyboardfunc (keyboard); Glutmousefunc (mouse);  Glutmainloop ();  return 0;   }

  

OpenGL draws a simple parametric curve--three-order Bezier Curve (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.