SlideShare a Scribd company logo
1 of 179
Download to read offline
Analysis of Algorithms
Computational Geometry
Andres Mendez-Vazquez
November 16, 2015
1 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
2 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
3 / 88
Computational Geometry
Motivation
We want to solve geometric problems!!!
4 / 88
Field of Application
VLSI design - Generation for Fast Voronoi Diagrams for Massive
Layouts Under Strict Distances to avoid Tunneling Effects!!
5 / 88
Field of Application
Databases - Octrees for fast localization of information in database
tables
6 / 88
Field of Application
Synthetic Biology - Geometric Algorithms to Obtain new DNA
configurations for Molecular Machines
7 / 88
Field of Application
Computer Graphics for more engaging Virtual Environments - For
example: Bump Mapping!!!
8 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
9 / 88
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 88
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 88
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 88
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 88
The Plane Representation
Although 3D algorithms exist...
We will deal only with algorithms working in the plane.
Object Representation
Each object is a set of points {p1, p2, ..., pn} where
pi = (xi, yi) and xi, yi ∈ R.
Example
For example an n-vertex polygon P is the following order sequence:
p0, p2, ..., pn
10 / 88
Example
Polygon
11 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
12 / 88
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 88
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 88
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 88
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 88
Line-segment Properties
A convex combination
Given two distinct points p1 = (x1, y1)T
and p2 = (x2, y2)T
, a convex
combination of {p1, p2} is any point p3 such that:
p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1.
Line Segment as Convex Combination
Given two points p1 and p2 (Known as End Points), the line segment
p1p2 is the set of convex combinations of p1 and p2.
Directed Segment
Here, we care about the direction with initial point p1 for the directed
segment −−→p1p2:
If p1 = (0, 0) then −−→p1p2 is the vector p2.
13 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
14 / 88
Cross Product
Question!!!
Given two directed segments −−→p0p1 and −−→p0p2,
Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?
Cross Product
Cross product p1 × p2 as the signed area of the parallelogram formed
by
15 / 88
Cross Product
Question!!!
Given two directed segments −−→p0p1 and −−→p0p2,
Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?
Cross Product
Cross product p1 × p2 as the signed area of the parallelogram formed
by
15 / 88
Cross Product
Question!!!
Given two directed segments −−→p0p1 and −−→p0p2,
Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?
Cross Product
Cross product p1 × p2 as the signed area of the parallelogram formed
by
15 / 88
Cross Product
A shorter representation
p1 × p2 = det p1 p2 =det
x1 x2
y1 y2
= x1y2 − x2y1 = −p2 × p1
Thus
if p1 × p2 is positive, then p1 is clockwise from p2.
if p1 × p2 is negative, then p1 is counterclockwise from p2.
16 / 88
Cross Product
A shorter representation
p1 × p2 = det p1 p2 =det
x1 x2
y1 y2
= x1y2 − x2y1 = −p2 × p1
Thus
if p1 × p2 is positive, then p1 is clockwise from p2.
if p1 × p2 is negative, then p1 is counterclockwise from p2.
16 / 88
Cross Product
A shorter representation
p1 × p2 = det p1 p2 =det
x1 x2
y1 y2
= x1y2 − x2y1 = −p2 × p1
Thus
if p1 × p2 is positive, then p1 is clockwise from p2.
if p1 × p2 is negative, then p1 is counterclockwise from p2.
16 / 88
Regions
Clockwise and Counterclockwise Regions
Figure: Darker counterclockwise; lighter clockwise with respect to p
17 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
18 / 88
Turn Left or Right
Question
Given two line segments −−→p0p1 and −−→p1p2,
if we traverse −−→p0p1 and then −−→p1p2, do we make a left turn at point p1?
19 / 88
Turn Left or Right
Simply use the following idea
Compute cross product (p2 − p0) × (p1 − p0)!!!
This translates p0 to the origin!!!
What about (p2 − p0) × (p1 − p0) = 0?
Left Turn = counterclockwise; Right Turn = clockwise
20 / 88
Turn Left or Right
Simply use the following idea
Compute cross product (p2 − p0) × (p1 − p0)!!!
This translates p0 to the origin!!!
What about (p2 − p0) × (p1 − p0) = 0?
Left Turn = counterclockwise; Right Turn = clockwise
20 / 88
Turn Left or Right
Simply use the following idea
Compute cross product (p2 − p0) × (p1 − p0)!!!
This translates p0 to the origin!!!
What about (p2 − p0) × (p1 − p0) = 0?
Left Turn = counterclockwise; Right Turn = clockwise
20 / 88
Turn Left or Right
Simply use the following idea
Compute cross product (p2 − p0) × (p1 − p0)!!!
This translates p0 to the origin!!!
What about (p2 − p0) × (p1 − p0) = 0?
Left Turn = counterclockwise; Right Turn = clockwise
counterclockwise clockwise
20 / 88
Code for this
We have the following code
Direction(pi, pj, pk)
1 return (pk − pi) × (pj − pi)
21 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
22 / 88
Intersection
Question
Do line segments −−→p1p2 and −−→p3p4 intersect?
Very Simple!!! We have two possibilities
1 Each segment straddles the line containing the other.
2 An endpoint of one segment lies on the other segment.
23 / 88
Intersection
Question
Do line segments −−→p1p2 and −−→p3p4 intersect?
Very Simple!!! We have two possibilities
1 Each segment straddles the line containing the other.
2 An endpoint of one segment lies on the other segment.
23 / 88
Intersection
Question
Do line segments −−→p1p2 and −−→p3p4 intersect?
Very Simple!!! We have two possibilities
1 Each segment straddles the line containing the other.
2 An endpoint of one segment lies on the other segment.
23 / 88
Case I This summarize the previous two possibilities
The segments straddle each other’s lines.
Oposite Signs
Figure: Using Cross Products to find intersections
24 / 88
Case II No intersection
The segment straddles the line, but the other does not straddle the
other line
Same Signs
Figure: Using Cross Products to find that there is no intersection
25 / 88
Code
Code
Segment-Intersection(p1, p2, p3, p4)
1 d1 = Direction (p3, p4, p1)
2 d2 = Direction (p3, p4, p2)
3 d3 = Direction (p1, p2, p3)
4 d4 = Direction (p1, p2, p4)
5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0) and
6 (d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0))
7 return TRUE
Figure: The Incomplete Code, You still need to test for endpoints over the
segment
26 / 88
Code
Code
Segment-Intersection(p1, p2, p3, p4)
1 d1 = Direction (p3, p4, p1)
2 d2 = Direction (p3, p4, p2)
3 d3 = Direction (p1, p2, p3)
4 d4 = Direction (p1, p2, p4)
5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0) and
6 (d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0))
7 return TRUE
Figure: The Incomplete Code, You still need to test for endpoints over the
segment
26 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
27 / 88
Sweeping
Sweeping
Use an imaginary vertical line to pass through the n segments with events
x ∈ {r, t, u}:
Figure: Vertical Line to Record Events
28 / 88
Thus
This can be used to record events given two segments s1 and s2
Event I: s1 above s2 at x, written s1 x s2.
This is a total preorder relation for segment intersecting the
line at x.
The relation is transitive and reflexive.
Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or
both (if s1 and s2 intersect at x)
29 / 88
Thus
This can be used to record events given two segments s1 and s2
Event I: s1 above s2 at x, written s1 x s2.
This is a total preorder relation for segment intersecting the
line at x.
The relation is transitive and reflexive.
Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or
both (if s1 and s2 intersect at x)
29 / 88
Thus
This can be used to record events given two segments s1 and s2
Event I: s1 above s2 at x, written s1 x s2.
This is a total preorder relation for segment intersecting the
line at x.
The relation is transitive and reflexive.
Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or
both (if s1 and s2 intersect at x)
29 / 88
Thus
This can be used to record events given two segments s1 and s2
Event I: s1 above s2 at x, written s1 x s2.
This is a total preorder relation for segment intersecting the
line at x.
The relation is transitive and reflexive.
Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or
both (if s1 and s2 intersect at x)
29 / 88
Example
Example: a r c a t c
Figure: Vertical Line to Record Events
30 / 88
Change in direction
When e and f intersect, e v f and f w e. In the Shaded Region,
any sweep line will have e and f as consecutive
V z w
Figure: Vertical Line to Record Events
31 / 88
Moving the sweep line
Something Notable
Sweeping algorithms typically manage two sets of data.
Sweep-line status
The sweep-line status gives the relationships among the objects that the
sweep line intersects.
Event-point schedule
The event-point schedule is a sequence of points, called event points,
which we order from left to right according to their x-coordinates.
As the sweep progress from left to right, it stops and processes each
event point, then resumes.
It is possible to use a min-priority queue to keep those event points
sorted by x-coordinate.
32 / 88
Moving the sweep line
Something Notable
Sweeping algorithms typically manage two sets of data.
Sweep-line status
The sweep-line status gives the relationships among the objects that the
sweep line intersects.
Event-point schedule
The event-point schedule is a sequence of points, called event points,
which we order from left to right according to their x-coordinates.
As the sweep progress from left to right, it stops and processes each
event point, then resumes.
It is possible to use a min-priority queue to keep those event points
sorted by x-coordinate.
32 / 88
Moving the sweep line
Something Notable
Sweeping algorithms typically manage two sets of data.
Sweep-line status
The sweep-line status gives the relationships among the objects that the
sweep line intersects.
Event-point schedule
The event-point schedule is a sequence of points, called event points,
which we order from left to right according to their x-coordinates.
As the sweep progress from left to right, it stops and processes each
event point, then resumes.
It is possible to use a min-priority queue to keep those event points
sorted by x-coordinate.
32 / 88
Moving the sweep line
Something Notable
Sweeping algorithms typically manage two sets of data.
Sweep-line status
The sweep-line status gives the relationships among the objects that the
sweep line intersects.
Event-point schedule
The event-point schedule is a sequence of points, called event points,
which we order from left to right according to their x-coordinates.
As the sweep progress from left to right, it stops and processes each
event point, then resumes.
It is possible to use a min-priority queue to keep those event points
sorted by x-coordinate.
32 / 88
Sweeping Process
First
We sort the segment endpoints by increasing x-coordinate and
proceed from left to right.
However, sometimes they have the same x-coordinate (Covertical)
If two or more endpoints are covertical, we break the tie by putting all the
covertical left endpoints before the covertical right endpoints.
33 / 88
Sweeping Process
First
We sort the segment endpoints by increasing x-coordinate and
proceed from left to right.
However, sometimes they have the same x-coordinate (Covertical)
If two or more endpoints are covertical, we break the tie by putting all the
covertical left endpoints before the covertical right endpoints.
Covertical Left
End Points
Covertical Right
End Points
33 / 88
Then
Second
Within a set of covertical left endpoints, we put those with lower
y-coordinates first, and we do the same within a set of covertical right
endpoints.
Covertical Left
End Points
Covertical Right
End Points
FIRST
34 / 88
Then
Process
1 When we encounter a segment’s left endpoint, we insert the segment
into the sweep-line status.
2 We delete the segment from the sweep-line status upon encountering
its right endpoint.
Thus
Whenever two segments first become consecutive in the total preorder, we
check whether they intersect.
35 / 88
Then
Process
1 When we encounter a segment’s left endpoint, we insert the segment
into the sweep-line status.
2 We delete the segment from the sweep-line status upon encountering
its right endpoint.
Thus
Whenever two segments first become consecutive in the total preorder, we
check whether they intersect.
35 / 88
Then
Process
1 When we encounter a segment’s left endpoint, we insert the segment
into the sweep-line status.
2 We delete the segment from the sweep-line status upon encountering
its right endpoint.
Thus
Whenever two segments first become consecutive in the total preorder, we
check whether they intersect.
35 / 88
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 88
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 88
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 88
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 88
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 88
Operations
Operations to keep preorder on the events for algorithm
INSERT(T, s): insert segment s into T.
DELETE(T, s): delete segment s from T.
ABOVE(T, s): return the segment immediately above segment s in T.
BELOW(T, s): return the segment immediately below segment s in T.
Note
Each operation can be performed in O(log2 n) using a red-black-tree by
using comparisons by cross product to find the above and below.
This allows to see
The relative ordering of two segments.
36 / 88
What the algorithm does?
Moving the sweeping line discretely - Event-point schedule
Here, moving through the x-axis
discrete events
Using the RBT
to Store the Info
about the segment
order
37 / 88
Event-Point Schedule Implementation
For this
We can use a Priority Queue using lexicographic order
The interesting part is the Sweeping-Line Satus
Because the way we build the balanced tree
38 / 88
Event-Point Schedule Implementation
For this
We can use a Priority Queue using lexicographic order
The interesting part is the Sweeping-Line Satus
Because the way we build the balanced tree
38 / 88
Sweep-Line Status
The Above and Below relation
TIME
Sweeping Line
39 / 88
Sweeping Line Status Implementation
Use the following relation of order to build the binary tree
Given a segment x, then you insert y
Case I if y is counterclockwise, it is below x (Go to the left).
Case II if y is clockwise, it is above x (Go to the Right)
In addtion
If you are at a leaf do the insertion, but also insert the leaf at the left or
right given the insertion.
40 / 88
Sweeping Line Status Implementation
Use the following relation of order to build the binary tree
Given a segment x, then you insert y
Case I if y is counterclockwise, it is below x (Go to the left).
Case II if y is clockwise, it is above x (Go to the Right)
In addtion
If you are at a leaf do the insertion, but also insert the leaf at the left or
right given the insertion.
40 / 88
Example
We insert th first element in the circular leaves list
41 / 88
Example
We insert a inner node after binary search
Below Above
42 / 88
Example
Similar
Below Above
43 / 88
Example
Etc....
Below Above
Trigger
Rebalancing
in the inner
nodes
44 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Pseudo-code with complexity O (n log2 n)
Any-Segment-Intersect(S)
1 T = ∅
2 Sort the endpoints of the segments in S from left to right
Breaking ties by putting left endpoints before right endpoints
and breaking further ties by putting points with lower
y−coordinates first
3 for each point p in the sorted list
4 if p is the left endpoint of a segment s
5 INSERT(T, s)
6 if (ABOVE(T, s) exists and intersect s)
or (BELOW(T, s) exists and intersect s)
7 return TRUE
8 if p is the right endpoint of a segment s
9 if (both ABOVE(T, s) and BELOW(T, s) exist)
and (ABOVE(T, s) intersect BELOW(T, s))
10 return TRUE
11 DELETE(T, s)
12 return FALSE 45 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
46 / 88
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T is
correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 88
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T is
correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 88
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T is
correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 88
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T is
correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 88
Correctness
The ANY-SEGMENTS-INTERSECT returns TRUE
If it finds an intersection between two of the input segments.
Proof:
Observation: What if there is the leftmost intersection, p?
Then, let a and b be the segments to intersect at p
Then, for a and b
Since no intersections occur to the left of p, the order given by T is
correct at all points to the left of p.
Assuming that no three segments intersect at the same point, a and b
become consecutive in the total preorder of some sweep line z.
47 / 88
Now, we have two cases
First Case
a
b
p
z SWEEPING LINE
48 / 88
First Case
Moreover
z is to the left of p or goes through p.
In addition
Exist a endpoint q where a and b become consecutive.
49 / 88
First Case
Moreover
z is to the left of p or goes through p.
In addition
Exist a endpoint q where a and b become consecutive.
a
b
p
z
q
49 / 88
Finally
Then a and b
They become consecutive in the total pre-order of a sweep line.
50 / 88
Case II
We have that q is a left endpoint where a and b stop being
consecutive
a
b
p
z
q
51 / 88
Correctness
Then, we need to handle two cases
1 if p is in the sweep line ⇒ p == q.
1 Observe that can be handled by using the idea of case II.
2 If not q is at the left of p and is the nearest left one:
1 This can be handled by the case I.
52 / 88
Correctness
Then, we need to handle two cases
1 if p is in the sweep line ⇒ p == q.
1 Observe that can be handled by using the idea of case II.
2 If not q is at the left of p and is the nearest left one:
1 This can be handled by the case I.
52 / 88
Correctness
Then, we need to handle two cases
1 if p is in the sweep line ⇒ p == q.
1 Observe that can be handled by using the idea of case II.
2 If not q is at the left of p and is the nearest left one:
1 This can be handled by the case I.
52 / 88
Correctness
Then, we need to handle two cases
1 if p is in the sweep line ⇒ p == q.
1 Observe that can be handled by using the idea of case II.
2 If not q is at the left of p and is the nearest left one:
1 This can be handled by the case I.
52 / 88
Case I
Either a or b is inserted into T, and the other segment is above or
below it in the total pre-order.
a
b
p
z
q
53 / 88
When is this detected?
In the following lines of the code
Lines 4–7 detect this case.
54 / 88
Case II
Segments a and b are already in T , and a segment between them in
the total pre-order is deleted, making a and b to become consecutive
a
b
p
z
qI
a
I
b
a
b
55 / 88
When is this detected?
In the following lines of the code
Lines 8–11 detect this case.
56 / 88
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
57 / 88
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
57 / 88
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
57 / 88
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
57 / 88
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
57 / 88
Running Time
Something Notable
1 Line 1 takes O(1) time.
2 Line 2 takes O(n log2 n) time, using merge or heap sort
3 The for loop iterates at most 2n times
1 Each iteration takes O (log2 n) in a well balanced tree.
2 Each intersection test takes O (1)
Total Time
O (n log2 n)
57 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
58 / 88
Convex Hull
Convex Hull
Given a set of points, Q, find the smallest convex polygon P such
that Q ⊂ P. This is denoted by CH(Q).
Bunch of Points in 2D
59 / 88
Convex Hull
Convex Hull
Given a set of points, Q, find the smallest convex polygon P such
that Q ⊂ P. This is denoted by CH(Q).
Their Convex Hull
60 / 88
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
61 / 88
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
61 / 88
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
61 / 88
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
61 / 88
Convex Hull
The two main Algorithms (Using the “Rotational Sweep”) that we are
going to explore
Graham’s Scan.
Jarvis’ March.
Nevertheless there are other methods
The incremental method
Divide-and-conquer method.
Prune-and-search method.
61 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
62 / 88
Graham’s Scan
Graham’s Scan Basics
It keeps a Stack of candidate points.
It Pops elements that are not part of the CH(Q).
Whatever is left in the Stack is part of the CH(Q).
63 / 88
Graham’s Scan
Graham’s Scan Basics
It keeps a Stack of candidate points.
It Pops elements that are not part of the CH(Q).
Whatever is left in the Stack is part of the CH(Q).
63 / 88
Graham’s Scan
Graham’s Scan Basics
It keeps a Stack of candidate points.
It Pops elements that are not part of the CH(Q).
Whatever is left in the Stack is part of the CH(Q).
63 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Graham’s Scan Code
Algorithm
GRAHAM-SCAN(Q)
1. Let p0 be the point Q with
the minimum y-coordinate or
the leftmost such point in
case of a tie
2. let p1, p2, ..., pn be the
remaining points in Q, sorted
by polar angle in counter
clockwise order around p0 (If
more than one point has the
same angle, remove all but
one that is farthest from p0)
3. Let S be an empty stack
4. PUSH(p0, S)
5. PUSH(p1, S)
6. PUSH(p2, S)
7. for i = 3 to n
8. while ccw(next-top(S), pi, top (S)) ≤ 0
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(p1, p2, p3)
1. return (p3 − p1 × p2 − p1)
64 / 88
Example
Sort points using the smallest to largest polar coordinate
65 / 88
Example
Push the first points into the stack
66 / 88
Example
ccw(p1, p2, p3) > 0, do not get into the loop and push p3 into S
clockwise
67 / 88
Example
ccw(p2, p3, p4) > 0, do not get into the loop and push p4 into S
clockwise
68 / 88
Example
Counterclockwise - Pop p4
counterclockwise
69 / 88
Example
Clockwise - Push p5
clockwise
70 / 88
Example
Counterclockwise - Pop p5 and push p6
counterclockwise
71 / 88
Example
Clockwise push p7
clockwise
72 / 88
Example
Counterclockwise pop p7 and push p8
counterclockwise
73 / 88
Example
Clockwise push p9
clockwise
74 / 88
Example
Clockwise pop p9 and push p10
counterclockwise
75 / 88
Example
Keep going until you finish
76 / 88
We have the following theorem for correctness of the
algorithm
Theorem 33.1 (Correctness of Graham’s scan)
If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then at
termination, the stack S consists of, from bottom to top, exactly the
vertices of CH(Q) in counterclockwise order.
The proof is based in loop invariance
I leave this to you to read!!!
77 / 88
We have the following theorem for correctness of the
algorithm
Theorem 33.1 (Correctness of Graham’s scan)
If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then at
termination, the stack S consists of, from bottom to top, exactly the
vertices of CH(Q) in counterclockwise order.
The proof is based in loop invariance
I leave this to you to read!!!
77 / 88
Using Aggregate Analysis to obtain the complexity
O (n log2 n)
Complexity
1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar
angles and cross product.
2 Lines 3-6 take O (1).
For the Lines 7-10
The for loop executes at most n − 3 times because we have |Q| − 3 points
left
78 / 88
Using Aggregate Analysis to obtain the complexity
O (n log2 n)
Complexity
1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar
angles and cross product.
2 Lines 3-6 take O (1).
For the Lines 7-10
The for loop executes at most n − 3 times because we have |Q| − 3 points
left
78 / 88
Using Aggregate Analysis to obtain the complexity
O (n log2 n)
Complexity
1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar
angles and cross product.
2 Lines 3-6 take O (1).
For the Lines 7-10
The for loop executes at most n − 3 times because we have |Q| − 3 points
left
78 / 88
In addition
Given
PUSH takes O(1) time:
Each iteration takes O (1) time not taking in account the time spent
in the while loop in lines 8-9.
Then
The for loop take overall time O(n) time
Here is the aggregate analysis
Here, we will prove that the overall time for all the times the while loop is
touched by the for loop is going to be O (n).
79 / 88
In addition
Given
PUSH takes O(1) time:
Each iteration takes O (1) time not taking in account the time spent
in the while loop in lines 8-9.
Then
The for loop take overall time O(n) time
Here is the aggregate analysis
Here, we will prove that the overall time for all the times the while loop is
touched by the for loop is going to be O (n).
79 / 88
In addition
Given
PUSH takes O(1) time:
Each iteration takes O (1) time not taking in account the time spent
in the while loop in lines 8-9.
Then
The for loop take overall time O(n) time
Here is the aggregate analysis
Here, we will prove that the overall time for all the times the while loop is
touched by the for loop is going to be O (n).
79 / 88
Aggregate Analysis
We have that
For i = 0, 1, ..., n, we push each point pi into the stack S exactly once.
Remember Multipop?
We can pop at most the number of items that we push on it.
Thus
At least three points p0, p1 and pm are never popped out of the stack!!!
pm is the last point being taken in consideration!!! With m ≤ n
80 / 88
Aggregate Analysis
We have that
For i = 0, 1, ..., n, we push each point pi into the stack S exactly once.
Remember Multipop?
We can pop at most the number of items that we push on it.
Thus
At least three points p0, p1 and pm are never popped out of the stack!!!
pm is the last point being taken in consideration!!! With m ≤ n
80 / 88
Aggregate Analysis
We have that
For i = 0, 1, ..., n, we push each point pi into the stack S exactly once.
Remember Multipop?
We can pop at most the number of items that we push on it.
Thus
At least three points p0, p1 and pm are never popped out of the stack!!!
pm is the last point being taken in consideration!!! With m ≤ n
80 / 88
Aggregate Analysis
Thus
We have m − 2 POP operations are performed in total!!! If we had pushed
m elements into S.
Thus, each iteration of the while loop
It performs one POP, and there are at most m − 2 iterations of the while
loop altogether.
Now
Given that the test in line 8 takes O(1) times, each call of the POP takes
O(1) and m ≤ n − 1.
81 / 88
Aggregate Analysis
Thus
We have m − 2 POP operations are performed in total!!! If we had pushed
m elements into S.
Thus, each iteration of the while loop
It performs one POP, and there are at most m − 2 iterations of the while
loop altogether.
Now
Given that the test in line 8 takes O(1) times, each call of the POP takes
O(1) and m ≤ n − 1.
81 / 88
Aggregate Analysis
Thus
We have m − 2 POP operations are performed in total!!! If we had pushed
m elements into S.
Thus, each iteration of the while loop
It performs one POP, and there are at most m − 2 iterations of the while
loop altogether.
Now
Given that the test in line 8 takes O(1) times, each call of the POP takes
O(1) and m ≤ n − 1.
81 / 88
Aggregate Analysis
We have that
The total time of the while loop is O (n).
Finally
The Running Time of GRAHAM − SCAN is O (n log2 n)
82 / 88
Aggregate Analysis
We have that
The total time of the while loop is O (n).
Finally
The Running Time of GRAHAM − SCAN is O (n log2 n)
82 / 88
Outline
1 Introduction
What is Computational Geometry?
2 Representation
Representation of Primitive Geometries
3 Line-Segment Properties
Using Point Representation
Cross Product
Turn Left or Right
Intersection
4 Classical Problems
Determining whether any pair of segments intersects
Correctness of Sweeping Line Algorithm
Finding the Convex Hull
Graham’s Scan
Jarvis’ March
83 / 88
Jarvis’ March
Jarvis’ March Basics
It computes CH by using
A technique called Package Wrapping.
At each point calculate the minimum polar angle.
Create a left and right chain with the convex hull points.
84 / 88
Jarvis’ March
Jarvis’ March Basics
It computes CH by using
A technique called Package Wrapping.
At each point calculate the minimum polar angle.
Create a left and right chain with the convex hull points.
84 / 88
Jarvis’ March
Jarvis’ March Basics
It computes CH by using
A technique called Package Wrapping.
At each point calculate the minimum polar angle.
Create a left and right chain with the convex hull points.
84 / 88
Jarvis’ March
Jarvis’ March Basics
It computes CH by using
A technique called Package Wrapping.
At each point calculate the minimum polar angle.
Create a left and right chain with the convex hull points.
84 / 88
Formally
Jarvis’s march builds a sequence
H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q)
First
We start with p0 the next vertex p1 in the convex hull has the smallest
polar angle with respect to p0.
Next
p2 has the smallest polar angle with respect to p1.
85 / 88
Formally
Jarvis’s march builds a sequence
H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q)
First
We start with p0 the next vertex p1 in the convex hull has the smallest
polar angle with respect to p0.
Next
p2 has the smallest polar angle with respect to p1.
85 / 88
Formally
Jarvis’s march builds a sequence
H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q)
First
We start with p0 the next vertex p1 in the convex hull has the smallest
polar angle with respect to p0.
Next
p2 has the smallest polar angle with respect to p1.
85 / 88
Now
Then
When we reach the highest vertex, pk (Breaking ties by choosing the
farthest such vertex), we have constructed the right chain of CH(Q).
To construct the left chain
We start at pk, then we choose pk+1 as the point with the smallest polar
angle with respect to pk negative , but from the negative x-axis.
Next
The next point is selected in the same manner until we have reached p0.
86 / 88
Now
Then
When we reach the highest vertex, pk (Breaking ties by choosing the
farthest such vertex), we have constructed the right chain of CH(Q).
To construct the left chain
We start at pk, then we choose pk+1 as the point with the smallest polar
angle with respect to pk negative , but from the negative x-axis.
Next
The next point is selected in the same manner until we have reached p0.
86 / 88
Now
Then
When we reach the highest vertex, pk (Breaking ties by choosing the
farthest such vertex), we have constructed the right chain of CH(Q).
To construct the left chain
We start at pk, then we choose pk+1 as the point with the smallest polar
angle with respect to pk negative , but from the negative x-axis.
Next
The next point is selected in the same manner until we have reached p0.
86 / 88
Jarvis’ March
Example
LEFT CHAIN RIGHT CHAIN
Figure: Wrapping the Gift. Here the Right Chain finishes at p6, then
the Left Chain is started
87 / 88
Complexity
Something Notable
Complexity O (hn)
h number of points in CH.
O (n) for finding the minimum angle and the farthest point by
y-axis
88 / 88
Complexity
Something Notable
Complexity O (hn)
h number of points in CH.
O (n) for finding the minimum angle and the farthest point by
y-axis
88 / 88
Complexity
Something Notable
Complexity O (hn)
h number of points in CH.
O (n) for finding the minimum angle and the farthest point by
y-axis
88 / 88
Complexity
Something Notable
Complexity O (hn)
h number of points in CH.
O (n) for finding the minimum angle and the farthest point by
y-axis
88 / 88

