You are on page 1of 38

Elective III: Computational

Geometry
Code: CSEB 604
Pritha Banerjee

Resources
Books:
Computational Geometry : Algorithms and
Applications, Mark De Berg, Marc van Kreveld, Mark
Overmars, Otfried Schwarzkopf, Springer
Computational Geometry in C, Joseph O. Rourke,
Cambridge
Lecture notes of David Mount from the web

Convex Hull
Input : a set of points P = {p1, p2, p3pn} on
a plane
Output : CH(P) , Find an unique convex
polygon whose vertices are points from P &
contains all points of P.
CH(P): List the vertices of convex polygon in
clockwise order.

Convex Hull: Naive Algo

Consider all nC2 line segments and check if rest of


the other n-2 points are to the right of the chosen line segment
Elective III : CG

Convex Hull: Naive Method


Input : P = {p1, p2, p3pn} on a plane
Output : List L ,the vertices of convex polygon in
clockwise order.
1. For all ordered pairs (p,q) PxP ( p q)
2.
valid = true; {(p,q) is edge of CH}
3.
for all points r P p or q
4.
if r lies left of line (p,q) (ordered) , edge (p,q)
is not CH edge, valid = false
5.
If valid then add (p,q) to E
6. Construct list L of vertices , sorted in clock wise order
Elective III : CG

Naive Algo:Slow Convex Hull


All the nC2 line segments => n(n-1) = n2 n pair of
points
For each pair, check all n-2 (O(n)) points are left/right
(constant time operation)
=> O(n3 )
Last step: edges in E are directed, start from any
edge, put both points in L, take end point, find edge
starting with this in E, put in L..=> n+ n-1 + n2=> O(n2 ) { can do O(nlogn) but of no use here)
Total: O(n3 ) + O(n2 ) => O(n3 )
Elective III : CG

Incremental algorithm
Take help of Incremental algorithm : std.
design technique
Add points one at a time in the Polygon ,
updating solution after each addition
Construct upper and lower hull and join them
Sort on x coordinate
Take points from left to right constructing
upper hull
Take points right to left to construct lower
hull

Graham Scan: Incremental algorithm


5

10
8

4
3

14

11

7
6

15
13

9
12

1, 2

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

4
3

14

11

7
6

15
13

9
12

1, 2, 3,
1,2,3,
Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

4
3

14

11

7
6

15
13

9
12

1, 2, 3, 4
1, 2, 4
Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

9
12

1,2,4
1, 2, 4, 5,

1,2,5

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

9
12

1, 2, 5
1, 2, 5, 6

1, 2, 5, 6, 7

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

9
12

1, 2, 5,6
1, 2, 5, 6, 7
1, 2, 5, 7

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

9
12

1, 2, 5, 7

1, 2, 5, 7, 8
1, 2, 5, 8
Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

15
13

1, 2, 5, 8
1, 2, 5, 8, 9

12

1, 2, 5, 8, 9
1, 2, 5, 8, 9, 10
1, 2, 5, 8, 10

14

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

1, 2, 5, 8, 10
12

1, 2, 5, 10

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

1, 2, 5, 10
1, 2, 5, 10, 11
1, 2, 5, 10, 11
1, 2, 5, 10, 11, 12
1, 2, 5, 10, 11, 12, 13

12

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

1, 2, 5, 10, 11, 13

12

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

1, 2, 5, 10, 11, 13
1, 2, 5, 10, 13
1, 2, 5, 10, 13, 14

12

Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

1, 2, 5, 10, 14

12

1, 2, 5, 10, 14, 15
1, 2, 5, 10, 14, 15

Upper Hull constructed


Elective III : CG

Graham Scan: Incremental algorithm


5

10
8

11

7
3
1

14

15
13

9
12

Lower Hull constructed by moving


from right to left in similar fashion
Elective III : CG

Graham Scan: Incremental algorithm


Sort points based on x coordinate: O(n log n)
Append left most points p1, p2 in L_upper
For i = 2 to n
While L_upper contains more than 2 points
Append pi to L_upper if last two points in L_upper and
pi makes right turn at last point of L_upper
Else delete the last point from L_upper

Similarly for Lower Hull L_lower


Start with pn and pn-1 in L_lower

Graham Scan: complexity

Complexity:
Sorting : O(n log n)
What data structure we use ? Stack
How many times an element is inserted or
deleted from stack for constructing L_upper?
Only once=> over the for loop : O(n)
Similarly for L_lower
Total : O(n) + O(n log n) => O(n log n)

