181
Analysis of Algorithms Computational Geometry Andres Mendez-Vazquez November 30, 2015 1 / 90

26 Computational Geometry

Embed Size (px)

Citation preview

Page 1: 26 Computational Geometry

Analysis of AlgorithmsComputational Geometry

Andres Mendez-Vazquez

November 30, 2015

1 / 90

Page 2: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

2 / 90

Page 3: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

3 / 90

Page 4: 26 Computational Geometry

Computational Geometry

MotivationWe want to solve geometric problems!!!

4 / 90

Page 5: 26 Computational Geometry

Field of Application

VLSI design - Generation for Fast Voronoi Diagrams for MassiveLayouts Under Strict Distances to avoid Tunneling Effects!!

5 / 90

Page 6: 26 Computational Geometry

Field of Application

Databases - Octrees for fast localization of information in databasetables

6 / 90

Page 7: 26 Computational Geometry

Field of ApplicationSynthetic Biology - Geometric Algorithms to Obtain new DNAconfigurations for Molecular Machines

7 / 90

Page 8: 26 Computational Geometry

Field of Application

Computer Graphics for more engaging Virtual Environments - Forexample: Bump Mapping!!!

8 / 90

Page 9: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

9 / 90

Page 10: 26 Computational Geometry

The Plane Representation

Although 3D algorithms exist...We will deal only with algorithms working in the plane.

Object RepresentationEach object is a set of points {p1, p2, ..., pn} where

I pi = (xi , yi) and xi , yi ∈ R.

ExampleFor example an n-vertex polygon P is the following order sequence:

I 〈p0, p2, ..., pn〉

10 / 90

Page 11: 26 Computational Geometry

The Plane Representation

Although 3D algorithms exist...We will deal only with algorithms working in the plane.

Object RepresentationEach object is a set of points {p1, p2, ..., pn} where

I pi = (xi , yi) and xi , yi ∈ R.

ExampleFor example an n-vertex polygon P is the following order sequence:

I 〈p0, p2, ..., pn〉

10 / 90

Page 12: 26 Computational Geometry

The Plane Representation

Although 3D algorithms exist...We will deal only with algorithms working in the plane.

Object RepresentationEach object is a set of points {p1, p2, ..., pn} where

I pi = (xi , yi) and xi , yi ∈ R.

ExampleFor example an n-vertex polygon P is the following order sequence:

I 〈p0, p2, ..., pn〉

10 / 90

Page 13: 26 Computational Geometry

The Plane Representation

Although 3D algorithms exist...We will deal only with algorithms working in the plane.

Object RepresentationEach object is a set of points {p1, p2, ..., pn} where

I pi = (xi , yi) and xi , yi ∈ R.

ExampleFor example an n-vertex polygon P is the following order sequence:

I 〈p0, p2, ..., pn〉

10 / 90

Page 14: 26 Computational Geometry

The Plane Representation

Although 3D algorithms exist...We will deal only with algorithms working in the plane.

Object RepresentationEach object is a set of points {p1, p2, ..., pn} where

I pi = (xi , yi) and xi , yi ∈ R.

ExampleFor example an n-vertex polygon P is the following order sequence:

I 〈p0, p2, ..., pn〉

10 / 90

Page 15: 26 Computational Geometry

Example

Polygon

11 / 90

Page 16: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

12 / 90

Page 17: 26 Computational Geometry

Line-segment Properties

A convex combinationGiven two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convexcombination of {p1, p2} is any point p3 such that:

I p3 = αp1 + (1− α) p2 with 0 ≤ α ≤ 1.

Line Segment as Convex CombinationGiven two points p1 and p2 (Known as End Points), the line segmentp1p2 is the set of convex combinations of p1 and p2.

Directed SegmentHere, we care about the direction with initial point p1 for the directedsegment −−→p1p2:

I If p1 = (0, 0) then −−→p1p2 is the vector p2.

13 / 90

Page 18: 26 Computational Geometry

Line-segment Properties

A convex combinationGiven two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convexcombination of {p1, p2} is any point p3 such that:

I p3 = αp1 + (1− α) p2 with 0 ≤ α ≤ 1.

Line Segment as Convex CombinationGiven two points p1 and p2 (Known as End Points), the line segmentp1p2 is the set of convex combinations of p1 and p2.

Directed SegmentHere, we care about the direction with initial point p1 for the directedsegment −−→p1p2:

I If p1 = (0, 0) then −−→p1p2 is the vector p2.

13 / 90

Page 19: 26 Computational Geometry

Line-segment Properties

A convex combinationGiven two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convexcombination of {p1, p2} is any point p3 such that:

I p3 = αp1 + (1− α) p2 with 0 ≤ α ≤ 1.

Line Segment as Convex CombinationGiven two points p1 and p2 (Known as End Points), the line segmentp1p2 is the set of convex combinations of p1 and p2.