More Related Content

What's hot

Discrete mathematics
Discrete mathematicsDiscrete mathematics
Discrete mathematicsM.Saber
 
22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_finalvibhuti bansal
 
Sep logic slide
Sep logic slideSep logic slide
Sep logic sliderainoftime
 
3. Linear Algebra for Machine Learning: Factorization and Linear Transformations
3. Linear Algebra for Machine Learning: Factorization and Linear Transformations3. Linear Algebra for Machine Learning: Factorization and Linear Transformations
3. Linear Algebra for Machine Learning: Factorization and Linear TransformationsCeni Babaoglu, PhD
 
Regression &amp; Classification
Regression &amp; ClassificationRegression &amp; Classification
Regression &amp; Classification주영 송
 
Fourier series Introduction
Fourier series IntroductionFourier series Introduction
Fourier series IntroductionRizwan Kazi
 
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)Angel David Ortiz Resendiz
 
3.6 applications in optimization
3.6 applications in optimization3.6 applications in optimization
3.6 applications in optimizationmath265
 
Convex Optimization
Convex OptimizationConvex Optimization
Convex Optimizationadil raja
 
S1 3 derivadas_resueltas
S1 3 derivadas_resueltasS1 3 derivadas_resueltas
S1 3 derivadas_resueltasjesquerrev1
 
