2013-06-03 3 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)를 살펴보십시오. –

답변

2

, 그것은 인수로 사각형이 필요하지만 당신은 사각형이 정말 작게 경우, 동일한 결과를 얻을 수 있습니다 polygon.intersects()

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

보십시오. 그것 이외에, Polygon 클래스가 당신이 원하는 것을 정확하게 가지고있는 것처럼 보이지 않습니다. 다른 한편으로, 나는 특정 오류 마진이 더 좋지 않을 것 같은 아주 적은 응용 프로그램을 생각할 수 있습니다 ...

0

공용 영역이 없어도 2 개의 다각형이 교차하는지 확인하기위한 솔루션을 발견했습니다 . 나는 이것을 위해 math.geom2D 라이브러리 (http://sourceforge.net/apps/mediawiki/geom-java/index.php?title=Main_Page)를 사용하고있다. 개인적으로 Polygons2D 클래스를 사용하면 클립, 교차 및 결합과 같은 작업을 상대적으로 쉽게 처리 할 수 ​​있습니다.

이 메소드는 addVertex (Point2D point)를 사용하여 데이터로 생성 할 수있는 2 개의 SimplePolygon2D 객체를 입력으로 사용합니다.

이 방법이 효과가 없을 수있는 경우가 있지만 문제를 해결하는 방법을 찾으면 바로 게시 해 드리겠습니다.

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

이 어떤 문제가 발생하는 경우 알려 주시면 최선을 내가 할 수있는 한이를 해결하겠습니다.

감사합니다.