Вопрос

I have two concave polygons on input represented as two vectors of points. I want to do some polygon operation on it - union, intersection and difference. I found intersection points between these polygons and insert them into the right place in each polygon. Then I give an information about its position (Inner - it is inside the other polygon, Outer - it is outside the other polygon, Intersection - point, where two edges of polygons intersects) to each vertex. Now I know which points create the union of these polygons (Outer and Intersection) etc., but I need to know how to sort them to the right order. In case of the intersection operation I need to divide these sorted points into the right number of sets, because the result of intersection could be more than one polygon.

I am using C++, but I don't need necessarily the code, I only want to need how to sort these final polygon points. And I don't want to use any library for these operations because I already have my own functions and want to use them.

I looked at this question How to intersect two polygons? and also some others but none of them is solving final sorting of points. I also read this article http://www.gvu.gatech.edu/~jarek/graphics/papers/04PolygonBooleansMargalit.pdf , but I probably don't get it.

Any help would be appreciated.

Это было полезно?

Решение

If you follow all my recommendations from my comments:

  • Distinguish between intersection and touching points
  • Keep a link between the two equivalents of the intersection points in the two polygons

The solution for the union will be:

  1. Consider only outer, touching and intersection points
  2. Make sure the points in the two polygons are ordered in different direction (one of the sets is in clockwise direction, the other one in counter-clockwise)
  3. Start from random point in any of the two polygons.
  4. For every vertex in any of the two polygons keep if you have visited it
  5. Every time you encounter an intersection point keep on traversing from the next to follow point in the other polygon after the equivalent of the intersection point.
  6. If you come back to the point you started from this closes one of the components of the join of the two polygons. If not all vertices were traversed repeat the whole of it from any unvisited vertex.
  7. In the end calculate the area of all the polygons you have found. The largest in area will be the real union. The rest will be holes in the union.

The solution for the join will be:

  1. Consider only inner and intersection points
  2. Make sure the points in the two polygons are ordered in the same direction
  3. Start from random point in any of the two polygons.
  4. For every vertex in any of the two polygons keep if you have visited it
  5. Every time you encounter an intersection point keep on traversing from the next to follow point in the other polygon after the equivalent of the intersection point.
  6. If you come back to the point you started from this closes one of the components of the join of the two polygons. If not all vertices were traversed repeat the whole of it from any unvisited vertex.

EDIT: As I already mentioned, I have the god feeling my approach with the polygon orientation needs to be revised. However, when searching through the web I found a description of algorithm that might do the work for you: The Vatti clipping algorithm

EDIT2 One more article describing such clipping algorithm.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top