ArticlePDF Available

Optimal Reliable Point-in-Polygon Test and Differential Coding Boolean Operations on Polygons

Authors:

Abstract

This paper provides a full theoretical and experimental analysis of a serial algorithm for the point-in-polygon test, which requires less running time than previous algorithms and can handle all degenerate cases. The serial algorithm can quickly determine whether a point is inside or outside a polygon and accurately determine the contours of input polygon. It describes all degenerate cases and simultaneously provides a corresponding solution to each degenerate case to ensure the stability and reliability. This also creates the prerequisites and basis for our novel boolean operations algorithm that inherits all the benefits of the serial algorithm. Using geometric probability and straight-line equation F (P) = ( yi − yi + 1 )( xp − xi ) − ( yi − yp )( xi + 1 − xi ) , it optimizes our two algorithms that avoid the division operation and do not need to compute any intersection points. Our algorithms are applicable to any polygon that may be self-intersecting or with holes nested to any level of depth. They do not have to sort the vertices clockwise or counterclockwise beforehand. Consequently, they process all edges one by one in any order for input polygons. This allows a parallel implementation of each algorithm to be made very easily. We also prove several theorems guaranteeing the correctness of algorithms. To speed up the operations, we assign each vector a number code and derive two iterative formulas using differential calculus. However, the experimental results as well as the theoretical proof show that our serial algorithm for the point-in-polygon test is optimal and the time complexities of all algorithms are linear. Our methods can be extended to three-dimensional space, in particular, they can be applied to 3D printing to improve its performance.
symmetry
S
S
Article
Optimal Reliable Point-in-Polygon Test and
Differential Coding Boolean Operations on Polygons
Jianqiang Hao * ID , Jianzhi Sun, Yi Chen, Qiang Cai and Li Tan
Beijing Key Laboratory of Big Data Technology for Food Safety, Beijing Technology and Business University,
Beijing 100048, China; sunjz@th.btbu.edu.cn (J.S.); chenyi@th.btbu.edu.cn (Y.C.); caiq@th.btbu.edu.cn (Q.C.);
tanli_913@sina.com (L.T.)
*Correspondence: Bshjq@vip.163.com; Tel.: +86-10-6898-5704
Received: 8 August 2018; Accepted: 7 October 2018; Published: 11 October 2018


