Combine Arbitrary number of polygons together

Posted by Jakobud on Stack Overflow See other posts from Stack Overflow or by Jakobud
Published on 2012-12-01T23:02:11Z Indexed on 2012/12/01 23:03 UTC
Read the original article Hit count: 145

Filed under:
|
|
|

I have an arbitrary number of polygons (hexes in this case) that are arranged randomly, but they are all touching another hex.

enter image description here

Each individual hex has 6 x,y vertices. The vertex's are known for all the hexes.

Can anyone point me in the direction of an algorithm that will combine all the hexes into a single polygon? Essentially I'm just looking for a function that spits out an array of vertex locations that are ordered in a way that when drawing lines from one to the next, it forms the polygon.

This is my method so far:

  1. Create array of all the vertices for all the hexes.
  2. Determine the number of times a vertex occurs in the array
  3. If vertex is in the array 3+ times, delete the vertices from the array.
  4. If vertex is in the array 2 times, delete one of them.

The next step is tricky though. I'm using canvas to draw out these polygons, which essentially involves drawing a line from one vertex to the next. So the order of the vertices in the final array is important. It can't be sorted arbitrarily.

Also, I'm not looking for a "convex hull" algorithm, as that would not draw the polygon correctly.

Are there any functions out there that do something like this? Am I on the right track or is there a better more efficient way?

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about hex