资源描述:
《计算机图形学Cohen-Sutherland直线裁剪算法实验.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、.《计算机图形学实验》报告任课教师:钱文华2016年春季学期实验:Cohen-Sutherland直线裁剪算法实验时间:2016年11月3日Word资料.实验地点:信息学院2204实验目的:掌握Cohen-Sutherland直线裁剪算法程序代码:#include#include#include#include#include#include#includeinta,b,a1,b1,pp0,pq0,pp1,pq1;voi
2、dsetPixel(GLintx,GLinty){glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}voidinit(void){glClearColor(1.0,1.0,1.0,0.0);Word资料.glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,200.0,0.0,150.0);}voidLineDDA(intx0,inty0,intxEnd,intyEnd){intdx=xEnd-x0;intdy=yEnd-y0;intsteps,k;floatxIncrement
3、,yIncrement,x=x0,y=y0;if(abs(dx)>abs(dy))steps=abs(dx);elsesteps=abs(dy);xIncrement=float(dx)/float(steps);yIncrement=float(dy)/float(steps);for(k=0;k4、ta){returnGLint(a+0.5);}constGLintwinLeftBitCode=0x1;constGLintwinRightBitCode=0x2;constGLintwinBottomBitCode=0x4;constGLintwinTopBitCode=0x8;inlineGLintinside(GLintcode){returnGLint(!code);}inlineGLintreject(GLintcode1,GLintcode2){returnGLint(code1&code2);}inlineGLintaccept(G
5、Lintcode1,GLintcode2){returnGLint(!(code1
6、code2));}Word资料.GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax){GLubytecode=0x00;if(pt.x7、winLeftBitCode;if(pt.x>winMax.x)code=code
8、winRightBitCode;if(pt.y9、winBottomBitCode;if(pt.y>winMax.y)code=c
10、ode
11、winTopBitCode;return(code);}voidswapPts(wcPt2D*p1,wcPt2D*p2){wcPt2Dtmp;tmp=*p1;*p1=*p2;*p2=tmp;}voidswapCodes(GLubyte*c1,GLubyte*c2){GLubytetmp;tmp=*c1;Word资料.*c1=*c2;*c2=tmp;}voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2){GLubytecode1,code2;GLintdone=fal
12、se,plotLine=false;GLfloatm;intx0=0;inty0=0;intx1=0;inty1=0;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
13、(&code1,&code2);Word资料.}if(p2.x!=p1.x)m=(p2.y-p1.y)/(p2.x-p1.