S1 3 derivadas_resueltas
S1 3 derivadas_resueltasS1 3 derivadas_resueltas
S1 3 derivadas_resueltasjesquerrev1
 

What's hot (19)

residue
residueresidue
residue
 
Discrete mathematics
Discrete mathematicsDiscrete mathematics
Discrete mathematics
 
Numerical Methods 1
Numerical Methods 1Numerical Methods 1
Numerical Methods 1
 
22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final
 
Fmincon
FminconFmincon
Fmincon
 
Analysis of Algorithm
Analysis of AlgorithmAnalysis of Algorithm
Analysis of Algorithm
 
Sep logic slide
Sep logic slideSep logic slide
Sep logic slide
 
3. Linear Algebra for Machine Learning: Factorization and Linear Transformations
3. Linear Algebra for Machine Learning: Factorization and Linear Transformations3. Linear Algebra for Machine Learning: Factorization and Linear Transformations
3. Linear Algebra for Machine Learning: Factorization and Linear Transformations
 
C1 - Insertion Sort
C1 - Insertion SortC1 - Insertion Sort
C1 - Insertion Sort
 
Regression &amp; Classification
Regression &amp; ClassificationRegression &amp; Classification
Regression &amp; Classification
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Fourier series Introduction
Fourier series IntroductionFourier series Introduction
Fourier series Introduction
 
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)
 
