直线段裁剪的Cohen-Sutherland算法实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

packagemyProject;importjava.util.*;classPoint{publicintx,y;//点的横纵坐标publicPoint(intx,inty){this.x=x;this.y=y;}}classWindow{publicintwxl,wxr,wyb,wyt;//窗口边界值publicWindow(intwxl,intwxr,intwyb,intwyt){this.wxl=wxl;this.wxr=wxr;this.wyb=wyb;this.wyt=wyt;}}publicclasscutLine{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);System.out.print(PleaseinputtheLine's2Point\np1:);Pointp1=newPoint(sc.nextInt(),sc.nextInt());System.out.print(p2:);Pointp2=newPoint(sc.nextInt(),sc.nextInt());System.out.println(Pleaseinputthewindow'swxl,wxr,wyb,wyt:);Windowwin=newWindow(sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextInt());cutLinecut=newcutLine();cut.run(p1,p2,win);if(cut.result==false)System.out.println(TheLinehasbeenabandoned);elseSystem.out.println(Aftercut,thecoordinateoftheline:);System.out.println(p1:+cut.resultP1.x++cut.resultP1.y+\np2:+cut.resultP2.x++cut.resultP2.y);}PointresultP1=newPoint(0,0);PointresultP2=newPoint(0,0);booleanresult=false;bytegetCode(Pointp,Windowwin){bytecode=0;if(p.xwin.wxl)code+=1;if(p.xwin.wxr)code+=2;if(p.ywin.wyb)code+=4;if(p.ywin.wyt)code+=8;returncode;}voidrun(Pointp1,Pointp2,Windowwin){bytecodeP1=getCode(p1,win);bytecodeP2=getCode(p2,win);if(codeP1==0&&codeP2==0){result=true;this.resultP1=p1;this.resultP2=p2;return;}if((codeP1&codeP2)!=0){result=false;return;}if(codeP1==0){search(p2,p1,win,codeP2);//如果P1在窗口内,通过外侧的P2找交点}else{search(p1,p2,win,codeP1);//如果P2在窗口内,通过外侧的P1找交点}//确保了search函数里第一个参数在窗口外run(p1,p2,win);//递归调用此函数来求}voidsearch(PointoutP,PointstayP,Windowwin,bytecode){if((code&1)!=0){outP.y=(int)(outP.y-stayP.y)/(outP.x-stayP.x)*(win.wxl-outP.x)+outP.y;outP.x=win.wxl;}elseif((code&8)!=0){outP.x=(int)(outP.x-stayP.x)/(outP.y-stayP.y)*(win.wyt-outP.y)+outP.x;outP.y=win.wyt;}elseif((code&2)!=0){outP.y=(int)(outP.y-stayP.y)/(outP.x-stayP.x)*(win.wxr-outP.x)+outP.y;outP.x=win.wxr;}elseif((code&4)!=0){outP.x=(int)(outP.x-stayP.x)/(outP.y-stayP.y)*(win.wyt-outP.y)+outP.x;outP.y=win.wyb;}}}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功