Abstract:
This paper provides a full theoretical and experimental analysis of a serial algorithm for the
point-in-polygon test, which requires less running time than previous algorithms and can handle all
degenerate cases. The serial algorithm can quickly determine whether a point is inside or outside a
polygon and accurately determine the contours of input polygon. It describes all degenerate cases and
simultaneously provides a corresponding solution to each degenerate case to ensure the stability and
reliability. This also creates the prerequisites and basis for our novel boolean operations algorithm
that inherits all the benefits of the serial algorithm. Using geometric probability and straight-line
equation
F(P)=(yiyi+1)(xpxi)(yiyp)(xi+1xi)
, it optimizes our two algorithms that
avoid the division operation and do not need to compute any intersection points. Our algorithms are
applicable to any polygon that may be self-intersecting or with holes nested to any level of depth.
They do not have to sort the vertices clockwise or counterclockwise beforehand. Consequently, they
process all edges one by one in any order for input polygons. This allows a parallel implementation of
each algorithm to be made very easily. We also prove several theorems guaranteeingthe correctness of
algorithms. To speed up the operations, we assign each vector a number code and derive two iterative
formulas using differential calculus. However, the experimental results as well as the theoretical
proof show that our serial algorithm for the point-in-polygon test is optimal and the time complexities
of all algorithms are linear. Our methods can be extended to three-dimensional space, in particular,
they can be applied to 3D printing to improve its performance.
Keywords: 3D printing; point-in-polygon; boolean operations; code; probability; parallel
1. Introduction
1.1. The Point-in-Polygon Test
Let
P
be a point and
S
a polygon on a plane. In computer graphics, finding whether
P
is inside,
outside, or on the boundary of
S
is a fundamental problem, which is called the point-in-polygon
test [
1
], point in polygon test [
2
], or inside-outside test [
3
] in different studies. In this paper, we call
it the point-in-polygon test. Throughout this paper, let
S
denote a complex polygon that may be
self-intersecting, concave, or have holes nested to any level of depth.
Currently, many algorithms are available for the point-in-polygon test. Jiménez et al.
[1]
improved
the triangle-based algorithm and did not require any preprocessing, decomposition, or feature
classification. However, they are sensitive to whether a polygon is clockwise or counterclockwise
oriented and cannot be applied to a self-intersecting polygons or efficiently cope with degenerate cases
(see Figure 1).
Symmetry 2018,10, 477; doi:10.3390/sym10100477 www.mdpi.com/journal/symmetry
Symmetry 2018,10, 477 2 of 26
(a) (b) (c)
(d) (e) (f)
(g)
Figure 1.
A comparison of several algorithms for the point-in-polygon test. Apart from (
b
),
the remaining tests, including (
c
g
), all contain bugs that can be observed by enlarging them: (
a
) the
initial contour; (
b
) using Algorithm 1; (
c
) using the algorithms of Jiménez et al.
[1]
; (
d
) using Half-plane
testing; (
e
) using Barycentric coordinates; (
f
) using Spackman barycentric; and (
g
) using Efficient
boundary [2].
Presently, there is no ray-intersection algorithm that can handle all degenerate cases in which the
tested point
P
is on
S
(the boundary of
S
). This includes the eight kinds of degenerate cases of the
26 kinds of positional relationships existing between
PiPi+1and
PQ , as shown in Figure 2a,b,p–u.
Despite many efforts [
2
,
4
], these approaches have the same problems as previous algorithms with
degenerate cases (see Figure 1g). A detailed theoretical analysis and the proof of the correctness of
the method for the point-in-polygon test are missing. In addition, apart from [
4
6
], few algorithms
do not need to perform calculations for intersection points and costly division operations. However,
these algorithms overlook some problems, such as how to identify a point lying on
S
and apply the
point-in-polygon test for a polygon containing nested holes, as shown in Figure 3a. No attempt to
optimize the algorithms for computing speed has been done.
Based on current knowledge, Haines [
4
,
5
] achieved the best performance. This algorithm is based
on the ray intersection algorithm. This paper examines each aspect of the problem, including the
various advantages and disadvantages of the previous algorithms, and provides a complete theoretical
and experimental analysis. Finally, it presents a new algorithm for the point-in-polygon test that can
handle all degenerate cases.
Symmetry 2018,10, 477 3 of 26
Pi
Pi+1
PQ
(a)
PiPi+1
PQ
(b) Pi
Pi+1
PQ
(c)
Pi
Pi+1
PQ
(d)
PiPi+1
PQ
(e)
Pi
Pi+1
PQ
(f) Pi
Pi+1
PQ
(g)
Pi
Pi+1
PQ
(h)
Pi
Pi+1
PQ
(i)
Pi
Pi+1
PQ
(j)
Pi
Pi+1
PQ
(k)
Pi
Pi+1
PQ
(l)
Pi
Pi+1
PQ
(m)
Pi
Pi+1PQ
(n)
Pi
Pi+1
PQ
(o)
Pi
Pi+1PQ
(p)
Pi
Pi+1(P)Q
(q)
Pi
Pi+1
PQ
(r)
Pi
Pi+1
PQ
(s)
Pi
Pi+1(P)
Q
(t)
Pi(P)
Pi+1
Q
(u)
Pi
Pi+1
P
Q
(v)
PiPi+1
P
Q
(w) Pi
Pi+1
PQ
(x)
Pi
Pi+1
PQ
(y)
Pi
Pi+1
PQ
(z)
Figure 2.
A detailed description of the 26 kinds of positional relationships existing between
PiPi+1
and
PQ
, and the corresponding processing results. Here,
k
is the variable to accumulate the total number
of intersections made by
PQ
and all edges of
S
: (
a
)
P
is on
PiPi+1
; (
b
)
P
is on
PiPi+1
; (
c
)
k=k+
1;
(
d
)
k=k+
1; (
e
)
k
does not change; (
f
)
k
does not change; (
g
)
k
does not change; (
h
)
k
does not change;
(
i
)
k=k+
1; (
j
)
k=k+
1; (
k
)
k
does not change; (
l
)
k
does not change; (
m
)
k
does not change; (
n
)
k
does not change; (
o
)
k
does not change; (
p
)
P
is on
PiPi+1
; (
q
)
P
is on
PiPi+1
; (
r
)
P
is on
PiPi+1
; (
s
)
P
is
on
PiPi+1
; (
t
)
P
is on
PiPi+1
; (
u
)
P
is on
PiPi+1
; (
v
)
k
does not change; (
w
)
k
does not change; (
x
)
k
does
not change; (y)kdoes not change; and (z)kdoes not change.
Symmetry 2018,10, 477 4 of 26
(a) (b) (c)
Figure 3.
The point-in-polygon test for polygon containing nested holes: (
a
) contour containing nested
holes; (b) using Algorithm 1; and (c) using crossings multiply [5].
1.2. Boolean Operations
In computer graphics, boolean operations [
3
] on general polygons occur frequently in applications.
The aim of boolean operations on polygons is to extract all contours of the resultant polygons or obtain
all inner regions enclosed by the resultant polygons. If the goal is the latter, after the algorithm has
extracted all contours of the resultant polygons, it would need to perform additional processing steps,
such as scan-conversion or area-filling.
A traditional algorithm should follow the five main steps below: computing, ordering, traversing,
tracking intersection points, and establishing contours. When degenerate cases occur, Weiler
[7]
would
require special steps to be taken to deal with them. A more detailed discussion about the issue is
beyond the scope of this article. The main problem encountered by boolean operation algorithms is
that, because any two edges from different input polygons may overlap each other, the calculation of
intersection points becomes more complicated and may cause program instability.
This paper considers the point-in-polygon test and the rasterization algorithm, and presents a
novel method that can simultaneously perform both tasks, extracting the polygon boundaries and
obtaining the interior regions. Our method can be applied to 3D printing [
8
] to improve its performance.
We compare this method with that in [
9
], and find that there are some commonalities, but they are
actually considerably different.
1.3. The Major Contributors of This Paper
The major contributions of this paper are as follows.
1.
For the point-in-polygon test, it provides a full theoretical and experimental analysis and presents
the serial Algorithm 1, which requires less running time than previous algorithms, can handle all
degenerate cases, and can both quickly determine whether a point is inside or outside a polygon
and accurately determine the contours of input polygon (see the example in Figure 3b).
2.
It describes all degenerate cases and provides corresponding solutions to ensure the stability
and reliability (see Figure 2). This also creates the prerequisites and basis for our boolean
operations algorithm.
3. It also presents a novel Algorithm 2for boolean operations, as shown in Figure 4.
4.
Our algorithms avoid the division operation and expensive intersection calculations, do not have
to sort the vertices clockwise or counterclockwise beforehand, process all edges one by one in
any order for an input polygon, are parallelizable because their many operations can be done in
parallel, and are applicable to any polygons, including self-intersecting polygons or those with
holes nested to any level of depth (see Figure 3).
Symmetry 2018,10, 477 5 of 26
5.
To speed up boolean operations, it provides each vector a number code (see Figure 2) and derives
two iterative formulas (Equations (4) and (5)) using differential calculus.
6.
A detailed theoretical and experimental analysis of Algorithms 1and 2, including the proofs of
their correctness (see Theorems 14), are shown.
7. Using geometric probability and Equation (3), it optimizes all algorithms.
π1π2
(a)
π1π2
(b)
π1π2
(c)
π1π2
(d)
Figure 4.
Boolean operations of two polygons
π1and π2
: (
a
) polydons
π1and π2
; and (
b
)
π1π2
;
(c)π1π2; (d)π1π2.
2. Related Work
2.1. The Point-in-Polygon Test
By examining theoretical principles and technical features, we can classify the existing algorithms
into the following five categories:
Ray-intersection algorithm
In many studies, this type of algorithm is called the ray–crossing [
2
], crossings–count [
4
],
odd–even [
3
], odd parity [
3
], or even–odd [
2
,
10
] algorithm because the structures and principles
of the different algorithms are all slightly different. However, virtually all of these algorithms evolved
from the ray-intersection algorithm, which works by shooting a ray
PQ
in any direction (see Figure 5;
in general, this is done parallel to the
x
-axis) from the test point
P
to infinity and counts the number
of intersections between the ray
PQ
and all edges of input polygon. Wang et al.
[11]
classified edges
into layers and calculated the number of intersections, but did not apply to self-intersecting polygons.
Symmetry 2018,10, 477 6 of 26
Moreover, the time complexities of the preprocessing steps ranges from
O(n)
to
O(n2)
, depending on
the polygon shape and the test direction.
PQ
T
Pn5
Pn4
Pn3
Pn2
Pn1
Pn
P1
P2
P3
Pi
Pi
Pi+1
Pi+1
Figure 5. A polygon Sand ray
PQ,
PT.
If the boundary curve is represented as a spline curve, then the ray-intersection algorithm can be
easily modified as one only needs to compute the intersection points with a spline curve and that can
be easily achieved by efficient root-finding algorithms [1215].
The paper considers the multiplicity of the ray-vs.-edge intersection as shown in Figure 2c–z,
in particular in Figure 2c–j. Further, the proof of Lemma 1also provides relevant evidence and reasons
in detail.
Sum of angles algorithm
The sum of angles algorithm [
1
] is equivalent to the angle summation algorithm [
2
], which needs
to calculate the angle sum of a polygon. Thus, the algorithms carry out trigonometric operations that
lead to floating-point operations and have substantial time and space costs.
Winding number algorithms
The winding number algorithm [
2
], also called the nonzero winding number algorithm [
3
],
includes two approaches: the first is an improved algorithm from the sum of angles that introduces the
cross and dot product operations of two vectors and thereby avoids angle calculations [
3
]. However, if
the two vectors are parallel or vertical, the calculations would fail. In addition, if the input polygon is
self-intersecting or has holes, the results would be incorrect. By improving the even-odd algorithm
step by step, the second approach shows the even-odd and the nonzero winding number algorithms
are identical except in their interpretation of the intersection count [2].
Grid algorithm
Gombos
˘
i and Žalik
[16]
considered a polygon as a group of grid cells and used
O(nn)
time
to first preprocess a given polygon. Following the rule in [
16
], the quasi-closest point algorithm
must also be preprocessed to subdivide the bounding rectangle of a polygon into uniform-sized
cells [
17
]. However, this method can only be used on raster-based problems and cannot be used on
self-intersecting polygons.
Symmetry 2018,10, 477 7 of 26
Decomposition algorithm
The decomposition algorithms with
O(nlog n)
time complexity must perform preprocessing to
partition an input polygon into a series of simpler components, including trapezoids (swaths) [
18
],
convex sub-polygons, quad meshes, or triangles (tri-cones) [
1
]. The multi-
L-REP
[
19
] and the
EL-REP
[
20
] algorithms extended the
LREP
decomposition algorithm. At worst, the latter has time
complexity O(log2n).
2.2. Boolean Operations
Many available algorithms exist for boolean operations, but some of them have limitations
on the shape of input polygons. For example, Andreev
[21]
required a rectangular clip polygon,
while Rappoport
[22]
required a convex clip polygon. Greiner and Hormann
[23]
handled degenerate
cases by perturbing the position of the vertex, but, by doing so, it required some additional
computational overhead, which makes its running time undeterminable. Weiler
[7]
, Weiler and
Atherton
[24]
, and Andreev
[21]
permitted any clip or subject polygons to be concave or with holes,
but could not handle self-intersecting polygons. Vatti
[25]
and Greiner and Hormann
[23]
could
perform boolean operations for any clip and subject polygons including those that are self-intersecting,
concave, or with holes.
Vatti
[25]
used the plane sweep technique [
9
]. Martínez et al.
[26]
subdivided the edges of
polygons at the points of intersection. Therefore, the algorithm must be modified when it encounters
self-intersecting or unexpected input polygons, such as two edges overlapping.
Based on simple chains [
27
], Rivero and Feito
[28]
could avoid the treatment of degenerate cases.
However, the approach is invalid for self-intersecting polygons. The execution time of [
29
] is less than
one-third of that of [
28
]. Weiler
[7]
provided an improvement over [
24
]. However, Weiler
[7]
could not
handle self-intersecting polygons.
Graysmith and Shaw
[30]
performed boolean operations by dividing the geometric shape into
a set of meshes. Although the plane sweep algorithm [
9
] attempts to do this, it does not satisfy the
necessary conditions.
3. The Point-in-Polygon Test Principle
Definition 1.
A polygon
S
is composed of rings. A
ring
is a sequence of edges, which link together and form a
closed circuit. A ring containing all other rings is the
outer ring
of
S
, while each of the remaining rings is the
inner ring of S, also known as inner hole of S, and referred to as hole of S.
Let
S
be a closed polygon on a plane with
n
vertices
P1(x1
,
y1)
,
P2(x2
,
y2)
,
···
,
Pi(xi
,
yi)
,
···
,
Pn(xn
,
yn)
such that there exists an edge between
Pi
and
Pi+1
for all
i∈ {
1,2,
···
,
n
1
}
and between
Pn
and
P1
(see
Figure 5). Let
PiPi+1
be an edge vector of
S
. Let
P(x
,
y)
be a point on the plane. We define a function
F(x
,
y)
as follows:
F(x,y) = (xi+1xi)y(yi+1yi)x+xiyi+1xi+1yi(1)
= (yiyi+1)(xxi)(yiy)(xi+1xi). (2)
Equation (1) requires one addition, four subtractions, four multiplications, and a total of nine
operations while Equation (2) requires only five subtractions, two multiplications, and a total of seven
operations. In addition, we know that multiplication takes more time than addition or subtraction.
Therefore, to reduce the running times of our algorithms below, we always use Equation (2).
Definition 2.
Given an edge vector
PiPi+1
whose function
F(x
,
y) = (yiyi+1)(xxi)(yiy)(xi+1xi)
with Pi= (xi,yi)and Pi+1= (xi+1,yi+1).
Symmetry 2018,10, 477 8 of 26
F(x
,
y)
satisfying the Jordan property means that the edge vector
PiPi+1
divides the plane into two half
plane that are disconnected from each other (see Figure 6). Therefore,
F(x
,
y)
satisfying the Jordan property can
also be called the vector
PiPi+1satisfies Jordan property.
Pi
Pi+1
F(x,y)>0
F(x,y) = 0
F(x,y)<0
Figure 6. F(x,y)satisfies Jordan property.
Assume that
PQ
is a horizontal ray that starts at
P(xp
,
yp)
, passes through
Q(xq
,
yq)
, and extends
infinitely in the
+x
direction (see Figure 5). We define a variable
k=
0 to accumulate the total number
of intersections made by
PQ and all edges of S. By (2) for
PQ , it follows that
F(x,y) = (ypyp)(xxp)(ypy)(xqxp)
= (xqxp)(yyp).
because
(xqxp)>
0,
F(x
,
y)
and
(yyp)
are either all positive or all negative. For
Pi(xi
,
yi)
with
i∈ {
1, 2,
···
,
n}
, if
yi>yp
, then
Pi
is on the left side of
PQ
(above). Otherwise, if
yi<yp
, then
Pi
is on
the right side of
PQ
(below). If neither is true, then
Pi
is on the ray
PQ
. By substituting
P(xp
,
yp)
into
Equation (2), we establish the following equation.
F(P) = (yiyi+1)(xpxi)(yiyp)(xi+1xi). (3)
Starting from the point
P1
, we traverse each edge vector
PiPi+1
of
S
exactly once and manage
to determine the positional relationship between
PiPi+1
and
PQ
. Thus, without calculating
the intersection point, one can accurately determine whether
PiPi+1
and
PQ
intersect each other.
Furthermore, if
PiPi+1
and
PQ
intersect each other, we set
k=k+
1. By carefully analyzing whether
PQ
and
PiPi+1
intersect each other, and whether the point
P
is on
PiPi+1
, it can be seen that
PQ
with
PiPi+1can have the following 26 different positional relationships (see Figure 2).
1. xi+1xpxiyi+1=yp=yi,Pis on
PiPi+1(see Figure 2a). Thus, Pis on S.
2. xixpxi+1yi=yp=yi+1,Pis on
PiPi+1(see Figure 2b. Thus, Pis on S.
3. F(P)>
0
yi=ypyp<yi+1
,
P
is on the back-left side of
PiPi+1
( the viewing direction is
along the direction of
PiPi+1
, which is maintained below. see Figure 2c). Thus,
PiPi+1
intersects
PQ and we set k=k+1.
4. F(P)<
0
yi>ypyp=yi+1
,
P
is on the front-right side of
PiPi+1
(see Figure 2d).
Thus,
PiPi+1intersects
PQ and we set k=k+1.
5. yp=yi=yi+1xi+1>xi>xp
,
PiPi+1
and
PQ
have the same direction, and the edge
PiPi+1
is
shorter than the edge
PQ
(see Figure 2e). Although both overlap each other on
PiPi+1
, we regard
them as having no intersection, and kremains unchanged.
6. yp=yi=yi+1xi>xi+1>xp
,
PiPi+1
overlaps
PQ
, and both have opposite directions
(see Figure 2f). Although both overlap on
PiPi+1
, we regard them as having no intersection,
and remain kunchanged.
Symmetry 2018,10, 477 9 of 26
7. F(P)>
0
yp=yi+1yi+1>yi
,
P
is on the front-left side of
PiPi+1
(see Figure 2g).
Although
PiPi+1
intersects
PQ
at
Pi+1
, we regard them as having no intersection, and
k
remains unchanged.
8. F(P)<
0
yp=yiyi>yi+1
,
P
is on the back-right side of
PiPi+1
(see Figure 2h).
Although
PiPi+1
intersects
PQ
at
Pi
, we regard them as having no intersection, and
k
remains
unchanged (similar to Case 7).
9. F(P)>
0
yi+1>yp>yi
,
P
is on the left-middle side of
PiPi+1
(see Figure 2i).
Thus,
PiPi+1intersects
PQ , and we set k=k+1.
10. F(P)<
0
yi>yp>yi+1
,
P
is on the right-middle side of
PiPi+1
(see Figure 2j).
Thus,
PiPi+1intersects
PQ , and we set k=k+1.
11. yp>yiyp>yi+1
,
PiPi+1
is on the right side of
PQ
(below) (see Figure 2k). Thus, both are
disjoint, and kremains unchanged.
12. F(P)>
0
yp<yiyi+1<yp
,
PQ
is also on the left side of
PiPi+1
(see Figure 2l). Thus, both are
disjoint, and kremains unchanged.
13. F(P)<
0
yp<yi+1yp>yi
,
PQ
is on the right side of
PiPi+1
(see Figure 2m). Thus, both are
disjoint, and kremains unchanged.
14. F(P)<
0
yp=yi+1yp>yi
,
PQ
is on the right side of
PiPi+1
(see Figure 2n). Thus, both are
disjoint, and kremains unchanged.
15. F(P)>
0
yp=yiyp>yi+1
,
PQ
is on the left side of
PiPi+1
(see Figure 2o). Thus, both are
disjoint, and kremains unchanged.
16. F(P) = 0yi<ypyp<yi+1,Pis on S
PiPi+1(see Figure 2p).
17. P=Pi+1yp>yi,Pis on S
PiPi+1(see Figure 2q).
18. P=Piyp>yi+1,Pis on S
PiPi+1(see Figure 2r).
19. F(P) = 0yi>ypyp>yi+1,Pis on S
PiPi+1(see Figure 2s).
20. P=Pi+1yp<yi,Pis on S
PiPi+1(see Figure 2t).
21. P=Piyp<yi+1,Pis on S
PiPi+1(see Figure 2u).
22. yp=yi=yi+1xi+1<xi<xp
,
PiPi+1
and
PQ
are mutually separate, and both have opposite
directions
PiPi+1(see Figure 2v). Thus, they are disjoint, and kremains unchanged.
23. yp=yi=yi+1xp>xi+1>xi
,
PiPi+1
and
PQ
are disjoint
PiPi+1
(see Figure 2w), and
k
remains unchanged.
24. F(P)<
0
yi=ypyp<yi+1
,
P
is on the back-right side of
PiPi+1
, and
PiPi+1
and
PQ
are
disjoint
PiPi+1(see Figure 2x). Thus, kremains unchanged.
25. F(P)>
0
yi+1=ypyp<yi
,
P
is on the front-left side of
PiPi+1
, and
PiPi+1
and
PQ
are
disjoint
PiPi+1(see Figure 2y). Thus, kremains unchanged.
26. yp<yiyp<yi+1
,
PiPi+1
is on the left side of
PQ
(above), and
PiPi+1
and
PQ
are disjoint
PiPi+1
(see Figure 2z). Thus, kremains unchanged.
Further, we can classify and simplify the 26 positional relationships into three classes:
1.
Class 1 includes Cases 3, 4, 9, and 10, for each of which
PiPi+1
intersects
PQ
. Therefore, we set
k=k+1.
2. Class 2 includes Cases 1, 2, and 16–21, for each of which Pis on S.
3.
Class 3 includes the remaining cases, for each of which
PiPi+1
and
PQ
are disjoint. Therefore,
k
remains unchanged.
To facilitate the analysis and processing of the problem, we give each vector involved in these
operations a number code of 1–26 such that each vector
PiPi+1
in Figure 2a–z has a number code of
1–26, which is one-to-one correspondence with Cases 126.
Symmetry 2018,10, 477 10 of 26
Definition 3.
Let
S
consist of a set of closed polygons. The minimum bounding box of
S
, denoted by
MBB
,
is the smallest box that encloses the entire S and is axis-aligned rather than oriented-aligned.
For the point-in-polygon test and boolean operations, our methodology can be divided into
two steps. The first step performs preprocessing to determine whether a point lies inside or outside the
minimum bounding box
MBB
of input polygons. If the point lies within the
MBB
, we then perform
the second step to determine whether the point is inside or outside the polygon.
Occasionally, a user may need to handle polygons with small sizes. Clearly, the occurrence
probability of input polygons with limited sizes, involved in operations, is low. For example, let us
consider the possibility of occurrence that the input polygon has vertices (0,0), (1,1), (1,1
t
) with small
t. It is true that this probability is tiny. Furthermore, it follows that the probability of a point lying on
the boundary of a polygon is approximately zero, since the actual width the edge of a polygon in use
is not 0.
To optimize our algorithms, we can let the program automatically calculates the probabilities of
a point lying within and outside a polygon. If the probability of the point lying within the polygon
is greater than the probability of the point lying outside the polygon when the minimum bounding
box
MBB
has been calculated, we let the program first decide whether the point is within the polygon,
and then decide whether the point is outside the polygon. Conversely, if the probability of the point
lying outside the polygon is greater than the probability of the point lying within the polygon, we let
the program first decide whether the point is outside the polygon, and then decide whether the point
is within the polygon. Since the probability of the point lying on the boundary of the polygon is
approximately zero, we let the program at last decide whether the point is on the polygon. In this
paper, we assume that the probability of a point lying within a polygon is greater than the probability
of the point lying outside the polygon.
Definition 4.
Let
S
be a closed polygon and
P
a point on the plane. If
P
is on an edge of
S
, then we set
k=
1.
Otherwise, we set
k
equal to the number of intersections made by the ray
PQ
and all edges of
S
. We define
the variable
k
as the point
P
is odd-even number around
S
, referred to as the point
P
is odd-even number,
denoted by S(P).
In addition, when
P
is inside the
outer ring
of
S
(see Definition 1), if the
k
is even, then we call
P
outside
S. On the contrary, if the k is odd, then we call P inside S.
Lemma 1.
Let
S
be a closed polygon and
P
a point on the plane. By Definition 1, assume that
Γ
is a ring of
S
,
which intersects with
PQ
. Assume that
U1
,
U2
,
···
,
Ut
are all intersections produced by
PQ
and all edges of
Γ
with t 6=0. Then, the P is outside Γif and only if the t is an even.
Proof.
By the conditions of Lemma 1, we have that
U1(x1
,
y1)
,
U2(x2
,
y2)
,
···
,
Ut(xt
,
yt)
are the
intersections produced by
PQ
and all edges of
Γ
. In Figure 2a–z, it can be observed that
PQ
and just
the
PiPi+1in Figure 2c,d,i or Figure 2j can have intersections.
Let us first prove the necessity. Suppose the Pis outside S.
At first, we let
U=U1
and
k=
1. For the intersections
U
, our proof is divided into the following
two main steps.
1.
If the intersection of
PQ
and the
PiPi+1
(denoted by
I9J10
) in Figure 2i or Figure 2j is
U
, then
I9J10
crosses
PQ
from one side to another. Since
I9J10
belongs to the closed
Γ
, there must be another
edge
PiPi+1
in Figure 2c,d,i, or Figure 2j that also intersects with
PQ
at another different
intersection (denoted by V1). Let us assume that V1=U2. Therefore, k=k+1=2.
2.
Otherwise, if the intersection of
PQ
and the
PiPi+1
(denoted by
C3D4
) in Figure 2c or Figure 2d is
U
, then the other side connecting to
U
(denoted by
U
38) can only be the
PiPi+1
in Figure 2c–g, or
Figure 2h.
Symmetry 2018,10, 477 11 of 26
Besides, our proof for Lemma 1is divided into the following three main sub-steps.
(a)
If
U
38 is the
PiPi+1
(denoted by
C1
3D1
4
) in Figure 2c or Figure 2d, then
PQ
and
C1
3D1
4
must
produce a new intersection (denoted by
V2
), which is equal to
U
and let us assume that
V2=U2
.
U2
increases the number of intersections of
Γ
and
PQ
by 1. Therefore,
k=k+
1
=
2.
(b)
Otherwise, if
U
38 is the
PiPi+1
(denoted by
E5F6
) in Figure 2e or Figure 2f, then
E5F6
belongs to the closed
Γ
of
S
. Then, next, the other side connecting to
E5F6
(denoted by
U381) can only be the
PiPi+1in Figure 2c–g, or Figure 2h.
i.
If
U
38
1
is the
PiPi+1
(denoted by
C2
3D2
4
) in Figure 2c or Figure 2d, then
PQ
and
C2
3D2
4
must produce a new intersection, which is different from
U
(denoted by
V3
).
Let us assume that V3=U2. Therefore, k=k+1=2.
ii.
Otherwise, if
U
38
1
is the
PiPi+1
(denoted by
E1
3F1
4
) in Figure 2e or Figure 2f,
the procedure returns to Step 2b above. Since the number of edges of
Γ
is limited,
this iteration will end after finite number of steps.
iii.
Otherwise, if
U
38
1
is the
PiPi+1
(denoted by
G7H8
) in Figure 2g or Figure 2h,
then the path
C3D4E5F6G7H8
crosses
PQ
from one side to another.
Since
G7H8
belongs to the closed
Γ
of, there must be another edge
PiPi+1
in Figure 2c,d,i, or Figure 2j that also intersects with
PQ
at another different
intersection (denoted by
V4
). Let us assume that
V4=U2
. Therefore,
k=k+
1
=
2.
(c)
Otherwise, if
U
38 is the
PiPi+1
(denoted by
G7H8
) in Figure 2g or Figure 2h, the path
C3D4G7H8
crosses
PQ
from one side to another. Since
G7H8
belongs to the closed
Γ
, there must be another edge
PiPi+1
in Figure 2c,d,i, or Figure 2j that also intersects
with
PQ
at another different intersection (denoted by
V5
). Let us assume that
V5=U2
.
Therefore, k=k+1=2.
From the intersection sequence
U1
,
U2
,
···
,
Ut
, we remove
U1
and
U2
, and let
U=U3
and
k=k+1
. Then, we repeat the above Steps 1and 2until there is no intersection in the intersection
sequence U1,U2,··· ,Ut. Finally, it can be seen that the conclusion of Lemma 1holds.
Let us prove the sufficiency in Lemma 1. Suppose the tis even.
We use the induction for m=t/2.
If
m=
1, by Figure 2a–z, suppose the pair of intersections are
U1
and
U2
. Further, let us assume
that
U=U1
, then
U2
must belong to an element of
S1={V1
,
V2
,
V3
,
V4
,
V5}
(see the proof of the
necessity of Lemma 1). Regardless of which element
U2
belongs to
S1
, eventually it can be seen that
P
is outside Γ. Therefore, it is established that Pis outside Γfor m=1.
Suppose that Pis outside Γfor m=k.
Let us prove that Pis outside Γfor m=k+1.
Suppose that there exists a point that is outside
Γ
for
m=k
; it can be asserted that there is always
a point P0(x0,y0)outside Γsuch that the number of pairs of intersection points of
P0Qand Γis k.
Assume that only the
x
coordinate of the two intersections
U1(x1
,
y1)
and
U2(x2
,
y2)
is less than
the xcoordinate of any other intersection, and U1(x1,y1)and U2(x2,y2)meet condition x1x2.
Further, let us assume that
U=U1
, then
U2
must belong to an element of
S1={V1
,
V2
,
V3
,
V4
,
V5}
(see the proof of the necessity of Lemma 1). Regardless of which element
U2
belongs to
S1
, on the
ray
PQ
, there is only one pair of intersections
U1(x1
,
y1)
and
U2(x2
,
y2)
produced by
PP0
and all edges
of Γ. Therefore, it can be inferred that Pis outside Γ.
Eventually, it can be seen that
P
is outside
Γ
for
m=k+
1. Therefore, it is established that
P
is
outside Γfor m=k+1.
Symmetry 2018,10, 477 12 of 26
Lemma 2.
Let
S
be a closed polygon and
P
a point on the plane. By Definition 1, assume that
Γ
is a ring of
S
,
which intersects with
PQ
. Assume that
U1
,
U2
,
···
,
Ut
are all intersections produced by
PQ
and all edges of
Γ
with t 6=0. Then, P is inside Γif and only if the t is an odd.
Proof.
Let us first prove the necessity. Suppose the
P
is inside
Γ
. We now prove the necessity of
Lemma 2by contradiction. Assume by contradiction that the tis an even.
Then, by the sufficiency of Lemma 1, it follows that the
P
is outside
Γ
, leading to a contradiction
with the constraint that the
P
is inside
Γ
. Therefore, the assumption that the
t
is an even does not hold.
The necessity of Lemma 2immediately follows.
Next, let us prove the sufficiency in Lemma 2. Suppose the
t
is an odd. We now prove the
sufficiency of Lemma 2by contradiction. Assume by contradiction that the Pis outside Γ.
Then, by the necessity of Lemma 1, it is clear that the
t
is an even, leading to a contradiction with
the constraint that the
t
is an odd. Therefore, the assumption that the
P
is outside
Γ
does not holds.
The sufficiency of Lemma 2immediately follows.
Theorem 1.
Let
S
be a closed polygon and
P
a point on the plane. If
P
is on the boundary of
S
, then the point
P
is odd-even number
S(P) =
1. Otherwise, if
P
is outside
S
, then
S(P)
is 0 or even. Otherwise, if
P
is inside
S, S(P)is odd.
Proof. Our proof of Theorem 1is divided into the following four main steps.
1.
If
P
is on the boundary of
S
, then
P
is on an edge vector of
S
. Therefore, by Definition 4, it follows
that the point Pis odd-even number S(P) = 1.
2.
Otherwise, if
P
is outside
S
and
PQ
does not intersect any edge of
S
, then
S(P) =
0 by Definition 4.
3.
Otherwise, if
P
is outside the
outer ring
of
S
(see Definition 1) and condition the number of
intersections
>
0 holds between
PQ
and all edges of
S
, below let us prove that the number of
intersections is even.
Let us assume that
PQ
intersects with the
rings Γ1
,
Γ2
,
···
,
Γl
of
S
. By Lemma 1, it follows that
the number of intersections produced by
PQ
and all edges of
ring Γi
is an even for
i=
1, 2,
···
,
l
.
Therefore, the total number of intersections produced by
PQ
and the
rings Γ1
,
Γ2
,
···
,
Γl
of
S
is
an even and this conclusion that S(P)is even holds.
4.
Otherwise, if
P
is inside the
outer ring
of
S
(see Definition 1). Let us assume that
Γ1
,
Γ2
,
···
,
Γl
are all rings of
S
each of which the
P
is outside. Similarly, let us assume that
γ1
,
γ2
,
···
,
γm
are all
rings of Seach of which the Pis within.
By the necessity of Lemma 1, it follows that the number of intersections between
PQ
and
Γi
is an
even for
i=
1, 2,
···
,
l
. As a result, the total number of intersections produced by
PQ
and
Γi
is an
even with i=1, 2, ··· ,l.
Similarly, by the necessity of Lemma 2, it follows that the number of intersections between
PQ
and
γi
is an odd(denoted by
ti
) for
i=
1,2,
···
,
m
. As a result, the total number of intersections
produced by
PQ
and
γi
with
i=
1, 2,
···
,
m
is
m
i=1ti
whose parity is consistent with the parity of
m
.
Accordingly, if
m
is an even, then the
k
that denotes the number of intersections made by
PQ
and
all edges of Sis also an even, and vice versa.
Eventually, when Pis inside the outer ring of S, by Definition 4, it follows that
(a) If the kis even, then the Pis outside Sand S(P)is even.
(b) Conversely, if the kis odd, then the Pis inside Sand S(P)is odd.
Symmetry 2018,10, 477 13 of 26
Algorithm 1:
Determine whether
P
is inside, outside, or on
S
. It cannot solve the problem of
instability that can result from the comparison operations of floating-point numbers.
Input : A tested point P= (xp,yp)and the edge sequence (P1P2,P2P3,· ··,PiPi+1,· ··,
PnPn+1)of a closed polygon Swith i=1, 2, ·· · ,n.
Output : Return an integer 1, 0, or 1 depending on whether the point Pis within, outside,
or on the polygon S, respectively.
1i0; k0; f0;
2u10; v10; u20; v20;
3for i1to ndo
4v1yiyp;v2yi+1yp;
5if (v1<0and v2<0)or(v1>0and v2>0)then // Case 11 or 26
6go back to the beginning of the for-loop;
7u1xixp;u2xi+1xp;
8if v2>0and v1<=0then // Case 3, 9, 16, 21, 13, or 24
9fu1v2u2v1;
10 if f>0then // Case 3 or 9
11 kk+1; // Handle Case 3 or 9
12 else if f ==0then // Case 16 or 21.The rest are Case 13 or 24
13 return 1; // Handle Case 16 or 21
14 else if v1>0and v2<=0then// Case 4, 10, 19, 20, 12, or 25
15 fu1v2u2v1 ;
16 if f<0then // Case 4 or 10
17 kk+1; // Handle Case 4 or 10
18 else if f ==0then // Case 19 or 20.The rest are Case 12 or 25
19 return 1; // Handle Case 19 or 20
20 else if v2== 0and v1<0then// Case 7, 14, or 17
21 fu1v2u2v1;
22 if f== 0then return 1; // Case 17.The rest are Case 7 or 14
23 else if v1== 0and v2<0then// Case 8, 15, or 18
24 fu1v2u2v1;
25 if f== 0then return 1; // Case 18.The rest are Case 8 or 15
26 else if v1== 0and v2== 0then // Case 1, 2, 5, 6, 22, or 23
27 if u2<=0and u1>=0then // Case 1
28 return 1; // Handle Case 1
29 else if u1<=0and u2>=0then // Case 2.The rest are Case 5, 6, 22, or 23
30 return 1; // Handle Case 2
31 if k%2 == 0then return 0;
32 else return 1;
Symmetry 2018,10, 477 14 of 26
4. A Serial Algorithm for the Point-in-Polygon Test
In this section, we show the serial Algorithm 1for the point-in-polygon test that uses many
comparison operations of floating-point numbers. One may worry that the comparison operations
of floating-point numbers can lead to the floating point errors, which would cause the program to
run incorrectly. The results of the experiment show that this worry is superfluous (see the conclusion
of Section 7).
Now, let us present the serial Algorithm 1in detail. By using calculated values from previous
Steps 4and 7in Algorithm 1, Steps 9,15,21, and 24 calculate the variable
f
that corresponds to the
function
F
in Equation (3). The for-loop in Steps 330 handles each edge
PiPi+1
in turn and determines
which case the positional relationship between
PiPi+1and Pbelongs to, as shown in Figure 2.
Steps 56deal with Cases 11 and 26, as shown in Figure 2k,z.
Steps 813 handle Cases 3,9,16,21,13, and 24 (see Figure 2c,i,p,u,m,x). Furthermore, Step 11
corresponds to Case 3or 9, while Step 13 corresponds to Case 16 or 21. The rest correspond to Case 13
or 24.
Steps 1419 handle Cases 4,10,19,20,12, and 25 (see Figure 2d,j,s,t,l,y). Furthermore, Step 17
corresponds to Case 4or 10, while Step 19 corresponds to Case 19 or 20. The rest correspond to Case 12
or 25.
Steps 2022 handle Cases 7,14, and 17 (see Figure 2g,n,q). Furthermore, Step 22 corresponds to
Case 17. The rest correspond to Case 7or 14.
Steps 2325 handle Cases 8,15, and 18 (see Figure 2h,o,r). Furthermore, Step 25 corresponds to
Case 18. The rest correspond to Case 8or 15.
Steps 2630 handle Cases 1,2,5,6,22, and 23 (see Figure 2a,b,e,f,v,w). Furthermore, Step 28
corresponds to Case 1. Step 30 corresponds to Case 2. The rest correspond to Case 5,6,22,or23.
Algorithm 1does not clearly indicate how to deal with the remaining cases,
including Cases 58,1215, and 2225 (see Figure 2e–h,l–o,v–y). However, no matter which
of them appears,
k
does not change and
P
is not on
PiPi+1
, therefore Algorithm 1does not require any
additional process step.
From the above discussion, it follows that Algorithm 1can operate correctly under any condition
and has been optimized for speed and robustness. Finally, using
k
, by Steps 3132 Algorithm 1can
determine whether
P
is within, outside, or on
S
. Algorithm 1is parallelizable because many of its
operations can be done in parallel.
5. Boolean Operations Principle and Algorithm
In this section, we show the basic principle for boolean operations by deriving Theorems 2
and 3. Furthermore, we present a new Algorithm 2for boolean operations. Let
π1
be a polygon
with
m
vertices
P1
,
P2
,
···
,
Pm
and
m
corresponding edges
P1P2
,
P2P3
,
···
,
Pm1Pm
,
PmP1
. Let
π2
be a
polygon with
n
vertices
Q1
,
Q2
,
···
,
Qn
and
n
corresponding edges
Q1Q2
,
Q2Q3
,
···
,
Qn1Qn
,
QnQ1
.
Let S=π1or π2.
First, Algorithm 2calculates the minimum bounding box of
π1
and
π2
denoted by
MBB
.
Then, starting from the top-left corner of the
MBB
, Algorithm 2scans the
MBB
point by point,
from left to right and from top to bottom until reaching the bottom-right corner. For each point
involved, Algorithm 2determines the positional relationship between
P
(
MBB
) and
π1
and between
P(MBB) and π2.
Using differential calculus, we will derive two iterative formulas by which Algorithm 2can
quickly determine the positional relationships between a set of points and a set of polygons. For this
purpose, assume that Algorithm 2sweeps across an intermediate point
Rk= (xk
,
yk)
(
RkMBB
and
see Figure 7), and the meaning of
Q
(below) is the same as the previous definition of
Q
. Suppose that
Algorithm 2already knows if
Rk= (xk
,
yk)
is inside, outside, or on the boundary of
S
. Let
Tk=
(xmin
,
yk)
(
TkMBB
and see Figure 7),
Rk+1= (xk+1
,
yk+1) = (xk+x
,
yk)
, and
Tk+1= (xmin
,
yk+1) =
Symmetry 2018,10, 477 15 of 26
(xmin
,
yk+y)
where
xmin
is the smallest
x
-coordinate of the
MBB
. For the point
Rk
, the following
situations may occur:
1. Rkis in the top-left corner of MBB (see Figure 8).
2. Rkis on the left border of MBB (see Figure 9).
3. Rkis inside the MBB (see Figure 7).
Q1
Q2
Q3
Qk
Qk+1
Qm
P2
P3
Pi
Pi+1
Pn
P1
TkRkRk+1
Tk+1
x=1, y=1, Rk= (xk,yk)
Rk+1= (xk+1, yk),Tk+1= (xmin ,yk1)
π1
π2
Figure 7. Point Rkis inside MBB.
Q1
Q2
Q3
Qk
Qk+1
Qm
P2
P3
Pi
Pi+1
Pn
P1
Rk=TkRk+1
Tk+1
x=1, y=1
Rk+1= (xk+1, yk),Tk+1= (xmin ,yk1)
π1
π2
Figure 8. Point Rk=Tk, and Rkis at the top-left corner.
Q1
Q2
Q3
Qk
Qk+1
Qm
P2
P3
Pi
Pi+1
Pn
P1
Tk=RkRk+1
Tk+1
x=1, y=1
Rk+1= (xk+1, yk),Tk+1= (xmin ,yk1)
π1
π2
Figure 9. Point Rk=Tk, and Rkis on the left border of MBB.
Symmetry 2018,10, 477 16 of 26
Algorithm 2: Performing boolean operations on two polygons.
Input : Parameter t(the type of Boolean operation) and two closed polygons S1and S2.
They are defined by an edge array of double type. S1and S2may be arbitrarily
complex. They may be convex or concave, self-intersecting, contain holes, or be
comprised of various contours.
Output : Return integer tequal to 0 or 1. If Boolean operation is successful, return integer 1;
otherwise, return integer 0.
1k10 ;// Store the number of intersections of
PQ with S1.
2k20 ;// Store the number of intersections of
PQ with S2.
3T(0, 0);R(0, 0);// Tand Rare Tkand Rk, respectively.
4to p_Le f t (0, 0); bottom_Right (0, 0);
5Compute the top-left and the bottom-right point of S1and S2;
6Tto p_Le f t ;
7for T.y=to p_Le f t.yto bottom_Right.ydo
8RT;
9for R.x=T.xto bottom_Right.xdo
10 Determine whether Ris inside, outside, or on the boundary of S1and S2, respectively;
11 According to the result of Step 10 and value t, display R;
12 if R!=Tthen
13 if R is inside, or outside S1or S2then
//
PiPi+1=
(xi,yi)(xi+1,yi+1)
14 forall
PiPi+1of S1or S2do
15 code the coding of
PiPi+1;
16 if 3code 10 then
17 F(R)F(R) + (yiyi+1);
18 else if k1== 0or k2== 0then
19 forall
PiPi+1of S1or S2do
20 if 5code 8then
21 F(R)F(R) + (yiyi+1);
22 else if R is on the boundary of S1or S2then
23 forall
PiPi+1of S1or S2do
24 if 1code 10 then
25 F(R)F(R) + (yiyi+1);
26 else
27 if R is inside, or outside S1or S2then
28 forall
PiPi+1of S1or S2do
29 if 7code 15 then
30 F(T)F(T) + (xixi+1);
31 else if 3code 10 then
32 F(R)F(R) + (yiyi+1);
Symmetry 2018,10, 477 17 of 26
33 else if k1== 0or k2== 0then
34 forall
PiPi+1of S1or S2do
35 if 7code 15 then
36 F(T)F(T) + (xixi+1);
37 else if 5code 8then
38 F(R)F(R) + (yiyi+1);
39 else if R is on the boundary of S1or S2then
40 forall
PiPi+1of S1or S2do
41 if 7code 19 then
42 F(T)F(T) + (xixi+1);
43 else if 1code 10 then
44 F(R)F(R) + (yiyi+1);
In Case 1, the equalities
Rk=Tk= (xk
,
yk) = (xmin
,
ymax )
hold, where
xmin
is the smallest
x
-coordinate of
MBB
and
ymax
is the largest
y
-coordinate of
MBB
. Thus, we have
Rk+1=
(xk+1,yk+1) = (xk+x,yk) = (xk+x,ymax ),Tk+1= (xmin, and yk+y).
In Case 2, the equalities
Rk=Tk= (xk
,
yk)=(xmin
,
yk)
hold, where
xmin
is the smallest
x
-coordinate of
MBB
. Therefore, we have
Rk+1= (xk+1
,
yk+1) = (xk+x
,
yk) = (xmin +x
,
yk)
,
Tk+1= (xmin, and yk+y).
In Case 3, with
Rk= (xk
,
yk)
,
Tk= (xmin
,
yk)
, the condition
Rk6=Tk
holds, where
xmin
is the
smallest
x
-coordinate of
MBB
. Therefore, we have
Rk+1= (xk+1
,
yk+1)=(xk+x
,
yk)
,
Tk+1= (xmin
,
and yk+y).
In Figure 7, assume that
x=
1 and
y=
1. Then,
Rk+1= (xk+x
,
yk) = (xk+
1,
yk)
and
Tk+1= (xmin
,
yk+y) = (xmin
,
yk
1
)
. If
Rk
(
P
in Figure 2) is outside
S
and the variable
k
is 0,
then
RkQ
does not intersect
PiPi+1
for any
i=
1, 2,
···
,
m
. Thus, the positional relationships between
RkQ
and
PiPi+1
belong to Cases 58,1115, or 2226 (see Figure 2). Therefore, to determine the
positional relationships between
Rk+1Q
and
PiPi+1
, Algorithm 2only must recheck those edges whose
positional relationships with
RkQ
belong to Cases 58. Likewise, if
Tk
(
P
in Figure 2) is outside
S
and
the variable
k
is 0, to determine the positional relationships between
Tk+1Q
and
PiPi+1
, Algorithm 2
only must recheck those edges whose positional relationships with
TkQbelong to Cases 715.
If the variable
k
is even or odd, then the positional relationships between
RkQ
(
Rk
corresponds to
P
in Figure 2) and
PiPi+1
do not belong to Cases 1,2, and 1621. Therefore, to determine the positional
relationships between
Rk+1Q
and
PiPi+1
, Algorithm 2only must recheck those edges whose positional
relationships with
RkQ
belong to Cases 310. Likewise, if the variable
k
is even or odd, the positional
relationships between
TkQ
(
Tk
corresponds to
P
in Figure 2) and
PiPi+1
do not belong to Cases 1,2,
and 1621, to determine the positional relationships between
Tk+1Q
(
Tk+1
corresponds to
P
in Figure 2)
and
PiPi+1
, Algorithm 2only must recheck those edges whose positional relationships with
TkQ
belong to Cases 715.
If
Rk
(
P
in Figure 2) is on the boundary of
S
, then the positional relationships between
RkQ
and
PiPi+1
belong to Cases 1,2, or 1621. Therefore, to determine the positional relationships between
Rk+1Q
and
PiPi+1
, Algorithm 2only must recheck those edges whose positional relationships with
RkQ
belong to Case 110. Likewise, if
Tk
(
P
in Figure 2) is on the boundary of
S
, to determine the
Symmetry 2018,10, 477 18 of 26
positional relationships between
Tk+1Q
and
PiPi+1
, Algorithm 2only must recheck those edges whose
positional relationships with
TkQbelong to Cases 719.
From the above comparative analysis, it can be seen that to determine the positional relationship
between
Rk+1
and
S
, Algorithm 2does not need to recheck all edges of
S
and usually needs only to
recheck a small number of the edges whose number depends on the positional relationship between
Rk+1and S. Summarizing these findings, we get the following Theorem 2by Definition 4.
Theorem 2.
Suppose that
π
1and
π
2are two polygons. Assume that
MBB
is the minimum bounding
box of
π
1and
π
2. Let
Rk= (xk
,
yk)
be a point inside
MBB
or on the boundary of
MBB
, and assume
Tk= (xmin
,
yk)
is a point on the left border of
MBB
, where
xmin
is the smallest
x
-coordinate of
MBB
(see Figure 8). Let
Rk+1= (xk+1
,
yk+1) = (xk+x
,
yk)
and
Tk+1= (xmin
,
yk+1) = (xmin
,
yk+y)
.
Let S =π1or S =π2. Assume that x=1, and y=1.
1.
If the odd-even number
S(Rk) =
0, to calculate
S(Rk+1)
one needs only to recheck the edges of
S
belonging
to Cases 58(see Figure 2). Likewise, if the odd-even number
S(Tk) =
0, to calculate
S(Tk+1)
, one needs
only to recheck the edges of S belonging to Cases 715 (see Figure 2).
2.
If the odd-even number
S(Rk)
is even or odd, to calculate
S(Rk+1)
one needs only to recheck the edges of
S
belonging to Cases 310 (see Figure 2). Likewise, if the odd-even number
S(Tk)
is even or odd, to calculate
S(Tk+1), one needs only to recheck the edges of S belonging to Cases 715 (see Figure 2).
3.
If the odd-even number
S(Rk) =
1, to calculate
S(Rk+1)
, one needs only to recheck the edges of
S
belonging to Cases 110 (see Figure 2). Likewise, if the odd-even number
S(Tk) =
1, to calculate
T(Rk+1), one needs only to recheck the edges of S belonging to Cases 719 (see Figure 2).
Proof.
1. If the odd-even number
S(Rk) =
0, then
Rk
(
P
in Figure 2) is outside
S
and
RkQ
does not
intersect
PiPi+1
for any
i=
1, 2,
···
,
m
. Thus, the positional relationships between
RkQ
and
PiPi+1
only
belong to Cases 58,1115,or2226 (see Figure 2). Therefore, to determine the positional relationships
between
Rk+1Q
and
PiPi+1
for calculating
S(Rk+1)
, one needs only to recheck the edges of
S
belonging
to Cases 58(see Figure 2). Likewise, if the odd-even number
S(Tk) =
0, then
Tk
(
P
in Figure 2) is
outside
S
. Thus, the positional relationships between
TkQ
and
PiPi+1
only belong to Cases 58,1115,
or 2226. Therefore, to determine the positional relationships between
Tk+1Q
and
PiPi+1
for calculatint
S(Tk+1), one needs only to recheck the edges of Sbelonging to Cases 715 (see Figure 2).
2. If the odd-even number
S(Rk)
is even or odd, then the positional relationships between
RkQ
(
Rk
corresponds to
P
in Figure 2) and
PiPi+1
do not belong to Cases 1,2, and 1621. Therefore, to determine
the positional relationships between
Rk+1Q
and
PiPi+1
for calculating
S(Rk+1)
, one needs only to
recheck the edges of
S
belonging to Cases 310 (see Figure 2). Likewise, if the odd-even number
S(Tk)
is even or odd, then the positional relationships between
TkQ
(
Tk
corresponds to
P
in Figure 2) and
PiPi+1
also do not belong to Cases 1,2, and 1621. Therefore, to determine the positional relationships
between
Tk+1Q
(
Tk+1
corresponds to
P
in Figure 2) and
PiPi+1
for calculating
S(Tk+1)
, one needs only
to recheck the edges of Sbelonging to Cases 715 (see Figure 2).
3. If the odd-even number
S(Rk) =
1, then
Rk
(
P
in Figure 2) is on the boundary of
S
, then the
positional relationships between
RkQ
and
PiPi+1
only belong to Cases 1,2, or 1621. Therefore, to
determine the positional relationships between
Rk+1Q
and
PiPi+1
for calculating
S(Rk+1)
, one needs
only to recheck the edges of
S
belonging to Cases 110 (see Figure 2). Likewise, if the odd-even number
S(Tk) =
1, then
Tk
(
P
in Figure 2) is on the boundary of
S
. Thus, the positional relationships between
TkQ
and
PiPi+1
only belong to Cases 1,2, or 1621. Therefore, to determine the positional relationships
between
Tk+1Q
and
PiPi+1
for calculating
T(Rk+1)
, one needs only to recheck the edges of
S
belonging
to Cases 719 (see Figure 2).
Theorem 3.
Suppose that
π
1and
π
2are two polygons. Assume that
MBB
is the minimum bounding box of
π
1and
π
2. Let
Rk= (xk
,
yk)
be a point inside or on the edges of
MBB
, and
Tk= (xmin
,
yk)
is a point on the
Symmetry 2018,10, 477 19 of 26
left border of
MBB
, where
xmin
is the smallest
x
-coordinate of
MBB
. Let
Rk+1= (xk+1
,
yk+1) = (xk+x
,
yk)
and
Tk+1= (xmin
,
yk+1)=(xmin
,
yk+y)
(see Figures 79). Let
S=π
1or
S=π
2. Suppose that
PiPi+1
is
an edge of
S=π
1or
S=π
2with
Pi= (xi
,
yi)
and
Pi+1= (xi+1
,
yi+1)
. Assume that
x=
1and
y=
1.
If F(Rk)and F(Tk)satisfy Equation (1), then the following two iterative formulas hold.
F(Rk+1) = F(Rk) + (yiyi+1). (4)
F(Tk+1) = F(Tk) + (xixi+1). (5)
By Equations (4) and (5), if Algorithm 2already knows
F(RK)
and
F(TK)
, Algorithm 2can quickly
calculate
F(Rk+1)
and
F(Tk+1)
. Therefore, using Equations (4) and (5), one can simplify the calculation
and improve processing speed significantly.
By Theorems 2and 3, and Algorithm 1, vertex by vertex, Algorithm 2determines the positional
relationship between
Rk
and
π1
, yet between
Rk
and
π2
. Furthermore, according to the types of
boolean operations, if
Rk
is simultaneously inside
π1
and
π2
(also including on their border), then
Rk
(π1π2)
. Otherwise, if
Rk
is inside
π1
or
π2
(also including on their border), then
Rk(π1π2)
.
Otherwise, if
Rk
is both inside
π1
(also including on its border) and outside
π2
, then
Rk(π1π2)
.
Otherwise, if
Rk
is both outside
π1
and inside
π2
(also including on their border), then
Rk(π2π1)
.
Step-by-step, Algorithm 2is well able to complete the corresponding boolean operations. Algorithm 2is
a comprehensive presentation and summary for all preceding discussion.
Proof. By Equation (1), for Rk+1and Tk+1we have
F(Rk+1) = F(xk+1,yk+1) = F(xk+1, yk)
= (yiyi+1)(xk+1) + (xi+1xi)yk+xiyi+1xi+1yi
= (yiyi+1)xk+ (xi+1xi)yk+xiyi+1xi+1yi+ (yiyi+1)
=F(Rk) + (yiyi+1).
F(Tk+1) = F(xk+1,yk+1) = F(xmin ,yk+1) = F(xmin ,yk1)
= (yiyi+1)xmin + (xi+1xi)(yk1) + xiyi+1xi+1yi
= (yiyi+1)xmin + (xi+1xi)yk+xiyi+1xi+1yi(xi+1xi)
=F(Tk) + (xixi+1).
6. Complexity Analysis of Algorithms
In this section, we analyze the time and space complexities of our algorithms. First, let us
consider Algorithm 1. Assume that the number of edges of a polygon is
n
. The for-loop in
Steps 330 determines the positional relationship between point
P
and the
n
edge vectors
PiPi+1
with
i=1, 2, ··· ,n.
Steps 4and 7each perform two subtractions. One subtraction and two multiplication
calculations are performed in function
f
in Steps 9,15,21, and 24. Step 5performs at most seven
operations, including four comparison operations and three logical operations. Steps 8,14,20,23,26,27,
and 29 each perform at most two comparison operations and one logical operation.
In the worst case, the for-loop must perform Steps 47,8,14,20,23,26,27,29, and 30
simultaneously. Thus, the number of operations in the for-loop is equal to
2+7+2+3×7+1=33.
Therefore, the total number of operations required is 33
n+
11. Conversely, in the best case, the point
P
is on
S
, and the for-loop must only perform Steps 410,12, and 13 one time. As a result, the number
of operations required is 7 +2+7+2+3+3+3=27.
Furthermore, let us compute the average running time of Algorithm 1. Algorithm 1includes
many branch statements whose execution probabilities are all different. From the previous discussions,
Symmetry 2018,10, 477 20 of 26
we know that the probability of a point lying on the boundary is far less than the probability of
the point not lying on the boundary. Therefore, to calculate the average running time required by
Algorithm 1, we only must consider the case in which the point is not on
S
. Furthermore, we only
must consider the paths that the for-loop most likely performs, 4
5
6,4
5
7
8
9
10
11,
or 4578141516 17.
For an edge, the average number of operations required is
(1/3)×(3×2+3×7+2×2+3×2+2×3+2×1+3+2)
= (1/3)×(50) = (50/3) = 16.7.
Because the number of polygon edges is
n
, the total average number of operations required is
16.7
n
for Steps 330. Steps 3132 require at most two logical operations, and one modulo operation.
Therefore, the total average number of operations required is 16.7
n+
3. Because Algorithm 1uses an
array to store the edge information, including the end nodes of each edge, its space complexity is also
O(n). The time and space complexities of Algorithm 1are the same as Algorithm 1.
Second, let us consider Algorithm 2. Assume that the numbers of edges of two input polygons
are
n
,
m
respectively. In addition, assume that execution probability of each branch is different for
the for-loop in Algorithm 1. Step 5requires at most 4
(n+m)
operations. For
R
, on average, Step 10
requires 16.7
(n+m) +
6 operations. Step 11 requires at most three operations. In the worst case,
Steps 1244 require at most 10
+
4
(n+m)
operations. Therefore, in the worst case, the time complexity
of Algorithm 2is O((n+m)l), where
l= (Bottom_Right.xTop_Le f t.x)×(Top_Le f t.yBottom_Right.y). (6)
Under normal circumstances, because the numbers of operations for Steps 10 and 1244 are far
less than 16.7
(n+m) +
6 and 10
+
4
(n+m)
, the average time complexity of Algorithm 2is far less
than
O((n+m)l)
. No matter how complex the input polygons are, it can be seen that
l
is nearly
constant. Thus, Algorithm 2has an average time complexity of
O(n+m)
, reconfirmed through the
experimental results in Section 7.
7. Experiment and Comparison
We have completed experimental tests to evaluate the performances of Algorithms 1and 2.
Our computing environment used an Intel(R) Core(TM)2 Quad CPU Q6600 @2.40 GHz with 4.00 GB
of RAM. The operating system is Microsoft Windows 10 Professional Edition. The graphics card is an
NVIDIA GeForce 9800 GT. The display resolution is 1024
×
768
×
32 bits (RGB). The internal hard drive
is 1TB. We used Microsoft Visual C++ 2017 compiler as our programming environment. The testing
program randomly generates vertices of a polygon using the methodology in [31].
7.1. The Point-in-Polygon Test
From the online library [
5
], we select 11 algorithms for testing. In addition, we test [
1
,
6
] and
efficient boundary methods [
2
], and CGAL4.2
[31]
with a 2D Kernel. Thus, the algorithms tested
include a total of 16 algorithms (see Table 1).
Figure 10 shows the performance comparison of the 10 algorithms in running time. We can see
that, although the running times of the 10 algorithms changed almost linearly as the number of vertices
of input polygons increase, Algorithm 1increases more slowly than the other algorithms. From the
top-left corner to the bottom-right corner of the
MBB
, the testing program determines point-by-point
whether a point is inside, outside, or on the boundary of the polygons. For all points in the
MBB
,
the algorithm tested uses a timer to record the total execution time of the program. Finally, the results
of the executions were used to plot the curves with the
numb er o f verti ces =
522, 569, 1020, 1505,
2023, 2529, 3019, 3515, 4000, 4499, 5003, 5570, and 6021 (see Figure 10). The x-axis denotes the number
Symmetry 2018,10, 477 21 of 26
of vertices of an input polygon and the y-axis denotes the running time of an algorithm in seconds
(same as below) as shown in Figure 10. Because the implementations of half-plane testing, Spackman
barycentric, Trapezoid testing, Grid testing, and Efficient boundary [
2
] all contain bugs, we do not plot
their corresponding graphs.
Table 1.
Performance comparison of the different algorithms for the point-in-polygon test.
CROSS
indicates that an algorithm can handle self-intersecting polygons.
HOLE
denotes that an
algorithm can deal with polygons with holes(not nested).
NHOLE
denotes that an algorithm can
deal with polygons with nested holes at any depth.
KHOLE
denotes that an algorithm can deal with
polygons with keyhole edges formed by single contours.
DIR
indicates that an algorithm must specify
whether each contour of input polygons is oriented clockwise or counterclockwise.
SENS
indicates that
an algorithm is sensitive to whether a polygon is oriented clockwise or counterclockwise.
ON
indicates
that an algorithm can determine whether a point is on the polygon boundary.
BUG
denotes that an
algorithm has bugs.
Library
Result Properties CROSS HOLE NHOLE KHOLE DIR SENS ON BUG
1 Algorithm 1X X X X × × X×
2 Crossings ×X× × × × × ×
3 Crossings-multiply [4,5]X X ×X× × × ×
4 Angle summation X X ×X× × × ×
5 Weiler angle summation ×X× × × × × ×
6 Half-plane testing X X ×X× × × X
7 Barycentric coordinates X X ×X× × × X
8 Spackman barycentric X X ×X× × × X
9 Trapezoid testing ×X× × × × × X
10 Grid testing X X ×X× × × X
11 Exterior test ×X× × × × × ×
12 Inclusion test ×X× × × × × ×
13 Jiménez et al. [1]×X× × X X ×X
14 Galacticomm [6]X X ×X× × × ×
15 Efficient boundary [2]× × × × X X ×X
16 CGAL4.2 [31]X X X X × × X×
0
1,000
2,000
3,000
4,000
5,000
6,000
0
500
1,000
1,500
numb er o f verti ces
running time(sec)
Algorithm 1
Crossings-multiply [5]
Crossings
Weiler angle summation
Barycentric coordinates
Exterior test
Inclusion test
Angle summation
the algorithms of Jiménez et al. [1]
and the algorithm of Galacticomm [6]
Figure 10.
Run time comparison of different algorithms for the point-in-polygon test. The figure can
be enlarged enough to show in full the differences of different algorithms.
Symmetry 2018,10, 477 22 of 26
Half-plane testing, Barycentric coordinates, and Spackman barycentric algorithms sometimes
misjudge internal points as external points (see Figure 1). To store the number of trapezoids,
the implementation of Trapezoid algorithm uses the global variable Trapezoid
_
Bins that has initial
value 20. The performance of Trapezoid algorithm changes as the value of the variable Trapezoid
_
Bins
changes. To keep the value of resolution, the implementation of Grid testing algorithm uses the
global variable Grid
_
Resolution that has initial value 20. The performance of Grid testing algorithm
changes as Grid
_
Resolution changes. According to the resolution of the screen, Grid testing algorithm
partitions the bounding box into grids, the number of which varies as the screen resolution varies.
Grid testing consumes more memory and time to the extent that it frequently causes the program to
crash. Jiménez et al.
[1]
were sensitive to whether a polygon is clockwise or counterclockwise oriented
and do not apply to a self-intersecting polygons (see Figure 1). In addition, it cannot efficiently deal
with degenerate cases.
From the comparison results, it can be seen that, although Algorithm 1uses many comparison
operations of floating-point numbers, this does not cause the program to run incorrectly. Performance
of Algorithm 1is only subject to the number of vertices of tested polygon, rather than the number
of floating point operations involved. Therefore, introducing errors
e
for comparison operation of
floating point numbers is not essential for Algorithm 1. Of course, if people want “fat edges”, they can
enlarge the bounds by an epsilon.
7.2. Boolean Operations Test
From the online library, we selected three algorithms for testing of boolean operations. Thus, the
comparison includes a total of four algorithms (see Table 2).
All algorithms except Algorithm 2perform boolean operations to generate the corresponding
resultant polygons. They then use crossings-multiplication [
4
,
5
] to fill the interior regions of the
resultant polygons. For the input polygons, Algorithm 2takes only a calculation step to fill the interior
regions of the resultant polygons. For measuring the running times of the algorithms, each algorithm
uses a timer to record the total execution time. Finally, based on the results of the operations, we plot