This is a very good question. I implemented the same algorithm in C # some time ago. The algorithm constructs the general contour of two polygons (i.e. creates a union without holes). Here it is.
Step 1. Create a graph that describes the polygons.
Input: first polygon (n points), second polygon (m points). Exit: schedule. The vertex is the polygon point of the intersection point.
We have to find the intersections. We pass through all sides of the polygon in both polygons [O (n * m)] and find any intersections.
If no intersection is found, just add vertices and connect them to the edge.
If any intersections are found, sort them by length to their starting point, add all the vertices (start, end and intersections) and connect them (already in sorted order) to the edge.
Step 2. Check the constructed graph
If we did not find intersection points when plotting, we have one of the following conditions:
- Polygon1 contains polygon2 - return polygon1
- Polygon2 contains polygon1 - return polygon2
- Polygon1 and polygon2 do not intersect. Return polygon1 and polygon2.
Step 3. Find the bottom left vertex.
Find the minimum x and y coordinates (minx, miny). Then find the minimum distance between (minx, miny) and the points of the polygon. This item will be bottom left.
Step 4. Build a common path.
We begin to move along the chart from the left point and continue until we return to it. At the beginning, we mark all edges as invisible. At each iteration, you must select the next point and mark it as having visited.
To select the next point, select the edge with the maximum inside angle in the counterclockwise direction.
I compute two vectors: vector1 for the current edge and vector2 for each next invisible edge (as shown in the picture).
For vectors I calculate:
- Scalar product (point product). It returns the value associated with the angle between the vectors.
- Vector product (cross product). It returns a new vector. If the z-coordinate of this vector is positive, the scalar product gives a right angle in counterclockwise direction. Else (the z-coordinate is negative), I calculate the angle between the vectors as 360 - the angle from the scalar product.
As a result, I get an edge (and the corresponding next vertex) with the maximum angle.
I add each vertex passed to the list of results. The list of results is a union polygon.
Notes
- This algorithm allows us to combine several polygons - to apply iteratively with polygonal pairs.
- If you have a path that consists of many curves and bezier lines, you must first smooth this path.
xtmq Oct 20 '13 at 8:37 2013-10-20 08:37
source share