宁夏师范学院数学与计算机科学学院《计算机图形学》实验报告实验序号:8实验项目名称:图形裁剪学号姓名专业、班实验地点指导教师时间一、实验目的:1.理解区域码(regionCode,RC);2.设计Cohen-Sutherland直线裁剪算法和梁友栋-Barsky线段裁剪算法3.编程实现Cohen-Sutherland直线裁剪算法和梁友栋-Barsky线段裁剪算法二、实验内容与步骤1.Cohen-Sutherland算法分析:Cohen-Sutherland线段裁剪算法也称之为编码算法,第一步判断线段两端是否都位于窗口内,如果是,则线段完全可见;否则进入第二步,判断线段是否为显然不可见,即线段的两端点均落在窗口某边所在直线的外侧,如果是,则裁减结束;否则进入第三步,求线段与窗口边延长线的交点,这个交点将线段分为两段,其中一段显然不可见,丢弃。对余下的另一段重新进行第一步,第二步判断,直至结束。整个裁减的过程可以看作一个丢弃的过程。情况1情况2情况3情况42.梁友栋-Barsky算法分析:(1)初始化线段交点的参数:u1=0,u2=1;(2)计算出各个裁剪边界的p、q值;(3)调用函数clipTest(),在函数中根据p、q来判断:是舍弃线段还是改变交点的参数。SP外侧内侧SPi内侧SP内侧SPi外侧①当p0时,参数r用于更新u1;(u1=max{u1,…,rk})②当p0时,参数r用于更新u2。(u2=min{u2,…,rk})③如果更新了u1或u2后,使u1u2,则舍弃该线段。④当p=0且q0时,因为线段平行于边界并且位于边界之外,则舍弃该线段。见下图所示。(4)p、q的四个值经判断后,如果该线段未被舍弃,则裁剪线段的端点坐标由参数u1和u2的值决定。三、程序代码:1.Cohen-Sutherland程序代码:#includeGL/glut.h#includemath.hclasswcPt2D{public:GLfloatx,y;};inlineGLintround(constGLfloata){returnGLint(a+0.5);}constGLintwinLeftBitCode=0x1;constGLintwinRightBitCode=0x2;constGLintwinBottomBitCode=0x4;constGLintwinTopBitCode=0x8;voidinit(void){glClearColor(1.0,1.0,1.0,0.0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,640.0,0.0,480.0);glMatrixMode(GL_MODELVIEW);}voidsetPixel(GLintxCoord,GLintyCoord){glBegin(GL_POINTS);glVertex2i(xCoord,yCoord);glEnd();}voidlineBres(intx0,inty0,intxend,intyend){intdx=fabs(xend-x0),dy=fabs(yend-y0);intp=2*dy-dx;inttwoDy=2*dy,twoDyMinusDx=2*(dy-dx);intx,y;if(x0xend){x=xend;y=yend;xend=x0;}else{x=x0;y=y0;}setPixel(x,y);while(xxend){x++;if(p0)p+=twoDy;else{y++;p+=twoDyMinusDx;}setPixel(x,y);}}inlineGLintinside(GLintcode){returnGLint(!code);}inlineGLintreject(GLintcode1,GLintcode2){returnGLint(code1&code2);}inlineGLintaccept(GLintcode1,GLintcode2){returnGLint(!(code1|code2));}GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax){GLubytecode=0x00;if(pt.xwinMin.x)code=code|winLeftBitCode;if(pt.xwinMax.x)code=code|winRightBitCode;if(pt.ywinMin.y)code=code|winBottomBitCode;if(pt.ywinMax.y)code=code|winTopBitCode;return(code);}voidswapPts(wcPt2D*p1,wcPt2D*p2){wcPt2Dtmp;tmp=*p1;*p1=*p2;*p2=tmp;}voidswapCodes(GLubyte*c1,GLubyte*c2){GLubytetmp;tmp=*c1;*c1=*c2;*c2=tmp;}voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLubytecode1,code2;GLintdone=false,plotLine=false;GLfloatm;while(!done){code1=encode(p1,winMin,winMax);code2=encode(p2,winMin,winMax);if(accept(code1,code2)){done=true;plotLine=true;}elseif(reject(code1,code2))done=true;else{if(inside(code1)){swapPts(&p1,&p2);swapCodes(&code1,&code2);}if(p2.x!=p1.x)m=(p2.y-p1.y)/(p2.x-p1.x);if(code1&winLeftBitCode){p1.y+=(winMin.x-p1.x)*m;p1.x=winMin.x;}elseif(code1&winRightBitCode){p1.y+=(winMax.x-p1.x)*m;p1.x=winMax.x;}elseif(code1&winBottomBitCode){if(p2.x!=p1.x)p1.x+=(winMin.y-p1.y)/m;p1.y=winMin.y;}elseif(code1&winTopBitCode){if(p2.x!=p1.x)p1.x+=(winMax.y-p1.y)/m;p1.y=winMax.y;}}}if(plotLine)lineBres(round(p1.x),round(p1.y),round(p2.x),round(p2.y));//直线生成算法}voiddisplay(void){intflag=1;wcPt2DwinMin,winMax,p1,p2;winMin.x=100;//定义裁剪窗口winMin.y=200;winMax.x=400;winMax.y=400;p1.x=50;//定义被裁剪的直线端点p1.y=50;p2.x=450;p2.y=450;glClear(GL_COLOR_BUFFER_BIT);glColor3f(0.0,0.0,1.0);glRectf(winMin.x,winMin.y,winMax.x,winMax.y);//绘制蓝色裁剪矩形glColor3f(1.0,1.0,1.0);if(flag){lineClipCohSuth(winMin,winMax,p1,p2);//绘制裁剪线段}else{glBegin(GL_LINES);glVertex2i(p1.x,p1.y);glVertex2i(p2.x,p2.y);glEnd();}glFlush();}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){intflag;if(key=='c'||key=='C')flag=1;if(key=='r'||key=='R')flag=0;glutPostRedisplay();}voidmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(100,100);glutInitWindowSize(640,480);glutCreateWindow(chaungkou);init();glutDisplayFunc(display);glutReshapeFunc(Reshape);glutKeyboardFunc(keyboard);glutMainLoop();}2.梁友栋-Barsky程序代码:#includeglut.h#includemath.h#includestdlib.hclasswcPt2D{public:GLfloatx,y;setCoords(GLfloatxCoord,GLfloatyCoord){x=xCoord;y=yCoord;}GLfloatgetx()const{returnx;}GLfloatgety()const{returny;}};voidinit(void){glClearColor(1.0,1.0,1.0,0.0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,640.0,0.0,480.0);}voidReshape(intw,inth){glViewport(0,0,(GLsizei)w,(GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);}inlineGLintround(constGLfloata){returnGLint(a+0.5);}GLintclipTest(GLfloatp,GLfloatq,GLfloat*u1,GLfloat*u2){GLfloatr;GLintreturnValue=true;if(p0.0){r=q/p;if(r*u2)returnValue=false;elseif(r*u1)*u1=r;}elseif(p0.0){r=q/p;if(r*u1)returnValue=false;elseif(r*u2)*u2=r;}elseif(q0.0)returnValue=false;return(returnValue);}voidsetPixel(GLintxCoord,GLintyCoord){glBegin(GL_POINTS);glVertex2i(xCoord,yCoord);glEnd();}voidlineBres(intx0,inty0,intxEnd,intyEnd){intdx=fabs(xEnd-x0),dy=fabs(yEnd-y0);intp=2*dy-dx;inttwoDy=2*dy,twoDyMinusDx=2*(dy-dx);intx,y;if(x0xEnd){x=xEnd;y=yEnd;xEnd=x0;}else{x=x0;y=y0;}setPixel(x,y);while(xxEnd){x++;if(p0)p+=twoDy;else{y++;p+=twoDyMinusDx;}setPixel(