A Simple and Correct Even-Odd Algorithm for the Point-in-Polygon Problem for Complex Polygons Michael Galetzka1 and Patrick Glauner2 1 Disy 2 Interdisciplinary

Informationssysteme GmbH, Ludwig-Erhard-Allee 6, 76131 Karlsruhe, Germany Centre for Security, Reliability and Trust, University of Luxembourg, 4 rue Alphonse Weicker, 2721 Luxembourg, Luxembourg [email protected], [email protected]

Keywords:

Complex Polygon, Even-Odd Algorithm, Point-in-Polygon.

Abstract:

Determining if a point is in a polygon or not is used by a lot of applications in computer graphics, computer games and geoinformatics. Implementing this check is error-prone since there are many special cases to be considered. This holds true in particular for complex polygons whose edges intersect each other creating holes. In this paper we present a simple even-odd algorithm to solve this problem for complex polygons in linear time and prove its correctness for all possible points and polygons. We furthermore provide examples and implementation notes for this algorithm.

1

INTRODUCTION

At a first glance, the point-in-polygon problem seems to be a rather simple problem of geometry: given an arbitrary point Q and a closed polygon P, the question is whether the point lies inside or outside the polygon. There exist different algorithms to solve this problem, such as a cell-based algorithm (Zalik and Kolingerova), the winding number algorithm (Hormann and Agathos, 2001) or the even-odd algorithm (Foley et al., 1990) that is used in this paper. The problem is not as trivial as it seems to be if the edges of the polygon can intersect other edges as seen in Figure 1. This kind of polygon is also often called a complex polygon since it can contain ”holes”.

A lot of special cases have to be considered (e.g. if the point lies on an edge) and most of the existing even-odd algorithms either fail one or more of these special cases or have to implement some kind of workaround (Schirra, 2008). The rest of this paper is organized as follows. In Section 2, we present an even-odd algorithm and prove its correctness for all possible points and polygons with no special cases to be considered. In Section 3, we apply this algorithm to an example complex polygon. We then provide implementation notes in Section 4. Section 5 summarizes this work.

2

THE EVEN-ODD ALGORITHM

The basic even-odd algorithm itself is fairly simple: cast an infinite ray from the point in question and count how many edges of the polygon the ray intersects (Foley et al., 1990).

Figure 1: A self-intersecting polygon.

Definition Let P be a polygon with n vertices P1 , ..., Pn−1 , Pn where the vertices are sorted in such a way that there exists an edge between Pi and Pi+1 for every index 1 ≤ i < n and between P1 and Pn . The algorithm computes if the arbitrary point Q lies either inside or outside of P.

The steps of the algorithm To ease the presentation of the steps of the algorithm, it defines its own coordinate system with (0|0) = Q by moving the polygon. This translation is only for illustration purposes. The algorithm then uses the positive x-axis as ray to calculate intersections with the edges of P. So the start vertex of the ray is (0|0) and the end vertex is (xmax |0) where xmax is an x value greater than that of any of the vertices of P. 1. First it is determined if Q is equal to any of the vertices of P or lies on any of the edges connecting the vertices. If so the result is inside. 2. A vertex Ps that does not lie on the x-axis is searched in the set of vertices of P. If no such vertex can be found the result is outside. 3. Set i to 1. Beginning from that vertex Ps the following steps are repeated until all vertices of P have been visited: (a) The index s is increased to s + i until the next vertex Ps+i not lying on the x-axis is found. If the index s + i > n then i is set to −s and the search is continued. (b) Depending on the course of step (a) one of the following steps is taken: i. No vertex has been skipped: the line segment from Ps to Ps+i is intersected with the positive x-axis. ii. At least one vertex with a positive x-value has been skipped: the line segment from Ps to Ps+i is intersected with the complete x-axis. iii. At least one vertex with a negative x-value has been skipped: nothing is done. (c) Ps+i is the starting vertex for the next iteration. 4. If the count of intersections with the x-axis is even then the result is outside, if it is odd the result is inside. Proof of correctness To prove that the oven-odd algorithm in general is correct one can generalize it to the Jordan Curve Theorem and prove its correctness (Tverberg, 1980). What has to be proven is that the given algorithm is in fact a correct even-odd algorithm, meaning that the count of edges is correct under all circumstances. A lot of challenges emerge when trying to intersect two line segments and one of the segments has one or two of its vertices lying on the other segment. If the count of intersections with the x-axis are to be counted correctly, then each edge of P must either clearly intersect the x-axis or not intersect it at all. There must be no case where the starting or end vertex of a line segment lies on the line segment it should be intersected with.

