Points, Lines and Polygons

TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 10
Chapter 2. Points, Lines, and Polygons
2.1 Point Relationships
Placing a set of points in sorted order can significantly speed up the computation of some computational
geometry problems.
 A lexicographic order relation ( or, dictionary order relation) between two points p and q along
the x-direction can be defined as
p < q if
px < qx ;
or,
px = qx AND py < qy
 A lexicographic order relation along the y-direction can be defined as
p < q if
py < qy ;
or,
py = qy AND px < qx
 Sorting N points in a lexicographic order can be done in O(N log2N) time, using a quick sort or
merge sort algorithm.
 Searching sorted arrays can be done in O(N log2N) time (using a binary search).
2.2 Point-Line Relationships
The two-point form equation of a line passing through two points p and q can be written as
y  py
x  px

qx  px qy  py
This formula is easily derived from proportional triangles. To avoid division by zero for vertical or
horizontal lines, the equation can be rewritten as:
fpq(x, y)  (qx px) (y py)  (qy py) (x px) = 0.
r
 A point r lies on the line pq if fpq(r) = 0.
q
 A point r lies on the on the left side of line pq if fpq(r) > 0.
 A point r lies on the on the right side of line pq if fpq(r) < 0.
Note:
p
fpq(r) (qxpx)(rypy) (qypy)(rxpx) = pq  pr  sin  pq pr (for 2D vectors)
Therefore, r is on the left side of pq if it forms a positive angle from the vector pq .
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
The following statements are equivalent:

The point r is on the left of the directed line pq .

Points p, q, and r are oriented in an anti-clockwise sense.

The triangle pqr is positively oriented.

fpq(r) > 0
Note the following:
 The area of the triangle pqr is
f pq(r)
2
.
 The line equation can be written in determinant form as
px
py
1
fpq(r) = q x
rx
qy
1 = (qxry qyrx ) + (pyrx pxry) + (pxqy pyqx)
ry
1
= qx rypyqxpxry  qyrx +pxqypyrx
= (qx rypyqxpxry + pxpy) (qyrxpxqypyrx + pxpy)
= (qxpx)(rypy) (qypy)(rxpx)
To summarize, the orientation of the triplet pqr is:
 anti-clockwise if fpq(r) > 0.
 collinear if fpq(r) = 0.
 clockwise if fpq(r) < 0.
The line pq divides the entire plane into two half planes
 fpq(x, y)0
 fpq(x, y) 0.
Two points, r and s, are on the same side of a line pq , if they belong to the same half-plane.
 if fpq(r) fpq(s) > 0, r and s lie on the same side of a line pq .
 if fpq(r) fpq(s) < 0, r and s lie on opposite sides of a line pq .
page 11
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 12
2.3 Line Segment Intersection
The parametric form of the equation of a line in 2 dimensions, defined by 2 points p and q, is given by
x = px + t (qx  px)
y = py + t (qy  py)
t>1
q
t=1

If 0  t  1, the point (x, y) lies on the line segment pq
0<t<1
p

If t > 1, the point (x, y) lies beyond the point q on the line.
t=0

If t < 0, the point (x, y) lies beyond the point p on the line.
t<0
Two line segments pq and rs intersect at a point w, if and only if the
point of intersection is between the end points of both the line segments. Using t1 and t2 as parameters
for pq and rs respectively, at the intersection point w:
px + t1 (qx  px) = rx + t2 (sx  rx)
py + t1 (qy  py) = ry + t2 (sy  ry)
w
These two linear equations can be solved for the two unknowns
t1 and t2. If the solution satisfies the relations
q
s
t1
p
t2
r
0  t1  1 , and 0  t2  1,
then a valid intersection point between the two line segments exists.
The most straightforward way to solve for the intersection of two lines is to rearrange the above
equations into a matrix formalization and use Cramer’s rule to solve for t1 and t2. This looks like the
following:
t1 (qx  px)  t2 (sx  rx) = rx  px
t1 (qy  py)  t2 (sy  ry) = ry  py
Can be written as
 (q x  p x )  ( s x  rx )   t1   rx  p x 
(q  p )  ( s  r )    r  p 
y
y
y  t 2 
y
 y
 y
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 13
Using Cramer’s rule gives:
det =
(q x  p x )
(q y  p y )  ( s y  ry )
(rx  p x )
t1 =
 ( s x  rx )
 (q x  p x )( ry  s y )  (rx  s x )(q y  p y )
 ( s x  rx )
(ry  p y )  ( s y  ry )
det

(rx  p x )( ry  s y )  (rx  s x )( ry  p y )
det
(q x  p x ) (rx  p x )
t2 =
(q y  p y ) (ry  p y )
det

