检测触摸多边形

时间:2013-06-03 18:00:18

标签: java awt polygon contains java-2d

作为我正在研究的一个小项目的一部分,我正在使用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。

有没有办法检测这些“只是触摸”的多边形?

2 个答案:

答案 0 :(得分:2)

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

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

它需要一个矩形作为参数,但是如果你使矩形非常小,你可以获得相同的结果。除此之外,Polygon类似乎没有你想要的。另一方面,我可以想到很少有具有指定误差范围的应用程序会更好......

答案 1 :(得分:0)

我找到了一个解决方案,用于检查2个多边形是否相交,即使它们没有公共区域。我正在使用math.geom2D库(http://sourceforge.net/apps/mediawiki/geom-java/index.php?title=Main_Page)。我个人喜欢它,因为Polygons2D类允许你相对容易地处理像clip,intersection和union这样的操作。

该方法使用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;
    }
}

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

谢谢!