Directed SegmentHere, we care about the direction with initial point p1 for the directedsegment −−→p1p2:

I If p1 = (0, 0) then −−→p1p2 is the vector p2.

13 / 90

Page 20: 26 Computational Geometry

Line-segment Properties

A convex combinationGiven two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convexcombination of {p1, p2} is any point p3 such that:

I p3 = αp1 + (1− α) p2 with 0 ≤ α ≤ 1.

Line Segment as Convex CombinationGiven two points p1 and p2 (Known as End Points), the line segmentp1p2 is the set of convex combinations of p1 and p2.

Directed SegmentHere, we care about the direction with initial point p1 for the directedsegment −−→p1p2:

I If p1 = (0, 0) then −−→p1p2 is the vector p2.

13 / 90

Page 21: 26 Computational Geometry

Line-segment Properties

A convex combinationGiven two distinct points p1 = (x1, y1)T and p2 = (x2, y2)T , a convexcombination of {p1, p2} is any point p3 such that:

I p3 = αp1 + (1− α) p2 with 0 ≤ α ≤ 1.

Line Segment as Convex CombinationGiven two points p1 and p2 (Known as End Points), the line segmentp1p2 is the set of convex combinations of p1 and p2.

Directed SegmentHere, we care about the direction with initial point p1 for the directedsegment −−→p1p2:

I If p1 = (0, 0) then −−→p1p2 is the vector p2.

13 / 90

Page 22: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

14 / 90

Page 23: 26 Computational Geometry

Cross Product

Question!!!Given two directed segments −−→p0p1 and −−→p0p2,

I Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?

Cross ProductCross product p1 × p2 as the signed area of the parallelogram formedby

15 / 90

Page 24: 26 Computational Geometry

Cross Product

Question!!!Given two directed segments −−→p0p1 and −−→p0p2,

I Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?

Cross ProductCross product p1 × p2 as the signed area of the parallelogram formedby

15 / 90

Page 25: 26 Computational Geometry

Cross ProductQuestion!!!

Given two directed segments −−→p0p1 and −−→p0p2,I Is −−→p0p1 clockwise from −−→p0p2 with respect to their common endpoint p0?

Cross ProductCross product p1 × p2 as the signed area of the parallelogram formedby

15 / 90

Page 26: 26 Computational Geometry

Cross Product

A shorter representation

p1 × p2 = det(

p1 p2)=det

(x1 x2y1 y2

)= x1y2 − x2y1 = −p2 × p1

Thusif p1 × p2 is positive, then p1 is clockwise from p2.if p1 × p2 is negative, then p1 is counterclockwise from p2.

16 / 90

Page 27: 26 Computational Geometry

Cross Product

A shorter representation

p1 × p2 = det(

p1 p2)=det

(x1 x2y1 y2

)= x1y2 − x2y1 = −p2 × p1

Thusif p1 × p2 is positive, then p1 is clockwise from p2.if p1 × p2 is negative, then p1 is counterclockwise from p2.

16 / 90

Page 28: 26 Computational Geometry

Cross Product

A shorter representation

p1 × p2 = det(

p1 p2)=det

(x1 x2y1 y2

)= x1y2 − x2y1 = −p2 × p1

Thusif p1 × p2 is positive, then p1 is clockwise from p2.if p1 × p2 is negative, then p1 is counterclockwise from p2.

16 / 90

Page 29: 26 Computational Geometry

Regions

Clockwise and Counterclockwise Regions

Figure: Darker counterclockwise; lighter clockwise with respect to p

17 / 90

Page 30: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

18 / 90

Page 31: 26 Computational Geometry

Turn Left or Right

QuestionGiven two line segments −−→p0p1 and −−→p1p2,

if we traverse −−→p0p1 and then −−→p1p2, do we make a left turn at point p1?

19 / 90

Page 32: 26 Computational Geometry

Turn Left or Right

Simply use the following ideaCompute 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 / 90

Page 33: 26 Computational Geometry

Turn Left or Right

Simply use the following ideaCompute 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 / 90

Page 34: 26 Computational Geometry

Turn Left or Right

Simply use the following ideaCompute 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 / 90

Page 35: 26 Computational Geometry

Turn Left or RightSimply 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 = clockwisecounterclockwise clockwise

20 / 90

Page 36: 26 Computational Geometry

Code for this

We have the following codeDirection(pi , pj , pk)

1 return (pk − pi)× (pj − pi)

21 / 90

Page 37: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

22 / 90

Page 38: 26 Computational Geometry

Intersection

QuestionDo line segments −−→p1p2 and −−→p3p4 intersect?

Very Simple!!! We have two possibilities1 Each segment straddles the line containing the other.2 An endpoint of one segment lies on the other segment.

23 / 90

Page 39: 26 Computational Geometry

Intersection

QuestionDo line segments −−→p1p2 and −−→p3p4 intersect?