02 asymp
02 asymp02 asymp
02 asymp
 
3.6 applications in optimization
3.6 applications in optimization3.6 applications in optimization
3.6 applications in optimization
 
Convex Optimization
Convex OptimizationConvex Optimization
Convex Optimization
 
S1 3 derivadas_resueltas
S1 3 derivadas_resueltasS1 3 derivadas_resueltas
S1 3 derivadas_resueltas
 
S1 3 derivadas_resueltas
S1 3 derivadas_resueltasS1 3 derivadas_resueltas
S1 3 derivadas_resueltas
 

Viewers also liked

31 Machine Learning Unsupervised Cluster Validity
31 Machine Learning Unsupervised Cluster Validity31 Machine Learning Unsupervised Cluster Validity
31 Machine Learning Unsupervised Cluster ValidityAndres Mendez-Vazquez
 
factors affecting Evaporation
factors affecting Evaporationfactors affecting Evaporation
factors affecting Evaporationdeepak190
 
Preparation Data Structures 11 graphs
Preparation Data Structures 11 graphsPreparation Data Structures 11 graphs
Preparation Data Structures 11 graphsAndres Mendez-Vazquez
 
11 Machine Learning Important Issues in Machine Learning
11 Machine Learning Important Issues in Machine Learning11 Machine Learning Important Issues in Machine Learning
11 Machine Learning Important Issues in Machine LearningAndres Mendez-Vazquez
 
07 Machine Learning - Expectation Maximization
07 Machine Learning - Expectation Maximization07 Machine Learning - Expectation Maximization
07 Machine Learning - Expectation MaximizationAndres Mendez-Vazquez
 
27 Machine Learning Unsupervised Measure Properties
27 Machine Learning Unsupervised Measure Properties27 Machine Learning Unsupervised Measure Properties
27 Machine Learning Unsupervised Measure PropertiesAndres Mendez-Vazquez
 
03 Probability Review for Analysis of Algorithms
03 Probability Review for Analysis of Algorithms03 Probability Review for Analysis of Algorithms
03 Probability Review for Analysis of AlgorithmsAndres Mendez-Vazquez
 
Preparation Data Structures 07 stacks
Preparation Data Structures 07 stacksPreparation Data Structures 07 stacks
Preparation Data Structures 07 stacksAndres Mendez-Vazquez
 
Periodic table (1)
Periodic table (1)Periodic table (1)
Periodic table (1)jghopwood
 
Preparation Data Structures 10 trees
Preparation Data Structures 10 treesPreparation Data Structures 10 trees
Preparation Data Structures 10 treesAndres Mendez-Vazquez
 
13 Machine Learning Supervised Decision Trees
13 Machine Learning Supervised Decision Trees13 Machine Learning Supervised Decision Trees
13 Machine Learning Supervised Decision TreesAndres Mendez-Vazquez
 
24 Machine Learning Combining Models - Ada Boost
24 Machine Learning Combining Models - Ada Boost24 Machine Learning Combining Models - Ada Boost
24 Machine Learning Combining Models - Ada BoostAndres Mendez-Vazquez
 

Viewers also liked (20)

31 Machine Learning Unsupervised Cluster Validity
31 Machine Learning Unsupervised Cluster Validity31 Machine Learning Unsupervised Cluster Validity
31 Machine Learning Unsupervised Cluster Validity
 
01 Machine Learning Introduction
01 Machine Learning Introduction 01 Machine Learning Introduction
01 Machine Learning Introduction
 