(q x  p x )( ry  p y )  (rx  p x )( q y  p y )
det
Note:
 Both t1 and t2.must lie between zero and one for a valid intersection to exist.
 If the determinant of the matrix (det) is zero, the lines are parallel. There are two cases:
1) there is no solution or
2) the lines are colinear and there are an infinite number of solutions.
 The determinant of the matrix is the cross product of pq and rs , which calculates the
sin of the angle between the two vectors.
The necessary and sufficient condition for two line segments pq and rs to intersect is that the points p
and q lie on either side of the line rs , and the points r and s lie on either sides of line pq . That is:
fpq(r) fpq(s)  0, and
frs(p) frs(q)  0.
2.4 Convex Polygons
A region in a plane is convex if, for any two points in the region, the line segment between the two
points lies entirely in the region.
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 14
A convex polygon has the following properties:
 Every diagonal of a convex polygon is a chord (from definition).
 Every vertex of a convex polygon is convex (less than 180).
 Every convex polygon is simple (no edge intersections).
 Every anti-clockwise traversal of a convex polygon either continues straight or turns left at every
vertex. Similarly a clockwise traversal turns right at every
vertex.
p4
p5
 The intersection of two convex polygons is also convex
p3
p6
(prove!).
 A convex polygon can be easily triangulated using chords from
p2
p0
p1
any vertex connecting all its non-adjacent vertices.
The above properties make convex polygons easier to work with than arbitrary polygons.
The area of a convex polygon  with n vertices p0 , p1 , p2 ,...., pn-1 is
=
n2
n2
p0 pi x p0 pi 1
i 1
i 1
2
 ( Area of Triangle p 0 pi pi1 )  
The point inclusion test refers to the problem of determining whether a given point is inside a convex
polygon. The point inclusion test can be formally stated as follows.
A point q is inside a convex polygon if and only if it is on the same side of all its edges.
For example, if the polygon is traversed in the anti-clockwise sense, then the point q is an interior point
only if it lies on the left of all the edges. In this case, the vertices p0 , p1 , p2 ,...., pn-1 are in anticlockwise order, and if a point q has to be inside the polygon, then
fpipi+1 (q)  0,
for i = 0, 1, 2, ...n1;
2.5 Star-Shaped Polygons
Pn = P0 .
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 15
Consider two points p and q inside a polygon. The point p is said to be visible at q (or p “sees” q) if the
line segment pq is entirely within the polygon and does not intersect any of the edges. The set of those
q
Kernel
p
r
p “sees” q, not r.
Star-shaped polygon
Not Star-shaped
(no kernel)
points in a polygon that see every point of the polygon’s interior is called the kernel of the polygon. A
polygon is called star-shaped if its kernel is non-empty.
The kernel of a convex polygon is the whole of its interior.
2.6 Monotone Polygons
A simple polygon is called monotone with respect to a line l if, for any line lperpendicular to l, the
intersection of the polygon with lis connected (i.e., the intersection must be a single line segment, or a
point or empty).
A polygon that is monotone with respect to the x-axis is called x-monotone. An x-monotone polygon is
composed of two monotone chains: the polygon’s upper chain and lower chain. Each chain terminates
at the polygon’s leftmost and rightmost vertices. When an x-monotone chain is traversed beginning from
its leftmost vertex, its vertices are visited by increasing x-coordinates.
x-monotone
NOT x-monotone
y-monotone
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 16
A y-monotone polygon is also similarly defined with respect to the y-axis, and consists of two monotone
chains: the left and the right chain. When a y-monotone polygon is traversed along one of its chains
beginning at the topmost vertex, the vertices are visited in decreasing order of y-coordinates.
Note:
 The main advantage of monotone polygons is that they can be easily triangulated using a simple
algorithm (to be discussed later).
 Monotone polygons can contain reflex vertices.
 A convex polygon is monotone with respect to any line.
 A star-shaped polygon need not be a monotone polygon (give an example).
2.7 Monotone Decomposition of Simple Polygons
In general, simple polygons need not be monotone and can have an arbitrary shape which is not selfintersecting. A simple polygon which is not monotone, can be split into a set of monotone polygons.
Before an algorithm to accomplish this is presented, consider the following definitions:
 Merge vertex (): A reflex vertex which is greater than both its neighbors (in terms of x).
 Split vertex (<): A reflex vertex which less than both its neighbors (in terms of x).
Examples of these vertices are labeled in the figure below. Note that:
Vertex v3 is a merge vertex because it is a reflex vertex and has a x component greater than the x
components of vertex v2 and v4, its neighbors (v3x > v2x and v3x > v4x).
Vertex v9 is a split vertex because it is a reflex vertex and has a x component less than the x
components of vertex v8 and v10, its neighbors (v9x < v8x and v9x < v10x).
1
v1
v4
v2
v0
0
v3
4
2
v23
6
8
v21
v22
3
5
v5

