You are on page 1of 29

CSI2003: Advanced Algorithms

Computational Geometry
Prof. Lakshmanan K
SCOPE, VIT, Vellore

Computational Geometry
Overview from Cormen, et al.
Overview

 Computational Geometry Introduction

 Line Segment Intersection

 Convex Hull Algorithms

 Nearest Neighbors/Closest Points


What is Computational Geo.?

 It is a branch in CS that studies algorithms for


solving geometric problems.
 It has applications to computer graphics, VLSI
design, robotics, CAD, etc.
 Input: set of geometric objects, like set of
{points, line segments}, vertices of polygon.
 Output: whether intersects, which direction
one point lies, convex Hull, closest points, etc.
Convex Combination of 2 points
 p1 = (x1,y1), p2 = (x2,y2). Then, the convex
combination of these 2 points is any point p3 =
(x3, y3) such that for 0<α<=1,
 x3 = α x1 + (1- α)x2, y3= αy1 + (1- α)y2.
 Wc can also write it as p3= αp1 + (1- α)p2.

Intuitively, p3 is any point that is on the line


passing thro p1 and p2, and is on or btwn. p1& p2.
___ Given 2 distinct points p1 & p2, the line seg.
p1p2 is the set convex combinations of p1 & p2.
Line Segment Intersections
(2D)
Intersection of 2 Line Segments
Intersection of > 2Line Segments
Cross-Product-Based
Geometric Primitives
Some fundamental geometric questions:

source: 91.503 textbook Cormen et al.

p2 p3
p3 p2

p1 p2 p4

p0 p1 p1

Fig.(1a) Fig.(1b) Fig.(1c)


Cross-Product-Based
Geometric Primitives: (1)

p2
α

2
p1

p0
 x1 x2  (1)
p1  p2  det    x1 y2  x2 y1
 y1 y2 
Cross-Product-Based
Geometric Primitives: (2)
p2

p1

p0
3
(2)

source: 91.503 textbook Cormen et al.


Intersection of 2 Line Segments
Step 1: Bounding
Box Test is true: p3 and p4 on opposite sides of p1p2

(x2≥x3)ꓥ(x4 ≥x1)

p(y2
3
≥ y3)ꓥ(y4 ≥y1)
p2

p4

p1

Step 2: Does each 4

segment straddle
the line containing
the other?

source: 91.503 textbook Cormen et al.


Intersection of >2 Line Segments

Sweep-Line Algo Paradigm: sweeping lines are imaginary vertical lines

source: 91.503 textbook Cormen et al.


Intersection of >2 Line Segments
Assumptions:
1. No three segments have the same x-
coordinate Point
2. No vertical line segment is given

source: 91.503 textbook Cormen et al.


Intersection of >2 Line Segments

Time to detect if any 2


segments intersect:O(n lg n)

Balanced BST stores segments in order


of intersection with sweep line.
Associated operations take O(lgn) time.

Note that it exits as soon as one intersection is detected.

source: 91.503
source: textbook
91.503 Cormen
textbook Cormenet
et al.
al.
Convex Hull Algorithms

Definitions
Gift Wrapping
Graham Scan
QuickHull
Incremental
Divide-and-Conquer
Lower Bound in W(nlgn)
Algorithms: 2D Gift Wrapping

 Use one extreme edge as an


anchor for finding the next
q

Algorithm: GIFT WRAPPING


i0 index of the lowest point
i i0
repeat
for each j = i
Compute counterclockwise angle q from previous hull edge
k index of point with smallest q
Output (pi , pk) as a hull edge
i k
until i = i source: O’Rourke, Computational Geometry in C O(n2)
Gift Wrapping source: 91.503 textbook Cormen et al.

Output Sensitivity: O(n2) run-time is actually O(nh)


where h is the number of vertices of the convex hull.
Algorithms: 3D Gift Wrapping

O(n2) time
[output sensitive: O(nF) for F faces on hull]
CxHull Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html
Algorithms: 2D QuickHull

 Concentrate on points close to


a
hull boundary
 Named for similarity to b
Quicksort A
c
Algorithm: QUICK HULL finds one of upper or lower hull

function QuickHull(a,b,S)
if S = 0 return()
else
c index of point with max distance from ab
A points strictly right of (a,c)
B points strictly right of (c,b)
return QuickHull(a,c,A) + (c) + QuickHull(c,b,B) O(n2)
source: O’Rourke, Computational Geometry in C
Algorithms: 3D QuickHull

CxHull Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html


Algorithms: >= 2D

Convex Hull
boundary is
intersection of
hyperplanes, so
worst-case
combinatorial size
(not necessarily running
time) complexity is
in: d / 2 
( n )

Qhull: http://www.geom.umn.edu/software/qhull/
Graham’s Algorithm
source: O’Rourke, Computational Geometry in C

 Points sorted angularly provide


“star-shaped” starting point
 Prevent “dents” as you go via
convexity testing q
p0
Algorithm: GRAHAM SCAN, Version B
Find rightmost lowest point; label it p0.
Sort all other points angularly about p0.
In case of tie, delete point(s) closer to p0.
Stack S (p1, p0) = (pt, pt-1); t indexes top
i 2
while i < n do
if pi is strictly left of pt-1pt
then Push(pi,“multipop”
S) and set i i +1 O(nlgn)
Graham Scan

source: 91.503 textbook Cormen et al.


Graham Scan

33.7

source: 91.503 textbook Cormen et al.


Graham Scan

33.7

source: 91.503 textbook Cormen et al.


Graham Scan

source: 91.503 textbook Cormen et al.


Graham Scan

source: 91.503 textbook Cormen et al.


Algorithms: 2D Incremental
source: O’Rourke, Computational Geometry in C

 Add points, one at a time


 update hull for each new point
 Key step becomes adding a
single point to an existing hull.
 Find 2 tangents
 Results of 2 consecutive LEFT tests
differ
 Idea can be extended to 3D.
Algorithm: INCREMENTAL ALGORITHM
Let H2 ConvexHull{p0 , p1 , p2 }
for k 3 to n - 1 do
Hk ConvexHull{ Hk-1 U pk }
O(n2)
can be improved to O(nlgn)
Algorithms: 3D Incremental

O(n2) time
CxHull Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html
Algorithms:
2D Divide-and-Conquer
source: O’Rourke, Computational Geometry in C

 Divide-and-Conquer in a geometric
setting
 O(n) merge step is the challenge
B
 Find upper and lower tangents A
 Lower tangent: find rightmost pt of A &
leftmost pt of B; then “walk it downwards”
 Idea can be extended to 3D.

Algorithm: DIVIDE-and-CONQUER
Sort points by x coordinate
Divide points into 2 sets A and B:
A contains left n/2 points
B contains right n/2 points
Compute ConvexHull(A) and ConvexHull(B) recursively
Merge ConvexHull(A) and ConvexHull(B) O(nlgn)
Algorithms:
3D Divide and Conquer

O(n log n) time !

CxHull Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html

You might also like