You need to determine which points lie inside. After removing these points, you can insert one set of "outside" points into the other. Your insertion points (e.g. where you have the arrow in the picture on the right) are where you had to remove points from the input sets.
Good question! I've never attempted this before, but I'll take a crack at it now.
First: You need to know where these two shapes overlap. To do this, you could look at every edge in Polygon A and see where it intersects and edge in Polygon B. In this example, there should be two points of intersection.
Then: Make the union shape. You can take all of the vertices in A and B, and also the points of intersection, and then exclude the vertices that contained by the final shape. To find these points, it looks like you could just find any vertex of A that is inside B, and any vertext of B that is inside A.
When grouping countries, I'd hope there be no overlap -- you could take a fairly naive algorithm that looks for shared vertices - a simple view would be to iterate through the points on one polygon, see if it's on any of your other polygons, and shares the same next or previous point to see if there is a match. Then just remove the shared vertex to create your union
A solution I've seen using BSP trees is described here.
Basically, it describes intersection in terms of a union of the edges of polygon A that are inside polygon B (including partial edges, and calculated using a BSP tree). Then, you can define A / B as ~(~A /\ ~B), where ~ denotes reversing the winding of the polygon, / denotes union and /\ denotes intersection.
This is a very good question. I implemented the same algorithm on c# some time ago. The Algorithm constructs a common contour of two polygons (i.e. Constructs a union without holes). Here it is.
Step 1. Create graph that describes the polygons.
Input: first polygon (n points), second polygon (m points). Output: graph. Vertex - polygon point of intersection point.
We should find intersections. Iterate through all polygon sides in both polygons [O(n*m)] and find any intersections.
If an intersection is not found, simply add vertices and connect them
to the edge.
If any intersections are found, sort them by length to their start point, add all
vertexes (start, end and intersections) and connect them (already in
sorted order) to the edge.
Step 2. Check constructed graph
If we did not find any intersection points when graph was built, 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 left-bottom vertex.
Find the minimum x and y coordinates (minx, miny). Then find the minimum distance between (minx, miny) and the polygon's points. This point will be the left-bottom point.
Step 4. Construct common contour.
We start to traverse the graph from the left-bottom point and continue until we get back into it. At the beginning we mark all edges as unvisited. On every iteration you should select the next point and mark it as visited.
To choose the next point, choose an edge with a maximum internal angle in counter-clockwise direction.
I calculate two vectors: vector1 for current edge and vector2 for each next unvisited edge (as presented in the picture).
For vectors I calculate:
Scalar product (dot product). It returns a value related to an angle between vectors.
Vector product (cross product). It returns a new vector. If z-coordinate of this
vector is positive, scalar product gives me right angle in
counter-clockwise direction. Else (z-coordinate is negative), I
calculate get angle between vectors as 360 - angle from scalar
product.
As a result I get an edge (and a correspond next vertex) with the maximum angle.
I add to result list each passed vertex. Result list is the union polygon.
Remarks
This algorithm allows us to merge multiple of polygons - to
apply iteratively with polygon's pairs.
If you have a path that consists of many bezier curves and lines, you should flatten this path first.
This is a challenging but well-understood topic, that often goes
under the name "regularized Boolean operations on polygons."
You might look at
this MathOverflow answer,
which includes the figure below
(from Alan Murta's clipping library),
with the pink union the OP's combine: