convex hulls - department of computer sciencehacamero/convexhulls.pdf · convex hull: informally...
TRANSCRIPT
Convex Hulls
Helen Cameron
Helen Cameron Convex Hulls 1/101
What Is a
Convex Hull?
Starting Point: Points in 2D
x
y
Helen Cameron Convex Hulls 3/101
Convex Hull: Informally
Imagine that the x , y -plane is a board and the points are nailssticking out of the board. If you stretch an elastic so that all thenails are inside it and then let go of the elastic, the elastic willtighten into the boundary of the convex hull of the points.
x
y
Helen Cameron Convex Hulls 4/101
Starting Point: Simple Polygons
x
yPolygon: A sequenceof points connectedby edges into a closed“curve”.
Simple: Only consec-utive edges intersect(at their common end-points).
Helen Cameron Convex Hulls 5/101
Starting Point: Simple Polygons
x
y
For comparison, here’s anon-simple polygon.
Helen Cameron Convex Hulls 6/101
Inside and Outside
x
y
A simple polygon has aninside and an outside.
The inside is always onyour left if you walk theboundary in a counter-clockwise direction
Helen Cameron Convex Hulls 7/101
Interior Angles
x
yThe interior angle at apoint is the angle insidethe polygon between thepoint’s adjacent edges.
Helen Cameron Convex Hulls 8/101
Convex Angles
x
y
A convex angle is anangle < 180 degrees (πin radians).
convex
not convex
Helen Cameron Convex Hulls 9/101
Convex Polygon
x
y
A convex polygon hasonly convex interior an-gles.
Helen Cameron Convex Hulls 10/101
Convex Polygon
x
y Equivalent definition: A polygon isconvex if, for any two points in oron the polygon, the line segmentconnecting the two points is entirelycontained in the polygon.
Helen Cameron Convex Hulls 11/101
Convex Hull: Formally
The convex hull of a set of 2D points is the smallest convexpolygon that contains the points.
x
y
Helen Cameron Convex Hulls 12/101
Why Care About
Convex Hulls?(Some applications!)
Convex Hulls Are Easier
Collision avoidance is faster and easier using the convex hull of arobot or a car than working with its exact (more complex) shape.
SevenTransparent.jpg By Brian
Snelson (originally posted to Flickr
as My car photoshopped) [CC BY
2.0 (https://creativecommons.
org/licenses/by/2.0)], via
Wikimedia Commons.
Helen Cameron Convex Hulls 14/101
Removing Outliers
Often, we want to estimate someparameter from a small,randomly-chosen sample of apopulation of points.
This sort of estimation is used,for example, in medicaldiagnostics and network intrusiondetection.
File:Pankreas-Ca im CT -
Coeliacusblockade.png By Hellerhoff
[CC BY 2.0
(https://creativecommons.org/
licenses/by/2.0)], via Wikimedia
Commons.
Helen Cameron Convex Hulls 15/101
Removing Outliers
Some parameter estimations arevery sensitive to “outliers”(points that are far from most ofthe other points in thepopulation).
Example outliers
Helen Cameron Convex Hulls 16/101
Removing Outliers
Get rid of outlier points in 2D:Repeatedly find and then “peel”off the convex hull, until no morethan some chosen fraction of theoriginal points remains.
Then you can safely estimate thedesired parameter.
Originalpoints
Outermosthull
Remainingpoints
Next hull
Points to estimatefrom:
Helen Cameron Convex Hulls 17/101
Finding the Convex Hull:
Input and Output
The Input (If You’re Writing a Program)
Input: A list of 2D points (px , py ), in no particular order.
Example input:
(9.8,10.6) (7,13) (12.8,8.7)(10.35,12.1) (11.2, 14.5) (8,11)(7.5,9.8) (9.1,12.2) (7.8,7.3)(10.2,9.5) (11.7,9.6) (9.6,9.1)(9.5,7.8) (8.55,10.2) (10.7,10.1)(12.55,8.3) (8.85,9.1) (8.4,13.8)(10,8.7) (12.2,10.7) (11, 10.4)(10.55,8.3)
Helen Cameron Convex Hulls 19/101
The Output (If You’re Writing a Program)
Output: The points on the boundary of the convex hull of theinput points, listed in the order of a counter-clockwise traversal ofthe boundary.
Example output: (7,13) → (7.8,7.3) → (12.55,8.3) → (12.8,8.7)→ (11.2, 14.5) → (8.4,13.8) → (7,13).
Helen Cameron Convex Hulls 20/101
Left, Right, or Collinear?
Leftness Testing for a Line Segment
Useful test when finding the convex hull: Is point q to the left,
right or collinear with a directed line segment−→a, b — that is, if
we’re standing on endpoint a and facing the other endpoint b, is qto our left, to our right or somewhere on the infinite line through aand b?
a
bq0
q1
q2
Helen Cameron Convex Hulls 22/101
Leftness Testing from a Line Segment
A method that returns true if q is to the left of−→a, b (returns false
otherwise):
boolean isLeft( Point q, Point a, Point b ) {return 0 < (b.x-a.x)*(q.y-a.y) - (b.y-a.y)*(q.x-a.x);
}
A method that returns true if q is to the right of−→a, b (returns false
otherwise):
boolean isRight( Point q, Point a, Point b ) {return 0 < (b.x-a.x)*(q.y-a.y) - (b.y-a.y)*(q.x-a.x);
}
A method that returns true if q is collinear with−→a, b (returns false
otherwise):
boolean isCollinear( Point q, Point a, Point b ) {return 0 == (b.x-a.x)*(q.y-a.y) - (b.y-a.y)*(q.x-a.x);
}Helen Cameron Convex Hulls 23/101
Leftness Testing from a Line Segment
The leftness test is using the “right-hand rule”:
Lay the outer edge of your right hand along−→a, b (fingers
pointing from a to b) with your thumb sticking up.
If your fingers naturally curl towards q, then q is to the left.
The right-hand rule has to do with the “cross product” of twovectors . . .
Helen Cameron Convex Hulls 24/101
A Vector
A vector ~p is a directed line segment starting at the origin andending at point p.
Note: We’ll work with a directed line segment starting at somepoint a and ending at point b — we’ll have to translate a to theorigin by subtracting a from all points. (Really: subtract ax fromall points’ x-coordinates and subtract ay from all points’y -coordinates).
Helen Cameron Convex Hulls 25/101
Area of a Triangle: Vector Cross Product
The cross product p × q of two vectors ~p = (px , py , pz) and~q = (qx , qy , qz) only makes sense in 3D: it is the vectorperpendicular to both p and q given by the “right-hand rule” andwhose magnitude is given by the absolute value of∣∣∣∣∣∣~x ~y ~zpx py pzqx qy qz
∣∣∣∣∣∣ = (pyqz−pzqy )~x+(pzqx−pxqz)~y+(pxqy−pyqx)~z .
Note: ~x is the unit vector in the positive x-direction, ~y is the unitvector in the positive y -direction, and ~z is the unit vector in thepositive z-direction.
Helen Cameron Convex Hulls 26/101
Area of a Triangle: Vector Cross Product
For 2D vectors, the z-coordinates pz = qz = 0 and thecross-product formula reduces to
(pxqy − pyqx)~z .
If point q is to the left of vector ~p, then pxqy − pyqx ispositive (follows right-hand rule).
If point q is to the right, then pxqy − pyqx is negative(perpendicular in the opposite direction).
If point q is collinear with vector ~p, then pxqy − pyqx is zero.
Helen Cameron Convex Hulls 27/101
Examples
Example 1: If ~p =−−−→(3, 1) and q = (2, 4), then the calculation gives
(pxqy − pyqx)~z = (3× 4− 1× 2)~z = 10~z .
Conclusion: q is to the left of ~p.
(0, 0)
p
q
Helen Cameron Convex Hulls 28/101
Examples
Example 2: If ~p =−−−−→(−1, 4) and q = (2, 4), then the calculation is
(pxqy − pyqx)~z = (−1× 4− 4× 2)~z = −12~z .
Conclusion: q is to the right of ~p.
(0, 0)
p q
Helen Cameron Convex Hulls 29/101
Examples
Example 3: If ~p =−−−→(1, 1) and q = (2, 2), then the calculation gives
(pxqy − pyqx)~z = (1× 2− 1× 2)~z = 0~z .
Conclusion: q is collinear with ~p.
(0, 0)
p
q
Helen Cameron Convex Hulls 30/101
Leftness Testing and Line Segments
We’re usually working not with a vector (tail at the origin andhead at some point p), but with a directed line segment (tail atsome point a and head at some other pont b).
To test if point q is to the left of directed line segment−→a, b, we
take the cross product of vectors−−−→b − a and
−−−→q − a.
Helen Cameron Convex Hulls 31/101
Leftness Testing and Line Segments
Example: If we want to know if point q = (4, 5) is to the left of
line segment−→a, b, where a = (2, 1) and b = (5, 2), we translate to
asking if point q − a = (2, 4) is to the left of directed line segment−−−−−−−−→a− a, b − a = vector
−−−→b − a =
−−−→(3, 1) — this translation is
Example 1 above:
(0, 0)
b − a
q − a
a
b
q
Helen Cameron Convex Hulls 32/101
Graham’s Scan
Graham’s Scan: Overview
Graham’s scan: Finds a convex hull of a set S of two-dimensionalpoints.
Example input: Find the convex hull of (8, 8), (2, 4), (0, 11),(5, 6), (6, 10), (10, 5), (1, 6), (5, 12),(2, 8), and (9, 13):
(0, 11)
(1, 6)
(2, 8)
(2, 4)
(5, 12)
(6, 10)
(8, 8)
(9, 13)
(10, 5)
(5, 6)
Helen Cameron Convex Hulls 34/101
Graham’s Scan: Overview
Graham’s scan uses a list:
Each point is pushed onto the end of the list once, andeventually popped off the end of the list if the point is not onthe convex hull.
At the end of the algorithm, the list contains thecounterclockwise ordering of points on the hull, from thebeginning to the end of the list.
Helen Cameron Convex Hulls 35/101
Graham’s Scan: Overview
Example Output: For the previous example points, Graham’sscan returns (2, 4), (10, 5), (9, 13), (0, 11), and (1, 6) (in thatorder), because that’s what its list contains (from beginning to endof the list) at the end of Graham’s scan:
(0, 11)
(1, 6)
(2, 8)
(2, 4)
(5, 12)
(6, 10)
(8, 8)
(9, 13)
(10, 5)
(5, 6)
Helen Cameron Convex Hulls 36/101
Graham’s Scan Has Four Steps
Step 1: Find the point p0 with the lowest y -coordinate.
(0, 11)
(1, 6)
(2, 8)
(2, 4)p0
(5, 12)
(6, 10)
(8, 8)
(9, 13)
(10, 5)
(5, 6)
Helen Cameron Convex Hulls 37/101
Graham’s Scan Has Four Steps
Step 2: Sort the points by angle around p0 (that is, by “polarcoordinates”).
p7
p8
p6
p0
p5
p4
p2
p3
p1
discarded
Helen Cameron Convex Hulls 38/101
Graham’s Scan Has Four Steps
Step 3: Initialize the list L to contain p0, p1, and p2 (in thatorder):
p7
p8
p6
p0
p5
p4
p2
p3
p1
discarded
Helen Cameron Convex Hulls 39/101
Graham’s Scan Has Four Steps
Step 4: Process each of the other points in order, p3, p4, . . . ,pm−1.
When we’re processing point pi :(4a) Loop:
Suppose points p2nd last and plast are the last two points onthe end our list L.
Remove plast from the end of L if pi (the point we’reprocessing) is NOT to the left of directed line segment−−−−−−−→p2nd lastplast.
Repeat this until pi is to the left of −−−−−−−→p2nd lastplast.(4b) Add pi to the end of L.
Helen Cameron Convex Hulls 40/101
Graham-Scan( S )
1. Find p0, the point in S with the lowest y-coordinate(break ties by choosing the leftmost such point).
2. Sort the other points in S by polar angle around p0
(if two or more points have the same polar angle,
remove all but the one farthest from p0).
Let the sorted order be p1, p2, . . . , pm−1.
3. L = empty list.
Add p0 to the end of L.
Add p1 to the end of L.
Add p2 to the end of L.
4. For each point pi in order from p3 to pm−1 do
4a. While pi is not left of directed
line segment −−−−−−−−−−−→p 2nd lastp last
Remove the point p last from the end of L.
4b. Add pi to the end of L.
Step 1: Finding Lowest Point p0
Question: How do we find the point p0 with the lowesty -coordinate?
(0, 11)
(1, 6)
(2, 8)
(2, 4)p0
(5, 12)
(6, 10)
(8, 8)
(9, 13)
(10, 5)
(5, 6)
Answer: Simply loop through the points, looking only at they -coordinates of each point (do a findLowest).
Helen Cameron Convex Hulls 42/101
Step 1: Finding Lowest Point p0
Question: How do we find the point p0 with the lowesty -coordinate?
findLowest( Point[] S ) returns int {int minIndex = 0; // Assume S[0] is p0
for ( int i = 1; i < S.length; i++ )
{if (S[i].y < S[minIndex].y )
minIndex = i;
else if ((S[i].y == S[minIndex].y)
&& (S[i].x < S[minIndex].x))
minIndex = i;
} // end for
return minIndex;
} // end findLowest
At the end, S[minIndex] is the point with the lowest y -coordinate(in case of ties, it’s the leftmost of the lowest points).
Helen Cameron Convex Hulls 43/101
Step 2: Sorting by Polar Coordinate
Question: How do we sort by polar coordinate?
p7
p8
p6
p0
p5
p4
p2
p3
p1
discarded
Answer: Use any sorting algorithm such as merge sort or quicksort.
Helen Cameron Convex Hulls 44/101
Step 2: Sorting by Polar Coordinate
In the sorting algorithm, when comparing two points pi and pj todecide which comes first in the sorted order (“Is pi before pj in thesorted order?”):
Test if pj is to the left of directed line segment −−−→p0, pi usingthe cross product method.
If pj is to the left, then pi comes before pj in the sorted order.
If pj is to the right of −−−→p0, pi , then pi comes after pj in thesorted order.
If the cross product gives 0, then p0, pi and pj are collinear.Compute the distance of pi and pj from p0 and discard thecloser one.
Helen Cameron Convex Hulls 45/101
Step 2: Sorting by Polar Coordinate
Further note: If the cross product gives 0, do not perform thesquare root in the calculation of the distance: Just discard pi if
(pi .x − p0.x)2 + (pi .y − p0.y)2
is smaller than
(pj .x − p0.x)2 + (pj .y − p0.y)2;
otherwise, discard pj .
Computing square roots is too expensive and we do not need toknow the exact distance measurement, we just need to know whichis closer to p0.
Helen Cameron Convex Hulls 46/101
Graham’s Scan Example
Example: Given the following set of points, compute the convexhull.
Helen Cameron Convex Hulls 47/101
Graham’s Scan Example
Example (continued): First choose p0 to be the leftmost pointamong the two points with the same lowest y -coordinate. Thensort the remaining points by polar angle:
0p
1p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 48/101
Graham’s Scan Example
Example (continued): Now add p0, p1, p2 on the list L.
0p
1p
2p
3p
4p
5p
6p
7p
8p
Next, consider points p3, . . . , p8 in turn.
Helen Cameron Convex Hulls 49/101
Graham’s Scan Example
Example (continued): Consider point p3: Because p2, p3 is nota left turn from p1, p2, remove p2 from the list L.
0p
1p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 50/101
Graham’s Scan Example
Example (continued): Still considering point p3: Becausep1, p3 is a left turn from p0, p1, add p3 onto the list L.
0p
1p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 51/101
Graham’s Scan Example
Example (continued): Consider point p4: Because p3, p4 is aleft turn from p1, p3, add p4 onto the list L.
0p
1p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 52/101
Graham’s Scan Example
Example (continued): Consider point p5: Because p4, p5 is nota left turn from p3, p4, remove p4 from the list L.
0p
1p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 53/101
Graham’s Scan Example
Example (continued): Still considering point p5: Becausep3, p5 is a left turn from p1, p3, add p5 onto the list L.
0p 1
p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 54/101
Graham’s Scan Example
Example (continued): Consider point p6: Because p5, p6 is aleft turn from p3, p5, add p6 onto the list L.
0p 1
p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 55/101
Graham’s Scan Example
Example (continued): Consider point p7: Because p6, p7 is nota left turn from p5, p6, remove p6 from the list L.
0p 1
p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 56/101
Graham’s Scan Example
Example (continued): Still considering point p7: Becausep5, p7 is a left turn from p3, p5, add p7 onto the list L.
0p
1p
2p
3p
4p
5p
6p
7p
8p
Helen Cameron Convex Hulls 57/101
Graham’s Scan Example
Example (continued): Consider point p8: Because p7, p8 is aleft turn from p5, p7, add p8 onto the list L.
0p 1
p
2p
3p
4p
5p
6p
7p
8p
The convex hull is represented by the counterclockwise ordering
p0, p1, p3, p5, p7, p8.
Helen Cameron Convex Hulls 58/101
What Is Step 4(a) Doing?
Idea: When Graham’s scan is about to process pi , list L containsthe convex hull of the points previously processed p0, . . . , pi−1.Graham’s scan tries to add the next point pi by simply addingtriangle p0, pi−1, pi to the convex hull.
p0 p1
pi−1
pi
CH(p0 − pi−1)
Helen Cameron Convex Hulls 59/101
What Is Step 4(a) Doing?
Idea: But the result is not a convex hull if pi is not a left turnfrom the previous edge in CH(p0, . . . , pi−1)). Graham’s Scan fixesthe convexity problem by adding more triangles on the outside ofthe polygon (“filling in” non-left turns to pi with another triangle):
p0 p1
pk
pj
pi−1
pi
CH(p0 − pi−1)
Helen Cameron Convex Hulls 60/101
How Fast Is Graham’s Scan?
To find the convex hull of n points using Graham’s Scan:
Running time = Time to find p0 + Time to sort by polar angle
+ Time for adds, removes, and left-turn tests
= O(n) + O(n log n) + O(n)
= O(n log n).
Helen Cameron Convex Hulls 61/101
Divide and Conquer
Convex Hull
Divide and Conquer Algorithm
This Preparata and Hong (1977) algorithm can be extended tothree or higher dimensions.
To keep things simple, assume:
No three points lie on a line. (No three points are collinear.)
No two points lie on a vertical line.
These assumptions can be removed, but they make theexplanations simpler, so we will use them.
Helen Cameron Convex Hulls 63/101
Divide and Conquer Algorithm: The Idea
The algorithm sorts the points by x-coordinate.
The algorithm then builds many small convex hulls of small groupsof adjacent points. These small convex hulls will not intersect eachother — they’re disjoint.
Then the algorithm merges (joins together) adjacent small convexhulls to make bigger convex hulls of bigger groups of adjacentpoints.
The algorithm keeps merging adjacent convex hulls until it hasONE convex hull of all the points.
Helen Cameron Convex Hulls 64/101
Step 1: Sort the Points
Step 1 gets as its input an array p of points:
(8, 3) (−1, 4) (0, 1) (5, 2) (4, 4) (6, 0) (3, 6)
0 1 2 3 4 5 6
Here’s the input points plotted:
p[0]
p[1]
p[2]
p[3]
p[4]
p[5]
p[6]
You can see that the points are not in any particular order.Helen Cameron Convex Hulls 65/101
Step 1: Sort the Points
Step 1 sorts the points into ascending order by x-coordinate:
(−1, 4) (0, 1) (3, 6) (4, 4) (5, 2) (6, 0) (8, 3)
0 1 2 3 4 5 6
Here’s the points plotted, so you can see that they’re now sorted.
p[6]
p[0]
p[1]
p[4]
p[3]
p[5]
p[2]
Helen Cameron Convex Hulls 66/101
Step 1: Sort the Points
Step 1 can use any sorting method (e.g., quick sort) — when thesorting algorithm wants to compare two items (points), it simplycompares their x-coordinates.
Helen Cameron Convex Hulls 67/101
Step 2: Pair Up the Points
Step 2 pairs adjacent points to form edges. (If there’s an oddnumber of points, group the last three points into a triangle.)
p[6]
p[0]
p[1]
p[4]
p[3]
p[5]
p[2]
Helen Cameron Convex Hulls 68/101
Step 2: Pair Up the Points
An edge is a “degenerate” convex hull of the pair of points.
Consequence: Step 2 is making the list of points into a list ofconvex hulls of small groups of points.
Helen Cameron Convex Hulls 69/101
Step 2: Pair Up the Points
Note: Make sure you get a counter-clockwise ordering of thepoints of a triangle.
When p[i+2] is left of−−−−−−−−−→p[i],p[i+1]
p[i]
p[i+1]
p[i+2]
Order: p[i],p[i+1],p[i+2]
p[i]
p[i+1]
p[i+2]
When p[i+2] is right of−−−−−−−−−→p[i],p[i+1]
p[i]
p[i+1]
p[i+2]
Order: p[i],p[i+2],p[i+1]
p[i]
p[i+1]
p[i+2]
Helen Cameron Convex Hulls 70/101
Step 3: Merge Pairs of Hulls
Step 3 does the following Pair-and-Merge step over and overagain until we have ONE convex hull:
Pair up adjacent small convex hulls and merge each pair into oneconvex hull (more on merging in a minute).
Before Pair-and-Merge 1:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Helen Cameron Convex Hulls 71/101
Step 3: Merge Pairs of Hulls
Step 3 pairs up adjacent small convex hulls and merges each pairinto one convex hull (more on merging in a minute).
After Pair-and-Merge 1:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Helen Cameron Convex Hulls 72/101
Step 3: Merge Pairs of Hulls
Step 3 pairs up adjacent small convex hulls and merges each pairinto one convex hull (more on merging in a minute).
After Pair-and-Merge 2:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Helen Cameron Convex Hulls 73/101
Step 3: Merge Pairs of Hulls
Step 3 pairs up adjacent small convex hulls and merges each pairinto one convex hull (more on merging in a minute).
After Pair-and-Merge 3:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Helen Cameron Convex Hulls 74/101
Step 3: Merge Pairs of Hulls
Step 3 pairs up adjacent small convex hulls and merges each pairinto one convex hull (more on merging in a minute).
After Pair-and-Merge 4:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Helen Cameron Convex Hulls 75/101
Step 3: Merge Pairs of Hulls
Step 3 Each time we pair and merge, we are halving the numberof convex hulls we have and doubling the number of points thateach convex hull contains.
If we have n points, we can do that at most log2 n times.
Helen Cameron Convex Hulls 76/101
Step 3: Finding Tangent Lines to Merge Hulls
The merge step: How do we merge two non-overlapping convexhulls separated by an imaginary vertical line?
We find two “tangent lines”, one supporting the two hulls belowand the other resting on the two hulls above.
0
12
3
4
5
6
7
8
9
10
0
1
2
34
56
789
10
11
12
LR
Helen Cameron Convex Hulls 77/101
Finding the Lower Tangent Line
Step 3: Finding Tangent Lines to Merge Hulls
What is the lower tangent line T? The two points on L’s hullimmediately before and after T ’s endpoint in L are both to the leftof T (when T is directed from L to R).The two points on R’s hull immediately before and after T ’sendpoint in R are both to the left of T (when T is directed from Lto R).
0
12
3
4
5
6
7
8
9
10
0
1
2
34
56
789
10
11
12
LR
Helen Cameron Convex Hulls 79/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: Start by connecting therightmost point of L with the leftmost point of R with a linesegment T .
L R
T
Helen Cameron Convex Hulls 80/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) Then “walk” Tdownward . . .
Repeat while T is not tangent to both hulls at the same time:
Move T ’s endpoint down L’s hull while T is not tangent toL’s hull, then
Move T ’s endpoint down R’s hull while T is not tangent toR’s hull.
Helen Cameron Convex Hulls 81/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) Move T down Lwhile T is not tangent to L:
L R
Helen Cameron Convex Hulls 82/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) Now move T downR while T is not tangent to R:
L R
Helen Cameron Convex Hulls 83/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) Move T down Lagain, while T is not tangent to L:
L R
Helen Cameron Convex Hulls 84/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) When we try tomove T down R again, we see that T is already tangent to R.Now T is the lower tangent line because it is tangent to both Land R at the same time!
L R
Helen Cameron Convex Hulls 85/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) Let l be the index ofthe endpoint of T in L and r be the endpoint of T in R.
L R
Tl
r
Helen Cameron Convex Hulls 86/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) Assume that, ineach hull, the points are indexed from 0 and that arithmetic onindices is done using modular arithmetic so that we can say l − 1and r + 1 and get the index of the next point.
01
2
5
6
7
L
0
1
2
3
6
R
Tl = 4
l − 1 = 3
4 = r
5 = r + 1
Helen Cameron Convex Hulls 87/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) Notice that movingfrom l to l − 1 is moving clockwise in L and moving from r tor + 1 is moving counterclockwise in R.
01
2
5
6
7
L
0
1
2
3
6
R
Tl = 4
l − 1 = 3
4 = r
5 = r + 1
Helen Cameron Convex Hulls 88/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued) T will be tangent toboth hulls when l − 1 and l + 1 both lie above T (are to the left oflr) and when r − 1 and r + 1 lie above T (to the left of lr).
2
3
4
5
6
L
2
3
4
5
R
Tl = 0
l − 1 = 7
1 = l + 10 = r
1 = r + 1
r − 1 = 6
Helen Cameron Convex Hulls 89/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line: (continued)
Algorithm LowerTangent
1. l = rightmost point of L /* maximum x-coordinate */
2. r = leftmost point of R /* minimum x-coordinate */
3. while (T=lr is not lower tangent to both L and R) {4. while (T is not lower tangent to L) {5. l=l-1;6. }7. while (T is not lower tangent to R) {8. r=r+1;9. }10. }
Helen Cameron Convex Hulls 90/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the lower tangent line:
0
12
3
4
5
6
7
8
9
10
0
1
234567
8
9
10
L R
Helen Cameron Convex Hulls 91/101
Finding the Upper Tangent Line
Step 3: Finding Tangent Lines to Merge Hulls
To find the upper tangent line: We find the upper tangentsimilarly — we walk T up until it is upper tangent to both hulls.
Note: Instead of considering T to be the directed line segmentfrom l to r , we consider T to be the directed line segment from rto l . T is tangent if the before and after points on L and R are to
the left of−→rl .
Helen Cameron Convex Hulls 93/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the upper tangent line: Again, start by connecting therightmost point of L with the leftmost point of R with a linesegment T .
L R
Tl
r
Helen Cameron Convex Hulls 94/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the upper tangent line: Move T = rl up L while T isnot tangent to L:
L R
T
l
r
Helen Cameron Convex Hulls 95/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the upper tangent line: Then move T = rl up R whileT is not tangent to R:
L R
Tl r
Helen Cameron Convex Hulls 96/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the upper tangent line: Since T is not yet tangent toboth hulls at the same time, move T = rl up L while T is nottangent to L:
L R
Tlr
Helen Cameron Convex Hulls 97/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the upper tangent line: When we try to move T up Ragain, we see that T is already tangent to R. Now T is the uppertangent line because it is tangent both hulls at the same time,
L R
Tlr
Helen Cameron Convex Hulls 98/101
Step 3: Finding Tangent Lines to Merge Hulls
To find the upper tangent line:
Algorithm UpperTangent
1. l = rightmost point of L /* maximum x-coordinate */
2. r = leftmost point of R /* minimum x-coordinate */
3. while (T=rl is not upper tangent to both L and R) {4. while (T is not upper tangent to L) {5. l=l+1;6. }7. while (T is not upper tangent to R) {8. r=r-1;9. }10. }
Helen Cameron Convex Hulls 99/101
Merging the Two Convex Hulls
Step 3: Merging Hulls with Tangent Lines
The merged convex hull: Once we’ve found the two tangents’endpoints, we simply copy the pieces of L’s boundary and R’sboundary and the tangent lines into the boundary of the convexhull of L and R merged.
0
12
3
4
5
6
7
8
9
10
0
1
2
34
56
789
10
11
12
LR
Helen Cameron Convex Hulls 101/101