2013-06-03 149 views
1

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

我所用的含有方法检查对其它多边形的每个点,但代码:

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

返回false两次。

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

+0

看看[基于形状的碰撞检测](http://stackoverflow.com/a/14575043/418556)。 –

回答

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

请让我知道如果您遇到任何问题,我会尽我所能解决他们。

谢谢!