开发环境:
VC++6.0,OpenGL
实验内容:
使用中点Bresenham算法画圆。
实验结果:
代码:
#include <gl/glut.h> #define WIDTH 500
#define HEIGHT 500
#define OFFSET 15
#define R 8 void Init() //其它初始化
{
glClearColor(1.0f,1.0f,1.0f,1.0f); //设置背景颜色,完全不透明
glColor3f(1.0f,0.0f,0.0f); //设置画笔颜色 glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 30.0, 0.0, 30.0);
glMatrixMode(GL_MODELVIEW); //glMatrixMode(GL_PROJECTION); //设置投影
//gluOrtho2D(0.0,WIDTH,0.0,HEIGHT); //设置
} void CirclePoint(int x,int y)
{
glPointSize(); //设置点的粗细
glBegin(GL_POINTS); glVertex2i(OFFSET+x,OFFSET+y);
glVertex2i(OFFSET+y,OFFSET+x);
glVertex2i(OFFSET-y,OFFSET+x);
glVertex2i(OFFSET-x,OFFSET+y);
glVertex2i(OFFSET-x,OFFSET-y);
glVertex2i(OFFSET-y,OFFSET-x);
glVertex2i(OFFSET+y,OFFSET-x);
glVertex2i(OFFSET+x,OFFSET-y); glEnd();
} void MidBresenhamCircle(int r)
{
int x,y,d;
x=,y=r,d=-r;
while(x<=y){
CirclePoint(x,y); //画圆
if(d<)
d+=*x+;
else{
d+=*(x-y)+;
y--;
}
x++;
}
} void Display()
{
glClear(GL_COLOR_BUFFER_BIT); //清空颜色堆栈 MidBresenhamCircle(R); //画一个半径为150的圆(半径为8的太小了。。) glFlush(); //清空缓冲区指令
} int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //初始化显示模式
glutInitWindowSize(WIDTH,HEIGHT); //初始化窗口大小
glutInitWindowPosition(,); //初始化窗口出现位置
glutCreateWindow("中点Bresenham画圆"); //初始化窗口标题 glutDisplayFunc(Display); //注册显示函数
Init(); //其它初始化
glutMainLoop(); //进入程序循环 return ;
}
Freecode : www.cnblogs.com/yym2013