lecture 1: introduction and line segment intersectionsitharam/courses/cg/kreveldintroline...slabs,...

77
Course Organization Introduction Line segment intersection Plane sweep Geometric Algorithms Lecture 1: Course Organization Introduction Line segment intersection for map overlay Geometric Algorithms Lecture 1: Introduction and line segment intersection

Upload: hoangdan

Post on 27-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric Algorithms

Lecture 1:

Course Organization

Introduction

Line segment intersection for map overlay

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

This course

instructors:Kevin Buchin and Herman Haverkort

course website:http://www.win.tue.nl/~hermanh/teaching/2IL55/

content: algorithmic aspects of spatial data

How to store, analyze, create, and manipulate spatialdata

applications in robotics, computer graphics, virtualreality, geographic information systems ...

register on StudyWeb!

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

This course

learning objectives:At the end of this course you should be able to . . .

decide which algorithm or data structure to use in orderto solve a given basic geometric problem,

analyze new problems and come up with your ownefficient solutions using concepts and techniques fromthe course.

grading:

do 3 homework assignments: 20% each

write research report (in pairs): 40%

to pass: you need 50% of points in the homework ANDa 5 for the research report

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

This course

before the course you should . . .

have complete Advanced Algorithms (2IL45)

have experience with the following topics:

basic algorithm design techniques: divide-and-conquer,greedy algorithms, linear programming . . .basic analysis techniques: proofs with induction andinvariants, O-notation, solving recurrences andsummations, basic probability theory . . .basic data structures: binary search trees, heaps . . .

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

This course

book (compulsory):[BCKO] M. de Berg, O. Cheong, M. van Kreveld, M.Overmars, Computational Geometry: Algorithms andApplications (3rd edition). Springer-Verlag, Heidelberg, 2008.

acknowledgement: slides will be based on slides by M. vanKreveld

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Introduction

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Geometry: points, lines, ...

Plane (two-dimensional), R2

Space (three-dimensional), R3

Space (higher-dimensional), Rd

A point in the plane, 3-dimensional space, higher-dimensionalspace.p = (px,py), p = (px,py,pz), p = (p1,p2, . . . ,pd)

A line in the plane: y = m · x+ c; representation by m and c

A half-plane in the plane: y≤ m · x+ c or y≥ m · x+ c

Represent vertical lines? Not by m and c . . .

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Geometry: line segments

A line segment pq is defined by itstwo endpoints p and q:(λ ·px +(1−λ ) ·qx, λ ·py +(1−λ ) ·qy)where 0≤ λ ≤ 1

Line segments are assumed to beclosed = with endpoints, not open

Two line segments intersect if theyhave some point in common. It is aproper intersection if it is exactly oneinterior point of each line segment

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Polygons: simple or not

A polygon is a connected region ofthe plane bounded by a sequence ofline segments

simple polygon

polygon with holes

convex polygon

non-simple polygon

The line segments of a polygon arecalled its edges, the endpoints ofthose edges are the vertices

Some abuse: polygon is onlyboundary

interior

exterior

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Other shapes: rectangles, circles, disks

A circle is only the boundary, a diskis the boundary plus the interior

Rectangles, squares, quadrants,slabs, half-lines, wedges, . . .

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Convexity

A shape or set is convex if for anytwo points that are part of theshape, the whole connecting linesegment is also part of the shape

Question: Which of the followingshapes are convex? Point, linesegment, line, circle, disk, quadrant?

For any subset of the plane (set ofpoints, rectangle, simple polygon),its convex hull is the smallest convexset that contains that subset

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Relations: distance, intersection, angle

The distance between two points isgenerally the Euclidean distance:√

(px−qx)2 +(py−qy)2

Another option: the Manhattandistance:

|px−qx|+ |py−qy|

Question: What is the set of pointsat equal Manhattan distance to somepoint?

√(px − qx)2 + (py − qy)2)

|px − qx|

|py − qy|

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Relations: distance, intersection, angle

The distance between two geometric objects other than pointsusually refers to the minimum distance between two points thatare part of these objects

Question: How can the distance between two line segments berealized?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Relations: distance, intersection, angle

The intersection of two geometricobjects is the set of points (part ofthe plane, space) they have incommon

Question 1: How many intersectionpoints can a line and a circle have?