The x-axis is the first line segment to look at, since it is part of all the intersections. The start vertex of the x-axis, namely Q, is guaranteed not to lie on any edge or to be equal to any vertex of P. If this was the case, then the first step of the algorithm would have already returned the correct result. The end vertex is guaranteed to have an x value greater than any of the vertices of P, so no vertex of P can be equal to it and no edge of P can contain it. Of course there still exists the challenge that one or more vertices of P lie on the x-axis as seen in Figure 2. P5

P4

Q(0|0)

P2

P3

P1

Figure 2: One of the edges of P lies on the x-axis.

The algorithm deals with this kind of challenge by ignoring the vertices lying on the x-axis and stepping over them when trying to find an edge to intersect. It then creates a new auxiliary edge that it can intersect safely. So starting for example at vertex P1 it would ignore P2 and P3 and then create a new edge between P1 and P4 as shown in Figure 3. P5

P4

Q(0|0)

P2

P3

P1

Figure 3: A new auxiliary edge has been created.

At this point it is clear that none of the edges used to calculate the count of intersections with the x-axis has a vertex on the x-axis and is either clearly intersecting it or not. What has to be shown is that all created auxiliary edges are correct substitutes to calculate the intersections with the x-axis. Indeed they are not a correct substitute to intersect the positive x-axis, as can be seen in Figure 4. Here

the auxiliary edge would be the same as the edge between P1 and P3 and this edge does clearly not intersect the positive x-axis. So the total count of intersected edges of the polygon shown in Figure 4 would be zero - which is obviously wrong. The algorithm actually deals with this challenge in step 3.b, by extending the ray in that special case where a vertex lying on the positive x-axis has been skipped. The new ray is then the complete x-axis and not only the positive part of it. It can be seen that this would create the desired result in the example of Figure 4, because the total count of intersected edges would then be one. P3

P2 Q(0|0)

P1

count and the actual intersection count of the algorithm. It is clear by looking at the table that the algorithm satisfies the desired result for each possible scenario. Therefore the auxiliary line is indeed a correct substitute for the original edges. This leads to the conclusion that the algorithm provided can correctly determine if there is an even or an odd number of intersections with the polygon.  Time complexity All n vertices of the polygon are visited once during the translation and the first three steps of the algorithm. All other computations like step four can be completed within constant time. Therefore the time complexity for this algorithm is O (n). 

3

EXAMPLE

The algorithm is applied to the sample complex polygon P in Figure 5. P2

Figure 4: The auxiliary edge between P1 and P3 does not intersect the positive x-axis. P5

The question remains if this method always yields the correct result under all possible circumstances. If the skipped vertex lies on the negative x-axis then the auxiliary line will not be considered for intersection, since none of the original edges could have intersected the positive x-axis. So all cases that have to be looked at involve one or more vertices on the positive x-axis. The skipped vertices can never change from the positive to the negative x-axis since this would have been caught in the first step of the algorithm. So, all auxiliary edges that intersect the x-axis are created from the following order of vertices: Pu which does not lie on the x-axis, Pv 1 which does lie on the positive x-axis and Pw which does not lie on the xaxis. Each of the vertices Pu and Pw can lie in one of the four quadrants around the point Q and therefore there exist 4 × 4 = 16 different versions of the auxiliary edge that have to be considered. This number can be further reduced to 10 because six of these versions are created by switching start and end vertex and do not have to be considered as a separate case. All possibilities of the locations of Pu and Pw are shown in Table 2 as well as the desired intersection 1 Of

course there could be more than just one vertex on the x-axis but they are all skipped and the same auxiliary edge is created no matter how many additional vertices are on the x-axis.

P3 Q(0|0)

P6

P1

Figure 5: Sample complex polygon P.

1. Q lies neither on any vertex nor edge. 2. The start vertex Ps is P1 in this example. 3. All intersection and substitution steps can be found in Table 1. 4. Since the count of intersections with the x-axis is odd the result is inside.

4

IMPLEMENTATION

The translation of P can be done together with the first step of the algorithm by moving the ver-

P4

Table 1: Intersection and substitution steps.

Pu Pv Pw x-axis for intersection operation Inters. P1 − P2 positive no P2 (P3 , P4 ) P5 complete no P5 P6 P1 complete yes tices by the negative x- and y-values of Q. The geometric helper functions to intersect line segments can be found in (Stueker, 1999) which are an improved version of (Sedgewick, 1992). Our reference implementation of the even-odd algorithm for complex polygons presented in this paper is available as open source: http://github.com/pglauner/ point_in_polygon.

