实验4-编码裁剪算法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验4-编码裁剪算法实验4编码裁剪算法1.实验目的了解二维图形裁剪的原理(点的裁剪、直线的裁剪、多边形的裁剪),利用VC+OpenGL实现直线的裁剪算法。2.实验内容(1)理解直线裁剪的原理(Cohen-Surtherland算法、梁友栋算法)(2)利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。(3)调试、编译、修改程序。(4)尝试实现梁友栋裁剪算法。实验原理编码裁剪算法中,为了快速判断一条直线段与矩形窗口的位置关系,采用了如图所示的空间划分和编码方案。裁剪一条线段时,先求出两端点所在的区号code1和code2,若code1=0且code2=0,则说明线段的两个端点均在窗口内,那么整条线段必在窗口内,应取之;若code1和code2经按位与运算的结果不为0,则说明两个端点同在窗口的上方、下方、左方或右方。这种情况下,对线段的处理是弃之。如果上述两种条件都不成立,则按第三种情况处理。求出线段与窗口某边的交点,在交点处把线段一分为二,其中必有一段完全在窗口外,可弃之,对另一段则重复上述处理。实验代码#includeGL/glut.h#includestdio.h#includestdlib.h#defineLEFT_EDGE1#defineRIGHT_EDGE2#defineBOTTOM_EDGE4#defineTOP_EDGE8voidLineGL(intx0,inty0,intx1,inty1){glBegin(GL_LINES);glColor3f(1.0f,0.0f,0.0f);glVertex2f(x0,y0);glColor3f(0.0f,1.0f,0.0f);glVertex2f(x1,y1);glEnd();}structRectangle{floatxmin,xmax,ymin,ymax;};Rectanglerect;intx0,y0,x1,y1;intCompCode(intx,inty,Rectanglerect){//求端点所在区号intcode=0x00;if(yrect.ymin)code=code|4;//0100下if(yrect.ymax)code=code|8;//1000上if(xrect.xmax)code=code|2;//0010右if(xrect.xmin)code=code|1;//0001左returncode;}intcohensutherlandlineclip(Rectanglerect,int&x0,int&y0,int&x1,int&y1){intaccept,done;floatx,y;accept=0;done=0;intcode0,code1,codeout;code0=CompCode(x0,y0,rect);code1=CompCode(x1,y1,rect);do{if(!(code0|code1)){//端点均在视区内accept=1;//取done=1;}elseif(code0&code1){//端点在视区外侧x0=y0=x1=y1=0;done=1;accept=1;}else{if(code0!=0){codeout=code0;}else{codeout=code1;}if(codeout&LEFT_EDGE){y=y0+(y1-y0)*(rect.xmin-x0)/(x1-x0);//直线与视区边界的交点x=(float)rect.xmin;}elseif(codeout&RIGHT_EDGE){y=y0+(y1-y0)*(rect.xmax-x0)/(x1-x0);x=(float)rect.xmax;}elseif(codeout&BOTTOM_EDGE){x=x0+(x1-x0)*(rect.ymin-y0)/(y1-y0);y=(float)rect.ymin;}elseif(codeout&TOP_EDGE){x=x0+(x1-x0)*(rect.ymax-y0)/(y1-y0);y=(float)rect.ymax;}if(codeout==code0){x0=x;y0=y;code0=CompCode(x0,y0,rect);}else{x1=x;y1=y;code1=CompCode(x1,y1,rect);}}}while(!done);if(accept){LineGL(x0,y0,x1,y1);}returnaccept;}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glRectf(rect.xmin,rect.ymin,rect.xmax,rect.ymax);LineGL(x0,y0,x1,y1);glFlush();}voidInit(){glClearColor(0.0,0.0,0.0,0.0);glShadeModel(GL_FLAT);rect.xmin=100;rect.ymin=100;rect.xmax=300;rect.ymax=300;x0=0,y0=0,x1=250,y1=86;printf(Presskey'c'toClip!\nPresskey'r'toRestore!\n);}voidReshape(intw,inth){glViewport(0,0,(GLsizei)w,(GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);}voidkeyboard(unsignedcharkey,intx,inty){switch(key){case'c':cohensutherlandlineclip(rect,x0,y0,x1,y1);glutPostRedisplay();break;case'r':Init();glutPostRedisplay();break;case'x':exit(0);break;default:break;}}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowPosition(100,100);glutInitWindowSize(640,480);glutCreateWindow(HelloWorld!);Init();glutDisplayFunc(myDisplay);glutReshapeFunc(Reshape);glutKeyboardFunc(keyboard);glutMainLoop();return0;}实验结果图1裁剪前后图2裁剪前后图3裁剪前后实验分析在上述实验结果中,图2的裁剪出现了问题,究其原因,是因为当直线两端点都在视区外时,未对该线段进行舍弃,而直接不做处理,对此,改进代码为if(code0&code1){//端点在窗口外同一侧,弃之x0=y0=x1=y1=0;done=1;accept=1;}其结果截图如下:图4裁剪前后

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功