A New Algorithm For Computing Boolean Operations On Polygons
A New Algorithm For Computing Boolean Operations On Polygons
operations on polygons ⋆
Abstract
This paper presents a new algorithm for computing Boolean operations on polygons.
This kind of operations are frequently used in Geosciences in order to get spatial
information from spatial data modeled as polygons. The presented algorithm is
simple and easy to understand and implement. Let n be the total number of edges of
all the polygons involved in a Boolean operation and k be the number of intersections
of all the polygon edges. Our algorithm computes the Boolean operation in time
O((n + k) log n).
Finally, the proposed algorithm works with concave polygons with holes, and with
regions composed of polygon sets. Furthermore, it can be easily adapted to work
with self-intersecting polygons.
1 1 Introduction
21 For the general case of polygons, i.e. concave polygons with holes and self-
22 intersections, less solutions are available. Furthermore, some of the solutions
23 need complex, specific data structures as it is the case of Weiler’s (1980)
24 algorithm.
25 Greiner and Hormann (1998) propose a new algorithm for clipping polygons.
26 The algorithm is very easy to understand and implement. In addition, it is
27 very fast, especially for self-intersecting polygons. Nevertheless, the algorithm
28 treats degeneracy, which occurs when a vertex of a polygon lies on an edge
29 of the other, by perturbing the position of the vertex. Figure 2 shows that
30 perturbation is not always a good solution. As can be seen the result of the
31 boolean operation P − Q depends on the kind of perturbation, on the left fig-
32 ure a polygon with a hole is obtained, on the right figure a polygon with two
33 regions is obtained. Liu et al. (2007) propose some optimizations to Greiner
34 and Hormann’s algorithm, and explain how the algorithm can be adapted to
35 work with polygons with holes and regions composed of polygon sets. Unfor-
36 tunately, they do not bring new solutions to the degeneracy problem.
2
45 subdivision of edges provides a simple way of processing degeneracies.
46 It must be noted that Vatti’s (1992) algorithm is also based on the plane
47 sweep paradigm. However, our algorithm is quite different than Vatti’s one.
48 Concretely, our algorithm takes a different, more efficient, approach for com-
49 puting the intersections between the edges of the polygons involved in the
50 Boolean operation, making our algorithm much faster than Vatti’s one for the
51 computation of Boolean operations for large polygons.
52 The remainder of the paper is structured as follows. In the next section the
53 theoretical background on which the algorithm is based is stated. In Section
54 3 the overall algorithm is described. Sections 4 and 5 explain how the edges
55 belonging to the result of the Boolean operation are selected and connected
56 to form the solution. Section 6 makes a complexity analysis of the algorithm.
57 Section 7 describes how the special cases of the algorithm are processed. In
58 Section 8 a comparison with Vatti’s and Greiner and Hormann’s algorithms
59 is made. Finally, Section 9 brings some conclusions.
60 2 Basics
70 Suppose that the edges of two polygons are subdivided at their intersection
71 points, see Figure 4. In this case the boundaries of the polygons intersect at
72 endpoints of some of their edges. Therefore, the problem of computing the
73 boundary of the result of a Boolean operation on the polygons is reduced to
74 finding those edges of each polygon that lie or do not lie, depending on the
75 type of operation, inside the other polygon. Again, once these edges are found
76 they must be connected to form the result polygon.
77 We can therefore sketch the following approach for computing Boolean oper-
78 ations on polygons:
3
80 (2) Select those subdivided edges that lie inside the other polygon —or that
81 do not lie depending on the operation.
82 (3) Join the edges selected in step 2 to form the result polygon.
83 The algorithm proposed in this paper uses the plane sweep technique to effi-
84 ciently implement this approach.
85 3 The algorithm
97 Our algorithm also uses this approach to efficiently find the intersection points
98 between the edges of the polygons. Furthermore, the information available
99 during the plane sweep is used to subdivide the edges and decide which of
100 them should be included in the result of the Boolean operation. The algorithm
101 is described next.
102 We use a vertical line to sweep the plane from left to right. The sweep-line
103 status, S, consists of the ordered sequence of the edges of both polygons inter-
104 secting the vertical line. S will only change at the endpoints of the edges:
105 • When the left endpoint of an edge is reached the edge must be added to S.
106 • When the right endpoint is reached the edge must be removed from S.
107 Therefore, the event-point set is formed by the endpoints of the edges of the
108 polygons. This set changes dynamically because when an edge is subdivided
109 two new endpoints appear. The algorithm implements the event-point set
110 using a priority queue that holds the endpoints sorted from left to right.
111 Now, we can describe the algorithm, see Figure 5. Firstly, the endpoints of
112 the edges are placed into a priority queue sorted by x coordinate. Then the
113 endpoints are processed —from left to right— as follows. When a left endpoint
114 is found its associated edge is inserted into the sweep line status (S ). Then,
4
115 following the approach explained in Section 4, it is computed if the edge lies
116 inside the other polygon. Possible intersections with its neighbors along S
117 must also be processed. When a right endpoint is found its associated edge
118 is removed from S. Now, its two neighbors along S become adjacent, and are
119 tested for intersection. The removed edge is also considered for inclusion in
120 the result of the Boolean operation.
121 The procedure possibleInter is used to detect and process a possible intersec-
122 tion between two edges. If the edges belong to the same polygon or they only
123 intersect at one of their endpoints no extra processing is required. If the edges
124 belong to different polygons and they intersect at a point interior to one of
125 the edges then they must be subdivided. When an edge is subdivided the data
126 structures Q and S are updated to reflect the new status. Figure 6 shows the
127 types of intersections that lead to the subdivision of an edge and how they are
128 processed. We have used the intersection routine described in Schneider and
129 Eberly (2003) for detecting a possible intersection between two edges.
130 Let us see an example of edge subdivision, see Figure 7. When the sweep-line
131 reaches the point p1 , we have S is {q2 q3 , q1 q2 }. Then, the left endpoint of p1 p2
132 is processed, and p1 p2 is inserted into S (S = {q2 q3 , q1 q2 , p1 p2 }). p1 p2 intersects
133 with its neighbor q1 q2 at point i, so p1 p2 and q1 q2 must be subdivided into edges
134 p1 i, ip2 , q1 i and iq2 . Q must be updated to include the endpoints of these new
135 edges. S will also change to S = {q2 q3 , iq2 , p1 i}. After this, the left endpoint
136 of p0 p1 is processed, and p0 p1 is inserted into S (S = {q2 q3 , iq2 , p1 i, p0 p1 }).
138 When the sweep-line reaches the right endpoint of an edge e the algorithm
139 decides if e belongs to the result of the Boolean operation. As outlined in
140 Section 2, this decision is made by testing if e lies inside the other polygon P.
147 • pl: Indicates if the edge belongs to the subject or clipping polygon.
148 • inOut: Indicates if the edge determines an inside-outside transition into the
149 polygon, to which the edge belongs, for a vertical semi-line that goes up and
150 intersects the edge.
151 • inside: Indicates if the edge is inside the other polygon.
5
152 Figure 9 shows a routine that computes the inOut and inside flags of a left
153 endpoint event le, that has been inserted into S, given the left endpoint event
154 ple of the immediate predecessor of le in S. If ple is null then le is the first
155 event in S and the flags can be trivially set to false.
156 To correctly apply this routine endpoints placed at the same x coordinate must
157 be processed —that is, sorted into the priority queue— from bottom to top.
158 If two endpoints share the same point the right endpoints must be processed
159 before the left ones. If two left endpoints share the same point then they must
160 be processed in the ascending order of their associated edges in S.
162 The result of a Boolean operation on two polygons is a set, possibly empty, of
163 polygons. In the previous sections we have described how to find the edges of
164 these polygons. Next, we show how these edges can be connected to form the
165 result polygons.
166 We must hold a set C —initially empty— of chains of connected edges and a
167 set R that holds the result polygons. Every edge e that belongs to the solution
168 must be processed as follows:
169 • If e cannot be connected at any of the ends of any chain of C, then a new
170 chain, formed by e, is added to C.
171 • If e can be connected to only one chain c of C, then e is added to c. If the
172 first and last edges in c are connected, then c holds a result polygon and it
173 is moved to R.
174 • If e can be connected to two chains c1 and c2 of C, then the edges of c2 and
175 e are added to c1 , and c2 is removed from C. If the first and last edges in
176 c1 are connected then c1 is moved to R.
178 In this section we analyse the performance of the algorithm shown in Figure
179 5. We will use the following notation: let n be the total number of edges of
180 all the polygons involved in the Boolean operation and k be the number of
181 intersections of all the polygon edges.
182 The algorithm starts inserting all the endpoints of the edges on Q, which takes
183 O(nlog(n)). Then the plane sweep starts and all the events are processed in
184 the cycle. Let us analyse the cycle body:
6
185 • Lines 6, 11, 12, 13 and 16 are operations on S. S holds at most n edges and
186 it can be implemented as a dictionary, so these lines take each O(log(n)).
187 • Line 7 runs in time O(log(n)), since this is the time needed to determinate
188 the immediate predecessor of the event in S —the routine used to set the
189 inside and inOut flags runs in time O(1).
190 • The function possibleInter takes O(log(n+k)), because after an intersection
191 test, which uses constant time, four insertions on Q can be done, and Q has
192 an O(n + k) size.
193 • Line 3 runs in constant time, and line 4 takes O(log(n + k)).
194 • Finally, the inclusion of an edge in the result polygons—lines 14 and 15—
195 , which is treated in Section 5, takes O(log(n)). There can be at most n
196 chains of connected edges in which to include the edge. The endpoints of
197 the chains can be stored in a dictionary, so that finding the chain that joins
198 with the edge runs in time O(log(n)). The remainder of operations —joining
199 and edge to a chain or joining two chains— can be implemented in constant
200 time.
201 Therefore we can conclude that the cycle body runs in time O(log(n + k)).
202 The cycle is executed (n + 4k) times so the cycle takes O((n + k)log(n + k)),
203 i.e., O(n + k)log(n), since k ≤ n2 . This time clearly dominates the initial
204 O(nlog(n)) step 1, so the whole algorithm runs in time O(n + k)log(n).
206 In this section we discuss the special cases of the algorithm. As it will be
207 shown they are treated in an simple, elegant way.
209 Vertical edges are special because their two endpoints are placed at the same
210 x coordinate. However, they can be processed by the algorithm as “normal
211 edges” as long as the following simple rules are met:
212 (1) The lower endpoint of a vertical edge must be considered as its left end-
213 point and the upper endpoint as its right endpoint.
214 (2) To order the sweep-line status (S ) it must be considered that a vertical
215 edge intersects the sweep-line at the y coordinate of its lower endpoint —
216 remember that S is ordered by the y coordinate at which edges intersect
217 the sweep-line. If a non-vertical edge intersects the sweep-line at the lower
218 endpoint of a vertical edge, then the vertical edge is placed in S after the
219 non-vertical edge.
7
220 7.2 Overlapping edges
221 When two edges overlap they are subdivided so that their overlapping frag-
222 ments become an edge of each polygon —see the last type of intersection in
223 Figure 6. The algorithm must select at most one of these two “equal edges”
224 as part of the result of the Boolean operation. Unfortunately, the methods
225 explained in Section 4 to select the result edges does not work for overlapping
226 edges. Therefore the two “equal edges” representing an overlapping fragment
227 need a special processing, which is described next.
228 When overlapping between two edges is detected one of the edges represent-
229 ing the overlapping fragment is labeled as NON CONTRIBUTING, meaning
230 that the edge will not be considered for inclusion in the result of the Boolean
231 operation. The other edge is labeled as SAME TRANSITION or DIFFER-
232 ENT TRANSITION depending on the overlapping edges having the same
233 inOut flag, and it will be included in the result depending on its label and
234 on the type of Boolean operation. Edges labeled as SAME TRANSITION are
235 only included in the result of union and intersection operations. Edges labeled
236 as DIFFERENT TRANSITION are only included in the result of set theo-
237 retic difference operations. Figure 10 shows how overlapping fragments are
238 included in the result of Boolean operations depending on the inOut flag of
239 the overlapping edges and on the type of Boolean operation.
241 When the boundary of a polygon crosses itself, the polygon is called self-
242 intersecting. Figure 11 shows a polygon set consisting of three individual poly-
243 gons: a square, a triangle inside the square —a hole—, and a self-intersecting
244 bow-tie shaped polygon that, in turn, intersects with the square. To know
245 whether a point belongs to the interior of the polygon the even-odd rule can
246 be applied: let r be a ray thrown from the point to infinity in any direction,
247 such as the ray does not cross any polygon vertex or self-intersecting point,
248 and let c be the number of times that r crosses the boundary of the polygon.
249 Then, the point is inside the polygon if c is odd —and outside if c is even.
250 The algorithm does not work for polygons with self-intersections. The rea-
251 son is simple: the algorithm is not aware of self-intersection points. However,
252 these points should be processed as events of the plane sweep because self-
253 intersecting edges should exchange their positions at the sweep-line status at
254 their intersection points.
255 Fortunately, a small change in the algorithm can make it work for this kind
256 of polygons. It is enough to find and process intersection points not only
8
257 between the edges of different polygons, but also of the same polygon. In this
258 case, self-intersecting edges will be also subdivided at their intersection points,
259 and therefore, the result polygons will not contain self-intersections.
260 8 Evaluation
261 In this section we compare Greiner and Hormann’s and Vatti’s algorithms
262 with the one presented in this paper. We have implemented Greiner and Hor-
263 mann’s and our algorithm in C++, due to Vatti’s algorithm being difficult
264 to implement we have used the implementation available at 1 . To implement
265 our algorithm we have used a STL’s priority queue container to represent the
266 event queue and a STL’s set container to represent the status line. The pro-
267 grams have been executed on a Intel Pentium IV processor at 2.4 GHz under
268 Linux. Figure 12 shows a polygon representing the coastline of the Earth and
269 its main lakes and a second polygon set consisting in several squares. Figures
270 13, 14 and 15 show the result of the Boolean intersection, union and difference,
271 respectively.
272 We have computed the intersection of the polygon representing the earth with
273 several polygon sets with an increasing number of squares, the result are shown
274 in Table 1. The last column of the table shows the number of intersections be-
275 tween the edges of the polygons. Clearly, our algorithm performs better when
276 the number of edges is increased. To understand this it must be said that
277 Boolean operation algorithms spend the majority of their CPU time comput-
278 ing the intersection points between the polygons. The analyzed algorithms use
279 different approaches to compute these points:
280 • Greiner and Hormann’s algorithm uses the brute force approach. Of course,
281 Greiner and Hormann’s algorithm could also use a plane sweep technique
282 to compute the intersection points for large polygons.
283 • Our algorithm uses the classical plane sweep approach. Edges are only tested
284 for intersection when they become adjacent in the status line. At most a pair
285 of intersection tests are computed during the processing of a plane sweep
286 event.
287 • Although Vatti’s algorithm is also based on the plane sweep technique, it is
288 very different from our algorithm. For example, it does not use the classical
289 plane sweep approach for computing the intersection points: in Vatti’s algo-
290 rithm during the processing of a plane sweep event each edge in the status
291 line has to be tested for intersection with its immediate predecessor edge
292 in the status line. Obviously, this approach is slower than our method that,
1 General Polygon Clipper library, by Alan Murta,
http://www.cs.man.ac.uk/ toby/alan/software/
9
293 as mentioned above, only needs a pair of intersection tests for each plane
294 sweep event processed.
295 9 Conclusions
296 In this paper we have proposed a new algorithm for computing Boolean oper-
297 ations on polygons. The algorithm is based on the classical plane sweep tech-
298 nique for computing the intersection points between a set of segments. Our
299 algorithm subdivides the edges of the polygons at their intersection points.
300 This subdivision makes the algorithm quite simple, allowing an elegant way
301 of processing degeneracies.
305 Unlike some approaches, the proposed algorithm does not need to be adapted
306 to work with polygons with holes, and with regions composed of polygon sets.
307 Acknowledgements
308 This work has been partially granted by the Ministerio de Ciencia y Tecnologı́a
309 of Spain and the European Union by means of the ERDF funds, under the re-
310 search project TIN2007-67474-CO3-03, and by the Conserjerı́a de Innovación,
311 Ciencia y Empresa of the Junta de Andalucı́a and the European Union by
312 means of the ERDF funds, under the research projects P06-TIC-01403 and
313 P07-TIC-02773.
314 We also would like to thank the anonymous reviewers for their helpful com-
315 ments.
316 References
317 Andereev, R.D., 1989. Algorithm for clipping arbitrary polygons. Computer
318 Graphics Forum 8 (2), 183–191.
319 Foley, J.D., Van Dam, A., Feiner, S.K., Hughes, J.F., 1990. Computer Graph-
320 ics: Principles and Practice, Addison-Wesley, Reading, MA, 1174pp.
321 Greiner, G., Hormann, K., 1998. Efficient clipping of arbitrary polygons. Asso-
322 ciation for Computing Machinery—Transactions on Graphics 17 (2), 71–83.
10
323 Liang, Y.D., Barsky, B.A., 1983. An analysis and algorithm for polygon clip-
324 ping. Communications of the Association for Computing Machinery 26 (11),
325 868–877.
326 Liu, Y.K., Wang, X.Q., Bao, S.Z., Gombos̆i, M., Z̆alik, B., 2007. An algorithm
327 for polygon clipping, and for determining polygon intersections and unions.
328 Computers & Geosciences 33, 589–598.
329 Preparata, F., Shamos, M., 1985. Computational Geometry an Introduction,
330 Springer-Verlag, New York, NY, 398pp.
331 Sutherland, I.E., Hodgeman. G.W., 1974. Reentrant polygon clipping. Com-
332 munications of the Association for Computing Machinery 17 (1), 32–42.
333 Schneider, P.J., Eberly, D.H., 2003. Geometric Tools for Computer Graphics,
334 Elsevier Science, San Francisco, CA, 1060pp.
335 Vatti, B.R., 1992. A generic solution to polygon clipping. Communications of
336 the Association for Computing Machinery 35 (7), 56–63.
337 Weiler, K., 1980. Polygon comparison using a graph representation. In: Pro-
338 ceedings of the 7th annual conference on Computer graphics and iteractive
339 techniques (SIGGRAPH), Seattle, Washington, United States, pp.10–18.
11
P
P−Q P P−Q
Subdivision
12
01. Insert the endpoints of the edges of polygons into priority queue Q
02. while (! Q.empty ()) {
03. event = Q.top ();
04. Q.pop ();
05. if (event.left_endpoint ()) {
06. pos = S.insert (event);
07. event.setInsideOtherPolygonFlag (S.prev (pos));
08. possibleInter (pos, S.next (pos));
09. possibleInter (pos, S.prev (pos));
10. } else { // the event is a right endpoint
11. pos = S.find (*event.other);
12. next = S.next (pos);
13. prev = S.prev (pos);
14. if (event.insideOtherPolygon ()) Intersection.add (event.segment ());
15. if (! event.insideOtherPolygon ()) Union.add (event.segment ());
16. S.erase (pos);
17. possibleInter (prev, next);
18. }
19. }
Fig. 5. Algorithm.
e1 e11
Subdivision
e22
e2 e21 e12
e1 e1
e22
e2 e21
e11
e1 e12
e21
e2 e22
13
p0
Polygon P
q0
q1
q5
p1 p3
i q4
Polygon Q
p2
q2 q3
struct SweepEvent {
Point p; // point associated with the event
SweepEvent *other; // event associated to the other endpoint of the edge
bool left; // is the point the left endpoint of the edge (p, other->p)?
PolygonType pl; // it can be SUBJECT or CLIPPING
bool inOut; // inside-outside transition into the polygon
bool inside; // is the edge (p, other->p) inside the other polygon?
EdgeType type; // used for overlapping edges
};
Fig. 8. Data structure for representing events/edges.
14
INTERSECTION UNION P−Q
Overlapping edges with the same
inOut flags
P Q
15
Fig. 13. Intersection.
16
Table 1
Execution times of intersection operations (in seconds).
Number of vertices Greiner Vatti New algorithm Number of intersections
76696 x 32 0.03 0.55 0.16 178
76696 x 648 0.21 0.58 0.16 814
76696 x 3895 4.01 2.16 0.22 4294
76696 x 15580 17.04 6.30 0.38 8978
17