Lec21 Geometry
Lec21 Geometry
Lecture #19: Fundamentals of Computational Geometry last changed: November 22, 2022
Computational geometry is the design and analysis of algorithms for geometric problems that arise in low
dimensions, typically two or three dimensions. Many elegant algorithmic design and analysis techniques have
been devised to attack geometric problems, and these problems have huge applications in many other fields.
1 Introduction
Computational geometry has applications in, and in some cases is the entire basis of, many fields. E.g.,
- Computer Graphics (rendering, hidden surface removal, illumination)
- Robotics (motion planning)
- Geographic Information Systems (Height of mountains, vegetation, population, cities, roads, electric lines)
- Computer-aided design (CAD) /Computer-aided manufacturing (CAM)
- Computer chip design and simulations
- Scientific Computation (Blood flow simulations, Molecular modeling and simulations)
1
For the purpose of this course, we will assume a rather idealized model of computation, where we assume we
can compute on the objects we care about (which might be integers, or sometimes might be arbitrary real
numbers) in constant time without any loss of precision. For example, we will ignore things like rounding
errors which would be a real-life consideration if you were actually implementing many of these algorithms.
In real life, computational geometry algorithms are often extremely susceptible to rounding errors.
Algorithms which might be simple to describe on paper or in pseudocode can end up being very
subtle and difficult to implement correctly because of it. In this course, we will mostly sweep this
under the rug and focus on the elegance of the algorithms without this pain point in mind.
2 Fundamental Operations
2.1 Operations on points and vectors
In computational geometry, the most primitive object is a point, which we can represent as a tuple of real
numbers. We will mostly be focusing on 2D computational geometry, so points will be represented as pairs
(x, y) of real numbers. In 2D or 3D space, a vector is a pair of 3-tuple of real numbers, respectively. Wait,
that’s just the same thing as a point! Indeed, the difference between a point and a vector is often just context,
points are typically used to denote locations, and vectors are typically used to denote directions/lengths or
differences, but mathematically they are interchangeable.
p Al
ftp.q 8
q fig
Ojo
Scalar multiplication We can define the multiplication of apvector by a scalar. Geometrically, multiplying
Cross
a vector by a scalar α makes Product
it α times longer.
α(x, y) := (αx, αy)
q by the formula
0,0 is given
Magnitude/length The magnitude or length of a vector
pxq signed
∥(x, y)∥ := x2 + yarea of the
p
2
ftp.q 8
q fig
The dot product The dot product operation takes two vectors and returns a real number. It is the first
of two kinds of “multiplication” we will define for vectors. It is defined as
(x1 , y1 ) · (x2 , y2 ) = x1 x2 + y1 y2
Ojo
Dot products are useful for computing angles.
′ parallelogram
′
In thisfigure.)′ Call this point p′ .
q. Project p to the line L with a perpendicular (shown in green in the following
Then the value of p · q is the length of p , denoted |p |, times the length of q. That is, |p | ∗ |q|. Like the dot
case p′ and q then it’sby
product this value is signed. If (0, 0) is not between negative theotherwise it’s negative. The
positive,
right
figure below shows the case when |q| = 1, which is commonlyhandusedruleto compute projections.
P
Dot Product
r poof 0
Lpc
r geo
I
r
f
0,0
p′ = (p · q)q
a The projection p′ of the point p onto a line is the closest point on the line to p
Explain how to compute the projection of a point p in 2D onto any line L defined by two points a
and b by using the algorithm above with some extra steps.
The cross product The cross product is the other kind of “multiplication” between two points. In 2D,
the cross product is defined as
x1 y1
(x1 , y1 ) × (x2 , y2 ) := det = x1 y2 − y1 x2
x2 y2
where det(M ) is the determinant of M . The cross product of p and q is the signed area of the parallogram
with the four vertices (0, 0), p, q, p + q. Equivalently, it is twice the signed area of the triangle with vertices
3
p Al
ftp.q 8
q fig
(0, 0), p, q. The sign is determined by the “right hand” rule. This means that if the angle at (0, 0) starting
at p, going clockwise around (0, 0) and ending at q is less than 180 degrees, the cross product is negative,
otherwise it’s positive. It’s zero of the angle between p and q is 0 or 180 Ojo
degrees.
P Ptg
Cross Product
o.o q
pxq signed of the
areafor the cross product.
There is also a formula analogous to the dot product angle formula
LEFT
RIGHT
The algorithm is to construct vectors v1 = p2 − p1 and v2 = q − p1 , then take the cross product of v1 and v2
and look at its value compared to 0.
4
Notice that if a point q is on the right, then the cross product v1 × v2 will be negative, or if q is on the left,
it will be negative. If q happens to be on the ray, then the cross product is zero.
let v1 = p2 − p1 and v2 = q − p1
if v1 × v2 < 0 then return RIGHT
else if v1 × v2 > 0 then return LEFT
else return ON
Given two line segments defined by the points a and b and the points c and d respectively, determine
whether the two of them intersect or not. Hint: Use the line-side test primitive.
Given this definition, an interesting question to ask is given a set of points, what does the set of all possible
convex combinations of those points look like? We can start with just two points.
Given two points p and q, any convex combination of p and q is a point on the line connecting p and
q. More specifically, the set of all convex combinations of p and q is the line between p and q.
For example, if we take two points p and q and set α1 = α2 = 21 , we get 12 p + 21 q, which is the midpoint of p
and q (the middle of the line connecting them). What if we take three points p, q, r?
The convex combinations of three points p, q, r form the triangle with vertices p, q, r.
How can we justify this? Lets use the fact from above that the convex combinations of two points are the
line between them. So, for any point on the line between p and q, we can form a convex combination that
achieves that point. Now, for any point inside the triangle pqr, say x, that we want to reach, we can first
take a convex combination p′ = α1 p + α2 q such that p′ is the intersection of the lines pq and rx. Then take
a second convex combination with that point and r, to get x = β1 p′ + β2 r.
5
Exercise
Suppose we consider k > 3 points p1 , . . . , pk . Describe the set of points formed by all convex combi-
nations of them. Justify your answer by using the fact above that the convex combinations of three
points span a triangle.
Convex Non-convex
We saw convex sets before when we talked about linear programming. Based on this definition, we can define
the convex closure of a set of points P to be the smallest convex set containing P . This is well-defined and
unique for any point set P . This brings us to the definition of the object we really care about:
The convex hull of a set of points P is the boundary of the convex closure of P . That is, it is the
smallest convex polygon that contains all of the points in P , either on its boundary or interior.
These definitions are general and apply to any closed set of points, including infinite sets, but for our purposes
we’re only interested in the ConvexClosure(P ) and ConvexHull(P ) when P is a finite set of points.
A computer representation of a convex hull must include the combinatorial structure. In two dimensions,
6
this just means a simple polygon in, say counter-clockwise order. (In three dimensions it’s a planar graph of
vertices edges and faces) The vertices are a subset of the input points. So in this context, a 2D convex hull
algorithm takes as input a finite set of n points P ∈ R2 , and produces a list H of points from P which are
the vertices of the ConvexHull(A) in counter-clockwise order. This figure shows the convex hull of 10 points.
Today we’re going to focus on algorithms for convex hulls in 2-dimensions. We first present an O(n2 )
algorithm, than refine it to run in O(n log n). To slightly simplify the exposition we’re going to assume that
no three points of the input are colinear.
Claim
A directed segment between a pair of points (pi , pj ) is on the convex hull if and only if all other points
pk are to the left of the ray through pi and pj .
Note that no point to the right of the ray can be in the convex hull because that entire half-plane is devoid
of points from the input set. And the points on the segment (pi , pj ) are in the ConvexClosure of the input
points. Therefore the segment is on the boundary of the ConvexClosure. Therefore it is on the convex hull.
7
An O(n3 )-time algorithm Using this observation, we get an O(n3 ) algorithm by brute-force. Just try
every pair of points pi , pj and then line side test every other point pk . If every pk is on the left, then add
the edge pi → pj to the hull. After adding all of the edges, sort them into counterclockwise order.
An O(n2 )-time algorithm To get this to run in O(n2 ) time we just have to be a bit more organized. In
the first algorithm we just found all of the edges of the hull in an arbitrary order. Lets try to find them in
order this time. This will cut down the amount of work we have to do.
The first observation is that if we take the point with the lowest y-coordinate, this point must be on the
convex hull. Call it ps . Suppose we now measure the angle from ps to all the other points. These angles
range from 0 to π. If we take the point pt with the smallest such angle, then we know that (ps , pt ) is on the
convex hull. The following figure illustrates this.
All the other points must be to the left of segment (ps , pt ). We can continue this process to find the point
pu which is the one with the smallest angle with respect to (ps , pt ). This process is continued until all the
points are exhausted. The running time is O(n) to find each segment. There are O(n) segments, so the
algorithm is O(n2 ).
Actually we don’t need to compute angles. The line-side-test can be used for this instead. For example
look at what happens after we’ve found ps and pt . We process possibilities for the next point in any order.
Say we start from a in the figure. Then we try b, and note that b is on the right side of segment (pt , a) so
we jettison a and continue with (pt , b). But then we then throw out b in favor of c. It turns out that the
remaining points are all to the left of segment (pt , c). Thus c = pu is the next point on the convex hull.
We maintain a “chain” of points that starts with p0 , p1 , . . .. This chain has the property that each
step is always a left turn with respect to the previous element of the chain. We try to extend the
chain by taking the next point in the sorted order. If this has a left turn with respect to the current
8
chain, we keep it. Otherwise we remove the last element of the chain (repeatedly) until the chain is
again restored to be all left turns.
At this point we’ve formed the chain p0 , p1 , p2 , p3 , p4 . But the last step (from p3 to p4 ) is a right turn. So
we delete p3 from the chain. Now we have:
9
Now the process continues with points p5 and p6 . When p6 is added, p5 becomes a right turn, so it’s removed.
After all the points are processed in this way, we can just add the last segment from pn−1 to p0 , to close the
polygon, which will be the convex hull.
Each point can be added at most once to the sequence of vertices, and each point can be removed at most
once. Thus the running time of the scan is O(n). But remember we already paid O(n log n) for sorting the
points at the beginning of the algorithm, which makes the overall running time of the algorithm O(n log n).
The reason this algorithm works is because whenever we delete a point we have implicitly shown that it is a
convex combination of other points. For example when we deleted p3 we know that it is inside of the triangle
formed by p0 , p2 and p4 . Because of the presorting p3 is to the left of (p0 , p2 ), and to the right of (p0 , p4 ).
And because (p2 , p3 , p4 ) is a right turn, p3 is to the left of (p2 , p4 ).
At the end the chain is all left turns, with nothing outside of it. Therefore it must be the convex hull.
For any algorithm that computes a convex hull using line-side tests, at least Ω(n log n) line-side tests
are necessary.
10
The cool thing here is how we measure complexity. Just like how, for sorting, we counted the number of
comparisons and showed a lower bound based on that, for convex hull, we can provide a lower bound in
terms of the number of line-side tests that we need to perform. Just like we can also do sorting outside the
comparison model and obtain different bounds, it is also possible to write convex hull algorithms that do
not use line-side tests, so this result does not apply to those algorithms. It turns out, however, that the vast
majority of known convex hull algorithm are based on line-side tests, so it applies quite widely!
11