factors affecting Evaporation
factors affecting Evaporationfactors affecting Evaporation
factors affecting Evaporation
 
Preparation Data Structures 11 graphs
Preparation Data Structures 11 graphsPreparation Data Structures 11 graphs
Preparation Data Structures 11 graphs
 
11 Machine Learning Important Issues in Machine Learning
11 Machine Learning Important Issues in Machine Learning11 Machine Learning Important Issues in Machine Learning
11 Machine Learning Important Issues in Machine Learning
 
07 Machine Learning - Expectation Maximization
07 Machine Learning - Expectation Maximization07 Machine Learning - Expectation Maximization
07 Machine Learning - Expectation Maximization
 
Force resolution force
Force resolution forceForce resolution force
Force resolution force
 
27 Machine Learning Unsupervised Measure Properties
27 Machine Learning Unsupervised Measure Properties27 Machine Learning Unsupervised Measure Properties
27 Machine Learning Unsupervised Measure Properties
 
03 Probability Review for Analysis of Algorithms
03 Probability Review for Analysis of Algorithms03 Probability Review for Analysis of Algorithms
03 Probability Review for Analysis of Algorithms
 
27 NP Completness
27 NP Completness27 NP Completness
27 NP Completness
 
Preparation Data Structures 07 stacks
Preparation Data Structures 07 stacksPreparation Data Structures 07 stacks
Preparation Data Structures 07 stacks
 
What's the Matter?
What's the Matter?What's the Matter?
What's the Matter?
 
Periodic table (1)
Periodic table (1)Periodic table (1)
Periodic table (1)
 
Preparation Data Structures 10 trees
Preparation Data Structures 10 treesPreparation Data Structures 10 trees
Preparation Data Structures 10 trees
 
Force resultant force
Force resultant forceForce resultant force
Force resultant force
 
15 B-Trees
15 B-Trees15 B-Trees
15 B-Trees
 
20 Single Source Shorthest Path
20 Single Source Shorthest Path20 Single Source Shorthest Path
20 Single Source Shorthest Path
 
13 Machine Learning Supervised Decision Trees
13 Machine Learning Supervised Decision Trees13 Machine Learning Supervised Decision Trees
13 Machine Learning Supervised Decision Trees
 
Waves
WavesWaves
Waves
 
24 Machine Learning Combining Models - Ada Boost
24 Machine Learning Combining Models - Ada Boost24 Machine Learning Combining Models - Ada Boost
24 Machine Learning Combining Models - Ada Boost
 

Similar to 26 Computational Geometry

Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmShilpa Hait
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Finite elements : basis functions
Finite elements : basis functionsFinite elements : basis functions
Finite elements : basis functionsTarun Gehlot
 
planes and distances
planes and distancesplanes and distances
planes and distancesElias Dinsa
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in indiaEdhole.com
 
Orthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processOrthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processgidc engineering college
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithmPooja Dixit
 
Hormann.2001.TPI.pdf
Hormann.2001.TPI.pdfHormann.2001.TPI.pdf
Hormann.2001.TPI.pdfssuserbe139c
 
2 d transformations by amit kumar (maimt)
2 d transformations by amit kumar (maimt)2 d transformations by amit kumar (maimt)
2 d transformations by amit kumar (maimt)Amit Kapoor
 
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLahiru Danushka
 
Number Theory for Security
Number Theory for SecurityNumber Theory for Security
Number Theory for SecurityAbhijit Mondal
 
Class 14 3D HermiteInterpolation.pptx
Class 14 3D HermiteInterpolation.pptxClass 14 3D HermiteInterpolation.pptx
Class 14 3D HermiteInterpolation.pptxMdSiddique20
 
Matrix 2 d
Matrix 2 dMatrix 2 d
Matrix 2 dxyz120
 

Similar to 26 Computational Geometry (20)

Geometric algorithms
Geometric algorithmsGeometric algorithms
Geometric algorithms
 
Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithm
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Finite elements : basis functions
Finite elements : basis functionsFinite elements : basis functions
Finite elements : basis functions
 
planes and distances
planes and distancesplanes and distances
planes and distances
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
 
Atan2
Atan2Atan2
Atan2
 
Orthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processOrthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth process
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
 
Hormann.2001.TPI.pdf
Hormann.2001.TPI.pdfHormann.2001.TPI.pdf
Hormann.2001.TPI.pdf
 
2 d transformations by amit kumar (maimt)
2 d transformations by amit kumar (maimt)2 d transformations by amit kumar (maimt)
2 d transformations by amit kumar (maimt)
 
Convex hull
Convex hullConvex hull
Convex hull
 
Pairing scott
Pairing scottPairing scott
Pairing scott
 
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
 
1513 circles
1513 circles1513 circles
1513 circles
 
Calc 5.6
Calc 5.6Calc 5.6
Calc 5.6
 
Number Theory for Security
Number Theory for SecurityNumber Theory for Security
Number Theory for Security
 
Class 14 3D HermiteInterpolation.pptx
Class 14 3D HermiteInterpolation.pptxClass 14 3D HermiteInterpolation.pptx
Class 14 3D HermiteInterpolation.pptx
 
Parabola
ParabolaParabola
Parabola
 
Matrix 2 d
Matrix 2 dMatrix 2 d
Matrix 2 d
 

More from Andres Mendez-Vazquez

01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectorsAndres Mendez-Vazquez
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issuesAndres Mendez-Vazquez
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiationAndres Mendez-Vazquez
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep LearningAndres Mendez-Vazquez
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learningAndres Mendez-Vazquez
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusAndres Mendez-Vazquez
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusAndres Mendez-Vazquez
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesAndres Mendez-Vazquez
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variationsAndres Mendez-Vazquez
 

More from Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Recently uploaded

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 

Recently uploaded (20)

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

