绘制贝塞尔曲线

贝塞尔曲线介绍

http://blog.csdn.net/tianhai110/article/details/2203572


http://blog.csdn.net/dreamcs/article/details/5907734


  1. #include <windows.h>  
  2. #include <math.h>  
  3. #include <gl/GL.h>  
  4. #include <gl/glut.h>  
  5. int SCREEN_HEIGHT = 480;  
  6. int NUMPOINTS = 0;  
  7. class Point  
  8. {  
  9. public:  
  10.     float x, y;  
  11.     void setxy(float x2, float y2)  
  12.     {  
  13.         x = x2;  
  14.         y = y2;  
  15.     }  
  16.     Point  operator&(const Point & rPoint)  
  17.     {  
  18.         x = rPoint.x;  
  19.         y = rPoint.y;  
  20.         return * this;  
  21.     }  
  22. };  
  23. Point abc[3];  
  24. void myInit()  
  25. {  
  26.     glClearColor(0.0,0.0,0.0,0.0);  
  27.     glColor3f(1.0f, 0.0, 0.0);  
  28.     glPointSize(4.0);  
  29.     glMatrixMode(GL_PROJECTION);  
  30.     glLoadIdentity();  
  31.     gluOrtho2D(0.0, 640, 0.0, 480.0);  
  32. }  
  33. void drawDot(Point pt)   
  34. {  
  35.     glBegin(GL_POINTS);  
  36.     glVertex2f(pt.x, pt.y);  
  37.     glEnd();  
  38.     glFlush();  
  39. }  
  40. void drawLine(Point p1, Point p2)  
  41. {  
  42.     glBegin(GL_LINES);  
  43.     glVertex2f(p1.x, p1.y);  
  44.     glVertex2f(p2.x, p2.y);  
  45.     glEnd();  
  46.     glFlush();  
  47. }  
  48. //三个控制点的贝塞尔曲线  
  49. 二阶贝塞尔曲线(抛物线)

     


  50. Point drawBezier(Point A, Point B, Point C, double t)   
  51. {  
  52.     Point P;  
  53.     P.x = pow((1-t), 2) * A.x + 2*t*(1-t)*B.x + pow(t, 2)*C.x;  
  54.     P.y = pow((1-t), 2) * A.y + 2*t*(1-t)*B.y + pow(t, 2)*C.y;  
  55.     return P;  
  56. }  
  57. void myMouse(int button, int state, int x, int y)  
  58. {  
  59.     if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)  
  60.     {  
  61.         abc[NUMPOINTS].setxy((float)x, (float)(SCREEN_HEIGHT - y));  
  62.         NUMPOINTS++;  
  63.         if (NUMPOINTS == 3)  
  64.         {  
  65.             glColor3f(1.0, 0.0, 1.0);  
  66.               
  67.             drawDot(abc[0]);  
  68.             drawDot(abc[1]);  
  69.             drawDot(abc[2]);  
  70.             glColor3f(1.0, 1.0, 0.0);  
  71.             drawLine(abc[0], abc[1]);  
  72.             drawLine(abc[1], abc[2]);  
  73.             glColor3f(0.0, 1.0, 1.0);  
  74.             Point POld = abc[0];  
  75.             for (double t = 0.0; t<=1.0;t+=0.1)  
  76.             {  
  77.                 Point P = drawBezier(abc[0], abc[1], abc[2], t);  
  78.                 drawLine(POld, P);  
  79.                 POld = P;  
  80.             }  
  81.             glColor3f(1.0, 0.0, 0.0);  
  82.             NUMPOINTS = 0;  
  83.         }  
  84.     }  
  85. }  
  86. void myDisplay()  
  87. {  
  88.     glClear(GL_COLOR_BUFFER_BIT);  
  89.     glFlush();  
  90. }  
  91. int main(int argc, char * agrv[])  
  92. {  
  93.     glutInit(&argc, agrv);  
  94.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
  95.     glutInitWindowSize(640, 480);  
  96.     glutInitWindowPosition(100, 150);  
  97.     glutCreateWindow("Bezier Curve");  
  98.     glutMouseFunc(myMouse);  
  99.     glutDisplayFunc(myDisplay);  
  100.     myInit();  
  101.     glutMainLoop();  
  102.     return 0;  
  103. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值