我正在尝试使用提供的contains
方法来检测坐标。经过研究,我使用的代码似乎对其他人有效。但在这种情况下,我无法从我的布尔值中获得正回报,尽管它位于绘制的Path2D
多边形内。
PolygonCreation : For 循环绘制坐标包含检测坐标
Path2D prettyPoly =new Path2D.Double();
//Shape drawn
// for loop to retrieve x,y coordinates for each location
for (int i = 0; i < coord1.length; i++) {
// split to retrieve x and y
String[] splitCoord = coord1[i].split(" ");
// 0 - long 1 - lat
y1 = Double.parseDouble(splitCoord[0]);
x1 = Double.parseDouble(splitCoord[1]);
// to plot the starting point
if(i == 0){
System.out.println("Move to x"+x1 + "y" +y1);
prettyPoly.moveTo(x1, y1);
}else{
// continue from starting point
System.out.println("Line to x "+x1 + "y " +y1);
prettyPoly.lineTo(x1, y1);
}
}
prettyPoly.closePath();
// check if contain
Boolean h = prettyPoly.contains(1.38201544874088, 103.95321635529909);
if(h == true){
System.out.println("true");
}else{
System.out.println("false");
}
鉴于这组代码,我的坐标与第一个位置的起始坐标相同,并且已被检测为错误。
链接到路径 2d 文档:
https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Path2D.html
https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Path2D.Double.html