直线段裁剪的Cohen-Sutherland算法实现
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字,全部文档内容请下载后查看。喜欢就下载吧 ……相关推荐:
- [外语考试]管理学 第13章 沟通
- [外语考试]07、中高端客户销售流程--分类、筛选讲
- [外语考试]2015-2020年中国高筋饺子粉市场发展现
- [外语考试]“十三五”重点项目-汽车燃油表生产建
- [外语考试]雅培奶粉培乐系列适用年龄及特点
- [外语考试]九三学社入社申请人调查问卷
- [外语考试]等级薪酬体系职等职级表
- [外语考试]货物买卖合同纠纷起诉状(范本一)
- [外语考试]青海省实施消防法办法
- [外语考试]公交车语音自动报站系统的设计第3稿11
- [外语考试]logistic回归模型在ROC分析中的应用
- [外语考试]2017-2021年中国隔膜泵行业发展研究与
- [外语考试]神经内科下半年专科考试及答案
- [外语考试]园林景观设计规范标准
- [外语考试]2018八年级语文下册第一单元4合欢树习
- [外语考试]分布式发电及微网运行控制技术应用
- [外语考试]三人行历史学笔记:中世纪人文主义思想
- [外语考试]2010届高考复习5年高考3年联考精品历史
- [外语考试]挖掘机驾驶员安全生产责任书
- [外语考试]某211高校MBA硕士毕业论文开题报告(范
- 用三层交换机实现大中型企业VLAN方案
- 斯格配套系种猪饲养管理
- 涂层测厚仪厂家直销
- 研究生学校排行榜
- 鄱阳湖湿地景观格局变化及其驱动力分析
- 医学基础知识试题库
- 2010山西省高考历年语文试卷精选考试技
- 脉冲宽度法测量电容
- 谈高职院校ESP教师的角色调整问题
- 低压配电网电力线载波通信相关技术研究
- 余额宝和城市商业银行的转型研究
- 篮球行进间运球教案
- 气候突变的定义和检测方法
- 财经大学基坑开挖应急预案
- 高大支模架培训演示
- 一种改进的稳健自适应波束形成算法
- 2-3-鼎视通核心人员薪酬股权激励管理手
- 我国电阻焊设备和工艺的应用现状与发展
- MTK手机基本功能覆盖测试案例
- 七年级地理教学课件上册第四章第一节