5

APPENDIX Table 2: Possible cases when creating an auxiliary line (blue). The values q1 to q4 correspond to the quadrants of a cartesian coordinate system (q1 : x > 0 ∧ y > 0, q2 : x < 0 ∧ y > 0, ...). Rows that are just gained by switching the vertices Pu and Pw are omitted due to symmetry.

Example

Pu Pw

q1q1

Pv

Q(0|0)

even

no

even

no

odd

yes

odd

yes

even

no

odd

yes

odd

yes

even

no

even

no

even

no

Pw Pu

q1q2

Pv

Q(0|0) Pu

Pv

Q(0|0)

CONCLUSIONS q1q3

The implementation of point-in-polygon algorithms for complex polygons is error-prone since there are many special cases to be considered. We presented a correct even-odd algorithm that prevents special cases by substituting a sequence of edges with an auxiliary edge. The correctness of the algorithm has been proven for all possible polygons, including complex polygons. The algorithm is also timeefficient since it is O (n) for polygons with n vertices.

Desired countInters. Pw

Pu

Pw Pu

Pv

Q(0|0)

q1q4

Pw Pu

Q(0|0)

Pw

q2q2

Pv Pu

Q(0|0)

REFERENCES Foley, J. D., van Dam, A., Feiner, S. K. and Hughes, J. F. (1990). Computer Graphics: Principles and Practice. The Systems Programming Series. 2nd Edition. Addison-Wesley, Reading. Hormann, K. and Agathos, A. (2001). The point in polygon problem for arbitrary polygons. Computational Geometry, 20(3):131–144. Schirra, S. (2008). How reliable are practical pointin-polygon strategies? Algorithms - ESA 2008, 5193:744–755. Sedgewick, R. (1992). Algorithms in C++. 1st Edition. Addison-Wesley Professional. Stueker, D. (1999). Line segment intersection and inclusion in a polygon. Elementary geometric methods. Tverberg, H. (1980). A proof of the jordan curve theorem. Bulletin of the London Mathematical Society, 12(1):34–38. Zalik, B. and Kolingerova, I. (2001). A cell-based pointin-polygon algorithm suitable for large sets of points. Computers and Geosciences, 27(10):1135 – 1145.

q2q3 P

Pv w

Pu

Q(0|0) Pv

q2q4

Pw Q(0|0) Pu

q3q3

Pv

Pw Q(0|0) Pv

Pu

q3q4

Pw Q(0|0) Pv Pu

q4q4

Pw

A Simple and Correct Even-Odd Algorithm for the Point-in ... - ORBi lu

linear time and prove its correctness for all possible points and polygons. We furthermore provide ... (b) Depending on the course of step (a) one of the following steps is taken: .... Computer Graphics: Principles and Prac- tice. The Systems ...

3KB Sizes 1 Downloads 225 Views

Recommend Documents

Mining Families of Android Applications for ... - ORBi lu - UNI LU
Figure 2: Steps of our families mining approach. Package-based Categorization. In Android, each app is uniquely specified through a full Java-language-style pack- age name. This package can be recognized because it is declared in the app meta-data. A

Ergodicity and Gaussianity for spherical random fields - ORBi lu
hinges on the fact that one can regard T as an application of the type T: S2 ..... analysis of isotropic random fields is much more recent see, for instance, Ref. ...... 47 Yadrenko, M. I˘., Spectral Theory of Random Fields Optimization Software, In

Ergodicity and Gaussianity for spherical random fields - ORBi lu
the cosmic microwave background CMB radiation, a theme that is currently at the core of physical ..... 14 , we rather apply some recent estimates proved in Refs.

Feature Location Benchmark for Software Families using ... - ORBi lu
Java Development tools are shared by most of the packages. There are also common features for all the packages, like the Equinox features that implement the core functionality of the Eclipse architecture. The online documentation of each release prov

Gaussian Phase Transitions and Conic Intrinsic Volumes - ORBi lu
Nov 23, 2014 - role in convex optimization for the recovery of structured unknowns ...... Our main tool will be the powerful “Master Steiner Formula” stated in [32 ...

Gaussian Phase Transitions and Conic Intrinsic Volumes - ORBi lu
Nov 23, 2014 - of this point), in the case of the so-called descent cones arising in convex optimisation, the concentration of the distribution of VC around δC ...

Name Suggestions during Feature Identification: The ... - ORBi lu
Sep 23, 2016 - Word clouds gained momentum in the Web as aggrega- tors of activity, as a means to measure popularity, and ... element (e.g. the name of a java method). We overview how the relevant adapters to our .... Draw application are 12 variants

