0% found this document useful (0 votes)
25 views11 pages

Lec21-Convex-Hull-Convex Hull-5

The document discusses computational geometry, focusing on algorithms for geometric problems in low dimensions, particularly 2D and 3D. It covers applications in various fields such as computer graphics, robotics, and scientific computation, and introduces basic algorithmic design approaches like divide-and-conquer and line-sweep. The lecture outlines topics to be covered, including geometric primitives, convex hulls, and algorithms for point location problems.

Uploaded by

Anant Nimkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views11 pages

Lec21-Convex-Hull-Convex Hull-5

The document discusses computational geometry, focusing on algorithms for geometric problems in low dimensions, particularly 2D and 3D. It covers applications in various fields such as computer graphics, robotics, and scientific computation, and introduces basic algorithmic design approaches like divide-and-conquer and line-sweep. The lecture outlines topics to be covered, including geometric primitives, convex hulls, and algorithms for point location problems.

Uploaded by

Anant Nimkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

15-451/651: Design & Analysis of Algorithms November 13, 2018

Lecture #21: Computational Geometry Introduciton last changed: November 13, 2018

1 Introduction
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. This is why I’ve included this topic in
this course.
Some applications of CG:

Computer Graphics
images creation
hidden surface removal
illumination

Robotics
motion planning

Geographic Information Systems


Height of mountains
vegetation
population
cities, roads, electric lines

CAD/CAM computer aided design/computer aided manufacturing

Computer chip design and simulations

Scientific Computation
Blood flow simulations
Molecular modeling and simulations

Basic algorithmic design approaches:

• Divide-and-Conquer
• Line-Sweep (typically in 2D)
• Random Incremental

In this course there will be three lectures on computational geometry covering the following topics:

• Geometric primitives
• Convex hull in 2D
• Sweep line algorithm for intersecting a set of segments
• Two algorithms for the point location problem

1
1.1 Representations
The basic approach used by computers to handle complex geometric objects is to decompose the
object into a large number of very simple objects. Examples:

• An image might be a 2D array of dots.


• An integrated circuit is a planar triangulation.
• Mickey Mouse is a surface of triangles

It is traditional to discuss geometric algorithms assuming that computing can be done on ideal
objects, such as real valued points in the plane. The following chart gives some typical examples
of representations.

Abstract Object Representation


Real Number Floating Point Number, Big Number
Point Pair of Reals
OneNote Online Line Pair of Points, An Equation
https://onenote.officeapps.live.com/o/onenoteframe.aspx?Fi=SDACE4EBC921613D
Line Segment Pair of Endpoints
Triangle Triple of points
! Etc
"#$%&'(!)&*+,!-.(!/0-.! 121/!3)!

1.2 Using Points to Generate Objects

Suppose P1 , P2 , . . . Pk ∈ Rd . Below are several ways to use these points to generate more complex
objects.
Linear Combination
X
Subspace = αi Pi where αi ∈ R

Affine Combination
X X
Plane = αi Pi where αi = 1, αi ∈ R

Convex Combination
X X
Body = αi Pi where αi = 1, αi ≥ 0 αi ∈ R

2
2 Primitive Operations
Basics
I’ll be giving integer implementations of these primitives in ocaml. Let’s start with some basic
operations on vectors in 2D. The code below defines vector addition subtraction, cross product, dot
product and the sign of a number.

let (--) (x1,y1) (x2,y2) = (x1-x2, y1-y2)


let (++) (x1,y1) (x2,y2) = (x1+x2, y1+y2)
let cross (x1,y1) (x2,y2) = (x1*y2) - (y1*x2)
let dot (x1,y1) (x2,y2) = (x1*x2) + (y1*y2)
let sign x = compare x 0
(* returns -1 if x<0, 0 if x=0 and 1 if x>0 *)

The first two operations are subtraction and addition of vectors, as shown in the following figure.
Note that sometimes we view a pair (x, y) as a vector, and sometimes we view it as a point.

p Al

ftp.q 8

q
p fig Al

ftp.q 8

fig
Ojo
q
The cross product of p and q, denoted p × q, is the signed area of the parallogram with the four
p
vertices (0, 0), p, q, p + q. The sign is determined by the “right hand” rule. Alternatively if the angle
Cross Product
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
degrees. Ojo
0,0 q
P Ptg
pxqProduct
Cross of the
signed area
parallelogram In this
case negative
o.o q by the
right hand rule
pxq signed
3 area of the
parallelogramP In this
Dot Product case negative by the
o.o q
pxq signed area of the
parallelogram
The dot product is a projection operator. Consider the line L through (0, In0) and this
q. Project p to
the line L with a perpendicular (shown in green in the following figure.) Call this point p . Then 0

That is,the
denoted |p |, times the length of q.by
dot product this value is signed. If (0, 0) is notnegative
the value of p · q is the length of p ,case0 0 |p | ∗ |q|. Like the 0
0
between p and q then it’s positive, otherwise it’s
right
negative. The figure below shows the case when |q| = 1. hand rule
P
Dot Product
r poof 0
Lpc
r geo

