1

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.

Community
  • 1
  • 1
TinF
  • 89
  • 7
  • 1
    How do you define intersection point (you say you already defined them)? Would a touching point be intersection point? – Boris Strandjev Jan 03 '13 at 10:05
  • I define touching point as intersection point but I could easily define as touching if needed. I have a position attribute saved with every point. – TinF Jan 03 '13 at 10:09
  • One more thing: can you easily find the corresponding touching/ intersection point equivalent in the other polygon if you have it in one of them? – Boris Strandjev Jan 03 '13 at 10:11
  • I could find them only by testing if they have same coordinates. – TinF Jan 03 '13 at 10:14
  • Shouldn't be like that. After all you are the one to add those points in the two polygons. Keep in some data structure the correspondence of those points. Can you? – Boris Strandjev Jan 03 '13 at 10:16
  • I can save some serial number to the intersection points that will be same for intersection point in both polygons. – TinF Jan 03 '13 at 10:20

1 Answers1

1

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.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • Thanks for your advice but I think this won't work if there is a hole in union polygon - then it could find only the hole if I started with some of its points. – TinF Jan 03 '13 at 10:30
  • @user1945028: Fair point. Furthermore, I am not 100% convinced in my approach with the polygon orientation. However, what is the expected output in the case of union with hole? – Boris Strandjev Jan 03 '13 at 10:33
  • It could be more polygons - first one representing the union and the others representing holes. – TinF Jan 03 '13 at 10:36
  • @user1945028 Using visited array in the union case will enable you to find all polygons resulting from the union - both the union and the holes. The real union will be the polygon with the largest area. The others will be holes. – Boris Strandjev Jan 03 '13 at 10:41
  • Thanks. I think the second link describes exactly my situation, I will try to implement it. – TinF Jan 05 '13 at 18:18