知识大全 JAVA凸包算法
Posted 知
篇首语:人生遇到的每个人,出场顺序很重要,很多人如果换个时间认识,就会有不同的结局。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 JAVA凸包算法相关的知识,希望对你有一定的参考价值。
JAVA凸包算法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
源码一 JarvisMarch java
package nvex;
import static java lang Math abs;
import java util Iterator;
import java util LinkedList;
import java util List;
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 getInstance(count);
initialize();
public JarvisMarch(int[] x int[] y)
pf = PointFactory getInstance(x y);
initialize();
private void initialize()
hullPointList = new LinkedList<Point>();
indexList = new LinkedList<Integer>();
firstIndex = pf getFirstIndex();
ps = pf getPoints();
addToHull(firstIndex);
private void addToHull(int index)
indexList add(index);
hullPointList add(ps[index]);
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 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 % == )
System out println();
System out println();
System out println( **************************************************************** );
System out println( The count of all hull points is + index);
public int getNextIndex(int currentIndex)
double minAngle = MAX_ANGLE;
double pseudoAngle;
int minIndex = ;
for (int i = ; i < ps length; i++)
if (i != currentIndex)
pseudoAngle = getPseudoAngle(ps[i] getX() ps[currentIndex] getX()
ps[i] getY() ps[currentIndex] getY());
if (pseudoAngle >= currentMinAngle && pseudoAngle < minAngle)
minAngle = pseudoAngle;
minIndex = i;
else if (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;
return minIndex;
public double getPseudoAngle(double dx double dy)
if (dx > && dy >= )
return dy / (dx + dy);
if (dx <= && dy > )
return + (abs(dx) / (abs(dx) + dy));
if (dx < && dy <= )
return + (dy / (dx + dy));
if (dx >= && dy < )
return + (dx / (dx + abs(dy)));
throw new Error( Impossible );
源码二 Point java
package nvex;
public class Point
// 定义点的x y坐标 之所以是int类型 是为了日后可以在计算机屏幕上进行可视化
private int x;
private int y;
// x y的get方法
public int getX()
return x;
public int getY()
return y;
// 定义点到屏幕边缘的距离
private static double PADDING = ;
// 点在屏幕中的范围
private static double POINT_RANGE = ( PADDING * );
// 默认构造方法 产生随机点
public Point()
this x = (int) ((Math random() * POINT_RANGE) + PADDING);
this y = (int) ((Math random() * POINT_RANGE) + PADDING);
// 带参构造方法 可以实现手动输入固定点
public Point(int x int y)
this x = x;
this y = y;
// 覆写hashCode()和equals()方法 实现比较和Hash
@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 x) && (y == other y))
return true;
return false;
源码三 PointFactory java
package nvex;
public class PointFactory
/**
* 单例模式 大批量产生Point 也可以手动产生Point
*/
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 int[] y)
return new PointFactory(x y);
private PointFactory()
this( );
private PointFactory(int count)
points = new Point[count];
for (int i = ; i < count; i++)
points[i] = new Point();
newIndex = i;
validatePoints();
firstIndex = getFirstPoint();
public PointFactory(int[] x int[] y)
points = new Point[y length];
for (int i = ; i < y length; i++)
points[i] = new Point(x[i] y[i]);
firstIndex = getFirstPoint();
private void validatePoints()
for(int i = ; i < newIndex; i++)
if(points[i] equals(points[newIndex]))
points[newIndex] = new Point();
validatePoints();
public int getFirstPoint()
int minIndex = ;
for (int i = ; i < points length; i++)
if (points[i] getY() < points[minIndex] getY())
minIndex = i;
else if ((points[i] getY() == points[minIndex] getY())
&& (points[i] getX() < points[minIndex] getX()))
minIndex = i;
return minIndex;
源码四 Test java(主函数)
package nvex;
public class Test
public static void main(String[] args)
long start = System currentTimeMillis();
JarvisMarch j = new JarvisMarch( );
Point[] points = j getPs();
int firstIndex = j getFirstIndex();
// for (int i = ; i < points length; i++)
// System out print(i + :( + points[i] getX() + + points[i] getY() + ) );
// if((i+ ) % == )
// 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 );
cha138/Article/program/Java/hx/201311/26948
相关参考
最近在面试遇到很多排序算法问题总结一下 定义数组如下 [java] int[]array=newint[]; int[]array=newint[]; 首先是插入排序 [java]
HITS算法Java实现 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! HITS算法是重要的链接
JAVA垃圾回收算法摘要 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 垃圾收集的算法分析 j
Java通用权限控制算法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 一种常用的权限控制算法的
Java扫雷算法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 建立一个雷区可以用一个一个的JB
Hash算法大全(java实现) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Hash算法有很
知识大全 Java技术进阶 基于Java的IDEA加密算法探讨
Java技术进阶基于Java的IDEA加密算法探讨 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
Java的垃圾回收之算法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 引言 Java的堆是一
Java代码 插入排序: packagerututilalgorithmsupport; importrututilalgorithmSortUtil; publicclassInse
Java垃圾收集算法与内存泄露 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 垃圾收集算法的核心