Graham Scan: Proof of correctness


10
8

7
3

6
9

1. Consider p1.. p(i-1) are CH


pi
vertices, adding pi
14 2. p1.. p(i-1) are hull edges=> all
11
points lie below the chain
3. When pi is added, this new chain
must be above the old chain
13
;otherwise, there must be some
p(i-1)
point in the slab between p(i-1)
12
and pi that is violating hull
properties and it does not
represent a correct hull edges

Jarvis March (Gift Wrapping) :


Output Sensitive Algorithm
5

10
8

4
3

14

11

7
6

15
13

9
12

Start with one of the left-most, right-most, top-most,bottom-most point


consider a vertical line through this point
Rotate clockwise until it hits a point , that becomes next point in CH

Jarvis March (Gift Wrapping) :


Output Sensitive Algorithm
5

10
8

4
3

14

11

7
6

15
13

9
12

Start with one of the left-most, right-most, top-most,bottom-most point


consider a vertical line through this point
Rotate clockwise until it hits a point , that becomes next point in CH

Jarvis March (Gift Wrapping) :


Output Sensitive Algorithm
5

10
8

4
3

14

11

7
6

15
13

9
12

Start with one of the left-most, right-most, top-most,bottom-most point


consider a vertical line through this point
Rotate clockwise until it hits a point , that becomes next point in CH

Jarvis March (Gift Wrapping) :


Output Sensitive Algorithm
5

10
8

4
3

14

11

7
6

15
13

9
12

Jarvis March (Gift Wrapping) :


Output Sensitive Algorithm
5

10
8

4
3

14

11

7
6

15
9

13

12
Complexity :
Finding one of the extreme point : O(n)
Finds next Hull vertex by rotation=> O(n)
There are h hull vertex=> O(hn)
Time complexity : O(nh), If h is O(log n) => same as Graham Scan

Convex Hull
Input : a set of points P = {p1, p2, p3pn} on
a plane
Output : CH(P) , Find an unique convex
polygon whose vertices are points from P &
contains all points of P.
CH(P): List the vertices of convex polygon in
clockwise order.

Given two CH: how to Find


Combined Hull?

Resources
Books:
Computational Geometry in C, Joseph O. Rourke,
Cambridge, Pages: 91-95, Section 3.8 and Pages: 6972, Section 3.4
Lecture notes of David Mount from the web, Pages: 1417

32

Given two CH: how to Find


Combined Hull?
Upper
tangent
HA

HB
Lower
tangent

Find upper and lower tangent for both HA and HB disregarding all other
points lying between lower and upper tangents

Given two CH: how to Find


Combined Hull?
p1

p2

p3
HB

Tangent : if p1 and p3 are on the same side


of the tangent line at p2

HA
a-1 a+1
a

b-1

b+1
b

ab is Lower Tangent: if point (a-1) , (a+1) are on the left


(Orient() is +ve) of segment ab and point (b-1), (b+1) are also on the left
ab is Upper Tangent: if point (a-1) , (a+1) are on the right
(Orient() is -ve) of segment ab and point (b-1), (b+1) are also on the right
How to find lower tangent for two hulls: Check all possible pairs? O(n2)

Given two CH: how to Find


Combined Hull?
3
4

4
3

HA

HB

6
0

1
0
a = right most point of left Hull A
b = left most point of right Hull B
While T = ab is not lower tangent to both A and B
while T not lower tangent to A then a = a-1
while T not lower tangent to B then b = b + 1
Return ab

Complexity: finding lower tangent


Finding the leftmost and rightmost point in
HA and HB is : O(n)
Each vertex on the hull vertices are visited
only once: O(n)

Convex hull: divide and conquer


Applicable to any dimension
Like merge sort
Sort points P by x coordinate : O(n log n)
CH(S)
If |P| 3, CH(P) is a triangle
Else partition P into 2 sets A ( lowest x
coordinates) and B ( highest x coordinates)
Recursively compute HA= CH(A) , HB= CH(B)
Merge two hulls into CH by computation of
upper and lower tangents of HA and HB

Time complexity

Sorting : O(n log n)


T(n): time complexity for hull algo
Merge step : O(n)
T(n) = 2T(n/2) + O(n) = O(n log n)
Time complexity is O(n log n)

Elective III : CG

You might also like