資源描述:
《計(jì)算機(jī)圖形學(xué)Cohen-Sutherland直線裁剪算法實(shí)驗(yàn)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、《計(jì)算機(jī)圖形學(xué)實(shí)驗(yàn)》報(bào)告任課教師:錢文華2016年春季學(xué)期實(shí)驗(yàn):Cohen-Sutherland直線裁剪算法實(shí)驗(yàn)時(shí)間:2016年11月3日實(shí)驗(yàn)地點(diǎn):信息學(xué)院2204實(shí)驗(yàn)?zāi)康模赫莆誄ohen-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;}//簡(jiǎn)取elseif(reject(code1,code2))//簡(jiǎn)棄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);//計(jì)算kif(code1&winLeftBitCode){p1.y+=(win