2

作为我正在做的一个小项目的一部分,我正在使用 Polygon 类,但我在处理“接触”而不是正确相交的多边形时遇到了困难。

例如,在我有两个多边形的情况下:

Polygon a = new Polygon(new int[] {0,0,3,3}, new int[] {0,1,0,1}, 4);
Polygon b = new Polygon(new int[] {1,1,2,2}, new int[] {1,2,1,2}, 4);

我正在使用 contains 方法检查每个点与另一个多边形,但是代码:

System.out.print(a.contains(1,1));
System.out.print(a.contains(2,1));

返回 false 两次。

有没有办法检测这些“刚刚接触”的多边形?

4

2 回答 2

2

如果您可以接受误差范围,请尝试polygon.intersects()

http://docs.oracle.com/javase/6/docs/api/java/awt/Polygon.html#intersects%28double,%20double,%20double,%20double%29

它需要一个矩形作为参数,但如果你把矩形做得非常小,你可以获得相同的结果。除此之外,Polygon 类似乎并不完全符合您的要求。另一方面,我能想到的应用程序很少有指定的误差范围不会更好......

于 2013-06-03T18:04:35.220 回答
0

我找到了一个解决方案来检查 2 个多边形是否相交,即使它们没有公共区域。我为此使用了 math.geom2D 库(http://sourceforge.net/apps/mediawiki/geom-java/index.php?title=Main_Page)。我个人喜欢它,因为 Polygons2D 类允许您相对轻松地处理剪辑、交集和联合等操作。

该方法使用 2 个 SimplePolygon2D 对象作为输入,可以使用 addVertex(Point2D point) 从您的数据构造。

在某些情况下,这种方法可能不起作用,但我会在找到解决方法后立即发布更多信息。

public static boolean checkShapeAdjacency(SimplePolygon2D polygon1, SimplePolygon2D polygon2) {
    // Buffer distance. Depends on scale / data
    final float bufferDistance = 0.2f;

    if (polygon1 == polygon2) {
        return false;
    }

    List<Point2D> intersectingPoints = new ArrayList<Point2D>();

    if (polygon1.area() > 0 && polygon2.area() > 0) {

        try {
            // Make a buffer of one polygon
            CirculinearDomain2D bufferDomain = polygon1
                    .buffer(bufferDistance);
            /*               
             * Iterate through the points of the other polygon and see if they
             * are contained within the buffer
             */
            for (Point2D p : polygon2.vertices()) {
                if (bufferDomain.contains(p)) {
                    // Increase the intersecting point count
                    if (!intersectingPoints.contains(p)) {
                        intersectingPoints.add(p);
                    }
                }
            }
        } catch (Exception e) {
            // Try/Catch to avoid degenerated line exceptions (common with math.geom2d)s
            e.printStackTrace();
            return false;
        }
    }

    // Check against the number of intersecting points
    if (intersectingPoints.size() >= 2) {

        /*
         * It is safe enough to assume that with 2 intersecting points,
         * the shape are adjacent. It will not work in the case of bad
         * geometry though. There are other methods of cleaning bad
         * geometry up.
         */
        return true;
    } else if (intersectingPoints.size() == 1) {
        /*
         * It gets tricky in the case of 1 intersecting point as one line may
         * be longer than the other. Check to see if one edge is entirely
         * in the other polygon.
         */
        for (LineSegment2D edge1 : polygon1.edges()) {
            if (polygon2.distance(edge1.firstPoint()) < 0.001 
                    && polygon2.distance(edge1.lastPoint()) < 0.001
                    && edge1.length() > 1) {

                return true;
            }
        }
        for (LineSegment2D edge2 : polygon2.edges()) {
            if (polygon1.distance(edge2.firstPoint()) < 0.001 
                    && polygon1.distance(edge2.lastPoint()) < 0.001
                    && edge2.length() > 1) {

                return true;
            }
        }
        // One common point and no common edge returns false
        return false;
    } else {
        return false;
    }
}

如果您遇到任何问题,请告诉我,我会尽我所能解决它们。

谢谢!

于 2014-03-26T11:55:48.690 回答