简体   繁体   English

Path2D — .contain()无法正常工作

[英]Path2D — .contain() not working

I am trying to see if a point is contained within a polygon using Path2D. 我正在尝试使用Path2D查看多边形中是否包含点。

The last line, System.out.println(poly.contains(lat1, lon1)), prints "false" even though I know the coordinates(lat1, lon1) are within the polygon sapecified in "testBound". 最后一行System.out.println(poly.contains(lat1,lon1))打印“ false”,即使我知道坐标(lat1,lon1)在“ testBound”中指定的多边形内。 Is the ".contain()" not working? “ .contain()”不起作用吗? Am i missing something? 我想念什么吗?

    package poly;

import java.awt.Polygon;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.Arrays;

public class Polygon3 {
public static final double lat1 = 40.1032946;
public static final double lon1 = -84.5110052;
public static final String testBound = "40.203294,-84.521005;40.203294,-84.501005;40.003294,-84.521005;40.003294,-84.501005";




public static void main(String[] args) {
    String[] test = testBound.split(";");
    ArrayList<Double> latList = new ArrayList<Double>();
    ArrayList<Double> lonList = new ArrayList<Double>();
    for(String t : test) {
        String[] latlng = t.split(",");
        latList.add(Double.parseDouble(latlng[0]));
        lonList.add(Double.parseDouble(latlng[1]));
    }
    System.out.println(latList);
    System.out.println(lonList);

    Double latpoints[] = latList.toArray(new Double[latList.size()]);
    Double lonpoints[] = lonList.toArray(new Double[lonList.size()]);
    System.out.println(latpoints);
    System.out.println(lonpoints);
    Path2D poly = new Path2D.Double();
    for(int i = 0; i < latpoints.length; i++) {
        poly.moveTo(latpoints[i], lonpoints[i]);
    }
    poly.closePath();
    String testing = poly.toString();
    System.out.println(testing);

    System.out.println(poly.contains(lat1, lon1));
}


}

How to create a polygon? 如何创建多边形?

To create a closed polygon you need to: 要创建封闭的多边形,您需要:

  1. use moveTo to move to the first point 使用moveTo移至第一点
  2. use lineTo to connect all the other points 使用lineTo连接所有其他点
  3. use closePath to connect the last point with the first one 使用closePath将最后一个点与第一个点连接

For example: 例如:

for (int i = 0; i < latpoints.length; i++) {
    if (i == 0) {
        poly.moveTo(latpoints[i], lonpoints[i]);
    }
    else {
        poly.lineTo(latpoints[i], lonpoints[i]);
    }
}
poly.closePath();

Why contains returns false? 为什么包含返回false?

contains returns false because, due to the points order, you are not creating a square but an hourglass shape: contains返回false因为由于点数顺序,您不是在创建正方形而是在创建沙漏形状:

3   1
| X |
4   2

Since the point you are testing is in the center, it might not be considered inside the shape. 由于您要测试的点位于中心,因此可能不会考虑它在形状内部。

If you swap the order of the third and forth point you are then creating a square: 如果交换第三点和第四点的顺序,则将创建一个正方形:

4 - 1
|   |
3 - 2

and the point will be considered inside. 这一点将被考虑在内。

First of all, you do not create polygon correctly. 首先,您没有正确创建多边形。 moveTo moves every time first point of this polygon to different locations, but does not draw lines. moveTo每次将此多边形的第一个点移动到不同位置时,但不会绘制线。 You need to do something like this to create polygon: 您需要执行以下操作来创建多边形:

    poly.moveTo(latpoints[0], lonpoints[0]);
    for(int i = 1; i < latpoints.length - 1; i++) {
        poly.lineTo(latpoints[i], lonpoints[i]);
    }

Please notice, that we draw 2 lines from beginning, by closePath() we are able to finish this shape as closePath() draws straight line from last point to the first one. 请注意,我们从头开始画两条线,通过closePath()我们可以完成此形状,因为closePath()从最后一点到第一个点画直线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM