OpenGL画圆

中点画圆

 1 #include<gl/glut.h>
 2 
 3 
 4 void MidPointCirle(int r)
 5 {
 6     int x,y;
 7     float d;
 8     x=0;y=r;d=1.25-r;
 9     glColor3f(1.0f,1.0f,1.0f);
10     glVertex2i(x,y);
11     while(x<=y)
12     {
13         if(d<0)
14             d+=2*x+3;
15         else
16         {
17             d+=2*(x-y)+5;
18             y--;
19         }
20         x++;
21     glColor3f(1.0f,1.0f,1.0f);
22     glVertex2i(x+125,y+125);
23     glColor3f(1.0f,1.0f,1.0f);
24     glVertex2i(x+125,-y+125);
25     glColor3f(1.0f,1.0f,1.0f);
26     glVertex2i(-x+125,y+125);
27     glColor3f(1.0f,1.0f,1.0f);
28     glVertex2i(-x+125,-y+125);
29     glColor3f(1.0f,1.0f,1.0f);
30     glVertex2i(y+125,x+125);
31     glColor3f(1.0f,1.0f,1.0f);
32     glVertex2i(y+125,-x+125);
33     glColor3f(1.0f,1.0f,1.0f);
34     glVertex2i(-y+125,x+125);
35     glColor3f(1.0f,1.0f,1.0f);
36     glVertex2i(-y+125,-x+125);
37     }
38     glFlush(); 
39 
40 }
41 void ChangeSize(GLsizei w,GLsizei h)
42 {
43     if(h==0)
44         h=1;
45     glMatrixMode(GL_PROJECTION);
46     glLoadIdentity();
47     if(w<=h)
48         glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0f,-1.0f);
49     else
50         glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0f,-1.0f);
51     glMatrixMode(GL_MODELVIEW);
52     glLoadIdentity();
53 }
54 
55 void myDisplay(void)
56 {
57     glClear(GL_COLOR_BUFFER_BIT);
58     glPointSize(3);
59     glBegin(GL_POINTS);
60     glVertex2i(125,125);
61     MidPointCirle(125);
62     glEnd();
63 //    glDrawpixels(width,height,GL_RGBA,GL_UNSIGNED_BYTE,pixels);
64     glFlush();
65 }
66 int main(int argc,char *argv[])
67 {
68 
69     glutInit(&argc,argv);
70     glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
71     glutInitWindowPosition(100,100);
72     glutInitWindowSize(500,500);
73     glutCreateWindow("");
74     glutDisplayFunc(*myDisplay);
75 //    glutDisplayFunc(MidPointCirle);
76     glutReshapeFunc(ChangeSize);
77     glutMainLoop();
78     return 0;
79 }

多边形画圆

 1 #include<gl/glut.h>
 2 #include<math.h>
 3 const GLfloat Pi=301415927;  4 const GLfloat R=0.5f;  5 const int n=1000;  6  7 void myDisplay(void)  8 {  9 int i; 10  glClear(GL_COLOR_BUFFER_BIT); 11 glColor3f(1.0f,1.0f,1.0f); 12  glBegin(GL_POINTS); 13 for(i=0;i<n;++i) 14 glVertex2f(R*cos(2*Pi/n*i),R*sin(2*Pi/n*i)); 15  glEnd(); 16 17 18  glFlush(); 19 } 20 void ChangeSize(GLsizei w,GLsizei h) 21 { 22 if(h==0) 23 h=1; 24  glMatrixMode(GL_PROJECTION); 25  glLoadIdentity(); 26 if(w<=h) 27 glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0f,-1.0f); 28 else 29 glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0f,-1.0f); 30  glMatrixMode(GL_MODELVIEW); 31  glLoadIdentity(); 32 } 33 34 35 int main(int argc,char *argv[]) 36 { 37 38 glutInit(&argc,argv); 39 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 40 glutInitWindowPosition(100,100); 41 glutInitWindowSize(500,500); 42 glutCreateWindow(""); 43 glutDisplayFunc(*myDisplay); 44 45 // glutReshapeFunc(ChangeSize); 46  glutMainLoop(); 47 return 0; 48 }

 

转载于:https://www.cnblogs.com/alfredzhu/p/5323713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值