An efficient planar incremental convex hull algorithm to find the edges of the boundary polygon of the convex hull of a set of points

: The definition of the convex hull of a set of points is the smallest convex set containing all the points. Many algorithms have been proposed with the worst case time complexity is equal to O (n log n). It has been proved that the lower bound of time complexity for construction of the convex hull is O (n log n). However, these algorithms are static and require all points at the start. Suppose that points reach sequentially one after one and the convex hull needs to be maintained at each insertion of a point. The convex hull should be constructed from the scratch to handle each point insertion if a static convex hull algorithm was used. This process consumes O (n log n) time and therefore it is inefficient. An incremental convex hull algorithm can maintain the convex hull at each insertion performing a trivial modification to the existing convex hull without constructing the convex hull from the scratch. The optimal time complexity for insertion of a point in existing incremental convex hull algorithms is O (n). A new incremental convex hull algorithm with O (h 2 ) time complexity is proposed in this paper. Note that h represents the number of vertices of the convex hull. A set of line segments is used to represent the convex hull. Some of the existing line segments may be deactivated instead of deleting upon the successive growth of the convex hull. Thus, the computational cost is reduced. The proposed algorithm is faster than the existing algorithms when h < SQRT (n) and the concept can be extended to three dimensions.


INTRODUCTION
The convex hull is one of the most common structures found in the field of computational geometry. It is used in its own right and also as a tool to build other structures. The convex hull of a point set is defined as the smallest convex set containing all the points (O'Rourke, 1997).
The convex hull concept is widely used in computational geometry as well as in its applications. The applications dominate a wide variety of areas: image processing, statistics, analysis of spectrometry data, cartography, pattern recognition, file searching, crystallography, cluster analysis, collision detection, numerical integration, metallurgy, etc. Furthermore, there are some problems that can be brought down to the convex hull problem: half-space intersections, Voronoi diagrams, and Delaunay triangulations (Bayer, 1999).
Many algorithms have been proposed with the worst case time complexity is equal to O (n log n) Bently and Shamos, 1978;Graham, 1972;Preparata, 1979;Preparata and Hong, 1977;Shamos, 1978a). It has been proved in several research papers (Preparata and Hong, 1977;Shamos, 1978a;Avis, 1979;Boas, 1980;Yao, 1981) that the lower bound of time complexity for convex hull construction is O (n log n). Two algorithms (Eddy, 1977;Jarvis 1973) have been proposed with O (nh) worst case time complexity. Note that h is the number of vertices on the convex hull. Kirkpatrick and Seidel (Kirkpatrick and Seidel, 1986) proposed a two dimensional convex hull algorithm with O (n log h) worst case time complexity. Furthermore, they proved that O (n log h) is the theoretical lower bound of the problem.
Algorithms have also been proposed with linear expected time complexity (Bently and Shamos, 1978;Shamos, 1981b). Further, Akl, Toussaint, and Devroye (Akl and Toussaint, 1978;Devroye and Toussaint, 1981;Toussaint et al., 1978) have proved that a preprocessing step enable any of the algorithms mentioned above to run in O (n) expected time for particular distributions of input (Bayer, 1999). Practical running times have also been measured in (Bhattacharya and Toussaint, 1981).
All the algorithms discussed above are called static as they require all points at the start. Suppose that points reach sequentially one after one and the convex hull should be maintained at each point insertion. The convex hull should be constructed from the scratch if a point insertion is handled by a static convex hull algorithm. That means it needs O [(n + 1) log (n + 1)] = O (n log n) time to insert a point if there are n points already available (O'Rourke, 1997). An incremental convex hull algorithm can maintain the convex hull at each insertion with a trivial modification to the current convex hull without constructing the convex hull from the scratch.
The Beneath-Beyond algorithm is one of the oldest incremental type convex hull algorithms available in literature (Kallay, 1981;Grunbaum, 1961;Preparata and Shamos, 1985). A new point insertion is carried out in three steps. In the first step, the visible facets are detected. The horizon ridges set for the point marks the visible facets boundary. If the point is above a facet then the facet is visible. As the second step, a cone of novel facets is created from the point to its horizon edges. As the third step, the visible facets are removed to construct the convex hull of the current set of points that includes the new point. Clarkson and Shor (Clarkson and Shor, 1989) proposed a randomized incremental algorithm using dual space of half space intersections. A half space is added by computing its intersection with the polytope formed by the earlier intersections. A half space is randomly selected and added to the polytope. The list of polytope edges that intersect the half space is maintained for each unprocessed half space. All such lists together are called the conflict graph. The conflict graph is used to identify the modified edges when a half space is processed. Storing only a single modified edge for each unprocessed half space helps to reduce memory consumption to O (n). A simple search is performed through adjacent edges in order to identify the remaining modified edges. A modified version of Clarkson and Shor's algorithm was proposed later (Barber et al., 1996). Instead of dual space of half spaces and polytopes, their algorithm considers the space of points and convex hulls. Therefore, the conflict graph for each facet is an outside set. Only if a point is above the facet then it is in a facet's outside set. A point not yet processed belongs to exactly one outside set as in the algorithm by Clarkson and Shor. In this variation, the furthest point of an outside set is processed instead of processing a random point.
Conflict graphs or outside sets are not maintained in other modified versions of Clarkson and Shor's algorithm. Alternatively, they store old facets of the convex hull with related links to the novel facets that replaced them. A simplex is formed from d + 1 of the input points and it is the beginning of the hierarchy. The visible facets chain is searched through this hierarchy in order to find a visible facet for a given point. If all facets of the existing convex hull are above the point then the point is ignored. Thus these algorithms form the convex hull. The running times of these algorithms are closer with other algorithms (Fortune, 1993).The comparison of the quick hull algorithm against the randomized incremental algorithms can be performed simply by changing the selection step of the quick hull algorithm. Furthermore, the quick hull algorithm handles less number of interior points than the randomized algorithm. Therefore, the quick hull algorithm is faster. Memory consumption of the quick hull algorithm is also lesser due to the reuse of the memory used by old facets (Barber et al., 1996).
The time complexity of the quick hull algorithm for an input of size n with r processed points is O (n log r). The number of points processed by the quick hull algorithm is proportional to the number of vertices of the output. The output size may be considerably smaller than the worst case size. Therefore, output sensitivity is useful (Barber et al., 1996). A two dimensional optimal output sensitive algorithm was invented with O (n log h) time complexity given that h is the size of the output (Kirkpatrick and Seidel, 1986). A three dimensional output sensitive algorithm was invented with optimal time complexity (Charkson and Shor, 1989). Later, randomization of this three dimensional algorithm was removed (Chazelle and Matousek, 1992).
Investigation of existing incremental convex hull algorithms suggests that the optimal time complexity to handle an insertion of a point is O (n). This paper proposes a new algorithm that takes O (h 2 ) time to insert a point given that there are h points on the current convex hull. The proposed algorithm is relatively simple and represents the convex hull using a set of line segments. Furthermore, the three dimensional extension of the algorithm is possible.

MATERIALS AND METHODS
An incremental type convex hull algorithm is introduced in this section. The algorithm outputs a set of line segments forming the convex hull of the point set. The edges are stored using the end points. The number of edges is stored in the variable edges and the edges are numbered as 0, 1…, (edges -1). The variable edges is initialized to 0. The coordinates of the starting point and the ending point of the i th edge are represented by (sx [

i], sy[i]) and (ex[i], ey[i])
respectively. These four coordinates of the edges are stored using a set of four arrays. These four arrays together are called the edge list. When a new edge is inserted to the edge list then the variable dead [i] for that edge is set to 0. That means the edge is on the convex hull. However, some edges will no longer be on the convex hull due to successive growth of the convex hull. The corresponding variable dead[i] is set to 1 instead of deleting such edges. This means the edge no longer belongs to the convex hull. Therefore, the set of edges on the convex hull is a subset of edges from the edge list in which the variable dead[i] is 0.
The development of the convex hull is carried out in two phases: the generation of the primary hull and the generation of the secondary hull. Following sections describe each phase. The primary hull construction is handled using function makePHull() and the secondary hull construction is handled using function makeSHull(). A separate function makeHull() is used to choose appropriate construction phase. The function makeHull() is executed exactly once at each insertion of a data point.

The use of function makeHull()
The pseudo code for function makeHull() is given below.
CALL makeSHull(); 8. END IF 9. END If the number of edges in the edge list is less than 3 then the function makePHull() is executed (Line 5). Otherwise the function makeSHull() is executed (Line 7). The justification for this conditional logic is provided in the sections given below.

The use of function makePHull()
The pseudo code for function makePHull() is given below.
The primary hull is made using the first three noncollinear points. The variable distinct stores the number of non-collinear points and it is initially set into 0 (Line 1). The first point is included as the starting point of the 0 th edge (Line 5). The first point is itself the hull. Then the variable distinct is set to 1. The algorithm waits until a distinct point arrives (Line 8) and that point is set as the ending point of the 0 th edge (Line 9). Then the variable distinct is set to 2 and variable edges is set to 1. The next point has two possibilities: collinear or non-collinear with the 0 th edge. These two possibilities should be handled in two different ways and the following two sections describe these two techniques. The co-linearity can be tested using the area of the triangle made by (sx [

Next point is collinear with 0 th edge (Line 16 to 44)
The hull remains as a line segment in this situation. If the 0 th edge is parallel to y-axis (Line 16) then the projection of the three points on y-axis is considered (Line 17 to 28). Otherwise, the projection on x-axis is considered (Line 31 to 43). If the novel point is on the 0 th edge then no modification is needed. Otherwise, the closest vertex to the novel point should be replaced by the novel point. After this step, the next point also executes function makePHull() since variable edges < 3. When the number of edges in the edge list is three then the hull is called the primary hull as shown in Figure 1. According to function makeHull(), the new points execute function makeSHull() instead of function makePHull() after the convex hull has reached the state of primary hull.

Calculating the centroid of the primary hull
An internal point of the hull should be found after generating the primary hull. Centroid of any triangle falls inside the triangle. The centroid of the primary hull is inside the hull even through the successive growth of the hull. Therefore, the centroid of the primary hull is selected as the interior point. Calculating the centroid of the primary hull is done by using function calCentroid(). The relevant pseudo code is given below. should be set to 0. Otherwise it should be set to 1. If an edge is parallel to y-axis then its gradient is not defined (Line 6). The aim of calculating gradient and y-intercept of edges is to calculate the value of (a * xc + b * yc + c) for the edge (If the equation of the line is [a * x + b * y + c = 0]). Here (y = m * x + c) form of the equation is used for straight lines. Then it can be reformulated as (m * x -y + c = 0).

The Use of Function makeSHull()
The pseudo code for function makeSHull() is given below.
Initial number of edges in the edge list is first assigned to variable i_edges (Line 4). Then method calCentroid() is invoked to calculate the centroid of the primary hull (Line END FOR 58. END FOR 59. END 6). The variable calculated (Line 1, Line 5, Line 7) is used to avoid recalculation of the centroid. The primary hull has already been constructed using the function makePHull(). The next point may arrive as two possible situations as shown in Figure 2 and Figure 3 respectively. There, P 1 P 2 P 3 is the current hull and P is the novel point. If the novel point falls inside the triangle then it is ignored. If the coordinates of the novel point is (x, y) then the value of valc[i] * (m * x -y + c) is calculated with respect to each hull-edge (with determinate gradients) of the existing hull (Line 12). If the gradient of an edge is undefined then for those edges value of valc [i] * (x -sx[i]) should be calculated. If that value is negative then the centroid of the primary hull and new point are on the opposite sides of the relevant hull-edge (Line 12, Line 24). The variable dead[i] of those edges should be set into 1.
In situation shown in Figure 2, P 1 P 3 edge should set dead and in situation shown in Figure 3, both P 2 P 3 and P 1 P 3 edges should set dead. Once an existing hull-edge is dead, new two edges are included to the edge list as non-dead edges. Starting point of both of those two edges should be the new point. And the ending points should be the ending points of each dead edge. In Figure 2, PP 1 and PP 3 edges should be added to the edge list as non-dead edges. In Figure 3, PP 1 and PP 3 edges are included to the edge list as non-dead edges due to the death of P 1 P 3 , and also PP 2 and PP 3 edges are included to the edge list as non-dead edges due to the death of P 2 P 3 edge. It can be observed that PP 3 edge has been added to the edge list twice (Line 13 to 21, Line 25 to 33). After inclusion of new edges, values of their det [i], m[i], c[i], and valc[i] should be calculated (Line 37 to 47). Figure 4 is used to describe the subsequent part of the algorithm. Let P 1 P 2 P 3 P 4 P 5 P 6 is the current hull and P is the new point. Here P 2 P 3 , P 3 P 4 , P 4 P 5 , P 5 P 6 edges should set dead. Death of P 2 P 3 leads to include PP 2 and PP 3 . Death of P 3 P 4 leads to include PP 3 and PP 4 . Death of P 4 P 5 leads to include PP 4 and PP 5 . Death of P 5 P 6 leads to include PP 5 and PP 6 . Then the edge list contains PP 3 , PP 4 and PP 5 edges each twice as still non-dead edges. These edges should set dead. This can be done by searching for the duplicate edges after each inclusion of a new point (Line 51 to 58). Thus the resultant convex hull can be generated. Figure 5 illustrates an example of generation of convex hull dynamically using the proposed algorithm. The dotted lines show the dead edges in the edge list. And the thick lines show the hull-edges.

Construction of the convex hull using a set of points
A sequence of random points is used to demonstrate the algorithm. Let the number of random points in the sequence be p. A pseudo code is given below.

END FOR
A random point is generated in each time (Line 2 and Line 3). The function makeHull is executed for each random point (Line 4). Then the constructed convex hull is displayed (Line 5) and erased (Line 7). The convex hull is displayed for a small amount of time before it is erased (Line 6). Thus the evolution of the convex hull can be seen on the display.

RESULTS AND DISCUSSION
Suppose that the convex hull of n points has already been computed and h points are on the convex hull. It takes O (h) time to test whether the novel point is inside or outside with respect to each edge of the convex hull. The new point is outside (h -1) edges of the convex hull in the worst case. Therefore, [2 (h -1)] edges are included to the edge list. To set dead the duplicate edges, it takes O (h 2 ) time. Thus, it takes O (h) + O (h 2 ) = O (h 2 ) time to insert a novel point to the existing convex hull in the worst case. The number of additional memory locations is proportional to the number of edges included to the edge list. None of the edges are deleted and instead the state of each of them is set dead. Thus, the space complexity of the proposed algorithm is O (h).
The Beneath-Beyond algorithm deletes the old facets in order to insert a new point. Therefore, the time complexity of inserting a new point is O (n) (Kallay, 1981;Grunbaum, 1961;Preparata and Shamos, 1985).  The proposed algorithm does not delete the old facets and instead the state of each facet is set dead. Thus the proposed algorithm takes only O (h 2 ) time to insert a new point as discussed above. Later, output sensitive algorithms were invented with O (n log h) time complexity (Barber et al., 1996;Kirkpatrick and Seidel, 1986;Charkson and Shor, 1989;Chazelle and Matousek, 1992). However, these output sensitive algorithms also take O (n) time to insert a new point even though they take only O (n log h) time to construct the convex hull from the scratch. Therefore, if h < SQRT (n) then the proposed algorithm inserts a new point faster than the existing fastest algorithms.
The three dimensional extension of the proposed algorithm is possible (Wijeweera and Kodituwakku, 2018). A list of triangular facets is maintained instead of the edge list. The novel point is tested for being inside or outside each facet of the existing convex hull. The facets on the existing convex hull to which the new point is outside are set dead. Death of a single facet on the current convex hull leads to insertion of three new facets to the facet list. The duplicate facets are removed. Thus, the new convex hull can be constructed.

CONCLUSION
An incremental type planar convex hull algorithm was proposed in this paper. The algorithm takes O (h 2 ) time and O (h) space to insert a novel point. The proposed approach is faster than the available fastest algorithms when h < SQRT (n). Furthermore, the three dimensional extension of the proposed algorithm is possible.