Trying to triangulate a set of simple 2d polygons, I've come up with this algorithm:
I've tested it, and found it to work even on really big and complicated simple 2d polygon (it don't work for polygon with a hole or self intersecting polygons).
Is there corner cases that will produce degenerate result?
Does this algorithm is a known one?
If not, I would like to be sure this algorithm is rock solid but I have not the mathematical background to prove it.
Thanks a lot.
Computing the triangulation of a polygon is a fundamental algorithm in computational geometry. In computer graphics, polygon triangulation algorithms are widely used for tessellating curved geometries, as are described by splines [Kumar and Manocha 1994].
Let a polygon P with h holes have n vertices total, counting vertices on the holes as well as on the outer boundary. Then a triangulation of P has t = n + 2h - 2 triangles, and a quadrilateralization has q = n/2 + h — 1 quadrilaterals.
Theorem: Every polygon has a triangulation. Proof by Induction. Base case n = 3. Pick a convex corner p.
A triangulation of a polygon can be thought of as a set of chords that divide the polygon into triangles such that no two chords intersect (except possibly at a vertex). This is a triangulation of the same polygon: An optimal triangulation is one that minimizes some cost function of the triangles.
You are doing a version of "ear clipping" approach to triangulation, see: http://en.wikipedia.org/wiki/Polygon_triangulation
Your algorithm fails if another polygon vertex (say from the other side of the polygon) ends up inside the triangle 'ear' you form. Consider this example:
Your algorithm will choose A first, and make a triangle with the two adjacent edges (connected with the dashed line). But this triangle includes another vertex (B) and is clearly incorrect.
The generalized ear clipping approach depends on finding ears you can clip off that do not have any included vertices.
This is when you want to handle simple polygons like rectangles, pentagons, hexagons and so on. Here you just take a starting point and connect it to all other vertices. This algorithm is trivial and what I really wanted was a more general solution that could handle concave and polygons with holes.
In order to deal with more complex polygons like the one Payne provided...
While there are algorithms that run faster, the faster algorithms become very complicated. Kirkpatrick et al. found an algorithm to run in O(n log log n) and Chazelle did it in O(n). However, the easiest thing to implement is probably the Seidel algorithm that runs in O(n log n).
The algorithm is a 3-step process
If you're interested in C source it can be obtained from University of North Carolina at Chapel Hill. In general the code quality is good, handles holes, but will probably need to be massaged to your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With