Question 2: What are the possibleoutcomes of the intersection of arectangle and a quadrant?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Relations: distance, intersection, angle

Question 3: What is the maximumnumber of intersection points of aline and a simple polygon with 10vertices (trick question)?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Relations: distance, intersection, angle

Question 4: What is the maximumnumber of intersection points of aline and a simple polygon boundarywith 10 vertices (still a trickquestion)?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Description size

A point in the plane can berepresented using two reals

A line in the plane can berepresented using two reals and aBoolean (for example)

A line segment can be represented bytwo points, so four reals

A circle (or disk) requires three realsto store it (center, radius)

A rectangle requires four reals tostore it

false, m, c

true, .., c

y = m · x + c

x = c

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Description size

A simple polygon in the plane can be represented using 2n reals ifit has n vertices (and necessarily, n edges)

A set of n points requires 2n reals

A set of n line segments requires 4n reals

A point, line, circle, . . . requires O(1), or constant, storage.A simple polygon with n vertices requires O(n), or linear, storage

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Computation time

Any computation (distance, intersection) on two objects of O(1)description size takes O(1) time!

Question: Suppose that a simple polygon with n vertices is given;the vertices are given in counterclockwise order along theboundary. Give an efficient algorithm to determine all edges thatare intersected by a given line.

How efficient is your algorithm? Why is your algorithm efficient?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Convex hull problem (Advanced Algorithms)

Give an algorithm that computes theconvex hull of any given set of n points inthe plane efficiently

Question 1: What is the input size?

Question 2: Why can’t we expect to doany better than O(n) time?

Question 3: Is there any hope of findingan O(n) time algorithm?

Question 4: What’s the optimal runningtime?

Read Chapter 1 of [BCKO] today!

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Computational geometry scope

In computational geometry, problems on input with more thanconstant description size are the ones of interest

Computational geometry (theory): Study of geometric problems ongeometric data, and how efficient geometric algorithms that solvethem can be

Computational geometry (practice): Study of geometric problemsthat arise in various applications and how geometric algorithms canhelp to solve well-defined versions of such problems

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Computational geometry theory

Computational geometry (theory):Classify abstract geometric problemsinto classes depending on howefficiently they can be solved

smallest enclosing circle

closest pair

any intersection?

find all intersectionsGeometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Computational geometry practice

Application areas that require geometric algorithms are computergraphics, motion planning and robotics, geographic informationsystems, CAD/CAM, statistics, physics simulations, databases,games, multimedia retrieval, . . .

Computing shadows from virtual light sources

Spatial interpolation from groundwater pollutionmeasurements

Computing a collision-free path between obstacles

Computing similarity of two shapes for shape databaseretrieval

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Computational geometry history

Early 70s: First attention for geometric problems from algorithmsresearchers

1976: First PhD thesis in computational geometry (MichaelShamos)

1985: First Annual ACM Symposium on Computational Geometry.Also: first textbook

1996: CGAL: first serious implementation effort for robustgeometric algorithms

1997: First handbook on computational geometry (second one in2000)

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Geometric objectsGeometric relationsCombinatorial complexityComputational geometry

Line segment intersection

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

Map layers

In a geographic information system(GIS) data is stored in separatelayers

A layer stores the geometricinformation about some theme, likeland cover, road network,municipality boundaries, red foxhabitat, . . .

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

Map overlay

Map overlay is the combination oftwo (or more) map layers

It is needed to answer questions like:

What is the total length ofroads through forests?

What is the total area of cornfields within 1km from a river?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

Map overlay

To solve map overlay questions, weneed (at the least) intersectionpoints from two sets of line segments(possibly, boundaries of regions)

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

The (easy) problem

Let’s first look at the easiest versionof the problem:

Given a set of of n line segments inthe plane, find all intersection pointsefficiently

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

An easy, optimal algorithm?

Algorithm FindIntersections(S)Input. A set S of line segments in the plane.Output. The set of intersection points among the segments in S.1. for each pair of line segments ei,ej ∈ S2. do if ei and ej intersect3. then report their intersection point

Question: Why can we say that this algorithm is optimal?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

Output-sensitive algorithm

The asymptotic running time of analgorithm is always input-sensitive(depends on n)

We may also want the running timeto be output-sensitive: if the outputis large, it is fine to spend a lot oftime, but if the output is small, wewant a fast algorithm

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

Intersection points in practice

