地图判断点是否在面里面算法
直接上代码:
一个数据对类:PairValue
/**
* 数据对
*
*/
public class PairValue<T> {
private T x;
private T y;
public PairValue(){
}
public PairValue(T t1, T t2) {
this.x = t1;
this.y = t2;
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
isPointInArea 方法
/***
* 判断点是否在面里
* points 一组点集合
* point 判断点
*/
public static boolean isPointInArea(List<PairValue<Double>> points, PairValue<Double> point){
if(points == null || points.size() ==0 || point == null){
return false;
}
List<Point2D.Double> dPoints = new ArrayList<Point2D.Double>();
for(PairValue<Double> onePoint : points){
dPoints.add(new Point2D.Double(onePoint.getX(), onePoint.getY()));
}
GeneralPath path = getGeneralPath(dPoints);
if(path == null){
return false;
}
Point2D.Double dPpoint = new Point2D.Double(point.getX(), point.getY());
return path.contains(dPpoint);
}
protected static GeneralPath getGeneralPath(List<Point2D.Double> points) {
GeneralPath path = new GeneralPath();
if (points.size() < 3) {
return null;
}
path.moveTo((float) points.get(0).getX(), (float) points.get(0).getY());
for (Iterator<Point2D.Double> it = points.iterator(); it.hasNext();) {
Point2D.Double point = (Point2D.Double) it.next();
path.lineTo((float) point.getX(), (float) point.getY());
}
path.closePath();
return path;
}
转载自:https://blog.csdn.net/Barney_yangbeiyan/article/details/43965587