java 凸包算法_JAVA凸包算法

这段代码实现了 Jarvis March 算法,用于计算二维平面上点集的凸包。点由 Point 类表示,包含 x 和 y 坐标。PointFactory 类用于生成点的实例,而 JarvisMarch 类执行实际的凸包计算。主函数 Test.java 中展示了算法的使用,计算了 100000 个点的凸包并输出了运行时间。
摘要由CSDN通过智能技术生成

源码一:JarvisMarch.javapackagemtn.uis.xaltin.convex;importstaticjava.lang.Math.abs;importjava.util.Iterator;importjava.util.LinkedList;importjava.util.List;publicclassJarvisMarch {privateintcount;publicintgetCount() {returncount;

}publicvoidsetCount(intcount) {this.count=count;

}privatestaticintMAX_ANGLE=4;privatedoublecurrentMinAngle=0;privateListhullPointList;privateListindexList;privatePointFactory pf;privatePoint[] ps;publicPoint[] getPs() {returnps;

}privateintfirstIndex;publicintgetFirstIndex() {returnfirstIndex;

}publicJarvisMarch() {this(10);

}publicJarvisMarch(intcount) {

pf=PointFactory.getInstance(count);

initialize();

}publicJarvisMarch(int[] x,int[] y) {

pf=PointFactory.getInstance(x, y);

initialize();

}privatevoidinitialize() {

hullPointList=newLinkedList();

indexList=newLinkedList();

firstIndex=pf.getFirstIndex();

ps=pf.getPoints();

addToHull(firstIndex);

}privatevoidaddToHull(intindex) {

indexList.add(index);

hullPointList.add(ps[index]);

}publicintcalculateHull() {for(inti=getNextIndex(firstIndex); i!=firstIndex; i=getNextIndex(i)) {

addToHull(i);

}

showHullPoints();return0;

}privatevoidshowHullPoints() {

IteratoritPoint=hullPointList.iterator();

IteratoritIndex=indexList.iterator();

Point p;inti;intindex=0;

System.out.println("The hull points is: ->");while(itPoint.hasNext()) {

i=itIndex.next();

p=itPoint.next();

System.out.print(i+":("+p.getX()+","+p.getY()+")");

index++;if(index%10==0)

System.out.println();

}

System.out.println();

System.out.println("****************************************************************");

System.out.println("The count of all hull points is"+index);

}publicintgetNextIndex(intcurrentIndex) {doubleminAngle=MAX_ANGLE;doublepseudoAngle;intminIndex=0;for(inti=0; i

pseudoAngle=getPseudoAngle(ps[i].getX()-ps[currentIndex].getX(),

ps[i].getY()-ps[currentIndex].getY());if(pseudoAngle>=currentMinAngle&&pseudoAngle

minAngle=pseudoAngle;

minIndex=i;

}elseif(pseudoAngle==minAngle){if((abs(ps[i].getX()-ps[currentIndex].getX())>abs(ps[minIndex].getX()-ps[currentIndex].getX()))||(abs(ps[i].getY()-ps[currentIndex].getY())>abs(ps[minIndex].getY()-ps[currentIndex].getY()))){

minIndex=i;

}

}

}

}

currentMinAngle=minAngle;returnminIndex;

}publicdoublegetPseudoAngle(doubledx,doubledy) {if(dx>0&&dy>=0)returndy/(dx+dy);if(dx<=0&&dy>0)return1+(abs(dx)/(abs(dx)+dy));if(dx<0&&dy<=0)return2+(dy/(dx+dy));if(dx>=0&&dy<0)return3+(dx/(dx+abs(dy)));thrownewError("Impossible");

}

}

源码二:Point.javapackagemtn.uis.xaltin.convex;publicclassPoint {//定义点的x,y坐标,之所以是int类型,是为了日后可以在计算机屏幕上进行可视化。privateintx;privateinty;//x,y的get方法publicintgetX() {returnx;

}publicintgetY() {returny;

}//定义点到屏幕边缘的距离privatestaticdoublePADDING=20;//点在屏幕中的范围privatestaticdoublePOINT_RANGE=(800-PADDING*2);//默认构造方法,产生随机点publicPoint() {this.x=(int) ((Math.random()*POINT_RANGE)+PADDING);this.y=(int) ((Math.random()*POINT_RANGE)+PADDING);

}//带参构造方法,可以实现手动输入固定点publicPoint(intx,inty) {this.x=x;this.y=y;

}//覆写hashCode()和equals()方法,实现比较和Hash@OverridepublicinthashCode() {finalintprime=31;intresult=1;

result=prime*result+x;

result=prime*result+y;returnresult;

}

@Overridepublicbooleanequals(Object obj) {

Point other=(Point) obj;if((x==other.x)&&(y==other.y))returntrue;returnfalse;

}

}

源码三:PointFactory.javapackagemtn.uis.xaltin.convex;publicclassPointFactory {/*** 单例模式,大批量产生Point,也可以手动产生Point*/privatePoint[] points=null;privateintnewIndex;privateintfirstIndex=0;publicPoint[] getPoints() {returnpoints;

}publicintgetFirstIndex() {returnfirstIndex;

}publicstaticPointFactory getInstance() {returnnewPointFactory();

}publicstaticPointFactory getInstance(intcount) {returnnewPointFactory(count);

}publicstaticPointFactory getInstance(int[] x,int[] y) {returnnewPointFactory(x, y);

}privatePointFactory() {this(10);

}privatePointFactory(intcount) {

points=newPoint[count];for(inti=0; i

points[i]=newPoint();

newIndex=i;

validatePoints();

}

firstIndex=getFirstPoint();

}publicPointFactory(int[] x,int[] y) {

points=newPoint[y.length];for(inti=0; i

points[i]=newPoint(x[i], y[i]);

}

firstIndex=getFirstPoint();

}privatevoidvalidatePoints() {for(inti=0; i

points[newIndex]=newPoint();

validatePoints();

}

}

}publicintgetFirstPoint() {intminIndex=0;for(inti=1; i

minIndex=i;

}elseif((points[i].getY()==points[minIndex].getY())&&(points[i].getX()

minIndex=i;

}

}returnminIndex;

}

}

源码四:Test.java(主函数)packagemtn.uis.xaltin.convex;publicclassTest {publicstaticvoidmain(String[] args) {longstart=System.currentTimeMillis();

JarvisMarch j=newJarvisMarch(100000);

Point[] points=j.getPs();intfirstIndex=j.getFirstIndex();//for (int i = 0; i < points.length; i++) {//System.out.print(i + ":(" + points[i].getX() + "," + points[i].getY() + ") ");//if((i+1) % 10 == 0) {//System.out.println();//}//}//System.out.println();//System.out.println("*****************************************************************");System.out.println("the first point is:"+firstIndex+":("+points[firstIndex].getX()+","+points[firstIndex].getY()+")");

System.out.println("*****************************************************************");

j.calculateHull();

System.out.println("The total running time is"+(System.currentTimeMillis()-start)+"millis.");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值