Question: How many intersectionpoints do we typically expect in ourapplication?

If this number is k, and if k = O(n),it would be nice if the algorithm runsin O(n logn) time

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

First attempt

Observation: Two line segmentscan only intersect if their y-spanshave an overlap

So, how about only testing pairs ofline segments that intersect in they-projection?

1-D problem: Given a set of intervalson the real line, find all partlyoverlapping pairs

x

y

s1 s2s3 s4 s5 s6

(s1, s2), (s4, s6), (s5, s6)

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

Motivation: Map overlayProblemOutput-sensitive algorithmsSome attempts

Second attempt

Refined observation: Two linesegments can only intersect if theiry-spans have an overlap, and theyare adjacent in the x-order at thaty-coordinate (they are horizontalneighbors)

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Plane sweep

The plane sweep technique: Imagine a horizontal linepassing over the plane from top to bottom, solving theproblem as it moves

The sweep line stops and the algorithm computes atcertain positions ⇒ events

The algorithm stores the relevant situation at the currentposition of the sweep line ⇒ status

The algorithm knows everything it needs to know abovethe sweep line, and found all intersection points

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Sweep

computed

unexploredGeometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Sweep and status

computed

unexplored

status

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Status and events

The status of this particular plane sweep algorithm, at thecurrent position of the sweep line, is the set of line segmentsintersecting the sweep line, ordered from left to right

The events occur when the status changes, and when outputis generated

event ≈ interesting y-coordinate

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

add s1

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

add s2 after s1

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

add s3 between s1and s2

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

add s4 before s1

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

report intersection(s1,s2); swap s1and s3

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

remove s2

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

remove s1

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

add s5

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

s1

s2s3

s4

s6

s5

s7

s8

report intersection(s3,s4); swap s3and s4

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

. . . and so on . . .

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

The events

When do the events happen? When the sweep line is

at an upper endpoint of a line segment

at a lower endpoint of a line segment

at an intersection point of a line segment

At each type, the status changes; at the third type output isfound too

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Assume no degenerate cases

We will at first exclude degenerate cases:

No two endpoints have the samey-coordinate

No more than two line segmentsintersect in a point

. . .

Question: Are there more degeneratecases?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event list and status structure

The event list is an abstract data structure that stores allevents in the order in which they occur

The status structure is an abstract data structure thatmaintains the current status

Here: The status is the subset of currently intersected linesegments in the order of intersection by the sweep line

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Status structure

We use a balanced binary search tree with the line segmentsin the leaves as the status structure

s1s2

s3 s4s5

s6

s7

s8

s1

s2

s3

s4

s5

s6

s7

s1 s2 s3 s4 s5 s6 s7 s8

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Status structure

s1s2

s3 s4s5

s6

s7

s8

s1

s2

s3

s4

s5

s6

s7

s1 s2 s3 s4 s5 s6 s7 s8

s9

Upper endpoint: search, and insertGeometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Status structure

s1s2

s3 s4s5

s6

s7

s8

s1

s2

s3

s4

s5

s6

s7

s1 s2 s3 s4 s5 s6 s7 s8

s9

Upper endpoint: search, and insertGeometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Status structure

s1s2

s3 s4s5

s6

s7

s8

s1

s2

s3

s4

s5

s6

s7

s1 s2

s3

s4 s5 s6 s7 s8

s9

s9

s9

Upper endpoint: search, and insertGeometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Status structure

Sweep line reaches lower endpoint of a line segment: deletefrom the status structure

Sweep line reaches intersection point: swap two leaves in thestatus structure (and update information on the search paths)

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Finding events

Before the sweep algorithm starts, we know all upperendpoint events and all lower endpoint events

But: How do we know intersection point events???(those we were trying to find . . .)

Recall: Two line segments can only intersect if they arehorizontal neighbors

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Finding events

Lemma: Two line segments si and sj canonly intersect after (= below) they havebecome horizontal neighbors

Proof: Just imagine that the sweep line isever so slightly above the intersection pointof si and sj, but below any other event �

Also: some earlier (= higher) event madesi and sj horizontally adjacent!!!

si

sj

si

sj

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event list

The event list must be a balanced binary search tree, becauseduring the sweep, we discover new events that will happenlater

We know upper endpoint events and lower endpoint eventsbeforehand; we find intersection point events when theinvolved line segments become horizontal neighbors

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Structure of sweep algorithm

