在java中:
public static boolean checkWithJdkGeneralPath(Point2D.Double point, List<Point2D.Double> polygon) {
java.awt.geom.GeneralPath p = new java.awt.geom.GeneralPath();
Point2D.Double first = polygon.get(0);
p.moveTo(first.x, first.y);
for (Point2D.Double d : polygon) {
p.lineTo(d.x, d.y);
}
p.lineTo(first.x, first.y);
p.closePath();
return p.contains(point);
}
例子:
public static void main(String[] args) {
Point2D.Double pd = new Point2D.Double();
// 区域外的点
pd.x = 123.394535;
pd.y = 41.856486;
// 区域内的点
// pd.x = 123.37790583;
// pd.y = 41.8402475;
List<Point2D.Double> polygon = new ArrayList<Point2D.Double>();
// 描绘区域的各点坐标
Point2D.Double point1 = new Point2D.Double();
point1.x = 123.372104;
point1.y = 41.84894027;
Point2D.Double point2 = new Point2D.Double();
point2.x = 123.372104;
point2.y = 41.8302372;
Point2D.Double point3 = new Point2D.Double();
point3.x = 123.406135;
point3.y = 41.84729972;
Point2D.Double point4 = new Point2D.Double();
point4.x = 123.4055556;
point4.y = 41.82859638;
Point2D.Double point5 = new Point2D.Double();
point5.x = 123.36920305;
point5.y = 41.84680694;
Point2D.Double point6 = new Point2D.Double();
point6.x = 123.347353;
point6.y = 41.86403361;
polygon.add(point1);
polygon.add(point2);
polygon.add(point3);
polygon.add(point4);
polygon.add(point5);
polygon.add(point6);
boolean b = checkWithJdkGeneralPath(pd,polygon);
System.out.println(b);
}
public static boolean checkWithJdkGeneralPath(Point2D.Double point, List<Point2D.Double> polygon) { java.awt.geom.GeneralPath p = new java.awt.geom.GeneralPath(); Point2D.Double first = polygon.get(0); p.moveTo(first.x, first.y); for (Point2D.Double d : polygon) { p.lineTo(d.x, d.y); } p.lineTo(first.x, first.y); p.closePath(); return p.contains(point); }