Very Simple!!! We have two possibilities1 Each segment straddles the line containing the other.2 An endpoint of one segment lies on the other segment.

23 / 90

Page 40: 26 Computational Geometry

Intersection

QuestionDo line segments −−→p1p2 and −−→p3p4 intersect?

Very Simple!!! We have two possibilities1 Each segment straddles the line containing the other.2 An endpoint of one segment lies on the other segment.

23 / 90

Page 41: 26 Computational Geometry

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 / 90

Page 42: 26 Computational Geometry

Case II No intersection

The segment straddles the line, but the other does not straddle theother line

Same Signs

Figure: Using Cross Products to find that there is no intersection

25 / 90

Page 43: 26 Computational Geometry

Code

CodeSegment-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) and6 (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 thesegment

26 / 90

Page 44: 26 Computational Geometry

Code

CodeSegment-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) and6 (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 thesegment

26 / 90

Page 45: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

27 / 90

Page 46: 26 Computational Geometry

Sweeping

SweepingUse an imaginary vertical line to pass through the n segments with eventsx ∈ {r , t, u}:

Figure: Vertical Line to Record Events

28 / 90

Page 47: 26 Computational Geometry

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.I This is a total preorder relation for segment intersecting theline at x .

I The relation is transitive and reflexive.Event II: s1 intersect s2, then neither s1 <x s2 or s2 <x s1, orboth (if s1 and s2 intersect at x)

29 / 90

Page 48: 26 Computational Geometry

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.I This is a total preorder relation for segment intersecting theline at x .

I The relation is transitive and reflexive.Event II: s1 intersect s2, then neither s1 <x s2 or s2 <x s1, orboth (if s1 and s2 intersect at x)

29 / 90

Page 49: 26 Computational Geometry

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.I This is a total preorder relation for segment intersecting theline at x .

I The relation is transitive and reflexive.Event II: s1 intersect s2, then neither s1 <x s2 or s2 <x s1, orboth (if s1 and s2 intersect at x)

29 / 90

Page 50: 26 Computational Geometry

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.I This is a total preorder relation for segment intersecting theline at x .

I The relation is transitive and reflexive.Event II: s1 intersect s2, then neither s1 <x s2 or s2 <x s1, orboth (if s1 and s2 intersect at x)

29 / 90

Page 51: 26 Computational Geometry

Example

Example: a <r c a <t c

Figure: Vertical Line to Record Events

30 / 90

Page 52: 26 Computational Geometry

Change in directionWhen 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 / 90

Page 53: 26 Computational Geometry

Moving the sweep line

Something NotableSweeping algorithms typically manage two sets of data.

Sweep-line statusThe sweep-line status gives the relationships among the objects that thesweep line intersects.

Event-point scheduleThe 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 eachevent point, then resumes.It is possible to use a min-priority queue to keep those event pointssorted by x-coordinate.

32 / 90

Page 54: 26 Computational Geometry

Moving the sweep line

Something NotableSweeping algorithms typically manage two sets of data.

Sweep-line statusThe sweep-line status gives the relationships among the objects that thesweep line intersects.

Event-point scheduleThe 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 eachevent point, then resumes.It is possible to use a min-priority queue to keep those event pointssorted by x-coordinate.

32 / 90

Page 55: 26 Computational Geometry

Moving the sweep line

Something NotableSweeping algorithms typically manage two sets of data.

Sweep-line statusThe sweep-line status gives the relationships among the objects that thesweep line intersects.

Event-point scheduleThe 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 eachevent point, then resumes.It is possible to use a min-priority queue to keep those event pointssorted by x-coordinate.

32 / 90

Page 56: 26 Computational Geometry

Moving the sweep line

Something NotableSweeping algorithms typically manage two sets of data.

Sweep-line statusThe sweep-line status gives the relationships among the objects that thesweep line intersects.

Event-point scheduleThe 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 eachevent point, then resumes.It is possible to use a min-priority queue to keep those event pointssorted by x-coordinate.

32 / 90

Page 57: 26 Computational Geometry

Sweeping Process

FirstWe sort the segment endpoints by increasing x-coordinate andproceed 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 thecovertical left endpoints before the covertical right endpoints.

33 / 90

Page 58: 26 Computational Geometry

Sweeping ProcessFirst

We sort the segment endpoints by increasing x-coordinate andproceed 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 thecovertical left endpoints before the covertical right endpoints.

Covertical LeftEnd Points

Covertical RightEnd Points

33 / 90

Page 59: 26 Computational Geometry

Then

SecondWithin a set of covertical left endpoints, we put those with lowery-coordinates first, and we do the same within a set of covertical rightendpoints.

Covertical LeftEnd Points

Covertical RightEnd Points

FIRST

34 / 90

Page 60: 26 Computational Geometry

Then

Process1 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.

ThusWhenever two segments first become consecutive in the total preorder, wecheck whether they intersect.

35 / 90

Page 61: 26 Computational Geometry

Then

Process1 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.

ThusWhenever two segments first become consecutive in the total preorder, wecheck whether they intersect.

35 / 90

Page 62: 26 Computational Geometry

Then

Process1 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.

ThusWhenever two segments first become consecutive in the total preorder, wecheck whether they intersect.

35 / 90

Page 63: 26 Computational Geometry

Operations

Operations to keep preorder on the events for algorithmINSERT(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 .

NoteEach operation can be performed in O(log2 n) using a red-black-tree byusing comparisons by cross product to find the above and below.

This allows to seeThe relative ordering of two segments.

36 / 90

Page 64: 26 Computational Geometry

Operations

Operations to keep preorder on the events for algorithmINSERT(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 .

NoteEach operation can be performed in O(log2 n) using a red-black-tree byusing comparisons by cross product to find the above and below.

This allows to seeThe relative ordering of two segments.

36 / 90

Page 65: 26 Computational Geometry

Operations

Operations to keep preorder on the events for algorithmINSERT(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 .

NoteEach operation can be performed in O(log2 n) using a red-black-tree byusing comparisons by cross product to find the above and below.

This allows to seeThe relative ordering of two segments.

36 / 90

Page 66: 26 Computational Geometry

Operations

Operations to keep preorder on the events for algorithmINSERT(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 .

NoteEach operation can be performed in O(log2 n) using a red-black-tree byusing comparisons by cross product to find the above and below.

This allows to seeThe relative ordering of two segments.

36 / 90

Page 67: 26 Computational Geometry

Operations

Operations to keep preorder on the events for algorithmINSERT(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 .

NoteEach operation can be performed in O(log2 n) using a red-black-tree byusing comparisons by cross product to find the above and below.

This allows to seeThe relative ordering of two segments.

36 / 90

Page 68: 26 Computational Geometry

Operations

Operations to keep preorder on the events for algorithmINSERT(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 .

NoteEach operation can be performed in O(log2 n) using a red-black-tree byusing comparisons by cross product to find the above and below.

This allows to seeThe relative ordering of two segments.

36 / 90

Page 69: 26 Computational Geometry

What the algorithm does?

Moving the sweeping line discretely - Event-point scheduleHere, moving through the x-axis

discrete events

Using the RBT

to Store the Infoabout the segment

order

37 / 90

Page 70: 26 Computational Geometry

Event-Point Schedule Implementation

For thisWe can use a Priority Queue using lexicographic order

The interesting part is the Sweeping-Line SatusBecause the way we build the balanced tree

38 / 90

Page 71: 26 Computational Geometry

Event-Point Schedule Implementation

For thisWe can use a Priority Queue using lexicographic order

The interesting part is the Sweeping-Line SatusBecause the way we build the balanced tree

38 / 90

Page 72: 26 Computational Geometry

Sweep-Line StatusThe Above and Below relation

TIME

Sweeping Line

39 / 90

Page 73: 26 Computational Geometry

Sweeping Line Status Implementation

Use the following relation of order to build the binary treeGiven 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 addtionIf you are at a leaf do the insertion, but also insert the leaf at the left orright given the insertion.

40 / 90

Page 74: 26 Computational Geometry

Sweeping Line Status Implementation

Use the following relation of order to build the binary treeGiven 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 addtionIf you are at a leaf do the insertion, but also insert the leaf at the left orright given the insertion.

40 / 90

Page 75: 26 Computational Geometry

Example

We insert th first element in the circular leaves list

41 / 90

Page 76: 26 Computational Geometry

Example

We insert a inner node after binary search

Below Above

42 / 90

Page 77: 26 Computational Geometry

Example

Similar

Below Above

43 / 90

Page 78: 26 Computational Geometry

Example

Etc....

Below Above

TriggerRebalancingin the inner nodes

44 / 90

Page 79: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 80: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 81: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 82: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 83: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 84: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 85: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 86: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 87: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 88: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 89: 26 Computational Geometry

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 endpointsand breaking further ties by putting points with lowery−coordinates first

3 for each point p in the sorted list4 if p is the left endpoint of a segment s5 INSERT(T , s)6 if (ABOVE(T , s) exists and intersect s)

or (BELOW(T , s) exists and intersect s)7 return TRUE8 if p is the right endpoint of a segment s9 if (both ABOVE(T , s) and BELOW(T , s) exist)

and (ABOVE(T , s) intersect BELOW(T , s))10 return TRUE11 DELETE(T , s)12 return FALSE 45 / 90

Page 90: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

46 / 90

Page 91: 26 Computational Geometry

Correctness

The ANY-SEGMENTS-INTERSECT returns TRUEIf 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 bSince no intersections occur to the left of p, the order given by T(Sweeping Line Data Structure) is correct at all points to the left of p.Assuming that no three segments intersect at the same point, a and bbecome consecutive in the total preorder of some sweep line z.

47 / 90

Page 92: 26 Computational Geometry

Correctness

The ANY-SEGMENTS-INTERSECT returns TRUEIf 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 bSince no intersections occur to the left of p, the order given by T(Sweeping Line Data Structure) is correct at all points to the left of p.Assuming that no three segments intersect at the same point, a and bbecome consecutive in the total preorder of some sweep line z.

47 / 90

Page 93: 26 Computational Geometry

Correctness

The ANY-SEGMENTS-INTERSECT returns TRUEIf 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 bSince no intersections occur to the left of p, the order given by T(Sweeping Line Data Structure) is correct at all points to the left of p.Assuming that no three segments intersect at the same point, a and bbecome consecutive in the total preorder of some sweep line z.

47 / 90

Page 94: 26 Computational Geometry

Correctness

The ANY-SEGMENTS-INTERSECT returns TRUEIf 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 bSince no intersections occur to the left of p, the order given by T(Sweeping Line Data Structure) is correct at all points to the left of p.Assuming that no three segments intersect at the same point, a and bbecome consecutive in the total preorder of some sweep line z.

47 / 90

Page 95: 26 Computational Geometry

Correctness

The ANY-SEGMENTS-INTERSECT returns TRUEIf 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 bSince no intersections occur to the left of p, the order given by T(Sweeping Line Data Structure) is correct at all points to the left of p.Assuming that no three segments intersect at the same point, a and bbecome consecutive in the total preorder of some sweep line z.

47 / 90

Page 96: 26 Computational Geometry

Now, we have two possbibilities

Case I

a

b

p

z SWEEPING LINE

48 / 90

Page 97: 26 Computational Geometry

Case I

Moreoverz is to the left of p or goes through p.

In additionThere is a endpoint q where a and b become consecutive.

49 / 90

Page 98: 26 Computational Geometry

Case I

Moreoverz is to the left of p or goes through p.

In additionThere is a endpoint q where a and b become consecutive.

a

b

p

z

q

49 / 90

Page 99: 26 Computational Geometry

Finally

Then a and bThey become consecutive in the total pre-order of a sweep line.

50 / 90

Page 100: 26 Computational Geometry

Case II

We have that q is a left endpoint where a and b stop beingconsecutive

a

b

p

z

q

51 / 90

Page 101: 26 Computational Geometry

Correctness about the order given by T

Then, given the two following cases1 if p is in the sweep line ⇒ p == q.2 If q is at the left of p, and it is the nearest left one.

52 / 90

Page 102: 26 Computational Geometry

Correctness about the order given by T

Then, given the two following cases1 if p is in the sweep line ⇒ p == q.2 If q is at the left of p, and it is the nearest left one.

52 / 90

Page 103: 26 Computational Geometry

Do we mantain the correct preorder?

We have that given that p is firstThen, it is processed first becacuse the lexicographic order.

Therefore, two cases can happen1 The point is processed - then the algorithm returns true2 If the event is not processed - then the algorithm must have returned

true

53 / 90

Page 104: 26 Computational Geometry

Do we mantain the correct preorder?

We have that given that p is firstThen, it is processed first becacuse the lexicographic order.

Therefore, two cases can happen1 The point is processed - then the algorithm returns true2 If the event is not processed - then the algorithm must have returned

true

53 / 90

Page 105: 26 Computational Geometry

Handling Case I

Segments a and b are already in T , and a segment between them inthe total pre-order is deleted, making a and b to become consecutive

a

b

p

z

qI

aIb

ab

54 / 90

Page 106: 26 Computational Geometry

When is this detected?

In the following lines of the codeLines 8–11 detect this case.

55 / 90

Page 107: 26 Computational Geometry

Handling Case II

Either a or b is inserted into T , and the other segment is above orbelow it in the total pre-order.

a

b

p

z

q

56 / 90

Page 108: 26 Computational Geometry

When is this detected?

In the following lines of the codeLines 4–7 detect this case.

57 / 90

Page 109: 26 Computational Geometry

Finally

If event point q is not processedIt must have found an earlier intersection!!!

ThereforeIf there is an intersection Any-Segment-Intersect returns true all the time

58 / 90

Page 110: 26 Computational Geometry

Finally

If event point q is not processedIt must have found an earlier intersection!!!

ThereforeIf there is an intersection Any-Segment-Intersect returns true all the time

58 / 90

Page 111: 26 Computational Geometry

Running Time

Something Notable1 Line 1 takes O(1) time.2 Line 2 takes O(n log2 n) time, using merge or heap sort3 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 TimeO (n log2 n)

59 / 90

Page 112: 26 Computational Geometry

Running Time

Something Notable1 Line 1 takes O(1) time.2 Line 2 takes O(n log2 n) time, using merge or heap sort3 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 TimeO (n log2 n)

59 / 90

Page 113: 26 Computational Geometry

Running Time

Something Notable1 Line 1 takes O(1) time.2 Line 2 takes O(n log2 n) time, using merge or heap sort3 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 TimeO (n log2 n)

59 / 90

Page 114: 26 Computational Geometry

Running Time

Something Notable1 Line 1 takes O(1) time.2 Line 2 takes O(n log2 n) time, using merge or heap sort3 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 TimeO (n log2 n)

59 / 90

Page 115: 26 Computational Geometry

Running Time

Something Notable1 Line 1 takes O(1) time.2 Line 2 takes O(n log2 n) time, using merge or heap sort3 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 TimeO (n log2 n)

59 / 90

Page 116: 26 Computational Geometry

Running Time

Something Notable1 Line 1 takes O(1) time.2 Line 2 takes O(n log2 n) time, using merge or heap sort3 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 TimeO (n log2 n)

59 / 90

Page 117: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

60 / 90

Page 118: 26 Computational Geometry

Convex Hull

Convex HullGiven a set of points, Q, find the smallest convex polygon P suchthat Q ⊂ P. This is denoted by CH(Q).

Bunch of Points in 2D

61 / 90

Page 119: 26 Computational Geometry

Convex Hull

Convex HullGiven a set of points, Q, find the smallest convex polygon P suchthat Q ⊂ P. This is denoted by CH(Q).

Their Convex Hull

62 / 90

Page 120: 26 Computational Geometry

Convex Hull

The two main Algorithms (Using the “Rotational Sweep”) that we aregoing to explore

Graham’s Scan.Jarvis’ March.

Nevertheless there are other methodsThe incremental methodDivide-and-conquer method.Prune-and-search method.

63 / 90

Page 121: 26 Computational Geometry

Convex Hull

The two main Algorithms (Using the “Rotational Sweep”) that we aregoing to explore

Graham’s Scan.Jarvis’ March.

Nevertheless there are other methodsThe incremental methodDivide-and-conquer method.Prune-and-search method.

63 / 90

Page 122: 26 Computational Geometry

Convex Hull

The two main Algorithms (Using the “Rotational Sweep”) that we aregoing to explore

Graham’s Scan.Jarvis’ March.

Nevertheless there are other methodsThe incremental methodDivide-and-conquer method.Prune-and-search method.

63 / 90

Page 123: 26 Computational Geometry

Convex Hull

The two main Algorithms (Using the “Rotational Sweep”) that we aregoing to explore

Graham’s Scan.Jarvis’ March.

Nevertheless there are other methodsThe incremental methodDivide-and-conquer method.Prune-and-search method.

63 / 90

Page 124: 26 Computational Geometry

Convex Hull

The two main Algorithms (Using the “Rotational Sweep”) that we aregoing to explore

Graham’s Scan.Jarvis’ March.

Nevertheless there are other methodsThe incremental methodDivide-and-conquer method.Prune-and-search method.

63 / 90

Page 125: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

64 / 90

Page 126: 26 Computational Geometry

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).

65 / 90

Page 127: 26 Computational Geometry

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).

65 / 90

Page 128: 26 Computational Geometry

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).

65 / 90

Page 129: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 130: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 131: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 132: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 133: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 134: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 135: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 136: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 137: 26 Computational Geometry

Graham’s Scan CodeAlgorithmGRAHAM-SCAN(Q)1. Let p0 be the point Q with

the minimum y-coordinate orthe leftmost such point incase of a tie

2. let 〈p1, p2, ..., pn〉 be theremaining points in Q, sortedby polar angle in counterclockwise order around p0 (Ifmore than one point has thesame angle, remove all butone that is farthest from p0)

3. Let S be an empty stack4. PUSH(p0, S)5. PUSH(p1, S)6. PUSH(p2, S)

7. for i = 3 to n8. while ccw(next-top(S), pi , top (S)) ≤ 09. POP(S)10. PUSH(S)11. return S

. The clockwise and counter clockwise algorithm/

ccw(p1, p2, p3)1. return (p3 − p1 × p2 − p1)

66 / 90

Page 138: 26 Computational Geometry

Example

Sort points using the smallest to largest polar coordinate

67 / 90

Page 139: 26 Computational Geometry

Example

Push the first points into the stack

68 / 90

Page 140: 26 Computational Geometry

Example

ccw(p1, p2, p3) > 0, do not get into the loop and push p3 into S

clockwise

69 / 90

Page 141: 26 Computational Geometry

Example

ccw(p2, p3, p4) > 0, do not get into the loop and push p4 into S

clockwise

70 / 90

Page 142: 26 Computational Geometry

Example

Counterclockwise - Pop p4

counterclockwise

71 / 90

Page 143: 26 Computational Geometry

Example

Clockwise - Push p5

clockwise

72 / 90

Page 144: 26 Computational Geometry

Example

Counterclockwise - Pop p5 and push p6

counterclockwise

73 / 90

Page 145: 26 Computational Geometry

Example

Clockwise push p7

clockwise

74 / 90

Page 146: 26 Computational Geometry

Example

Counterclockwise pop p7 and push p8

counterclockwise

75 / 90

Page 147: 26 Computational Geometry

Example

Clockwise push p9

clockwise

76 / 90

Page 148: 26 Computational Geometry

Example

Clockwise pop p9 and push p10

counterclockwise

77 / 90

Page 149: 26 Computational Geometry

Example

Keep going until you finish

78 / 90

Page 150: 26 Computational Geometry

We have the following theorem for correctness of thealgorithm

Theorem 33.1 (Correctness of Graham’s scan)If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then attermination, the stack S consists of, from bottom to top, exactly thevertices of CH(Q) in counterclockwise order.

The proof is based in loop invarianceI leave this to you to read!!!

79 / 90

Page 151: 26 Computational Geometry

We have the following theorem for correctness of thealgorithm

Theorem 33.1 (Correctness of Graham’s scan)If GRAHAM-SCAN executes on a set Q of points, where |Q| ≥ 3, then attermination, the stack S consists of, from bottom to top, exactly thevertices of CH(Q) in counterclockwise order.

The proof is based in loop invarianceI leave this to you to read!!!

79 / 90

Page 152: 26 Computational Geometry

Using Aggregate Analysis to obtain the complexityO (n log2 n)

Complexity1 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-10The for loop executes at most n − 3 times because we have |Q| − 3 pointsleft

80 / 90

Page 153: 26 Computational Geometry

Using Aggregate Analysis to obtain the complexityO (n log2 n)

Complexity1 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-10The for loop executes at most n − 3 times because we have |Q| − 3 pointsleft

80 / 90

Page 154: 26 Computational Geometry

Using Aggregate Analysis to obtain the complexityO (n log2 n)

Complexity1 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-10The for loop executes at most n − 3 times because we have |Q| − 3 pointsleft

80 / 90

Page 155: 26 Computational Geometry

In addition

GivenPUSH takes O(1) time:

Each iteration takes O (1) time not taking in account the time spentin the while loop in lines 8-9.

ThenThe for loop take overall time O(n) time

Here is the aggregate analysisHere, we will prove that the overall time for all the times the while loop istouched by the for loop is going to be O (n).

81 / 90

Page 156: 26 Computational Geometry

In addition

GivenPUSH takes O(1) time:

Each iteration takes O (1) time not taking in account the time spentin the while loop in lines 8-9.

ThenThe for loop take overall time O(n) time

Here is the aggregate analysisHere, we will prove that the overall time for all the times the while loop istouched by the for loop is going to be O (n).

81 / 90

Page 157: 26 Computational Geometry

In addition

GivenPUSH takes O(1) time:

Each iteration takes O (1) time not taking in account the time spentin the while loop in lines 8-9.

ThenThe for loop take overall time O(n) time

Here is the aggregate analysisHere, we will prove that the overall time for all the times the while loop istouched by the for loop is going to be O (n).

81 / 90

Page 158: 26 Computational Geometry

Aggregate Analysis

We have thatFor 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.

ThusAt 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

82 / 90

Page 159: 26 Computational Geometry

Aggregate Analysis

We have thatFor 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.

ThusAt 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

82 / 90

Page 160: 26 Computational Geometry

Aggregate Analysis

We have thatFor 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.

ThusAt 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

82 / 90

Page 161: 26 Computational Geometry

Aggregate Analysis

ThusWe have m − 2 POP operations are performed in total!!! If we had pushedm elements into S .

Thus, each iteration of the while loopIt performs one POP, and there are at most m − 2 iterations of the whileloop altogether.

NowGiven that the test in line 8 takes O(1) times, each call of the POP takesO(1) and m ≤ n − 1.

83 / 90

Page 162: 26 Computational Geometry

Aggregate Analysis

ThusWe have m − 2 POP operations are performed in total!!! If we had pushedm elements into S .

Thus, each iteration of the while loopIt performs one POP, and there are at most m − 2 iterations of the whileloop altogether.

NowGiven that the test in line 8 takes O(1) times, each call of the POP takesO(1) and m ≤ n − 1.

83 / 90

Page 163: 26 Computational Geometry

Aggregate Analysis

ThusWe have m − 2 POP operations are performed in total!!! If we had pushedm elements into S .

Thus, each iteration of the while loopIt performs one POP, and there are at most m − 2 iterations of the whileloop altogether.

NowGiven that the test in line 8 takes O(1) times, each call of the POP takesO(1) and m ≤ n − 1.

83 / 90

Page 164: 26 Computational Geometry

Aggregate Analysis

We have thatThe total time of the while loop is O (n).

FinallyThe Running Time of GRAHAM − SCAN is O (n log2 n)

84 / 90

Page 165: 26 Computational Geometry

Aggregate Analysis

We have thatThe total time of the while loop is O (n).

FinallyThe Running Time of GRAHAM − SCAN is O (n log2 n)

84 / 90

Page 166: 26 Computational Geometry

Outline

1 IntroductionWhat is Computational Geometry?

2 RepresentationRepresentation of Primitive Geometries

3 Line-Segment PropertiesUsing Point RepresentationCross ProductTurn Left or RightIntersection

4 Classical ProblemsDetermining whether any pair of segments intersectsCorrectness of Sweeping Line AlgorithmFinding the Convex Hull

Graham’s ScanJarvis’ March

85 / 90

Page 167: 26 Computational Geometry

Jarvis’ March

Jarvis’ March BasicsIt computes CH by using

I A technique called Package Wrapping.I At each point calculate the minimum polar angle.I Create a left and right chain with the convex hull points.

86 / 90

Page 168: 26 Computational Geometry

Jarvis’ March

Jarvis’ March BasicsIt computes CH by using

I A technique called Package Wrapping.I At each point calculate the minimum polar angle.I Create a left and right chain with the convex hull points.

86 / 90

Page 169: 26 Computational Geometry

Jarvis’ March

Jarvis’ March BasicsIt computes CH by using

I A technique called Package Wrapping.I At each point calculate the minimum polar angle.I Create a left and right chain with the convex hull points.

86 / 90

Page 170: 26 Computational Geometry

Jarvis’ March

Jarvis’ March BasicsIt computes CH by using

I A technique called Package Wrapping.I At each point calculate the minimum polar angle.I Create a left and right chain with the convex hull points.

86 / 90

Page 171: 26 Computational Geometry

Formally

Jarvis’s march builds a sequenceH = 〈p0, p1, p2, ..., ph−1〉 of the vertices of CH(Q)

FirstWe start with p0 the next vertex p1 in the convex hull has the smallestpolar angle with respect to p0.

Nextp2 has the smallest polar angle with respect to p1.

87 / 90

Page 172: 26 Computational Geometry

Formally

Jarvis’s march builds a sequenceH = 〈p0, p1, p2, ..., ph−1〉 of the vertices of CH(Q)

FirstWe start with p0 the next vertex p1 in the convex hull has the smallestpolar angle with respect to p0.

Nextp2 has the smallest polar angle with respect to p1.

87 / 90

Page 173: 26 Computational Geometry

Formally

Jarvis’s march builds a sequenceH = 〈p0, p1, p2, ..., ph−1〉 of the vertices of CH(Q)

FirstWe start with p0 the next vertex p1 in the convex hull has the smallestpolar angle with respect to p0.

Nextp2 has the smallest polar angle with respect to p1.

87 / 90

Page 174: 26 Computational Geometry

Now

ThenWhen we reach the highest vertex, pk (Breaking ties by choosing thefarthest such vertex), we have constructed the right chain of CH(Q).

To construct the left chainWe start at pk , then we choose pk+1 as the point with the smallest polarangle with respect to pk negative , but from the negative x-axis.

NextThe next point is selected in the same manner until we have reached p0.

88 / 90

Page 175: 26 Computational Geometry

Now

ThenWhen we reach the highest vertex, pk (Breaking ties by choosing thefarthest such vertex), we have constructed the right chain of CH(Q).

To construct the left chainWe start at pk , then we choose pk+1 as the point with the smallest polarangle with respect to pk negative , but from the negative x-axis.

NextThe next point is selected in the same manner until we have reached p0.

88 / 90

Page 176: 26 Computational Geometry

Now

ThenWhen we reach the highest vertex, pk (Breaking ties by choosing thefarthest such vertex), we have constructed the right chain of CH(Q).

To construct the left chainWe start at pk , then we choose pk+1 as the point with the smallest polarangle with respect to pk negative , but from the negative x-axis.

NextThe next point is selected in the same manner until we have reached p0.

88 / 90

Page 177: 26 Computational Geometry

Jarvis’ MarchExample

LEFT CHAIN RIGHT CHAIN

Figure: Wrapping the Gift. Here the Right Chain finishes at p6, thenthe Left Chain is started

89 / 90

Page 178: 26 Computational Geometry

Complexity

Something NotableComplexity O (hn)

h number of points in CH.O (n) for finding the minimum angle and the farthest point byy-axis

90 / 90

Page 179: 26 Computational Geometry

Complexity

Something NotableComplexity O (hn)

h number of points in CH.O (n) for finding the minimum angle and the farthest point byy-axis

90 / 90

Page 180: 26 Computational Geometry

Complexity

Something NotableComplexity O (hn)

h number of points in CH.O (n) for finding the minimum angle and the farthest point byy-axis

90 / 90

Page 181: 26 Computational Geometry

Complexity

Something NotableComplexity O (hn)

h number of points in CH.O (n) for finding the minimum angle and the farthest point byy-axis

90 / 90