简体   繁体   English

如何检查二维点是否在 Java 中的二维多边形内?

[英]How to check if a 2d point is inside a 2D polygon in Java?

In my Java application I'm creating 2D polygons using an array of vertices.在我的 Java 应用程序中,我正在使用顶点数组创建二维多边形。 For example, I want to create a simple square using these 4 vertices例如,我想使用这 4 个顶点创建一个简单的正方形

[-130, -74], [-125, -74], [-125, -70], [-130, -70]

Then I want to check if a point is inside the generated Polygon.然后我想检查一个点是否在生成的多边形内。 But if I check, for example, this point但是,例如,如果我检查这一点

[-125, -73]

using polygon.contains(x, z) it says is not inside the Polygon.使用polygon.contains(x, z)它说不在多边形内。 Even if I check a corner, like [-125, -74] is returns false.即使我检查一个角落,例如[-125, -74]也会返回 false。 The strange part for me is that is I check this point [-126, -74] is returns true, so some points are actually seen as inside the polygon, while others are not, and I can't understand why is it.对我来说奇怪的是,我检查了这个点[-126, -74]是否返回 true,所以有些点实际上被视为在多边形内部,而另一些则不是,我不明白为什么会这样。 This is a sample code I set up to test this, nothing special about it这是我为测试而设置的示例代码,没什么特别的

public static void main(String[] args) {
   Polygon polygon = new Polygon(new int[]{-130, -125, -125, -130}, new int[]{-74, -74, -70, -70}, 4);
   System.out.println("" + polygon.contains(-125, -73));
   System.out.println("" + polygon.contains(-125, -74));
   System.out.println("" + polygon.contains(-126, -74));
}

And the output as well还有 output

false
false
true

I would also point out the fact that this is just a simple example, but the Polygon could be a really complex shape, for example something crazy like this我还要指出这只是一个简单的例子,但多边形可能是一个非常复杂的形状,例如像这样疯狂的东西

The document Polygon says文档多边形

This Polygon is defined with an even-odd winding rule.这个多边形是用奇偶缠绕规则定义的。 See WIND_EVEN_ODD for a definition of the even-odd winding rule.有关奇偶缠绕规则的定义,请参见WIND_EVEN_ODD

WIND_EVEN_ODD WIND_EVEN_ODD
The winding rule constant for specifying an even-odd rule for determining the interior of a path.用于指定用于确定路径内部的奇偶规则的缠绕规则常量。 The even-odd rule specifies that a point lies inside the path if a ray drawn in any direction from that point to infinity is crossed by path segments an odd number of times.奇偶规则指定如果从该点到无穷远的任何方向上绘制的射线被路径段交叉奇数次,则该点位于路径内。

So you can do like this.所以你可以这样做。

static Polygon mirror(Polygon p) {
    int npoints = p.npoints;
    int[] xpoints = new int[npoints];
    int[] ypoints = new int[npoints];
    for (int i = 0; i < npoints; ++i) {
        xpoints[i] = -p.xpoints[i];
        ypoints[i] = -p.ypoints[i];
    }
    return new Polygon(xpoints, ypoints, npoints);
}

static boolean onVertex(Polygon p, int x, int y) {
    int npoints = p.npoints;
    for (int i = 0; i < npoints; ++i)
        if (p.xpoints[i] == x && p.ypoints[i] == y)
            return true;
    return false;
}

static boolean contains(Polygon p, int x, int y) {
    return p.contains(x, y)
        || onVertex(p, x, y)
        || mirror(p).contains(-x, -y);
}

And

Polygon polygon = new Polygon(new int[]{-130, -125, -125, -130}, new int[]{-74, -74, -70, -70}, 4);
System.out.println("" + contains(polygon, -125, -73));
System.out.println("" + contains(polygon, -125, -74));
System.out.println("" + contains(polygon, -126, -74));

output: output:

true
true
true

A test for a polygon with a hole.对带孔的多边形的测试。

int width = 100, height = 100;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] xs = {20, 80, 80, 20, 20, 40, 60, 60, 40, 40};
int[] ys = {20, 20, 80, 80, 20, 40, 40, 60, 60, 40};
Polygon p = new Polygon(xs, ys, xs.length);
Graphics2D g = image.createGraphics();
try (Closeable c = () -> g.dispose()) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    g.setColor(Color.BLACK);
    g.drawPolygon(p);
    g.setColor(Color.RED);
    for (int x = 0; x < width; ++x)
        for (int y = 0; y < height; ++y)
            if (contains(p, x, y))
                g.fillRect(x, y, 1, 1);
}
ImageIO.write(image, "png", new File("data/testPolygon.png"));

output output

测试多边形.png

If contains(p, x, y) -> p.contains(x, y) then如果contains(p, x, y) -> p.contains(x, y)那么

在此处输入图像描述

Shoot a ray from point P(x, y) and count for intersection with the edges, if intersection count is odd, then P is inside polygon.从点 P(x, y) 射出一条射线并计算与边缘的交点,如果交点数为奇数,则 P 在多边形内。

However if ray intersects with one of the vertices it might be difficult to determine intersection point due to rounding problem.但是,如果光线与其中一个顶点相交,则可能由于舍入问题而难以确定交点。 Therefore you may follow these steps:因此,您可以按照以下步骤操作:

  • Shoot a ray in any direction, such that the ray does not directly hit any vertices.向任何方向发射光线,使光线不会直接射到任何顶点。
  • For each edge in polygon determine whether the ray intersects the edge, if yes - increase counter对于多边形中的每条边,确定光线是否与边相交,如果是 - 增加计数器
  • After all edges have been checked, P inside if intersection counter is odd.检查完所有边后,如果相交计数器为奇数,则 P inside。

https://en.wikipedia.org/wiki/Point_in_polygon https://en.wikipedia.org/wiki/Point_in_polygon

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

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