Prediction of Chromatin Accessibility in Gene-Regulatory ... - ORBi lu
Global mapping of protein-DNA interactions in vivo by digital genomic ... Characterization of the Contradictory Chromatin Signatures at the 3′ Exons of Zinc ...

This article appeared in a journal published by Elsevier. The ... - ORBi lu
Stochastic Processes and their Applications 121 (2011) 793–812 ...... C. Zheng, Multi-dimensional Gaussian fluctuations on the Poisson space, Electron.

This article appeared in a journal published by Elsevier. The ... - ORBi lu
Authors requiring further information regarding ... Available online 22 December 2010 ..... (with stationary increments) with covariance structure given by. E[Bt Bs] = 1. 2(|t| ... Let {ek,k ≥ 1} be a complete orthonormal system in H. Given f ∈ H

A Simple Algorithm for Clustering Mixtures of Discrete ...
mixture? This document is licensed under the Creative Commons License by ... on spectral clustering for continuous distributions have focused on high- ... This has resulted in rather ad-hoc methods for cleaning up mixture of discrete ...

A simple algorithm for decoding Reed-Solomon codes ...
relation to the Welch-Berlekamp [2] and Euclidean algorithms [3], [4] is given. II. DEFINITIONS AND NOTATIONS. Let us define the (n, k, d) Reed-Solomon (RS) code over GF(q) with length n = q − 1, number of information symbols k, designed distance d

A Simple Distributed Power Control Algorithm for ...
the following advantages: 1) the operations of each SU are simple and ... It is proved that, the CR network with this simple algorithm ...... Wireless Commun., vol.

A Fast and Simple Surface Reconstruction Algorithm
Jun 17, 2012 - Octree decomposition. Root cell smallest bounding cube of P. Splitting rule split a splittable leaf cell into eight children. Balancing rule split a leaf cell C if it has a neighbor C/ s.t. lC < lC /2. Apply the two rules alternately u

A Simple, Fast, and Effective Polygon Reduction Algorithm - Stan Melax
Special effects in your game modify the geometry of objects, bumping up your polygon count and requiring a method by which your engine can quickly reduce polygon counts at run time. G A M E D E V E L O P E R. NOVEMBER 1998 http://www.gdmag.com. 44. R

the matching-minimization algorithm, the inca algorithm and a ...
trix and ID ∈ D×D the identity matrix. Note that the operator vec{·} is simply rearranging the parameters by stacking together the columns of the matrix. For voice ...

A Formal Framework for the Correct-by-construction ...
to while building control models. These features helped the engineers to go back and forth between high level models and implementations. This is essential in ...

Internet Appendix for - Lu Zhang
∗Fisher College of Business, The Ohio State University, 820 Fisher Hall, 2100 Neil ... †Lindner College of Business, University of Cincinnati, 405 Lindner Hall, ...

S-SEBI: A Simple Remote Sensing Algorithm to ...
Aug 23, 1997 - M SatlI”A IE:(j-OS:6 aql ql!M puodsano:, ... xnv waq I!os. PIS. SES b8b. 96b. 66b. Z8b ..... Daughtry, C.S.T., Kustas, W.P., Moran, M.S.. Pinter, P.J. ...

A Simple Linear Ranking Algorithm Using Query ... - Research at Google
we define an additional free variable (intercept, or benchmark) for each ... We call this parameter .... It is immediate to apply the ideas here within each category. ... international conference on Machine learning, pages 129–136, New York, NY, ..

On a Simple Randomized Algorithm for Finding a 2 ... - Semantic Scholar
We analyze the performance of a simple randomized algorithm for finding 2-factors in directed. Hamiltonian graphs of out-degree at most two and in undirected Hamiltonian graphs of degree at most three. For the directed case, the algorithm finds a 2-f

On a Simple Randomized Algorithm for Finding a 2 ... - Semantic Scholar
However, they left open the question of whether an Hamiltonian path/cycle or even a long path/cycle can be found in sparse random graphs, e.g., graphs of degree 3. Our result on 3-regular random Hamiltonian graphs improves on a result of Karger et al

On a Simple Randomized Algorithm for Finding a 2 ...
note that the probability space is determined both by the space of all such random graphs and the random choices made by the algorithm). Given a undirected ...

Variable selection for dynamic treatment regimes: a ... - ORBi
will score each attribute by estimating the variance reduction it can be associ- ated with by propagating the training sample over the different tree structures ...