76
CS133 Computational Geometry Intersection Problems 4/24/2018 1

CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Embed Size (px)

Citation preview

Page 1: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

CS133Computational Geometry

Intersection Problems

4/24/2018 1

Page 2: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Riddle: Fair Cake-cutting

Using only one cut, how to split the cake into

two equal pieces (by area)?

4/24/2018 2

Cake

Page 3: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Riddle: Fair cake-cutting

Cake with a hole!

Still one cut

4/24/2018 3

Cake

Cake

Page 4: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Line Segment Intersections

Given a set of line segments, each defined by

two end points, find all intersecting line

segments

4/24/2018 4

Page 5: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Line Segment Intersection

4/24/2018 5

Page 6: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Line Segment Intersection

4/24/2018 6

Page 7: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Naïve Algorithm

Enumerate all possible pairs of lines

Test for intersection

Running time O(n2)

What is the lower bound of the running time?

Worst case: O(n2)

Is this optimal?

4/24/2018 7

Page 8: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 8

Page 9: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 9

Page 10: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 10

Page 11: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 11

Page 12: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 12

Page 13: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 13

Page 14: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 14

Page 15: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 15

Page 16: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 16

Page 17: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 17

Page 18: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 18

Page 19: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 19

Page 20: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 20

Page 21: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 21

Page 22: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 22

Page 23: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 23

Page 24: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 24

Page 25: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Algorithm

4/24/2018 25

Page 26: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Plane-sweep Simple Impl.

Input 𝐿 = 𝑙𝑖𝑙𝑖 = 𝑙𝑖 . 𝑝1, 𝑙𝑖 . 𝑝2Prepare a sorted list of all y coordinates

S={}

For each stop with a corresponding line 𝑙𝑖If top point

Compare 𝑙𝑖 to each 𝑠 ∈ 𝑆

Insert 𝑙𝑖 to S

If end point

Remove 𝑙𝑖 from S

4/24/2018 26

Page 27: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Bentley-Ottmann Algorithm

An improved scan-line algorithm

Maintains the state S in a sorted order to

speed up checking a line segment against

segments in S

4/26/2018 27

Page 28: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Example

4/26/2018 28

Sweep line state (in sorted order)

Page 29: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Algorithm Pseudo code

Create a list of event points P

P is always sorted by the y coordinate

Initialize the state S to an empty list

Initialize P with the first point (top point) of

each line segment

While P is not empty

p P.pop

processEvent(p)

4/26/2018 29

Page 30: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Process Event Point (p)

If p is the top point

Add p.l to S at the order p.x (p.l=Si)

checkIntersection(Si-1, Si)

checkIntersection(Si, Si+1)

Add the end point of p.l to P

If p is the bottom point

Remove p.l from S (p.l=Si before removal)

checkIntersection(Si-1,Si)

If p is an interior point

Report p as an intersection

Find p.l in S (p.l=Si)

Swap(Si,Si+1)

checkIntersection(Si-1, Si)

checkIntersection(Si, Si+1)

4/26/2018 30

Page 31: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Check Intersection(l1, l2)

If l1 does not intersect l2 then return

Compute the intersection pi of l1 and l2

If pi.y is above the sweep line then return

If 𝑝𝑖 ∈ 𝑃 then return

Insert pi into P

4/26/2018 31

Page 32: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Analysis

Initial sort of starting points O(n log n)

Number of processed event points (2n+k)

For each event point

Remove from P: O(log |P|)

Insert or remove from S: O(log |S|)

Check intersection with at most two lines: O(1)

Insert a new event points: O(log |P|)

Upper limit of |P| = 2n

Upper limit of |S| = n

Overall running time O((n+k)log n)4/24/2018 32

Page 33: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Corner Case 1: Horizontal Line

4/26/2018 33

l1

l2l3

If two points have the

same y-coordinate, sort

them by the x-coordinate

Starting point

Page 34: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Corner Case 2:

Three Intersecting Lines

4/26/2018 34

l1

l2l3

Allow the event point to store a

list of all intersecting line

segments

When processed, reverse the

order of all the lines

Page 35: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Rectangle Intersection

Given a set of rectangles, find all

intersections

5/3/2018 35

Page 36: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Example

5/3/2018 36

r1

r2

r3

r4

Page 37: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Example

5/3/2018 37

r1

r2

r3

r4

Page 38: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Rectangle Primitives

A rectangle is represented by its two corner

points, lower and upper

Test if two rectangles overlap

Two rectangles overlap if both their x intervals and

y-intervals overlap

Intervals overlap [x1,x2], [x3,x4]: x4>=x1 and

x2>=x3

R1(x1,y1,x2,y2)×R2(x3,y3,x4,y4) ➔

R3(Max(x1,x3), Max(y1,y3), Min(x2,x4),

Min(y2,y4))

5/3/2018 38

Page 39: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Naïve Algorithm

Test all pairs of rectangles and report the

intersections

Running time O(n2)

Is it optimal?

5/3/2018 39

Page 40: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Simple Plane-sweep Algo.

5/3/2018 40

r1

r2

r3

r4

Page 41: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Simple Plane-sweep Algo.

What is the state of the sweep line?

What is an event?

What processing should be done at each

event?

5/3/2018 41

Page 42: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Improved Plane-sweep Algo.

Keep the sweep line state sorted

But how?

Interval tree

