好文档 - 专业文书写作范文服务资料分享网站

计算机图形学实验报告

天下 分享 时间: 加入收藏 我要投稿 点赞

Bresenham算法 实验二 实验二 Bresenham绘制直线和圆

一、【实验目的】

1.掌握Bresenham算法扫描转换圆和直线的基本原理。

二、【实验内容】

1.利用Bresenham算法扫描转换圆和直线的基本原理编程实现对圆和直线的扫描转换。

三、【测试数据及其结果】

Bresenham算法 实验二

四、【实验源代码】

绘制直线:

#include #include #include #include

GLsizei winWidth=500; GLsizei winHeight=500;

void lineBres(int x0, int y0, int xEnd, int yEnd) {

glColor3f(0.0, 0.0, 1.0);

int dx=fabs(xEnd-x0), dy=fabs(yEnd-y0); int p=2*dy-dx;

int twoDy=2*dy, twoDyMinusDx=2*(dy-dx); int x, y;

if (x0>xEnd) { x=xEnd; y=yEnd; xEnd=x0; } else{

Bresenham算法 x=x0; y=y0; }

glPointSize(6);

glBegin(GL_POINTS); glVertex2i(x, y); glEnd();

while (x

p+=twoDyMinusDx; }

glPointSize(2);

glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } }

void init (void) {

glClearColor(1.0, 1.0, 1.0, 1.0); glShadeModel(GL_FLAT); }

void display (void) {

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); lineBres(10, 10, 400, 300); glFlush(); }

void winReshapeFcn(GLint newWidth, GLint newHeight) {

glMatrixMode(GL_PROJECTION); glLoadIdentity();

gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight)); glClear(GL_COLOR_BUFFER_BIT); winWidth=newWidth; winHeight=newHeight; }

void main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(10, 10);

glutInitWindowSize(winWidth, winHeight); glutCreateWindow(\

init();

实验二 Bresenham算法 glutDisplayFunc(display);

glutReshapeFunc(winReshapeFcn); glutMainLoop(); }

实验二 绘制圆:

#include void init() {

glClearColor(0,0,0,0); }

void MidBresenhamCircle(int r) {

int x,y,d; x=0; y=r; d=1-r;

glBegin(GL_LINE_STRIP); while(x<=y){

glVertex2f(x,y); if(d<0) d+=2*x+3; else{ d+=2*(x-y)+5; y--; } x++; }

glEnd(); }

void display() {

glClearColor(1,1,1,1);

glClear(GL_COLOR_BUFFER_BIT); glColor3f(1,0,0);

MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glutSwapBuffers(); }

void reshape(int w,int h) {

Bresenham算法 glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION); glLoadIdentity();

gluOrtho2D(-10,10,-10,10); }

int main(int argc,char**argv) {

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow(\扫描转换圆\ glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }

实验二

30xid2saoe38gut0xsx29kcek7hlwh013zo
领取福利

微信扫码领取福利

微信扫码分享