Determining polygon intersection and containment

前端 未结 4 1785
借酒劲吻你
借酒劲吻你 2021-02-05 18:09

I have a set of simple (no holes, no self-intersections) polygons, and I need to check that they don\'t intersect each other (one can be entirely contained in another; that is o

4条回答
  •  半阙折子戏
    2021-02-05 18:48

    First, your algorithm for testing containment does not test for intersection correctly. Imagine two rectangles like this:

        +--+
     +--+--+--+
     |  |  |  |
     +--+--+--+
        +--+
    

    Vertices would be at (1, 2) (1,3) (4,2) (4,3) and (2,1) (3,1) (2,4) (3,4) -- no vertex lies inside any polygon, but the polygons do in fact intersect.

    To test for this kind of intersection, it is necessary to determine whether any of the polygons' edges intersect. For your purposes, if edges intersect but one polygon is not contained within the other, then you know they overlap in a non-permitted fashion.

    As for determining the containment tree, one way to do that is to sort the polygons from smallest to largest by area. Provided the polygons don't overlap-without-containment, then any polygon's parent in the tree will be the first containing polygon coming after it in the list.

    Edit: Oh, also, I advise writing a fast bounding-box or bounding-circle overlap routine, and using that to avoid having to do all the line intersection and vertex containment tests. If that still isn't fast enough, you probably want to build a quad or BSP tree; that will complicate things quite a bit but will also eliminate a lot of the intersection checks entirely.

提交回复
热议问题