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