Union of many (more than two) polygons without holes

送分小仙女□ 提交于 2019-12-10 14:56:21

问题


Im creating union of polygons without holes. Input polygons are without holes and also output one should be. I already have working algorithm for finding it for two polygons. But in case of more than two there is a problem. As an union shouldn't be disjoint polygon, when I try to compute sum of them one by one I have a problem in such case:

Then polygon 1 meets polygon 2 the union is disjoint (so my algorithm does not compute sum). In second loop ofc it makes union with 3rd and 4th polygon, but output is wihout 2nd polygon. So does any one know kind of fast and accurate algorithm of doing it? Probably a good idea would be to sort polygons by intersections first, but I cant think of any fast algorithms for that and also not quite Im not sure how they should be sorted.


回答1:


You shouldn't look at the polygons one by one.

You can apply any of the 2 algorithms from this question polygon union without holes directly with n polygons.

hope it helps




回答2:


You could do this iteratively and produce a set of connected components (instead of always just a single connected component):

  1. Put all polygons in an "open" list. Initialize a components list to empty.
  2. While open is not empty:
    • Remove a polygon p from open and set flag changed to true.
    • Repeat while changed is true:
      • set changed to false
      • for each polygon q in open:
        • if q intersects p, remove q from open, set changed to true, and set p to the union of p and q.
    • add p to components.

At the end, components will consist of all the non-intersecting, connected components that can be formed by union of the input polygons. In your posted sample, it should produce a single polygon.

This is not the most efficient approach (see the algorithms in the link posted by Ricky Bobby), but it has the advantage of simplicity. Provided you aren't dealing with hundreds of polygons, it should perform just fine.

P.S. As @japreiss points out, a union can have holes even if none of the input polygons has a hole, even if the inputs are all convex polygons. If the inputs can be concave, then even a union of 2 polygons can have a hole. Does your 2-polygon algorithm handle that already?




回答3:


I would use a sweeping line algorithm to find all edge ntersections, break contours at intersection points, then starting at (say) lowest-x-coordinate vertex walk along the edges, always selecting the outermost one. With a bit more work it is possible to get rid of the no-holes condition (holes are just differently oriented contours).



来源:https://stackoverflow.com/questions/7150766/union-of-many-more-than-two-polygons-without-holes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!