Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with Lowest Memory Consumption

Polygons can conveniently represent real world objects. In automatic character recognition, shapes of individual letters are represented by polygons. In robotics, obstacles are represented using polygons. In computer graphics programming, solid objects are represented using polygons on the two dimensional screen. The polygons can be easily manipulated using known mathematical operations. That is the reason for representing real world objects using polygons. However, polygons can be in complicated shapes. Therefore, it is better if there is a way to partition a polygon into smaller pieces. Triangulation is a particular way of doing this from which polygons are partitioned into triangles. The basic triangulation algorithm is widely used in applications where 100% accuracy is necessary. Algorithms with better asymptotic order than the basic triangulation algorithm exist. However they are not 100% accurate and use advanced data structures causing higher memory consumption. This paper proposes a simple, efficient and 100% accurate algorithm which uses lowest amount of memory. The proposed algorithm is more suitable for embedded systems which do not possess large amount of memory. The proposed algorithm was experimentally compared with the basic triangulation algorithm. The experimental results prove that the proposed algorithm is faster than the basic triangulation algorithm.


INTRODUCTION
Polygons can be used to represent the real world objects conveniently.Once the real world objects were represented using polygons, the resultant polygons are too complicated most of the time.In order to manipulate the polygons faster and easier in applications, the polygons need to be partitioned into simpler components such as triangles (Chazelle, 1982;Chazelle, 1991), trapezoids (Seidel, 1991) or even sub-polygons (Feng et al., 1975).
Polygon triangulation can be defined as the decomposition of a polygonal area into a set of triangles (Berg et al., 2008;Garey et al., 1975).In other words, polygon triangulation can be defined as the creation of a set of triangles without non-intersecting interiors whose union is the original polygon.
According to the Meister's theorem (Meister, 1975), any simple polygon with at least four vertices without holes has at least two 'ears', which are triangles with two sides being the edges of the polygon and the third one completely inside it.Meister proposed a recursive algorithm which search for an ear and cut it off from current polygon.A new polygon is formed after the removal of an ear.That polygon also still meets the 'two ears' condition and repetitions can be done until there is only one triangle left.polygon and runs in O (n(r+1)) time, where r is the number of reflex vertices of the polygon.
A triangle with a sharp angle is called a silver triangle.The silver triangles are not suitable for applications due to their poor shape.Sloan (cis.uab.edu, 2016) developed an ear clipping algorithm with O (n 2 ) time complexity based on O'Rourke's algorithm.An optimization process by swapping diagonals is done to remove silver triangles after performing the triangulation.Held (Held, 2001) developed a package for called FIST for polygon triangulation.It was based on ear clipping and can be applied to handle complex polygons.
Gang et al (Mei et al., 2012) proposed a new algorithm based on ear clipping to generate high quality triangulations with fewer silver triangles.When locating an ear tip, the one with smallest interior angle is always selected and removed.If the ear tip with the smallest angle is not chosen at first, it will be divided into at least two smaller ones.Further silver triangles are reduced by edge swapping.But edge swapping is adopted during cutting ears rather than after performing the entire triangulation as in Sloan's algorithm (cis.uab.edu, 2016).
David (Eberly, 2016) proposed an algorithm of O (n 2 ) time complexity.It uses tree data structure for representation.According to David, even though the algorithms exist with better asymptotic order than his algorithm, they are more difficult to be implemented.An algorithm with O (n log n) time complexity uses horizontal decomposition into trapezoids followed by identification of monotone polygons that are themselves triangulated (Chazelle et al., 1984;Fournier et al., 1984).By improving further an incremental randomized algorithm produces an O (n log* n) where log* n is the iterated logarithm function (Seidel, 1991).This function is effectively a constant for very large values of n that can be observed in practice.Therefore the randomized method can be considered as having O (n) time complexity for practical purposes.An algorithm with O (n) time complexity exists (Chazelle, 1991).However it is extremely complex and no implementation has been proposed yet.
It has been mathematically proved that every polygon can be triangulated (O 'Rourke, 1998).Basically triangulation algorithms can be divided into two categories: diagonal insertion and ear clipping.The output of diagonal insertion methods is a list of diagonals which separate the polygon into a set of triangles.The output of the ear clipping methods is a list of triangles in which the union forms the original polygon.Therefore ear subtraction methods are more useful when the application needs to find the set of triangles along with the corresponding coordinates of the vertices of those triangles.The proposed method is inspired by ear subtraction method.
A polygon can be defined as the region of a plane bounded by a finite collection of line segments forming a simple closed curve.Let v 0 , v 1 , v 2 ..., v n-1 be n points in the plane.Here and throughout the paper, all index arithmetic is mod n, conveying a cyclic ordering of the points, with v 0 following v n-1 , since (n -1) + 1 ≡ n ≡ 0 (mod n).Let e 0 = v 0 v 1 , e 1 = v 1 v 2 …, e i = v i v i+1 …, e n-1 = v n-1 v 0 be n segments connecting the points.Then a polygon is bounded by these segments if and only if 1.The intersection of each pair of segments adjacent in the cyclic ordering is the single point shared between them: e i ∩ e i+1 = v i+1 , for all i = 0…, n -1. 2. Non adjacent segments do not intersect: e i ∩ e j = Ø, for all j ≠ i + 1. 3. None of three consecutive vertices are collinear.
These line segments define a curve due to the fact that they are connected end to end.The curve is said to be closed since they form a cycle.Also this closed curve is simple since non adjacent segments do not intersect.The points v i are called vertices of the polygon while the segments e i are called its edges.According to the definition a polygon is a closed region of a plane.If P denotes a polygon then ∂P is used to denote the boundary of the polygon.A polygon divides the plane into two mutually exclusive regions as interior and exterior.The interior region is bounded while the exterior region is unbounded.
A line segment along with its end points is called a closed line segment.Any two vertices of a polygon P can be joined using a line segment.Let A and B are the end points of such kind of a line segment.If the intersection of the closed segment AB with ∂P is exactly the set {A, B} and the closed segment AB is nowhere exterior to the polygon then the line segment is called a diagonal of the polygon.Suppose P, Q and R be the three consecutive vertices of a polygon.If PR is a diagonal then PQR is called an ear of the polygon (O 'Rourke, 1998).
The basic triangulation algorithm (O' Rourke, 1998) is a 100% accurate algorithm for polygon triangulation available in literature.It is a diagonal insertion method and based on the following theorem called triangulation theorem.
Triangulation Theorem: Every polygon P of n vertices may be partitioned into triangles by the addition of (zero or more) diagonals.
The method suggested by this theorem is an O (n 4 ) algorithm.There are n C 2 = O (n 2 ) diagonal candidates.The cost of testing each for diagonal hood costs O (n).Since this O (n 3 ) should be repeated for each of the (n -3) diagonals yields O (n 4 ).
The proposed algorithm stores the coordinates of the vertices of the polygon in two arrays.It detects the ears of the polygon and prunes them one by one.Thus the polygon is triangulated.The output of the proposed algorithm is a list of disjoint triangles in which the union forms the original polygon.This algorithm is 100% accurate algorithm and it avoids precision error.It utilizes static memory only.The proposed algorithm is faster than basic triangulation algorithm.

METHODOLOGY
In this section the proposed approach is discussed in detail.The implementation of the algorithm is given in the appendix in order to make it easy to understand the methodology.

Representation of the polygon
The polygon is stored in arrays.The points variable stores the number of vertices in the polygon.To store vertices of the polygon, two arrays are used.Then (x[i], y[i]) denotes the i th vertex of the polygon where i = 0, 1…, (points -1).

Some issues in implementation
The coordinates of the vertices may be floating point numbers.Since the line function takes integers as its inputs, those coordinates should be approximated into nearest integer.That is done by dpx and dpy functions.Adding 0.5 to a floating point number and truncating the decimal part will approximate it to the nearest integer.The origin of the coordinate system in the used software is situated in the top left corner of the screen.But in conventional coordinate systems origin should be in bottom left corner of the screen.Therefore, inversion of the y-coordinate has been done inside dpy function where the inbuilt getmaxy function returns the maximum ycoordinate of the display screen.The drawPolygon function has been used to draw the polygon on the screen.Modulo operator (%) has been used to draw the polygon handling the cyclic nature of the polygon.

Mathematical properties
The proposed algorithm uses some of the known theorems in the literature.They have been already proved (O' Rourke, 1998) and here only the results are shown.

Identifying the convex vertices of the polygon
The first phase of the proposed algorithm is to identify the convex vertices of the polygon.None of the three consecutive vertices of the input polygon should be collinear by the definition of a polygon.Therefore, all the convex vertices of the input polygon are strictly convex.That means the interior angle formed by each vertex is less than π.It can be easily deduced that the lowest vertex of such a polygon is always strictly convex as shown in Figure 1.
A horizontal line has been drawn in Figure 1 through the lowest vertex of the polygon.Then the entire polygon is upward with respect to that horizontal line.Since the angle formed by a straight line is π, it is obvious that the lowest vertex should always be strictly convex.When there are multiple lowest vertices, each of them is also strictly convex applying the same reasoning.First the algorithm should find one of those lowest vertices.This task is done by the lv function.And that function returns the index of one of those lowest vertices.
Beginning from 0 th vertex, the vertices of the polygon can be traversed.The traversal will be clockwise or anticlockwise depending on how the vertices are located in two dimensions.Suppose vertices are in anticlockwise order as shown in Figure 2. Suppose this is the original polygon provided to the algorithm as the input.The lv function will return the index of the lowest vertex as 0.
If the index of a vertex is v then three consecutive vertices (v -1, v, v + 1) can be considered.When v = 0, v -1 = -1, it should be corrected as v -1 = (points -1).When v = (points -1), v + 1 = points, it should be corrected as v + 1 = 0.The ts function decides the sign of t value of three consecutive vertices when middle vertex is provided.The isConvex function has been designed to return 1, if the passed index to this function corresponds to a convex vertex.If the multiplication of ts(v) and ts(lv()) is positive, then it can be decided that the vertex v and lowest vertex has same sign for their t expression.It has been mentioned earlier that lowest vertex should always be convex.Therefore, the vertex v should also be convex since they have the same sign.In this way, convex vertices of the polygon can be identified.

Identifying an ear of the polygon
The convex vertices of the polygon shown in Figure 2 are 0, 1, 3, 5, 6, 8, and 9.An ear should always be with a convex vertex.Then the triangles formed by those convex vertices with their two neighboring vertices are considered.If such a triangle does not contain any of the vertices of the polygons inside it, then that triangle should be an ear.In this way, ears can be found.To check whether a triangle is empty, isEmpty function is used.
To solve the problem inside isEmpty function, it is necessary to decide whether a given point is inside a triangle or not.Consider the triangle shown in Figure 3 where vertices are v, a, and b.This corresponds to the triangle inside isEmpty function.
The purpose of tv function is to derive only the sign of the expression t.The value of the expression t is a large floating point number.Since it is necessary to calculate multiplications of several t values, to reduce the computational cost their signs are derived.Here tsv = tv (v,a,b).According to the triangle in Figure 3, tsv = -1  (v, a, i) have the same sign then the point i is in the same side of the triangle with respect to the edge av.In this way, it can find the position of the point i with respect to other two edges of the triangle.Also if the point i is in the same side of the triangle with respect to each three edges then the point is inside the triangle.If the point i is on and edges of the triangle then it is considered that the point is inside the triangle.Therefore, when checking each vertex of the polygon whether they are inside the triangle or not, own three vertices of the triangle should be excluded.This has been successfully done inside isEmpty function using continue keyword.Now the algorithm seeks for ears in the available convex vertices (Theorem 1).In Figure 2, 0 th vertex is not an ear.Then algorithm moves to the next convex vertex. 1 st vertex is an ear.

Pruning an ear from the polygon
Once an ear was found then the algorithm prunes that ear from the original polygon.This is done by prune function.Figure 4 shows the polygon after pruning that ear from the original polygon.

Iterative process
The modified polygon shown in Figure 4 can again be considered as the original polygon for the algorithm.The previous process is applied iteratively until (n -3) diagonals can be found (Theorem 2). Figure 3 to Figure 10 shows the evolution of the polygon step by step.In this way given polygon can be triangulated.The triangulated polygon is shown in Figure 11.Also in this algorithm has been designed without division operator in order to avoid precision error.Therefore, the algorithm can accurately perform even if the coordinates are floating point numbers.

RESULS AND DISCUSSION
The proposed algorithm is a 100% accurate algorithm.That means it outputs the 100% accurate coordinate values of the vertices of the triangles.The operations like division, square root calculation can lead to precision error.The proposed algorithm does not involve such operations.Therefore accuracy of the computations is 100%.The basic triangulation algorithm is also a 100% accurate triangulation algorithm (O 'Rourke, 1998).Later more efficient algorithms were proposed.However they fail to maintain 100% accuracy and only provide approximate values of the coordinates of the triangles.The algorithm proposed by Gang et al (Mei et al., 2012) computes the interior angles of each vertex of the original polygon.The computation of an angle involves sine and cosine values which leads to the precision error.The algorithm proposed by David (Eberly, 2016) also has the same drawback since it involves angle computation.
The proposed algorithm is the simplest algorithm available and it does not use any advanced mathematical concepts or advanced data structures.It provides a successful way to manipulate array data structure to achieve the goal.Even though many algorithms were proposed after the basic triangulation algorithm, the authors fail to provide successful implementations for them (Eberly, 2016).For an example, Chazelle et al (Chazelle et al., 1982) proposed a very complicated algorithm with O (n) time complexity.That algorithm is just a theoretical methodology for triangulation and a suitable data structure to handle the algorithm has not yet been invented (Eberly et al., 2016).David (Eberly et al., 2016) claims his algorithm is simple, but it uses advanced data structures like trees.
Another advantage of the proposed algorithm is the minimum usage of memory.The coordinates of the vertices of the original polygon are stores using two arrays.Throughout the algorithm no additional memory is required and it is the minimum usage of memory.Further the arrays are created in static memory.Therefore the proposed algorithm is suitable for embedded systems where the memory is limited.The algorithm proposed by Meister (Meister, 1975) uses recursion which requires larger stack sizes.Therefore such algorithms are not suitable for embedded systems.The algorithm proposed by Gang et al (Mei et al., 2012) also uses recursion.David's algorithm (Eberly, 2016) uses dynamic memory to implement tree data structure and needs lots of memory.Providing an implementation for the David's algorithm is impossible due to the hierarchical representation.
The basic triangulation algorithm is still used when 100% accuracy is necessary.The proposed algorithm is a more efficient algorithm to replace the basic triangulation algorithm.The proposed algorithm is simple, accurate, and consumes lowest memory.There are no algorithms available in literature possessing all these properties together.To validate the proposed algorithm its running time is compared with that of the basic triangulation algorithm.
Both the proposed algorithm and the basic triangulation algorithm were implemented using C programming language.Following software and hardware resources were used.
Computer: Intel(R) Pentium(R) Dual CPU; E2180 @ 2.00 GHz; 2.00 GHz, 0.98 GB of RAM; IDE: Turbo C++; Version 3.0; Copyright(c) 1990, 1992 by Borland International, Inc; The clock() function is used to measure the time consumed by algorithms in terms of clock cycles.However the time consumed by a single execution of an algorithm is very small and cannot be measured using clock cycles.Therefore an algorithm is iteratively executed 10 6 times and number of clock cycles was counted (Wijeweera et al., 2013).Several polygons were used with different number of vertices as given in Table 1.
Using each polygon in Table 1, the number of clock cycles taken by 10 6 numbers of executions of each algorithm was counted and results are shown in Table 2.The problem in experimental comparison is that CPU is interrupted by the other processes running in the computer.Therefore, to compensate that average execution time is calculated after measuring the same execution five times.The results are shown in Table 2.The average ratio is calculated by using the following formula,

Average ratio = (clock cycles for basic triangulation algorithm)/(clock cycles for proposed algorithm);
The average ratios in Table 2 prove that the proposed algorithm is faster than the basic triangulation algorithm.The running times of the basic triangulation algorithm increases with the number of vertices as seen in Table 2.This is compatible with its theoretical O (n 4 ) time complexity.
However representing time complexity of the proposed algorithm using a function of n is impossible since its unexpected behavior.The running time of the proposed algorithm depends on two factors: the number of vertices of the polygon, the order in which the ears of the polygon are arranged.The second factor is unpredictable and that depends on the shape of the polygon and the orientation of the polygon.For an example, sudden drop of average ratio can be observed in 7 th polygon in Table 2.Each ear in each iteration has been detected near at the end of the polygon vertex chain.Therefore the running time of the proposed algorithm has become longer.Similarly other sudden drops of average ratio can be explained.
To further investigate the behavior of the proposed algorithm relative to the basic triangulation algorithm, the set of polygons available from Kasun (academia.edu,2016) is used.Since this set of polygons has large number of vertices, the execution time for 10 4 iterations is measured.The experimental results are shown in Table 3.The results in Table 3 also prove that the proposed algorithm is faster than the basic triangulation algorithm.The polygons with same number of vertices but with different shapes were also tested in Table 3.For an example, the polygons 2, 10, 11, and 12 all have 18 vertices.However the basic triangulation algorithm consumes same number of clock cycles for each different polygon.The proposed algorithm consumes different amount of time for each different polygon with same number of vertices.The proposed algorithm depends on the shape of the polygon.If the ear can be detected at the beginning of polygon chain, it takes less time.If not it takes more time.

CONCLUSION
A simple, efficient, accurate, and lowest memory consuming algorithm for triangulating a polygon was proposed.The output of the proposed algorithm is a list of triangles which is more useful in applications.It uses only the static memory and do not use mechanisms like recursion which needs larger stack memory sizes.Therefore it is more suitable for embedded systems than existing algorithms.The proposed algorithm works with 100% accuracy and suitable for applications where the exact accuracy is necessary.The proposed algorithm uses array data structure only and it does not use advanced data structures.Furthermore it uses only simple mathematical concepts.Therefore it can be used as a teaching tool for the students The experimental results show that the proposed algorithm is more efficient than the basic triangulation algorithm.
The proposed algorithm can be used for line clipping against any polygon.Since a polygon can be represented as a union of triangles, this algorithm can be used to clip a given line segment bylining against each of those triangles.Then merging those clipped line segments will give the final result of the line segment clipped against the polygon.The idea behind this algorithm could be extended to clip lines against polyhedrons.

Figure 1 :Figure 3 :
Figure 1: Lowest vertex of a polygon

Figure 7 :
Figure 4: After pruning one ear

Table 1 :
The list of polygons

Table 2 :
Number of clock cycles comparison

Table 3 :
Number of clock cycles comparisonwith little knowledge in computer programming.