Search code examples
javagraphicslibgdxtriangulationpolygons

Best way to triangulating complex irregular concave and convex polygons


I need to triangulate a polygon that will have lots vertices close together. The polygons are going to represent the shape of countries. I am using libgdx and want to utilise its PolygonRegion class:

PolygonRegion(TextureRegion region, float[] vertices, short[] triangles)

Creates a PolygonRegion by triangulating the polygon coordinates in vertices and calculates uvs based on that.

In libgdx there is EarClippingTriangulator for triangulating polygons. Its docs say:

If the input polygon is not simple (self-intersects), there will be output but it is of unspecified quality (garbage in, garbage out).

Other triangulator in libgdx don't say much about this and I cannot find anything on google to help.

I want to know if there is going to be any problems triangulating complex polygons with vertices that can be next to each other, or in close proximity. My polygons may contain a few hundred vertices (well I think it could be more).

[edit] I am unsure if there is even a point in triangulating the polygon, because of how many triangles it will create.


Solution

  • You don't have to program this yourself. Most high-level programming languages have a 2D graphics library, and offer a Polygon Shape programmable object. It would define a closed (simple) polygon as a list of vertices [x, y] visited in CCW order. This Polygon object comes with a "fill" function, which is designed for speed filling complex shapes like yours. Triangulation would be a relatively slow approach...a faster algorithm converts the shape into a set of horizontal scan lines (taking advantage of the fact that the graphics system at the lowest level has to fill in pixels). As far as islands, each would have to have its own polygon object. If there were cutouts in a region (swiss cheese), each would have its own polygon. Each polygon requires closure.