Line Segment Intersection Computational Geometry, WS 2007/08 Lecture 3 – Supplementary Prof. Dr....

37
Line Segment Intersection Computational Geometry, WS 2007/08 Lecture 3 – Supplementary Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät für Angewandte Wissenschaften Albert-Ludwigs-Universität Freiburg

Transcript of Line Segment Intersection Computational Geometry, WS 2007/08 Lecture 3 – Supplementary Prof. Dr....

Line Segment Intersection

Computational Geometry, WS 2007/08Lecture 3 – Supplementary

Prof. Dr. Thomas Ottmann

Algorithmen & Datenstrukturen, Institut für InformatikFakultät für Angewandte WissenschaftenAlbert-Ludwigs-Universität Freiburg

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 2

Line Segment Intersection: Additions

• Implementation of the event queue by a heap:

The complexity of heap construction

• Iso-oriented line segments• Intersection counting• Line segment intersection by Divide & Conquer

• Polygon intersection

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 3

Heaps

Definition. A sequence F = (k1, k2, . . . , kn) of keys is a (Max-) Heap,

iff for all i {1, 2, …, n/2} : ki ≥ k2i und ki ≥ k2i+1.

This requirement for an array can be interpreted as a condition on a

complete binary tree:

• Level i has 2i nodes (except, eventually, the last level)• Node i has node 2i as ist left and node 2i + 1 as its right successor

andnode i/2 as ist predecessor (if they exist).

1 2 3 4 5 6 7

7 6 5 2 3 4 1

7

6 5

2 3 4 1

1

2 3

54 6 7

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 4

Heap Construction

Level, #nodes, #comp./exchanges

k levels, total # of nodes = 2k+1 -1

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 5

Heap Construction

Heap construction by iterative trickling down:0 20

1 21

2 2² k = 3 2k

nodes

Total number of nodes: n = 2k+1 - 1

Total number of key comparisons and exchanges:

.

)()1(*2

)22(*2

2*2)(2*2

1

1

0 1

nkn

k

jik

k

k

i

k

j

jki

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 6

Complexity of Heap Construction

Total complexity = 20 2k + 21 2(k-1) + 22 2(k-2) + … + 2(k-1) 2

= 2 S(k),

where S(k) = 20 k + 21(k-1) + 22(k-2) + … + 2(k-1)1

= 2 (n – k – 1) = O(n)

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 7

Intersections of iso-oriented Line Segments

• Report all pairs of intersecting segments• Count the number of intersecting segments

...........

......

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 8

Iso-oriented Line Segments

Report all pairs of intersecting segments:

A sweep-line algorithm can solve this problem in optimal time

O(n log n + k) and space O(n), k = # of intersections!

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 9

Iso-oriented Line Segments

Count all pairs of intersecting segments:

A sweep-line algorithm can solve this problem in optimal time

O(n log n) and space O(n)!

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 10

A Divide and Conquer Algorithm

Report all pairs of intersecting segments

...........

......

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 11

How to divide the input set

A separate representation of segments allows to divide theinput set.

A

BC

D

E

A.

B.

C.

D.

E.

.A.D

.B.C

.E

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 12

ReportCuts(S)

Input: Set S of vertical segments and endpoints of horizontal segments.

Output: All pairs of intersecting vertical segments and those horizontal segments which are represented by at least one endpoint in S.

1. Divide

if |S| > 1

then divide S by a vertical line G into two sets S1 (to the left of G) and S2 (to the right of G) of equal size

else S contains no intersections

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 13

ReportCuts

A

B

C

D

E

AD

BC

ES

S1 S2

1. Divide

2. Conquer

ReportCuts(S1); ReportCuts(S2)

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 14

ReportCuts

3. Merge: ???

Different possibilities for intersections of a horizontal segment in S1

Case 1: both endpoints in S1

h

S1 S2

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 15

ReportCuts

Case 2: only one endpoint of h in S1

2 a) right endpoint in S1

h

S1 S2

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 16

ReportCuts

2 b) left endpoint of h in S1

h

S1

right endpointin S2