Algorithm FindIntersections(S)Input. A set S of line segments in the plane.Output. The intersection points of the segments in S, with for

each intersection point the segments that contain it.1. Initialize an empty event queue Q. Next, insert the

segment endpoints into Q; when an upper endpoint isinserted, the corresponding segment should be storedwith it

2. Initialize an empty status structure T3. while Q is not empty4. do Determine next event point p in Q and delete it5. HandleEventPoint(p)

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event handling

If the event is an upper endpointevent, and s is the line segment thatstarts at p:

1 Search with p in T, and insert s2 If s intersects its left neighbor in

T, then determine theintersection point and insert it Q

3 If s intersects its right neighborin T, then determine theintersection point and insert it Q

p

s

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event handling

If the event is a lower endpointevent, and s is the line segment thatends at p:

1 Search with p in T, and delete s2 Let sl and sr be the left and

right neighbors of s in T (beforedeletion). If they intersectbelow the sweep line, then inserttheir intersection point as anevent in Q

p

s

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event handling

If the event is an intersection pointevent where s and s′ intersect at p:

1 . . .

2 . . .

3 . . .

4 . . .

p

s s′

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event handling

If the event is an intersection pointevent where s and s′ intersect at p:

1 Exchange s and s′ in T2 . . .

3 . . .

4 . . .

p

s s′

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event handling

If the event is an intersection pointevent where s and s′ intersect at p:

1 Exchange s and s′ in T2 If s′ and its new left neighbor in

T intersect below the sweepline, then insert this intersectionpoint in Q

3 . . .

4 . . .

p

s s′

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event handling

If the event is an intersection pointevent where s and s′ intersect at p:

1 Exchange s and s′ in T2 If s′ and its new left neighbor in

T intersect below the sweepline, then insert this intersectionpoint in Q

3 If s and its new right neighborin T intersect below the sweepline, then insert this intersectionpoint in Q

4 . . .

p

s s′

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event handling

If the event is an intersection pointevent where s and s′ intersect at p:

1 Exchange s and s′ in T2 If s′ and its new left neighbor in

T intersect below the sweepline, then insert this intersectionpoint in Q

3 If s and its new right neighborin T intersect below the sweepline, then insert this intersectionpoint in Q

4 Report the intersection point

p

s s′

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Event handling

p

s

s′

Can it be that newhorizontal neighbors alreadyintersected above the sweepline?

Can it be that we insert anewly detected intersectionpoint event, but it alreadyoccurs in Q?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Efficiency

How much time to handle an event?

At most one search in T and/or one insertion, deletion, orswap

At most twice finding a neighbor in T

At most one deletion from and two insertions in Q

Since T and Q are balanced binary search trees, handling anevent takes only O(logn) time

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Efficiency

How many events?

2n for the upper and lower endpoints

k for the intersection points, if there are k of them

In total: O(n+ k) events

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Efficiency

Initialization takes O(n logn) time (to put all upper and lowerendpoint events in Q)

Each of the O(n+ k) events takes O(logn) time

The algorithm takes O(n logn+ k logn) time

If k = O(n), then this is O(n logn)

Note that if k is really large, the brute force O(n2) timealgorithm is more efficient

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Efficiency

Question: How much storage does the algorithm take?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Efficiency

Question: Given that the event list is a binary tree that maystore O(k) = O(n2) events, is the efficiency in jeopardy?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Degenerate cases

How do we deal with degenerate cases?

For two different events with the same y-coordinate, we treatthem from left to right ⇒ the “upper” endpoint of ahorizontal line segment is its left endpoint

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Degenerate cases

How about multiply coinciding event points?

p

Let U(p) and L(p) be the line segments that have p as upperand lower endpoint, and C(p) the ones that contain p

Question: How do we handle this multi-event?

Geometric Algorithms Lecture 1: Introduction and line segment intersection

Course OrganizationIntroduction

Line segment intersectionPlane sweep

IntroductionEvents, status, structuresEvent handlingEfficiency

Conclusion

For every sweep algorithm:

Define the status

Choose the status structure and the event list

Figure out how events must be handled (with sketches!)

To analyze, determine the number of events and howmuch time they take

Then deal with degeneracies

Geometric Algorithms Lecture 1: Introduction and line segment intersection