[英]Detecting Coordinates Using Path 2d
I am trying to use the contains
method provided to do a detection of the coordinates. 我正在尝试使用提供的contains
方法来检测坐标。 As researched, the codes I use seem to work fine for others. 根据研究,我使用的代码似乎对其他人也适用。 But in this case, I am unable to get a positive return from my boolean despite it being within the drawn Path2D
polygon. 但是在这种情况下,尽管布尔值位于绘制的Path2D
多边形内,但我无法从其布尔值中获得正回报。
PolygonCreation : For loop to draw coordinates contains to detect the coordinates 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");
}
given this set of codes, my coordinates are the same as the starting coordinate of the first location and have been detected false. 给定这组代码,我的坐标与第一个位置的起始坐标相同,并且被检测为错误。
Link to path 2d documentation : 链接到路径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.html
https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Path2D.Double.html https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Path2D.Double.html
You can try to do the following, instead of 您可以尝试执行以下操作,而不是
Boolean h = prettyPoly.contains(1.38201544874088, 103.95321635529909);
try to use 尝试使用
boolean h = prettyPoly.getBounds().contains(1.38201544874088, 103.95321635529909);
In my simple test with Path2D this worked as expected. 在我对Path2D的简单测试中,此方法按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.