我正在尝试使用提供的contains
方法来检测坐标。经过研究,我使用的代码似乎适用于其他人。但在这种情况下,我无法从布尔值得到正回报,尽管它在绘制的Path2D
多边形内。
PolygonCreation: 用于循环绘制坐标 包含检测坐标
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
答案 0 :(得分:1)
您可以尝试执行以下操作,而不是
Boolean h = prettyPoly.contains(1.38201544874088, 103.95321635529909);
尝试使用
boolean h = prettyPoly.getBounds().contains(1.38201544874088, 103.95321635529909);
在我使用Path2D的简单测试中,这按预期工作。