OpenGL绘制圆弧/圆/饼图

OpenGL绘制圆弧/圆/饼图  2

 

  1. //rorger,2011   
  2. //contact me: jhcyd112@163.com   
  3. //in this new version, we can both draw a arc/circle/pi with color filled or not.   
  4. #include "windows.h"   
  5. #include "glut.h"   
  6. #include "math.h"   
  7.   
  8. #define  width 640   
  9. #define  height 480   
  10.   
  11. void drawArc(double x,double y,double start_angle,double end_angle,double delta_angle,double radius,bool fill)  
  12. {  
  13.     if (fill)  
  14.     {  
  15.         glBegin(GL_TRIANGLE_FAN);  
  16.     }  
  17.     else  
  18.     {  
  19.         glBegin(GL_LINE_STRIP);  
  20.     }  
  21.   
  22.     for (double i=start_angle;i<=end_angle;i+=delta_angle)  
  23.     {  
  24.         double vx=x+radius * cos(i);  
  25.         double vy=y+radius*sin(i);  
  26.         glVertex2d(vx,vy);  
  27.     }  
  28.     glEnd();  
  29. }  
  30.   
  31. void drawCircle(double x, double y, double radius,bool fill)  
  32. {  
  33.     double pi=acos(-1.0);  
  34.     drawArc(x,y,0,2*pi,pi/180,radius,fill);  
  35. }  
  36.   
  37. void drawPie(double x,double y,double start_angle,double end_angle,double delta_angle,double radius,bool fill)  
  38. {  
  39.     drawArc(x,y,start_angle,end_angle,delta_angle,radius,fill);  
  40.     if (fill)  
  41.     {  
  42.         glBegin(GL_TRIANGLE_FAN);  
  43.     }  
  44.     else  
  45.     {  
  46.         glBegin(GL_LINES);  
  47.     }  
  48.     glVertex2d(x,y);  
  49.     glVertex2d(x+radius*cos(start_angle),y+radius*sin(start_angle));  
  50.     if (!fill)  
  51.     {  
  52.         glVertex2d(x,y);  
  53.     }  
  54.     glVertex2d(x+radius*cos(end_angle),y+radius*sin(end_angle));  
  55.        
  56.     glEnd();  
  57. }  
  58.   
  59. void myDisplay(void// plot the sinc function, using world coordinates   
  60. {  
  61.     double pi=acos(-1.0);  
  62.     glClear(GL_COLOR_BUFFER_BIT);      
  63.   
  64.     //画笛卡尔坐标系   
  65.     glColor3f(1.0,0.0,0.0);  
  66.     glBegin(GL_LINES);  
  67.     glVertex2d(-width/2.0,0);  
  68.     glVertex2d(width/2.0,0);  
  69.     glEnd();  
  70.     glBegin(GL_LINES);  
  71.     glVertex2d(0,height/2.0);  
  72.     glVertex2d(0,-height/2.0);  
  73.     glEnd();  
  74.   
  75.     glColor3f(1.0,1.0,0.0);  
  76.   
  77.     //draw a circle   
  78.     drawCircle(0,0,100,false);  
  79.   
  80.     //draw two arcs   
  81.     drawArc(0,0,0,pi/2.0,pi/180,200,false);  
  82.     drawArc(0,0,pi,3*pi/2.0,pi/180,200,true);  
  83.       
  84.     glEnable(GL_BLEND);   
  85.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);    
  86.     glColor4f(0.1,1.0f,0.0,0.5);  
  87.     //draw two pies   
  88.     drawPie(0,0,5*pi/4.0,7*pi/4.0,pi/180.0,200,false);  
  89.     drawPie(0,0,pi/4.0,3*pi/4.0,pi/180.0,200,false);  
  90.     glDisable(GL_BLEND);  
  91.   
  92.     glFlush();  
  93. }  
  94.   
  95. void myInit(void)  
  96. {             
  97.     glClearColor(0.0, 0.0, 0.0, 0.0);       
  98.     glColor3f(0.0f, 0.0f, 1.0f);            
  99.     glPointSize(4.0);                     
  100.     glMatrixMode(GL_PROJECTION);   
  101.     glLoadIdentity();  
  102.     gluOrtho2D(-width/2.0,width/2.0,-height/2.0,height/2.0);   
  103.     glLineWidth(2.0);  
  104. }  
  105.   
  106. void main(int argc, char** argv)  
  107. {  
  108.     glutInit(&argc, argv);  
  109.     glutInitDisplayMode(GLUT_SINGLE |GLUT_RGBA);  
  110.     glutInitWindowSize(width,height);  
  111.     glutInitWindowPosition(100,150);  
  112.     glutCreateWindow("LineInterSectoin");  
  113.     glutDisplayFunc(myDisplay);  
  114.     myInit( );  
  115.     glutMainLoop( );  
  116. }  

 

 

 

Note:你可以设置参数fill为true或者false来使图形得到填充或这空填充

其实还有一个不完善的地方,那就是没有自定义边缘颜色和填充颜色。不过相信这已经不是一个问题了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值