26 Computational Geometry

  • 1. Analysis of Algorithms Computational Geometry Andres Mendez-Vazquez November 16, 2015 1 / 88
  • 2. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 2 / 88
  • 3. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 3 / 88
  • 4. Computational Geometry Motivation We want to solve geometric problems!!! 4 / 88
  • 5. Field of Application VLSI design - Generation for Fast Voronoi Diagrams for Massive Layouts Under Strict Distances to avoid Tunneling Effects!! 5 / 88
  • 6. Field of Application Databases - Octrees for fast localization of information in database tables 6 / 88
  • 7. Field of Application Synthetic Biology - Geometric Algorithms to Obtain new DNA configurations for Molecular Machines 7 / 88
  • 8. Field of Application Computer Graphics for more engaging Virtual Environments - For example: Bump Mapping!!! 8 / 88
  • 9. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 9 / 88
  • 10. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 88
  • 11. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 88
  • 12. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 88
  • 13. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 88
  • 14. The Plane Representation Although 3D algorithms exist... We will deal only with algorithms working in the plane. Object Representation Each object is a set of points {p1, p2, ..., pn} where pi = (xi, yi) and xi, yi ∈ R. Example For example an n-vertex polygon P is the following order sequence: p0, p2, ..., pn 10 / 88
  • 16. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 12 / 88
  • 17. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 88
  • 18. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 88
  • 19. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 88
  • 20. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 88
  • 21. Line-segment Properties A convex combination Given two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convex combination of {p1, p2} is any point p3 such that: p3 = αp1 + (1 − α) p2 with 0 ≤ α ≤ 1. Line Segment as Convex Combination Given two points p1 and p2 (Known as End Points), the line segment p1p2 is the set of convex combinations of p1 and p2. Directed Segment Here, we care about the direction with initial point p1 for the directed segment −−→p1p2: If p1 = (0, 0) then −−→p1p2 is the vector p2. 13 / 88
  • 22. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 14 / 88
  • 23. Cross Product Question!!! Given two directed segments −−→p0p1 and −−→p0p2, Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0? Cross Product Cross product p1 × p2 as the signed area of the parallelogram formed by 15 / 88
  • 24. Cross Product Question!!! Given two directed segments −−→p0p1 and −−→p0p2, Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0? Cross Product Cross product p1 × p2 as the signed area of the parallelogram formed by 15 / 88
  • 25. Cross Product Question!!! Given two directed segments −−→p0p1 and −−→p0p2, Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0? Cross Product Cross product p1 × p2 as the signed area of the parallelogram formed by 15 / 88
  • 26. Cross Product A shorter representation p1 × p2 = det p1 p2 =det x1 x2 y1 y2 = x1y2 − x2y1 = −p2 × p1 Thus if p1 × p2 is positive, then p1 is clockwise from p2. if p1 × p2 is negative, then p1 is counterclockwise from p2. 16 / 88
  • 27. Cross Product A shorter representation p1 × p2 = det p1 p2 =det x1 x2 y1 y2 = x1y2 − x2y1 = −p2 × p1 Thus if p1 × p2 is positive, then p1 is clockwise from p2. if p1 × p2 is negative, then p1 is counterclockwise from p2. 16 / 88
  • 28. Cross Product A shorter representation p1 × p2 = det p1 p2 =det x1 x2 y1 y2 = x1y2 − x2y1 = −p2 × p1 Thus if p1 × p2 is positive, then p1 is clockwise from p2. if p1 × p2 is negative, then p1 is counterclockwise from p2. 16 / 88
  • 29. Regions Clockwise and Counterclockwise Regions Figure: Darker counterclockwise; lighter clockwise with respect to p 17 / 88
  • 30. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 18 / 88
  • 31. Turn Left or Right Question Given two line segments −−→p0p1 and −−→p1p2, if we traverse −−→p0p1 and then −−→p1p2, do we make a left turn at point p1? 19 / 88
  • 32. Turn Left or Right Simply use the following idea Compute cross product (p2 − p0) × (p1 − p0)!!! This translates p0 to the origin!!! What about (p2 − p0) × (p1 − p0) = 0? Left Turn = counterclockwise; Right Turn = clockwise 20 / 88
  • 33. Turn Left or Right Simply use the following idea Compute cross product (p2 − p0) × (p1 − p0)!!! This translates p0 to the origin!!! What about (p2 − p0) × (p1 − p0) = 0? Left Turn = counterclockwise; Right Turn = clockwise 20 / 88
  • 34. Turn Left or Right Simply use the following idea Compute cross product (p2 − p0) × (p1 − p0)!!! This translates p0 to the origin!!! What about (p2 − p0) × (p1 − p0) = 0? Left Turn = counterclockwise; Right Turn = clockwise 20 / 88
  • 35. Turn Left or Right Simply use the following idea Compute cross product (p2 − p0) × (p1 − p0)!!! This translates p0 to the origin!!! What about (p2 − p0) × (p1 − p0) = 0? Left Turn = counterclockwise; Right Turn = clockwise counterclockwise clockwise 20 / 88
  • 36. Code for this We have the following code Direction(pi, pj, pk) 1 return (pk − pi) × (pj − pi) 21 / 88
  • 37. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 22 / 88
  • 38. Intersection Question Do line segments −−→p1p2 and −−→p3p4 intersect? Very Simple!!! We have two possibilities 1 Each segment straddles the line containing the other. 2 An endpoint of one segment lies on the other segment. 23 / 88
  • 39. Intersection Question Do line segments −−→p1p2 and −−→p3p4 intersect? Very Simple!!! We have two possibilities 1 Each segment straddles the line containing the other. 2 An endpoint of one segment lies on the other segment. 23 / 88
  • 40. Intersection Question Do line segments −−→p1p2 and −−→p3p4 intersect? Very Simple!!! We have two possibilities 1 Each segment straddles the line containing the other. 2 An endpoint of one segment lies on the other segment. 23 / 88
  • 41. Case I This summarize the previous two possibilities The segments straddle each other’s lines. Oposite Signs Figure: Using Cross Products to find intersections 24 / 88
  • 42. Case II No intersection The segment straddles the line, but the other does not straddle the other line Same Signs Figure: Using Cross Products to find that there is no intersection 25 / 88
  • 43. Code Code Segment-Intersection(p1, p2, p3, p4) 1 d1 = Direction (p3, p4, p1) 2 d2 = Direction (p3, p4, p2) 3 d3 = Direction (p1, p2, p3) 4 d4 = Direction (p1, p2, p4) 5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0) and 6 (d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)) 7 return TRUE Figure: The Incomplete Code, You still need to test for endpoints over the segment 26 / 88
  • 44. Code Code Segment-Intersection(p1, p2, p3, p4) 1 d1 = Direction (p3, p4, p1) 2 d2 = Direction (p3, p4, p2) 3 d3 = Direction (p1, p2, p3) 4 d4 = Direction (p1, p2, p4) 5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0) and 6 (d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)) 7 return TRUE Figure: The Incomplete Code, You still need to test for endpoints over the segment 26 / 88
  • 45. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 27 / 88
  • 46. Sweeping Sweeping Use an imaginary vertical line to pass through the n segments with events x ∈ {r, t, u}: Figure: Vertical Line to Record Events 28 / 88
  • 47. Thus This can be used to record events given two segments s1 and s2 Event I: s1 above s2 at x, written s1 x s2. This is a total preorder relation for segment intersecting the line at x. The relation is transitive and reflexive. Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or both (if s1 and s2 intersect at x) 29 / 88
  • 48. Thus This can be used to record events given two segments s1 and s2 Event I: s1 above s2 at x, written s1 x s2. This is a total preorder relation for segment intersecting the line at x. The relation is transitive and reflexive. Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or both (if s1 and s2 intersect at x) 29 / 88
  • 49. Thus This can be used to record events given two segments s1 and s2 Event I: s1 above s2 at x, written s1 x s2. This is a total preorder relation for segment intersecting the line at x. The relation is transitive and reflexive. Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or both (if s1 and s2 intersect at x) 29 / 88
  • 50. Thus This can be used to record events given two segments s1 and s2 Event I: s1 above s2 at x, written s1 x s2. This is a total preorder relation for segment intersecting the line at x. The relation is transitive and reflexive. Event II: s1 intersect s2, then neither s1 x s2 or s2 x s1, or both (if s1 and s2 intersect at x) 29 / 88
  • 51. Example Example: a r c a t c Figure: Vertical Line to Record Events 30 / 88
  • 52. Change in direction When e and f intersect, e v f and f w e. In the Shaded Region, any sweep line will have e and f as consecutive V z w Figure: Vertical Line to Record Events 31 / 88
  • 53. Moving the sweep line Something Notable Sweeping algorithms typically manage two sets of data. Sweep-line status The sweep-line status gives the relationships among the objects that the sweep line intersects. Event-point schedule The event-point schedule is a sequence of points, called event points, which we order from left to right according to their x-coordinates. As the sweep progress from left to right, it stops and processes each event point, then resumes. It is possible to use a min-priority queue to keep those event points sorted by x-coordinate. 32 / 88
  • 54. Moving the sweep line Something Notable Sweeping algorithms typically manage two sets of data. Sweep-line status The sweep-line status gives the relationships among the objects that the sweep line intersects. Event-point schedule The event-point schedule is a sequence of points, called event points, which we order from left to right according to their x-coordinates. As the sweep progress from left to right, it stops and processes each event point, then resumes. It is possible to use a min-priority queue to keep those event points sorted by x-coordinate. 32 / 88
  • 55. Moving the sweep line Something Notable Sweeping algorithms typically manage two sets of data. Sweep-line status The sweep-line status gives the relationships among the objects that the sweep line intersects. Event-point schedule The event-point schedule is a sequence of points, called event points, which we order from left to right according to their x-coordinates. As the sweep progress from left to right, it stops and processes each event point, then resumes. It is possible to use a min-priority queue to keep those event points sorted by x-coordinate. 32 / 88
  • 56. Moving the sweep line Something Notable Sweeping algorithms typically manage two sets of data. Sweep-line status The sweep-line status gives the relationships among the objects that the sweep line intersects. Event-point schedule The event-point schedule is a sequence of points, called event points, which we order from left to right according to their x-coordinates. As the sweep progress from left to right, it stops and processes each event point, then resumes. It is possible to use a min-priority queue to keep those event points sorted by x-coordinate. 32 / 88
  • 57. Sweeping Process First We sort the segment endpoints by increasing x-coordinate and proceed from left to right. However, sometimes they have the same x-coordinate (Covertical) If two or more endpoints are covertical, we break the tie by putting all the covertical left endpoints before the covertical right endpoints. 33 / 88
  • 58. Sweeping Process First We sort the segment endpoints by increasing x-coordinate and proceed from left to right. However, sometimes they have the same x-coordinate (Covertical) If two or more endpoints are covertical, we break the tie by putting all the covertical left endpoints before the covertical right endpoints. Covertical Left End Points Covertical Right End Points 33 / 88
  • 59. Then Second Within a set of covertical left endpoints, we put those with lower y-coordinates first, and we do the same within a set of covertical right endpoints. Covertical Left End Points Covertical Right End Points FIRST 34 / 88
  • 60. Then Process 1 When we encounter a segment’s left endpoint, we insert the segment into the sweep-line status. 2 We delete the segment from the sweep-line status upon encountering its right endpoint. Thus Whenever two segments first become consecutive in the total preorder, we check whether they intersect. 35 / 88
  • 61. Then Process 1 When we encounter a segment’s left endpoint, we insert the segment into the sweep-line status. 2 We delete the segment from the sweep-line status upon encountering its right endpoint. Thus Whenever two segments first become consecutive in the total preorder, we check whether they intersect. 35 / 88
  • 62. Then Process 1 When we encounter a segment’s left endpoint, we insert the segment into the sweep-line status. 2 We delete the segment from the sweep-line status upon encountering its right endpoint. Thus Whenever two segments first become consecutive in the total preorder, we check whether they intersect. 35 / 88
  • 63. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 88
  • 64. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 88
  • 65. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 88
  • 66. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 88
  • 67. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 88
  • 68. Operations Operations to keep preorder on the events for algorithm INSERT(T, s): insert segment s into T. DELETE(T, s): delete segment s from T. ABOVE(T, s): return the segment immediately above segment s in T. BELOW(T, s): return the segment immediately below segment s in T. Note Each operation can be performed in O(log2 n) using a red-black-tree by using comparisons by cross product to find the above and below. This allows to see The relative ordering of two segments. 36 / 88
  • 69. What the algorithm does? Moving the sweeping line discretely - Event-point schedule Here, moving through the x-axis discrete events Using the RBT to Store the Info about the segment order 37 / 88
  • 70. Event-Point Schedule Implementation For this We can use a Priority Queue using lexicographic order The interesting part is the Sweeping-Line Satus Because the way we build the balanced tree 38 / 88
  • 71. Event-Point Schedule Implementation For this We can use a Priority Queue using lexicographic order The interesting part is the Sweeping-Line Satus Because the way we build the balanced tree 38 / 88
  • 72. Sweep-Line Status The Above and Below relation TIME Sweeping Line 39 / 88
  • 73. Sweeping Line Status Implementation Use the following relation of order to build the binary tree Given a segment x, then you insert y Case I if y is counterclockwise, it is below x (Go to the left). Case II if y is clockwise, it is above x (Go to the Right) In addtion If you are at a leaf do the insertion, but also insert the leaf at the left or right given the insertion. 40 / 88
  • 74. Sweeping Line Status Implementation Use the following relation of order to build the binary tree Given a segment x, then you insert y Case I if y is counterclockwise, it is below x (Go to the left). Case II if y is clockwise, it is above x (Go to the Right) In addtion If you are at a leaf do the insertion, but also insert the leaf at the left or right given the insertion. 40 / 88
  • 75. Example We insert th first element in the circular leaves list 41 / 88
  • 76. Example We insert a inner node after binary search Below Above 42 / 88
  • 79. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 80. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 81. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 82. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 83. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 84. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 85. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 86. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 87. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 88. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 89. Pseudo-code with complexity O (n log2 n) Any-Segment-Intersect(S) 1 T = ∅ 2 Sort the endpoints of the segments in S from left to right Breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y−coordinates first 3 for each point p in the sorted list 4 if p is the left endpoint of a segment s 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersect s) or (BELOW(T, s) exists and intersect s) 7 return TRUE 8 if p is the right endpoint of a segment s 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersect BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) 12 return FALSE 45 / 88
  • 90. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 46 / 88
  • 91. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 88
  • 92. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 88
  • 93. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 88
  • 94. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 88
  • 95. Correctness The ANY-SEGMENTS-INTERSECT returns TRUE If it finds an intersection between two of the input segments. Proof: Observation: What if there is the leftmost intersection, p? Then, let a and b be the segments to intersect at p Then, for a and b Since no intersections occur to the left of p, the order given by T is correct at all points to the left of p. Assuming that no three segments intersect at the same point, a and b become consecutive in the total preorder of some sweep line z. 47 / 88
  • 96. Now, we have two cases First Case a b p z SWEEPING LINE 48 / 88
  • 97. First Case Moreover z is to the left of p or goes through p. In addition Exist a endpoint q where a and b become consecutive. 49 / 88
  • 98. First Case Moreover z is to the left of p or goes through p. In addition Exist a endpoint q where a and b become consecutive. a b p z q 49 / 88
  • 99. Finally Then a and b They become consecutive in the total pre-order of a sweep line. 50 / 88
  • 100. Case II We have that q is a left endpoint where a and b stop being consecutive a b p z q 51 / 88
  • 101. Correctness Then, we need to handle two cases 1 if p is in the sweep line ⇒ p == q. 1 Observe that can be handled by using the idea of case II. 2 If not q is at the left of p and is the nearest left one: 1 This can be handled by the case I. 52 / 88
  • 102. Correctness Then, we need to handle two cases 1 if p is in the sweep line ⇒ p == q. 1 Observe that can be handled by using the idea of case II. 2 If not q is at the left of p and is the nearest left one: 1 This can be handled by the case I. 52 / 88
  • 103. Correctness Then, we need to handle two cases 1 if p is in the sweep line ⇒ p == q. 1 Observe that can be handled by using the idea of case II. 2 If not q is at the left of p and is the nearest left one: 1 This can be handled by the case I. 52 / 88
  • 104. Correctness Then, we need to handle two cases 1 if p is in the sweep line ⇒ p == q. 1 Observe that can be handled by using the idea of case II. 2 If not q is at the left of p and is the nearest left one: 1 This can be handled by the case I. 52 / 88
  • 105. Case I Either a or b is inserted into T, and the other segment is above or below it in the total pre-order. a b p z q 53 / 88
  • 106. When is this detected? In the following lines of the code Lines 4–7 detect this case. 54 / 88
  • 107. Case II Segments a and b are already in T , and a segment between them in the total pre-order is deleted, making a and b to become consecutive a b p z qI a I b a b 55 / 88
  • 108. When is this detected? In the following lines of the code Lines 8–11 detect this case. 56 / 88
  • 109. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 57 / 88
  • 110. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 57 / 88
  • 111. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 57 / 88
  • 112. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 57 / 88
  • 113. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 57 / 88
  • 114. Running Time Something Notable 1 Line 1 takes O(1) time. 2 Line 2 takes O(n log2 n) time, using merge or heap sort 3 The for loop iterates at most 2n times 1 Each iteration takes O (log2 n) in a well balanced tree. 2 Each intersection test takes O (1) Total Time O (n log2 n) 57 / 88
  • 115. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 58 / 88
  • 116. Convex Hull Convex Hull Given a set of points, Q, find the smallest convex polygon P such that Q ⊂ P. This is denoted by CH(Q). Bunch of Points in 2D 59 / 88
  • 117. Convex Hull Convex Hull Given a set of points, Q, find the smallest convex polygon P such that Q ⊂ P. This is denoted by CH(Q). Their Convex Hull 60 / 88
  • 118. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 61 / 88
  • 119. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 61 / 88
  • 120. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 61 / 88
  • 121. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 61 / 88
  • 122. Convex Hull The two main Algorithms (Using the “Rotational Sweep”) that we are going to explore Graham’s Scan. Jarvis’ March. Nevertheless there are other methods The incremental method Divide-and-conquer method. Prune-and-search method. 61 / 88
  • 123. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 62 / 88
  • 124. Graham’s Scan Graham’s Scan Basics It keeps a Stack of candidate points. It Pops elements that are not part of the CH(Q). Whatever is left in the Stack is part of the CH(Q). 63 / 88
  • 125. Graham’s Scan Graham’s Scan Basics It keeps a Stack of candidate points. It Pops elements that are not part of the CH(Q). Whatever is left in the Stack is part of the CH(Q). 63 / 88
  • 126. Graham’s Scan Graham’s Scan Basics It keeps a Stack of candidate points. It Pops elements that are not part of the CH(Q). Whatever is left in the Stack is part of the CH(Q). 63 / 88
  • 127. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 128. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 129. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 130. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 131. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 132. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 133. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 134. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 135. Graham’s Scan Code Algorithm GRAHAM-SCAN(Q) 1. Let p0 be the point Q with the minimum y-coordinate or the leftmost such point in case of a tie 2. let p1, p2, ..., pn be the remaining points in Q, sorted by polar angle in counter clockwise order around p0 (If more than one point has the same angle, remove all but one that is farthest from p0) 3. Let S be an empty stack 4. PUSH(p0, S) 5. PUSH(p1, S) 6. PUSH(p2, S) 7. for i = 3 to n 8. while ccw(next-top(S), pi, top (S)) ≤ 0 9. POP(S) 10. PUSH(S) 11. return S The clockwise and counter clockwise algorithm ccw(p1, p2, p3) 1. return (p3 − p1 × p2 − p1) 64 / 88
  • 136. Example Sort points using the smallest to largest polar coordinate 65 / 88
  • 137. Example Push the first points into the stack 66 / 88
  • 138. Example ccw(p1, p2, p3) > 0, do not get into the loop and push p3 into S clockwise 67 / 88
  • 139. Example ccw(p2, p3, p4) > 0, do not get into the loop and push p4 into S clockwise 68 / 88
  • 140. Example Counterclockwise - Pop p4 counterclockwise 69 / 88
  • 141. Example Clockwise - Push p5 clockwise 70 / 88
  • 142. Example Counterclockwise - Pop p5 and push p6 counterclockwise 71 / 88
  • 144. Example Counterclockwise pop p7 and push p8 counterclockwise 73 / 88
  • 146. Example Clockwise pop p9 and push p10 counterclockwise 75 / 88
  • 147. Example Keep going until you finish 76 / 88
  • 148. We have the following theorem for correctness of the algorithm Theorem 33.1 (Correctness of Graham’s scan) If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then at termination, the stack S consists of, from bottom to top, exactly the vertices of CH(Q) in counterclockwise order. The proof is based in loop invariance I leave this to you to read!!! 77 / 88
  • 149. We have the following theorem for correctness of the algorithm Theorem 33.1 (Correctness of Graham’s scan) If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then at termination, the stack S consists of, from bottom to top, exactly the vertices of CH(Q) in counterclockwise order. The proof is based in loop invariance I leave this to you to read!!! 77 / 88
  • 150. Using Aggregate Analysis to obtain the complexity O (n log2 n) Complexity 1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar angles and cross product. 2 Lines 3-6 take O (1). For the Lines 7-10 The for loop executes at most n − 3 times because we have |Q| − 3 points left 78 / 88
  • 151. Using Aggregate Analysis to obtain the complexity O (n log2 n) Complexity 1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar angles and cross product. 2 Lines 3-6 take O (1). For the Lines 7-10 The for loop executes at most n − 3 times because we have |Q| − 3 points left 78 / 88
  • 152. Using Aggregate Analysis to obtain the complexity O (n log2 n) Complexity 1 Line 2 takes O (n log2 n) using Merge sort or Heap sort by using polar angles and cross product. 2 Lines 3-6 take O (1). For the Lines 7-10 The for loop executes at most n − 3 times because we have |Q| − 3 points left 78 / 88
  • 153. In addition Given PUSH takes O(1) time: Each iteration takes O (1) time not taking in account the time spent in the while loop in lines 8-9. Then The for loop take overall time O(n) time Here is the aggregate analysis Here, we will prove that the overall time for all the times the while loop is touched by the for loop is going to be O (n). 79 / 88
  • 154. In addition Given PUSH takes O(1) time: Each iteration takes O (1) time not taking in account the time spent in the while loop in lines 8-9. Then The for loop take overall time O(n) time Here is the aggregate analysis Here, we will prove that the overall time for all the times the while loop is touched by the for loop is going to be O (n). 79 / 88
  • 155. In addition Given PUSH takes O(1) time: Each iteration takes O (1) time not taking in account the time spent in the while loop in lines 8-9. Then The for loop take overall time O(n) time Here is the aggregate analysis Here, we will prove that the overall time for all the times the while loop is touched by the for loop is going to be O (n). 79 / 88
  • 156. Aggregate Analysis We have that For i = 0, 1, ..., n, we push each point pi into the stack S exactly once. Remember Multipop? We can pop at most the number of items that we push on it. Thus At least three points p0, p1 and pm are never popped out of the stack!!! pm is the last point being taken in consideration!!! With m ≤ n 80 / 88
  • 157. Aggregate Analysis We have that For i = 0, 1, ..., n, we push each point pi into the stack S exactly once. Remember Multipop? We can pop at most the number of items that we push on it. Thus At least three points p0, p1 and pm are never popped out of the stack!!! pm is the last point being taken in consideration!!! With m ≤ n 80 / 88
  • 158. Aggregate Analysis We have that For i = 0, 1, ..., n, we push each point pi into the stack S exactly once. Remember Multipop? We can pop at most the number of items that we push on it. Thus At least three points p0, p1 and pm are never popped out of the stack!!! pm is the last point being taken in consideration!!! With m ≤ n 80 / 88
  • 159. Aggregate Analysis Thus We have m − 2 POP operations are performed in total!!! If we had pushed m elements into S. Thus, each iteration of the while loop It performs one POP, and there are at most m − 2 iterations of the while loop altogether. Now Given that the test in line 8 takes O(1) times, each call of the POP takes O(1) and m ≤ n − 1. 81 / 88
  • 160. Aggregate Analysis Thus We have m − 2 POP operations are performed in total!!! If we had pushed m elements into S. Thus, each iteration of the while loop It performs one POP, and there are at most m − 2 iterations of the while loop altogether. Now Given that the test in line 8 takes O(1) times, each call of the POP takes O(1) and m ≤ n − 1. 81 / 88
  • 161. Aggregate Analysis Thus We have m − 2 POP operations are performed in total!!! If we had pushed m elements into S. Thus, each iteration of the while loop It performs one POP, and there are at most m − 2 iterations of the while loop altogether. Now Given that the test in line 8 takes O(1) times, each call of the POP takes O(1) and m ≤ n − 1. 81 / 88
  • 162. Aggregate Analysis We have that The total time of the while loop is O (n). Finally The Running Time of GRAHAM − SCAN is O (n log2 n) 82 / 88
  • 163. Aggregate Analysis We have that The total time of the while loop is O (n). Finally The Running Time of GRAHAM − SCAN is O (n log2 n) 82 / 88
  • 164. Outline 1 Introduction What is Computational Geometry? 2 Representation Representation of Primitive Geometries 3 Line-Segment Properties Using Point Representation Cross Product Turn Left or Right Intersection 4 Classical Problems Determining whether any pair of segments intersects Correctness of Sweeping Line Algorithm Finding the Convex Hull Graham’s Scan Jarvis’ March 83 / 88
  • 165. Jarvis’ March Jarvis’ March Basics It computes CH by using A technique called Package Wrapping. At each point calculate the minimum polar angle. Create a left and right chain with the convex hull points. 84 / 88
  • 166. Jarvis’ March Jarvis’ March Basics It computes CH by using A technique called Package Wrapping. At each point calculate the minimum polar angle. Create a left and right chain with the convex hull points. 84 / 88
  • 167. Jarvis’ March Jarvis’ March Basics It computes CH by using A technique called Package Wrapping. At each point calculate the minimum polar angle. Create a left and right chain with the convex hull points. 84 / 88
  • 168. Jarvis’ March Jarvis’ March Basics It computes CH by using A technique called Package Wrapping. At each point calculate the minimum polar angle. Create a left and right chain with the convex hull points. 84 / 88
  • 169. Formally Jarvis’s march builds a sequence H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q) First We start with p0 the next vertex p1 in the convex hull has the smallest polar angle with respect to p0. Next p2 has the smallest polar angle with respect to p1. 85 / 88
  • 170. Formally Jarvis’s march builds a sequence H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q) First We start with p0 the next vertex p1 in the convex hull has the smallest polar angle with respect to p0. Next p2 has the smallest polar angle with respect to p1. 85 / 88
  • 171. Formally Jarvis’s march builds a sequence H = p0, p1, p2, ..., ph−1 of the vertices of CH(Q) First We start with p0 the next vertex p1 in the convex hull has the smallest polar angle with respect to p0. Next p2 has the smallest polar angle with respect to p1. 85 / 88
  • 172. Now Then When we reach the highest vertex, pk (Breaking ties by choosing the farthest such vertex), we have constructed the right chain of CH(Q). To construct the left chain We start at pk, then we choose pk+1 as the point with the smallest polar angle with respect to pk negative , but from the negative x-axis. Next The next point is selected in the same manner until we have reached p0. 86 / 88
  • 173. Now Then When we reach the highest vertex, pk (Breaking ties by choosing the farthest such vertex), we have constructed the right chain of CH(Q). To construct the left chain We start at pk, then we choose pk+1 as the point with the smallest polar angle with respect to pk negative , but from the negative x-axis. Next The next point is selected in the same manner until we have reached p0. 86 / 88
  • 174. Now Then When we reach the highest vertex, pk (Breaking ties by choosing the farthest such vertex), we have constructed the right chain of CH(Q). To construct the left chain We start at pk, then we choose pk+1 as the point with the smallest polar angle with respect to pk negative , but from the negative x-axis. Next The next point is selected in the same manner until we have reached p0. 86 / 88
  • 175. Jarvis’ March Example LEFT CHAIN RIGHT CHAIN Figure: Wrapping the Gift. Here the Right Chain finishes at p6, then the Left Chain is started 87 / 88
  • 176. Complexity Something Notable Complexity O (hn) h number of points in CH. O (n) for finding the minimum angle and the farthest point by y-axis 88 / 88
  • 177. Complexity Something Notable Complexity O (hn) h number of points in CH. O (n) for finding the minimum angle and the farthest point by y-axis 88 / 88
  • 178. Complexity Something Notable Complexity O (hn) h number of points in CH. O (n) for finding the minimum angle and the farthest point by y-axis 88 / 88
  • 179. Complexity Something Notable Complexity O (hn) h number of points in CH. O (n) for finding the minimum angle and the farthest point by y-axis 88 / 88