文档详情

直线段裁剪的CohenSutherland算法实现.docx

发布:2021-11-23约1.96千字共3页下载文档
文本预览下载声明
Word可编辑 package myProject; import .*; class Point{ public int x,y; //点的横纵坐标 public Point(int x,int y){ =x; =y; } } class Window{ public int wxl,wxr,wyb,wyt; //窗口边界值 public Window(int wxl,int wxr,int wyb,int wyt){ =wxl; =wxr; =wyb; =wyt; } } public class cutLine { public static void main(String[] args) { Scanner sc=new Scanner(System.in); (Please input the Lines 2 Point\np1: ); Point p1=new Point((),()); (p2: ); Point p2=new Point((),()); (Please input the windows wxl,wxr,wyb,wyt:); Window win=new Window((),(),(),()); cutLine cut=new cutLine(); (p1, p2, win); if(==false) (The Line has been abandoned); else (After cut,the coordinate of the line:); (p1:+cut.resultP1.x+ +cut.resultP1.y+\np2:+cut.resultP2.x+ +cut.resultP2.y); } Point resultP1=new Point(0,0); Point resultP2=new Point(0,0); boolean result=false; byte getCode(Point p,Window win){ byte code=0; if() code+=1; if() code+=2; if() code+=4; if() code+=8; return code; } void run(Point p1,Point p2,Window win){ byte codeP1=getCode(p1,win); byte codeP2=getCode(p2,win); if(codeP1==0codeP2==0){ result=true; this.resultP1=p1; this.resultP2=p2; return; } if((codeP1codeP2)!=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); //递归调用此函数来求 } void search(Point outP,Point stayP,Window win,byte code){ if((code1)!=0){ =(int)(outP.y-stayP.y)/(outP.x-stayP.x)*(win.wxl-outP.x)+outP.y; =; } else if((code8)!=0){ =(int)(outP.x-stayP.x)/(outP.y-stayP.y)*(win.wyt-outP.y)+outP.x; =; } else if((code2)!=0){ =(int)(outP.y-stayP.y)/(outP.x-stayP.x)*(win.wxr-outP.x)+outP.y; =; } else if((code4)!=0){ =(int)(outP.x-stayP.x)/(outP.y-stayP.y)*(win.wyt-outP.y)+outP.x; =; } } }
显示全部
相似文档