《图形学》实验六:中点Bresenham算法画圆
开发环境:
VC++6.0,OpenGL
实验内容:
使用中点Bresenham算法画圆。
实验结果:
代码:
1 #include <gl/glut.h>
2
3 #define WIDTH 500
4 #define HEIGHT 500
5 #define OFFSET 15
6 #define R 8
7
8 void Init() //其它初始化
9 {
10 glClearColor(1.0f,1.0f,1.0f,1.0f); //设置背景颜色,完全不透明
11 glColor3f(1.0f,0.0f,0.0f); //设置画笔颜色
12
13 glMatrixMode(GL_PROJECTION);
14 glLoadIdentity();
15 gluOrtho2D(0.0, 30.0, 0.0, 30.0);
16 glMatrixMode(GL_MODELVIEW);
17
18 //glMatrixMode(GL_PROJECTION); //设置投影
19 //gluOrtho2D(0.0,WIDTH,0.0,HEIGHT); //设置
20 }
21
22 void CirclePoint(int x,int y)
23 {
24 glPointSize(5); //设置点的粗细
25 glBegin(GL_POINTS);
26
27 glVertex2i(OFFSET+x,OFFSET+y);
28 glVertex2i(OFFSET+y,OFFSET+x);
29 glVertex2i(OFFSET-y,OFFSET+x);
30 glVertex2i(OFFSET-x,OFFSET+y);
31 glVertex2i(OFFSET-x,OFFSET-y);
32 glVertex2i(OFFSET-y,OFFSET-x);
33 glVertex2i(OFFSET+y,OFFSET-x);
34 glVertex2i(OFFSET+x,OFFSET-y);
35
36 glEnd();
37 }
38
39 void MidBresenhamCircle(int r)
40 {
41 int x,y,d;
42 x=0,y=r,d=1-r;
43 while(x<=y){
44 CirclePoint(x,y); //画圆
45 if(d<0)
46 d+=2*x+3;
47 else{
48 d+=2*(x-y)+5;
49 y--;
50 }
51 x++;
52 }
53 }
54
55 void Display()
56 {
57 glClear(GL_COLOR_BUFFER_BIT); //清空颜色堆栈
58
59 MidBresenhamCircle(R); //画一个半径为150的圆(半径为8的太小了。。)
60
61 glFlush(); //清空缓冲区指令
62 }
63
64 int main(int argc,char** argv)
65 {
66 glutInit(&argc,argv);
67 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //初始化显示模式
68 glutInitWindowSize(WIDTH,HEIGHT); //初始化窗口大小
69 glutInitWindowPosition(200,100); //初始化窗口出现位置
70 glutCreateWindow("中点Bresenham画圆"); //初始化窗口标题
71
72 glutDisplayFunc(Display); //注册显示函数
73 Init(); //其它初始化
74 glutMainLoop(); //进入程序循环
75
76 return 0;
77 }
Freecode : www.cnblogs.com/yym2013