A variation of BST

Stores intervals

Supports two operation

Find all intervals that overlap a query point 𝑝

Find all intervals that overlap a query interval 𝑞

5/3/2018 42

Page 43: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Interval Tree Structure

5/3/2018 43

X-center

Page 44: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

A Simpler Interval Tree

Store the intervals in a BST ordered by i.xmin

Augment the BST with the value xmax which

stores the maximum value of all the intervals

in the subtree

5/3/2018 44

Page 45: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Augmented BST

5/3/2018 45Credit: https://en.wikipedia.org/wiki/Interval_tree

Page 46: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Polygon Intersection

Given a set of polygons, find all intersecting

polygons

5/3/2018 46

Page 47: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Polygon Representation

A polygon is represented as a sequence of

points that form its boundary

A general polygon

might also contain

holes, e.g., a grass area

with a lake inside

For simplicity, we will only

deal with solid polygons

with no holes

5/3/2018 47

p1

p2

p3

p4p5

p6

p7

p8

Corners

Edge or

Segment

Page 48: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Filter-and-refine Approach

Convert all polygons to rectangles

For each polygon, compute its minimum bounding

rectangle (MBR)

Filter: Find all overlapping rectangles

Refine: Test the polygons that have

overlapping MBBs

5/3/2018 48

Page 49: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Filter-and-refine Approach

Filter step: Already studied

Refine: How to find the intersection of two

polygons?

For any two polygons, there are three cases:

1. Polygon boundaries intersect

2. Polygons are disjoint

3. One polygon is contained in the other polygon

5/3/2018 49

Page 50: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Test Case 1: Intersecting

If the boundaries of the two polygons overlap,

then there is at least two polygon edges that

overlap

Naïve solution: Compute all intersections

between every pair of edges 𝑂 𝑚 ⋅ 𝑛

Where 𝑚 and 𝑛 are the sizes of the two polygons

We can also use the line-segment sweep-line

algorithm

Run in 𝑂 𝑘 + 𝐼 log 𝑘 where 𝑘 = 𝑚 + 𝑛 and 𝐼 is the

number of intersections

If we only need to test, we can stop at the first

intersection5/3/2018 50

Page 51: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 51

P

Q

Page 52: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 52

P

Q

Page 53: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 53

P

Q

Page 54: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 54

P

Q

Page 55: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 55

P

Q

Page 56: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 56

P

Q

Page 57: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 57

P

Q

Page 58: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 58

P

Q

Page 59: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 59

P

Q

Page 60: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 60

P

Q

Page 61: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 61

P

Q

Page 62: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 62

P

Q

Page 63: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 63

P

Q

Page 64: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Computing the Intersection

5/3/2018 64

P

Q

Page 65: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Case 2: Contained

5/3/2018 65

P

Q

No intersection points

If 𝑃 ⊂ 𝑄, then any corner

of P is ∈ 𝑄

If 𝑄 ⊂ 𝑃, then any corner

of Q is ∈ 𝑃

The intersection is the

contained polygon

Page 66: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Case 3: Disjoint

5/3/2018 66

PQ

No intersection points

Neither 𝑃 ⊂ 𝑄 nor 𝑄 ⊂ 𝑃

The intersection is empty

Page 67: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Special Case: Convex Polygons

5/3/2018 67

P Q

Page 68: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Convex Polygon Overlaps

5/3/2018 68

Right-left Right-right

left-left

Left-right

Page 69: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Left-right Split

5/3/2018 69

Page 70: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Left-right Overlap Test

5/3/2018 70

Compare the end point of

one line segment to the

other line segment using

Half space

R L

If the end point of R is to

the right of the segment

in L ➔ Skip R.

Why?

Page 71: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Left-right Overlap Test

5/3/2018 71

Compare the end point of

one line segment to the

other line segment using

Half space

R L

Is the end point of R to the

right of the segment in L?

No! Do the line segments

intersect? No!

Switch to L

Why?

Page 72: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Left-right Overlap Test

5/3/2018 72

Compare the end point of

one line segment to the

other line segment using

Half space

R L

If the end point of L is to

the left of the segment in

R ➔ Skip L

Page 73: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Left-right Overlap Test

5/3/2018 73

Compare the end point of

one line segment to the

other line segment using

Half space

R L

Is the end point of L to the

left of R? No!

Do the line segments

intersect? No!

Switch to R

Page 74: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Left-right Overlap Test

5/3/2018 74

Compare the end point of

one line segment to the

other line segment using

Half space

R L

Is the end point of R to

the right of L? Yes!

Skip R

Page 75: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Left-right Overlap Test

5/3/2018 75

Compare the end point of

one line segment to the

other line segment using

Half space

R L

Is the end point of R to

the right of L? No!

Do the line segments

intersect? Yes!

Page 76: CS014 Introduction to Data Structures and Algorithmseldawy/18SCS133/slides/CS133-05-Intersection.pdf · Add p.l to S at the order p.x (p.l=S i) checkIntersection(S i-1, S i) checkIntersection(S

Convex Polygon Intersection

If only testing is required, the algorithm can

terminate as soon as the first intersection is

found

If the polygon intersection is needed, the

algorithm reports all intersections

The algorithm terminates when all segments

are inspected

Running time: 𝑂 𝑚 + 𝑛 , each iteration skips

one segment

5/3/2018 76