资源描述:
《计算机图形学代码_CS法直线段裁剪.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、这段代码主要是用CS法对一个直线段进行裁剪。运行在VC环境下。typedefstruct{floatx,y;}Point;typedefstruct{unsignedall;unsignedleft,right,top,bottom;}OutCode;voidCompOutCode(Pointp,RECT*rect,OutCode*outCode){outCode->all=0;outCode->top=outCode->bottom=0;if(p.y>(float)rect->bottom){outCode->top=1;outCode->a
2、ll+=1;}elseif(p.y<(float)rect->top){outCode->bottom=1;outCode->all+=1;}outCode->left=outCode->right=0;if(p.x>(float)rect->right){outCode->right=1;outCode->all+=1;}elseif(p.x<(float)rect->left){outCode->left=1;outCode->all+=1;}}voidCohen_SutherlandLineClip(Pointp0,Pointp1,REC
3、T*rect,CDC*pDC){boolaccept=false,done=false;OutCodeoutCode0,outCode1;OutCode*outCodeOut;Pointp;CompOutCode(p0,rect,&outCode0);CompOutCode(p1,rect,&outCode1);do{if(!outCode0.all&&!outCode1.all)accept=done=true;elseif((outCode0.all&outCode1.all)!=0)done=true;else{if(outCode0.a
4、ll)outCodeOut=&outCode0;elseoutCodeOut=&outCode1;if(outCodeOut->left){p.y=p0.y+(p1.y-p0.y)*(rect->left-p0.x)/(p1.x-p0.x);p.x=(float)rect->left;}elseif(outCodeOut->top){p.x=p0.x+(p1.x-p0.x)*(rect->top-p0.y)/(p1.y-p0.y);p.y=(float)rect->top;}elseif(outCodeOut->right){p.y=p0.y+
5、(p1.y-p0.y)*(rect->right-p0.x)/(p1.x-p0.x);p.x=(float)rect->right;}elseif(outCodeOut->bottom){p.x=p0.x+(p1.x-p0.x)*(rect->bottom-p0.y)/(p1.y-p0.y);p.y=(float)rect->bottom;}if(outCodeOut->all==outCode0.all){p0.x=p.x;p0.y=p.y;CompOutCode(p0,rect,&outCode0);}else{p1.x=p.x;p1.y=
6、p.y;CompOutCode(p1,rect,&outCode1);}}}while(!done);if(accept){pDC->MoveTo((int)p0.x,(int)p0.y);pDC->LineTo((int)p1.x,(int)p1.y);}}voidCCohen_SutherlandView::OnDraw(CDC*pDC){CCohen_SutherlandDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereRECTrect
7、={50,100,150,200};pDC->Rectangle(&rect);Pointp0,p1;p0.x=40;p0.y=90;p1.x=140;p1.y=190;Cohen_SutherlandLineClip(p0,p1,&rect,pDC);}