I
OneNote Online
r
f
https://onenote.officeapps.live.com/o/onenoteframe.aspx?Fi=SDACE4EBC921613DBE!...

0,0
!
Line Side Test
p
"#$%&'(!)&*+,!-.(!/0-.! 121/!3)!

of projects P onto the


left of ray P → P , “RIGHT” if the point P is to the right of rayl P → l P ,land “ON” if it is on
Given three points P , P , P , the output of the line side test is “LEFT” if the point P is to the
1 2 3 3
1 2 3 1 2
that ray.
The algorithm is to construct vectors V2 = P2 − P1 and V3 = P3 − P1 . Then take the cross product
of V2 and V3 and look at its value compared to 0.

Here is an implementation of this test in ocaml which returns 1 if p3 is LEFT of ray p1 → p2 , −1


if RIGHT, and 0 if ON.

let line_side_test p1 p2 p3 = sign (cross (p2--p1) (p3--p1))

Line segment intersection testing


Here we are given two line segments (a, b) and (c, d) (where a, b, c, d are points), and we have to
determine if they cross. We can do this using four line-side tests, as illustrated here.
4
OneNote Online https://onenote.officeapps.live.com/o/onenoteframe.aspx?Fi=SDACE4EBC921613DBE!...

!
"#$%&'(!)&*+,!-.(!/0-.! -0120!3)!

Fast Robust Predicates for Computational Geometry http://www.cs.cmu.edu/~quake/robust.html

Adaptive Precision Floating-Point Arithmetic and Fast Robust


let segments_intersect (a,b) (c,d) = !

Predicates for Computational


(line_side_test Geometry
a b d) * (line_side_test a b c) <= 0 &&
(line_side_test c d a) * (line_side_test c d b) <= 0
Jonathan Richard Shewchuk
By changing the <=
Computer Science into a <,
Division this can be changed into a strict intersection test, which would require
University of California
the segments to intersect at Berkeley
at a point interior to both of them.
Berkeley, California 94720-1776

In-circle test
Created as part of the Archimedes project (tools for parallel finite element methods).
ThreeSupported in part by NSF
non-colinear Grant CMS-9318163
points determine andaan circle.
NSERC 1967 TheScholarship.
in-circle test will tell us the relationship of a
fourth point to the circle determined by the other three points. So the test takes points a, b, c, and
Many computational geometry applications use numerical tests known as the orientation and incircle tests. The orientation test determines
d as whether
inputs, andliesreturns
a point to the left of, right−1
1,to0,theor asonfollows:
of, or a line or plane defined by other points. The incircle test determines whether a point
lies inside, outside, or on a circle defined by other points.
This returns 0 if the four points are on the same circle (or straight line.) Suppose I
Each of these tests is performed by evaluating the sign of a determinant (see the figure below). The determinant is expressed in terms of the
walk forward around the circle with my right hand on the circle from a → b → c. It
coordinates of the points. If these coordinates are expressed as single or double precision floating-point numbers, roundoff error may lead
toreturns
an incorrect1result
if dwhen
is the
ontruethe same isside
determinant of the
near zero. circle
In turn, as my body,
this misinformation can leadand −1 otherwise.
an application It’s
to fail or produce a
incorrect
results.
fourth degree function in the given coordinates.
let incircle (ax,ay) (bx,by) (cx,cy) (dx,dy) =
let det ((a,b,c),(d,e,f),(g,h,i)) =
a*(e*i - f*h) - b*(d*i - f*g) + c*(d*h - e*g)
in

let row ax dx ay dy =
let a = ax - dx in
let b = ay - dy in
1 of 1 3/16/15 3:00 AM

(a, b, (a*a) + (b*b))


in
sign (det (row ax dx ay dy, row bx dx by dy, row cx dx cy dy))

