源码一: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.");
}
}