h

S1

right endpoint not in S2

S2

S2

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 17

ReportCuts(S)

3. Merge:

Report intersections between vertical segments in S2 and those horizontal segments in S1, for which the

left endpoint is in S1 and the right endpoint is neither in S1 nor in S2

Analogously for S1

S1 S2

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 18

Implemention

Set S

L(S): y-coordinats of all left endpoints in S which have no right partner in S

R(S): y-coordinats of all right endpoints in S which have no left

partner in S

V(S): y-intervals of vertical segments in S

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 19

Base Cases

S contains only one element s

Case 1: s = (x,y) is left endpoint

L(S) = {y} R(S) = V(S) =

Case 2: s = (x,y) is right endpoint

L (S)= R(S) = {y} V(S) =

Case 3: s = (x,y1,y2) is vertical segment

L(S) = R(S) = V(S) = {[y1,y2]}

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 20

Merge Step

Assume L(Si), R(Si), V(Si) i=1,2 are known and S = S1 S2

L(S) =

R(S) =

V(S) =

L,R: sorted according to increasing y-coordinatslinked lists

V: sorted according to increasing lower endpoints linked list

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 21

Reporting of Intersections

V(S2)h3

h2

h1

L(S1)

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 22

Runtime

Input (vertical segments, left/right endpoints of horizontal segments)

has to be sorted initially and is stored in an array.

Divide-and-Conquer:

T(n) = 2T(n/2) + an + output-size

T(1) = O(1)

O(n log n + k) k = #intersections

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 23

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 24

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

Sweep direction

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 25

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

Status-structure T:

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 26

•G•EAD

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

Q1

ReportedIntersections1. Q1 (E--A)

Status-structure T:

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 27

GAED

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

Q2

Q3

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)

Q3

Status-structure T:

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 28

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)

Q3

GAED

Status-structure T:

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 29

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)

Q3

GAED (E• = •F)

Status-structure T:

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 30

GA•B•CDF

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)

Q3

GAED

Status-structure T:

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 31

GA•B•CDF

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)4. Q4 (G--B)

Q3

GAED

Status-structure T: AGBCDF

Q4

Q4

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 32

ABGCDF

AGBCDF

GA•B•CDF

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

Sweep direction

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)4. Q4 (G--B)5. Q5 (G--C)

Q3

GAED

Status-structure T:

Q5

Q4Q5

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 33

ABGCDF

AGBCDF

GA•B•CDF

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)4. Q4 (G--B)5. Q5 (G--C)

Q3

GAED

Status-structure T:

Q4Q5

A•B•GCDF

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 34

ABGCDF

AGBCDF

GA•B•CDF

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)4. Q4 (G--B)5. Q5 (G--C)6. Q6 (G--D)

Q3

GAED

Status-structure T:

Q4Q5

A•B•GCDF

CGDF

Q6

Q6

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 35

CDGF

CGDF

ABGCDF

AGBCDF

GA•B•CDF

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)4. Q4 (G--B)5. Q5 (G--C)6. Q6 (G--D)

Q3

GAED

Status-structure T:

Q4Q5

A•B•GCDF

Q6

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 36

CDGF

CGDF

ABGCDF

AGBCDF

GA•B•CDF

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)4. Q4 (G--B)5. Q5 (G--C)6. Q6 (G--D)

Q3

GAED

Status-structure T:

Q4Q5

A•B•GCDF

Q6

CDG•F•

Computational Geometry, WS 2007/08Prof. Dr. Thomas Ottmann 37

CDGF

CGDF

ABGCDF

AGBCDF

GA•B•CDF

GAD•F

GADE

Q2

Q1

Polygon Intersection by Line Sweep

A

B

CD

E

F

G

Event-queue Q

•A•D

•G•EAD

ReportedIntersections1. Q1 (E--A)2. Q2 (G--A)3. Q3 (E--D)4. Q4 (G--B)5. Q5 (G--C)6. Q6 (G--D)

Q3

GAED

Status-structure T:

Q4Q5

A•B•GCDF

Q6

CDG•F•

C•D•