Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

polygon union without holes

Im looking for some fairly easy (I know polygon union is NOT an easy operation but maybe someone could point me in the right direction with a relativly easy one) algorithm on merging two intersecting polygons. Polygons could be concave without holes and also output polygon should not have holes in it. Polygons are represented in counter-clockwise manner. What I mean is presented on a picture. As you can see even if there is a hole in union of polygons I dont need it in the output. Input polygons are for sure without holes. I think without holes it should be easier to do but still I dont have an idea. Polygons - blue and red are input, green is output

like image 226
Pax0r Avatar asked Jul 27 '11 12:07

Pax0r


2 Answers

  1. Remove all the vertices of the polygons which lie inside the other polygon: http://paulbourke.net/geometry/insidepoly/
  2. Pick a starting point that is guaranteed to be in the union polygon (one of the extremes would work)
  3. Trace through the polygon's edges in counter-clockwise fashion. These are points in your union. Trace until you hit an intersection (note that an edge may intersect with more than one edge of the other polygon).
  4. Find the first intersection (if there are more than one). This is a point in your Union.
  5. Go back to step 3 with the other polygon. The next point should be the point that makes the greatest angle with the previous edge.
like image 105
tskuzzy Avatar answered Nov 15 '22 09:11

tskuzzy


You can proceed as below:

First, add to your set of points all the points of intersection of your polygons.

Then I would proceed like graham scan algorithm but with one more constraint.

Instead of selecting the point that makes the highest angle with the previous line (have a look at graham scan to see what I mean (*), chose the one with the highest angle that was part of one of the previous polygon.

You will get an envellope (not convex) that will describe your shape.

Note:

It's similar to finding the convex hull of your points.

For example graham scan algorithm will help you find the convex hull of the set of points in O (N*ln (N) where N is the number of points.

Look up for convex hull algorithms, and you can find some ideas.

Remarques:

(*)From wikipedia:

The first step in this algorithm is to find the point with the lowest y-coordinate. If the lowest y-coordinate exists in more than one point in the set, the point with the lowest x-coordinate out of the candidates should be chosen. Call this point P. This step takes O(n), where n is the number of points in question.

Next, the set of points must be sorted in increasing order of the angle they and the point P make with the x-axis. Any general-purpose sorting algorithm is appropriate for this, for example heapsort (which is O(n log n)). In order to speed up the calculations, it is not necessary to calculate the actual angle these points make with the x-axis; instead, it suffices to calculate the cosine of this angle: it is a monotonically decreasing function in the domain in question (which is 0 to 180 degrees, due to the first step) and may be calculated with simple arithmetic.

In the convex hull algorithm you chose the point of the angle that makes the largest angle with the previous side.

To "stick" with your previous polygon, just add the constraint that you must select a side that previously existed.

And you take off the constraint of having angle less than 180°

like image 35
Ricky Bobby Avatar answered Nov 15 '22 09:11

Ricky Bobby