The One
picture above
way to solve illustrates
this problem is to useaexact
case whenUnfortunately,
arithmetic. the incircle testlibraries
traditional wouldforreturn 1. (This
arbitrary precision figure arithmetic
floating-point was taken
fromarehttp://www.cs.cmu.edu/~quake/robust.html)
quite slow, and can reduce the speed of an application by one or two orders of magnitude.

To minimize this problem, I've produced algorithms and implementations for performing the 2D and 3D orientation and incircle tests
5
1 of 2 3/16/15 3:26 AM
3 Computing the Convex Hull
This is the “sorting problem” of computational geometry. There are many algorithms for the
problem, and there are often analogous to well-known sorting algorithms.
A point set A ⊆ Rd is convex if it is closed under convex combinations. That is, if we take any
convex combination of any two points in A, the result is a point in A. In other words if when we
walk along the straight line between any pair of points in A we find that the entire path is also
inside of A, then the set A is convex.
We saw convex sets before when we talked about linear programming. One observation we used at
that time is that the intersection of any two convex sets is convex.
Definition: ConvexClosure(A) = smallest convex set containing A
This is well-defined and unique for any point set A. (We won’t prove this.) Assuming that the set
A is a closed set of points we can define the convex hull of A as follows:
Definition: ConvexHull(A) = boundary of ConvexClosure(A). (A point p is on the boundary of
S if for any  > 0 there exists a point within  of p that is inside S and also another point with 
of p that is outside of S.)
These definitions are general and apply to any closed set of points.
For our purposes we’re only interested in the ConvexClosure(A) and ConvexHull(A) when A is a
finite set of points. In this case the ConvexClosure will be a closed polyhedron.
A computer representation of a convex hull must include the combinatorial structure. In two
dimensions, 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 A ∈ R2 , and
produces a list L of points from A 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.

3.1 An O(n2 ) Algorithm for 2D Convex Hulls


First we give a trivial O(n3 ) algorithm for convex hull. The idea is that a directed segment between
a pair of points (Pi , Pj ) is on the convex hull iff all other points Pk are to the left of the ray from
6
Pi to 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.
Here’s the pseudo-code for this algorithm.
Slow-Hull(P1 , P2 , . . . , Pn ):
For each distinct pair of indices (i, j) do
if for all 1 ≤ k ≤ n and k 6= i and k 6= j
it is the case that Pk is to the left of segment (Pi , Pj )
Then output (i, j).
done
(To make this into a proper convex hull algorithm, a final pass would be required to turn this list
of pairs of indices into an ordered list of points in counterclockwise order.)
To get this to run in O(n2 ) time we just have to be a bit more organized.
The first observation is that if we take the point with the lowest y-coordinate, this point must be
on the contex 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.

7
3.2 Graham Scan, an O(n log n) Algorithm for 2D Convex Hulls
We can convert this into an O(n log n) algorithm with a slight tweek. Instead of processing the
points in an arbitrary order, we process them in order of increasing angle with respect to point ps .
Let’s relabel the points so that P0 is the starting point, and P1 , P2 . . . are the remaining points in
order of increasing angle with respect to P0 . From the discussion above we know that (P0 , P1 ) is
an edge of the convex hull.
The Graham Scan works as follows. 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 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. Here’s an example of
the algorithm.

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:

Now at P2 we have a right turn, so we remove it, giving:

8
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.
Complete ocaml code for the graham scan is at the end of these notes.

9
3.3 Lower bound for computing the convex hull
Suppose the input to a sorting problem is X1 , . . . , Xn . Consider computing the convex hull of the
following set of points:

(X1 , X12 ), . . . , (Xn , Xn2 )

All of these points are on the convex hull (they’re on a parabola). Thus they are returned in the
order they appear along the parabola. No matter which convex hull algorithm is used, the points
can be reflected and/or cyclically shifted so that their x coordinates are in sorted order. Thus, they
can be sorted by computing a convex hull followed by O(n) additional work. Thus any comparison
based convex hull algorithm must make Ω(n log n) comparisons. The figure below illustrates this
OneNote Online https://onenote.officeapps.live.com/o/onenoteframe.aspx?Fi=SDACE4EBC921613DBE!...
phenomenon.
!
"#$%&'(!"&)*+!,-(!./,0! ,1,.!2"!

1 of 1 3/16/15 5:22 AM

10
CSS: border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em; Highlight!

3.4Preview:
Ocaml code for the Graham Scan convex hull algorithm

let sq x = x * x
let (--) (x1,y1) (x2,y2) = (x1-x2, y1-y2)
let cross (x1,y1) (x2,y2) = (x1*y2) - (y1*x2)
let len2 (x,y) = (sq x) + (sq y)

type side = LEFT | ON | RIGHT

let line_side_test p1 p2 p3 =
let cp = cross (p2--p1) (p3--p1) in
if cp > 0 then LEFT else if cp < 0 then RIGHT else ON

let graham_convex_hull points =


(* This algorithm takes as input a list of points, and returns a
list of points that is the convex hull (in clockwise order) of
the input. Duplicates are allowed in the input, and the output
contains no three colinear points.

graham_convex_hull [(0,0);(0,2);(2,2);(2,0);(1,1);(1,2);(1,2)]
returns this list: [(0,2) (2,2) (2,0) (0,0)]
*)

let base = List.fold_left min (List.hd points) points in


let points = List.filter (fun pt -> pt <> base) points in

let compare_points pi pj =
match line_side_test base pi pj with
| ON -> compare (len2 (base -- pi)) (len2 (base -- pj))
| LEFT -> -1
| RIGHT -> 1
in

let points = List.sort compare_points points in

let rec scan chain points =


let (c1,c2,chainx) = match chain with
| c1::((c2::_) as chainx) -> (c1,c2,chainx)
| _ -> failwith "chain must have length at least 2"
in
match points with [] -> chain
| pt::tail ->
match line_side_test c2 c1 pt with
| ON ->
if len2 (pt--c2) > len2 (c1--c2)
then scan (pt::chainx) tail
else scan chain tail
| LEFT -> scan (pt::chain) tail
| RIGHT -> scan chainx points
in

match points with


| (p1::((_::(_::_)) as rest)) -> scan [p1;base] rest;
| _ -> List.rev (base::points)

11

API • Code • Made by Alexander Kojevnikov • Powered by Flask and Pygments

You might also like