Click here to get the C source.
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]. Methods of triangulation include greedy algorithms [O'Rourke 1994], convex hull differences [Tor and Middleditch 1984] and horizontal decompositions [Seidel 1991].
This Gem describes an implementation based on Seidel's algorithm (op. cit.) for triangulating simple polygons having no holes (The code has since then been extended to handle holes). It is an incremental randomized algorithm whose expected complexity is O(n log*n). In practice, it is almost linear time for a simple polygon having n vertices. The triangulation does not introduce any additional vertices and decomposes the polygon into n-2 triangles. Furthermore, the algorithm generates a query structure that can be used to determine the location of a point in logarithmic time. Related gems include incremental Delaunay triangulation of a set of points [Lischinski 1994] and polygonization of implicit surfaces [Bloomenthal 1994].
Generating monotone polygons from the trapezoid formation
The algorithm proceeds in three steps as shown in Figure 1.
All the data-structures used in the implementation are statically allocated. The trapezoid formation requires a structure where the neighbors of each trapezoid and its neighboring segments can be determined in constant time. Therefore, for every trapezoid, the indices of its neighbors and the segments are stored in its table-entry T.
The query-structure Q, used to determine the location of a point, is implemented as described by Seidel. The same Q can be later used for fast point-location queries. Both Q and T are updated as a new segment is added into the existing trapezoid formation. This entails splitting in two the trapezoid(s) in which the endpoints of the segment lie, then traversing along the edge of the segment to merge in any neighboring trapezoids which both share the same left and right edges and also share a horizontal edge. All the monotone polygons are stored in a single linked list with pointers to the first vertex in the list stored in a table.
Table 1 shows the average running time of the algorithm for randomly generated data sets of various sizes. All the measurements were taken on an HP Series 735 with execution times averaged over one hundred repetitions.
Empirical testing has proven the method robust across wide class of input data. The present implementation uses an epsilon tolerance when testing for floating-point equality. This computation occurs when determining whether a point lies to the left (right) of a segment or when detecting coincident points. This tolerance could potentially be removed by substituting a well-crafted point-in-polygon test [Haines 1994].
The triangulation code is invoked through the main interface routine,
int triangulate_polygon(n, vertices, triangles);with an n-sided polygon given for input (the vertices are specified in canonical anticlockwise order with no duplicate points). The output is an array of n-2 triangles (with vertices also in anticlockwise order). Once the triangulated, point-location queries can be invoked as
int is_point_inside_polygon(vertex);additional details appear in the C source code which accompanies this Gem.
This document was generated using the LaTeX2HTML translator Version 0.5.3 (Wed Jan 26 1994) Copyright © 1993, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
hits since November 1995.