源碼一
package nvex;
import static java
import java
import java
import java
public class JarvisMarch {
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
unt = count;
}
private static int MAX_ANGLE =
private double currentMinAngle =
private List<Point> hullPointList;
private List<Integer> indexList;
private PointFactory pf;
private Point[] ps;
public Point[] getPs() {
return ps;
}
private int firstIndex;
public int getFirstIndex() {
return firstIndex;
}
public JarvisMarch() {
this(
}
public JarvisMarch(int count) {
pf = PointFactory
initialize();
}
public JarvisMarch(int[] x
pf = PointFactory
initialize();
}
private void initialize() {
hullPointList = new LinkedList<Point>();
indexList = new LinkedList<Integer>();
firstIndex = pf
ps = pf
addToHull(firstIndex);
}
private void addToHull(int index) {
indexList
hullPointList
}
public int calculateHull() {
for (int i = getNextIndex(firstIndex); i != firstIndex; i = getNextIndex(i)) {
addToHull(i);
}
showHullPoints();
return
}
private void showHullPoints() {
Iterator<Point> itPoint = erator();
Iterator<Integer> itIndex = erator();
Point p;
int i;
int index =
System
while (itPoint
i = itIndex
p = itPoint
System
index++;
if (index %
System
}
System
System
System
}
public int getNextIndex(int currentIndex) {
double minAngle = MAX_ANGLE;
double pseudoAngle;
int minIndex =
for (int i =
if (i != currentIndex) {
pseudoAngle = getPseudoAngle(ps[i]
ps[i]
if (pseudoAngle >= currentMinAngle && pseudoAngle < minAngle) {
minAngle = pseudoAngle;
minIndex = i;
} else if (pseudoAngle == minAngle){
if((abs(ps[i]
abs(ps[minIndex]
|| (abs(ps[i]
abs(ps[minIndex]
minIndex = i;
}
}
}
}
currentMinAngle = minAngle;
return minIndex;
}
public double getPseudoAngle(double dx
if (dx >
return dy / (dx + dy);
if (dx <=
return
if (dx <
return
if (dx >=
return
throw new Error(
}
}
源碼二
package nvex;
public class Point {
// 定義點的x
private int x;
private int y;
// x
public int getX() {
return x;
}
public int getY() {
return y;
}
// 定義點到屏幕邊緣的距離
private static double PADDING =
// 點在屏幕中的范圍
private static double POINT_RANGE = (
// 默認構造方法
public Point() {
this
this
}
// 帶參構造方法
public Point(int x
this
this
}
// 覆寫hashCode()和equals()方法
@Override
public int hashCode() {
final int prime =
int result =
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
Point other = (Point) obj;
if ((x == other
return true;
return false;
}
}
源碼三
package nvex;
public class PointFactory {
/**
* 單例模式
*/
private Point[] points = null;
private int newIndex;
private int firstIndex =
public Point[] getPoints() {
return points;
}
public int getFirstIndex() {
return firstIndex;
}
public static PointFactory getInstance() {
return new PointFactory();
}
public static PointFactory getInstance(int count) {
return new PointFactory(count);
}
public static PointFactory getInstance(int[] x
return new PointFactory(x
}
private PointFactory() {
this(
}
private PointFactory(int count) {
points = new Point[count];
for (int i =
points[i] = new Point();
newIndex = i;
validatePoints();
}
firstIndex = getFirstPoint();
}
public PointFactory(int[] x
points = new Point[y
for (int i =
points[i] = new Point(x[i]
}
firstIndex = getFirstPoint();
}
private void validatePoints() {
for(int i =
if(points[i]
points[newIndex] = new Point();
validatePoints();
}
}
}
public int getFirstPoint() {
int minIndex =
for (int i =
if (points[i]
minIndex = i;
} else if ((points[i]
&& (points[i]
minIndex = i;
}
}
return minIndex;
}
}
源碼四
package nvex;
public class Test {
public static void main(String[] args) {
long start = System
JarvisMarch j = new JarvisMarch(
Point[] points = j
int firstIndex = j
// for (int i =
// System
// if((i+
// System
// }
// }
// System
// System
System
points[firstIndex]
System
j
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26948.html