Posted on 2009-03-29 22:02
风花雪月 阅读(580)
评论(0) 编辑 收藏 引用 所属分类:
计算机图形学
1 #include "stdio.h"
2 #include <windows.h>
3 #include <gl/glut.h>
4 #include<math.h>
5 void drawline() //画坐标轴线
6 {
7 glClear(GL_COLOR_BUFFER_BIT);
8 glColor3f(0.0f,0.0f,0.0f); //坐标线为黑色
9 glBegin(GL_LINES);
10 float i=0.0f;
11 for( i=0.0f;i<1.0f;i=i+0.02f)
12 {
13 //第一象限坐标
14 glVertex2f(0, i);
15 glVertex2f(1, i);
16 glVertex2f(i, 0);
17 glVertex2f(i, 1);
18
19 //第四象限坐标
20 glVertex2f(i,0);
21 glVertex2f(i,-1);
22 glVertex2f(0,-i);
23 glVertex2f(1,-i);
24 }
25 for( i=0.0f;i>-1.0f;i=i-0.02f)
26 {
27 //第二象限坐标
28 glVertex2f(i, 0);
29 glVertex2f(i, 1);
30 glVertex2f(0,-i);
31 glVertex2f(-1,-i);
32 //第三象限坐标
33 glVertex2f(i,0);
34 glVertex2f(i,-1);
35 glVertex2f(0,i);
36 glVertex2f(-1,i);
37 }
38
39 glEnd();
40 glFlush();
41 }
42
43 void putpixel(float x,float y) //绘制一个点
44 {
45 //glRectf(5*x,5*y,5*x+5,5*y+5); //绘制一个点,这里用正方形表示点
46 glPointSize(4.0);
47 glBegin(GL_POINTS);
48 glVertex2f(x,y);
49 glEnd();
50 glFlush();
51
52 }
53 //画x,y轴
54 void PaintXY()
55 {
56 glColor3f(0.0f,0.0f,0.0f);
57 glBegin(GL_LINES);
58 float i=0.0f;
59 for( i=-1.0f;i<1.0f;i=i+0.02f)
60 {
61 putpixel(0,i);
62 putpixel(i,0);
63 }
64 glEnd();
65 glFlush();
66 }
67 void CirclePoint(float x,float y)
68 {
69 putpixel(x,y);
70 putpixel(y,x);
71 putpixel(-y,x);
72 putpixel(-x,y);
73 putpixel(y,-x);
74 putpixel(x,-y);
75 putpixel(-x,-y);
76 putpixel(-y,-x);
77
78 }
79 void MidPointCircle(float radius)
80 {
81
82 glColor3f(0.0f,0.0f,0.0f);
83 float x,y;
84 float d;
85 x=0;
86 y=radius;
87 d=5.0-4*radius;
88 CirclePoint(x,y);
89 while(y>x)
90 {
91 if(d<=0)
92 {
93 d+=8*x+0.24;
94 }
95 else
96 {
97 d+=8*(x-y)+0.4;
98 y=y-0.02;
99 }
100 x=x+0.02;
101 CirclePoint(x,y);
102 }
103 }
104
105 void myDisplay() //绘制图形
106 {
107 glClear(GL_COLOR_BUFFER_BIT);//用当前背景色填充窗口
108 drawline(); //画坐标轴线
109 PaintXY();
110 MidPointCircle(0.6);
111 }
112
113 int main(int argc,char* argv[])
114 {
115 glutInit(&argc, argv);
116 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
117 glutInitWindowPosition(100, 100);
118 glutInitWindowSize(600, 600);
119 glutCreateWindow("生成圆弧的中点算法");
120 glClearColor(1.0f,1.0f,1.0f,0.0f); //设置窗口颜色为白色
121 glutDisplayFunc(&myDisplay);
122 glutMainLoop();
123 return 0;
124 }
125