the union computation, as cited by @martinfrank, would work, but is quite complex and more than is needed in this case. clearly if you can merge two polygons p1 and p2 that share some positive length of boundary, you can merge any number. from the image provided, it appears that the polygons may exactly (as opposed to approximately) share boundary portions. if this is the case, then the following would work.

walk (loop) around the boundary of p1, checking if the current vertex is also a vertex of p2. once you have identified a common shared vertex v, then it is easy to walk forward & backward around the two polygons until you identify starting a and stopping b vertices that delimit the extent of the shared boundary. then delete that ab portion, and stitch the two remaining boundary sections together to form p1 union p2.

Related Query

More Query from same tag