12
v6
10
v7
13
9
v20
v8 15
v19
v9
14
v17
7
v18
11
18
v11
20 
v10
v14
21
v15
17
16
19 v16
Fig.: A non-monotonic polygon with its merge and split vertices labeled.
v12
22
v13
23
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 17
Algorithm 1: A simple, but flawed, algorithm for dividing a simple, non-monotonic polygon into a set
of monotone polygons:
Step 1: Sort its vertices in a lexicographic order along the x-direction.
Step 2: Connect every merge vertex to the next vertex in the sorted list.
Step 3: Connect every split vertex to the previous vertex in the sorted list.
1
v1
v4
v2
v0
0
v3
4
2
v23
6
8
v22
v5

v21
12
v6
v8
v7
10
v9
13
14
9
v20
5
v19
v17
7
3
15
20 
v10
v14
21
v15
17
v12
22
v13
23
16
11
v18
18
v11
19 v16
Fig.: 5 monotonic polygons created from the non-monotonic polygon in the previous figure.
This algorithm performs correctly for many cases but it creates invalid diagonals (and therefore invalid
sub-polygons) when the diagonal to the previous vertex intersects an edge. This can happen for both
split and merge vertices. An example for a split vertex creating an invalid edge is show below:
3
1
5
2
4
7
0
6
This problem with the algorithm could be corrected in several ways. A "brute force" technique could test
for intersections between the new diagonal and the polygon's edges, but this would be computationally
expensive. Another approach is to recognize that the problem exists because a vertex from the "lower
chain of vertices" is being connected to a vertex in the "upper chain of vertices." If a diagonal from a
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 18
split or merge vertex was always connected to another vertex in the same chain, then we can guarantee
that the diagonal will not cross an existing polygon edge. A modified algorithm using this approach
follows:
Algorithm 2: Divide a simple, non-monotonic polygon into a set of monotone polygons:
Step 1: Sort its vertices in a lexicographic order along the x-direction.
Step 2: Create an "upper" and "lower" chain of vertices, where each list of vertices starts with the
first vertex in the lexicographic ordering and ends with the last vertex in the
lexicographic ordering.
Step 3: Traverse each "vertex chain" separately, doing the following:
a) Connect every merge vertex to the next vertex in its vertex chain.
b) Connect every split vertex to the previous vertex its vertex chain.
For example, this algorithm would do the following on the sample polygon below:
1
v1
v4
v2
v0
0
v3
4
2
v23
6
8
v21
v22
3
5
v5

12
v6
10
v7
13
9
v20
v8
15
v9
14
v19
v17
7
v18
11
18
v11
20 
v10
v14
21
v15
17
v12
22
v13
23
16
19 v16
Step 1: v0, v1, v23, v22, v2, v21, v4, v20, v3, v19, v5, v18, v6, v7, v9, v8, v17, v15, v11, v16, v10, v14, v12, v13
Step 2: Upper chain: v0, v1, v2, v4, v3, v5, v6, v7, v9, v8, v11, v10, v12, v13
Lower chain: v0, v23, v22, v21, v20, v19, v18, v17, v15, v16, v14, v13
Step 3: Traverse each "vertex chain" separately, doing the following:
a) Merge vertices: connect v3 to v5; connect v10 to v12
b) Split vertices: connect v9 to v7; connect v15 to v17
Algorithm 2 Analysis: This algorithm is efficient and can be accomplished in O(N Log N) time as
follows:
TCG2061 Computational Geometry
Chapter 2 (23/06/00)
page 19
Step 1: Use a "quick sort" to sort its vertices. O(N Log N).
Step 2: Make one pass through the sorted list from step one. For each vertex, if its index is
between the index of the first and last vertices in the sorted list, add it to the "upper
chain", otherwise add it to the lower chain (Assuming a clockwise ordering of the points).
O(N).
Step 3: Traverse each "vertex chain", connecting points as required. O(N)
In summary, a simple, non-monotonic polygon can be triangulated (divided into a set of triangles) using
a two step process:
 Use the above algorithm to create a set of monotonic polygons.
 Triangulate each of the monotonic polygons.
2.8 Summary of Classification of Simple Polygons
The following diagram shows the relationships between the various types of simple polygons.
Triangles
Convex
Polygons
Star-Shaped
Polygons
Monotone
Polygons
Simple
Polygons
 A triangle is a convex, star-shaped, monotonic, simple polygon.
 A convex polygon is a star-shaped, monotonic, simple polygon.
 A star-shaped polygon is a simple polygon, but it may, or may not be, monotonic.
 A monotonic polygon is a simple polygon, but it may, or may not be, star-shaped.