天天看点

贝塞罗曲线

给定点P0,P1,线性贝兹曲线是这两点之间连线的点,方程式如下:

B(t) = P0 + (P1 - P0)t t取[0,1];

假设P0(x0, y0),P1(x1, y1), Pn((1-t)x0+tx1),(1-t)y0+ty1)

1)通过点在直线上,斜率相等证明一阶贝塞罗曲线是一直线

K(PnP0) = K(PnP1)这种方法相对简单

2)通过P0P1两点根据点斜式求出直线的方程,然后将Pn点代入,

同样可以证明,相对繁琐

void myDisplay()

{

  glClear(GL_COLOR_BUFFER_BIT);

  glColor3f(1.0f,0.f,0.f);

  int P1x = 10;

  int P1y = 10;

  int P2x = 200;

  int P2y = 400;

  glPointSize(3);

  glLineWidth(1);

  glBegin(GL_POINTS);

  glVertex2d(P1x, P1y);

  glVertex2d(P2x, P2y);

  for (int i=0; i<10; i++)

  {

    double t = i*0.1;

    double x = (1-t)*P1x + t* P2x;

    double y = (1-t)*P1y + t* P2y;

    glVertex2d(x, y);

  }

  glEnd();

  glColor3f(0.f,1.f,0.f);

  glBegin(GL_LINES);

  glFlush();

}

二次方公式

二次方贝兹曲线的路径由给定点P0、P1、P2的函数B(t):

B(t) = (1-t)*(1-t)P0 + 2*t*(1-t)P1 + t*tP2 t取[0,1];

#include <windows.h>

#include <gl/gl.h>

#include <gl/glu.h>

#include <gl/glut.h>

//////////////////////////////////

void myInit()

  glClearColor(1.0,1.0,1.0,0.0);

  glLineWidth(3.0);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluOrtho2D(0.0,640,0.0,480);

/////////////////////////////////////

  int Pcx = 0;

  int Pcy = 200;

  glVertex2d(Pcx, Pcy);

  for (int i=0; i<100; i++)

    double t = i*0.01;

    double x = (1-t)*(1-t)*P1x + 2*(1-t)*Pcx + t*t*P2x;

    double y = (1-t)*(1-t)*P1y + 2*(1-t)*Pcy + t*t*P2y;

///////////////////////////////////////

void main(int argc,char **argv)

  glutInit(&argc,argv);

  glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

  glutInitWindowSize(640,480);

  glutInitWindowPosition(100,150);

  glutCreateWindow("example");

  glutDisplayFunc(myDisplay);

  myInit();

  glutMainLoop();

     本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1379629,如需转载请自行联系原作者