教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 外语考试 >

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

来源:网络收集 时间:2026-09-06
导读: java语言下的实现 package myProject; import java.util.*; class Point{ public int x,y; //点的横纵坐标 public Point(int x,int y){ this.x=x; this.y=y; } } class Window{ public int wxl,wxr,wyb,wyt; //窗口边界值 public Window(int wxl,int wxr,int

java语言下的实现

package myProject;

import java.util.*;

class Point{

public int x,y; //点的横纵坐标

public Point(int x,int y){

this.x=x;

this.y=y;

}

}

class Window{

public int wxl,wxr,wyb,wyt; //窗口边界值

public Window(int wxl,int wxr,int wyb,int wyt){

this.wxl=wxl;

this.wxr=wxr;

this.wyb=wyb;

this.wyt=wyt;

}

}

public class cutLine {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.print("Please input the Line's 2 Point\np1: ");

Point p1=new Point(sc.nextInt(),sc.nextInt());

System.out.print("p2: ");

Point p2=new Point(sc.nextInt(),sc.nextInt());

System.out.println("Please input the window's wxl,wxr,wyb,wyt:");

Window win=new Window(sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextInt()); cutLine cut=new cutLine();

cut.run(p1, p2, win);

if(cut.result==false)

System.out.println("The Line has been abandoned");

else

System.out.println("After cut,the coordinate of the line:");

System.out.println("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(p.x<win.wxl)

code+=1;

java语言下的实现

code+=2; if(p.y<win.wyb) code+=4; if(p.y>win.wyt) code+=8; return code; } void run(Point p1,Point p2,Window win){ byte codeP1=getCode(p1,win); byte codeP2=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); //递归调用此函数来求 } void search(Point outP,Point stayP,Window win,byte code){ 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; } else if((code&8)!=0){ outP.x=(int)(outP.x-stayP.x)/(outP.y-stayP.y)*(win.wyt-outP.y)+outP.x; outP.y=win.wyt; } else if((code&2)!=0){ outP.y=(int)(outP.y-stayP.y)/(outP.x-stayP.x)*(win.wxr-outP.x)+outP.y; outP.x=win.wxr; } else if((code&4)!=0){ outP.x=(int)(outP.x-stayP.x)/(outP.y-stayP.y)*(win.wyt-outP.y)+outP.x;

java语言下的实现

} } }

…… 此处隐藏:150字,全部文档内容请下载后查看。喜欢就下载吧 ……
直线段裁剪的Cohen-Sutherland算法实现.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1693122.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)