繁体   English   中英

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

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

在我的 Java 应用程序中,我正在使用顶点数组创建二维多边形。 例如,我想使用这 4 个顶点创建一个简单的正方形

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

然后我想检查一个点是否在生成的多边形内。 但是,例如,如果我检查这一点

[-125, -73]

使用polygon.contains(x, z)它说不在多边形内。 即使我检查一个角落,例如[-125, -74]也会返回 false。 对我来说奇怪的是,我检查了这个点[-126, -74]是否返回 true,所以有些点实际上被视为在多边形内部,而另一些则不是,我不明白为什么会这样。 这是我为测试而设置的示例代码,没什么特别的

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));
}

还有 output

false
false
true

我还要指出这只是一个简单的例子,但多边形可能是一个非常复杂的形状,例如像这样疯狂的东西

文档多边形

这个多边形是用奇偶缠绕规则定义的。 有关奇偶缠绕规则的定义,请参见WIND_EVEN_ODD

WIND_EVEN_ODD
用于指定用于确定路径内部的奇偶规则的缠绕规则常量。 奇偶规则指定如果从该点到无穷远的任何方向上绘制的射线被路径段交叉奇数次,则该点位于路径内。

所以你可以这样做。

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);
}

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:

true
true
true

对带孔的多边形的测试。

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

测试多边形.png

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

在此处输入图像描述

从点 P(x, y) 射出一条射线并计算与边缘的交点,如果交点数为奇数,则 P 在多边形内。

但是,如果光线与其中一个顶点相交,则可能由于舍入问题而难以确定交点。 因此,您可以按照以下步骤操作:

  • 向任何方向发射光线,使光线不会直接射到任何顶点。
  • 对于多边形中的每条边,确定光线是否与边相交,如果是 - 增加计数器
  • 检查完所有边后,如果相交计数器为奇数,则 P inside。

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

暂无
暂无

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

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