5
torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 1 SMD156 Lecture 2 Line segment intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 2 Determining whether at least one pair of line segments intersect s 3 s 7 s 1 s 5 s 6 s 2 s 4 torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 3 Line segment intersection Compute and report all intersections among a set of n line segments. A trivial solution based on brute-force: ! For each pair of line segments: Compute the point of intersection between the lines that contain the line segments; if this point lie on both line segments, we have found an intersection. ! There are O"n 2 # pairs so this takes O"n 2 # time. Natural question: Do we really need to spend O"n 2 # time? ! Note that the number of intersections could be much less than O"n 2 #Interesting question: Is there a way to relate the running time to the number of intersections that need to be reported? torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 4

Line segment intersection Determining whether at least one

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Line segment intersection Determining whether at least one

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 1

SMD156Lecture 2

Line segment intersection

Booleanoperations

on polygons

Subdivisions

Map overlay

Doubly-Connected Edge Lists

Plane sweep

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 2

Determining whether at least one pair

of line segments intersect

s3

s7

s1

s5s6

s2

s4

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 3

Line segment intersection

• Compute and report all intersections among a set of nline segments.

• A trivial solution based on brute-force:! For each pair of line segments: Compute the point of

intersection between the lines that contain the line segments;if this point lie on both line segments, we have found anintersection.

! There are O"n2# pairs so this takes O"n2# time.• Natural question: Do we really need to spend O"n2# time?

! Note that the number of intersections could be much lessthan O"n2#…

• Interesting question: Is there a way to relate the runningtime to the number of intersections that need to bereported?

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 4

Page 2: Line segment intersection Determining whether at least one

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 5

Output-sensitive algorithms

• An algorithm for which the time "or space#complexity depends not only on the size of the input"n in our case# but also the size of the output.

• Let k be the number of intersections found.! $Number of pairs of intersecting line segments.$

• We will now learn about an output-sensivitealgorithm that computes all intersections in

O!n log n + k log n" time

• "There exists O!n log n + k" time algorithms.#

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 6

Observation 1

• Only line segments that overlap if projectedonto the y-axis might intersect.

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 7

Algorithmic technique: Plane sweep

• Imagine a horizontal line sweeping downwards overthe plane, starting above all line segments.

• As the sweep line is moved, we keep track of the set ofline segments that it intersects - the status.! The status contains the intersected line segments ordered by

where they intersect the sweep line.

• In general, the status is invariant. It only changeswhen the sweep line reaches an event point.

• An event point is either! an end point of a line segment, or

! a point of intersection between two line segments.

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 8

Page 3: Line segment intersection Determining whether at least one

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 9

Observation 2

• If two line segments intersect, they must atsometime lie next to each other in the status!

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 10

Algorithm

• Use an event queue to store event pointsordered by their y-coordinate.! Insert all 2n end points of line segments in the

beginning.

• Start with an empty status, sweep from oneevent point to the next keeping the invariantthat:

$Above the sweep line all points of inter- section have been computed.$

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 11

Algorithm (cont.)

• This happens at an event point when…! An upper end point is reached: Insert the new

line segment "into the status# and test it forintersections with its neighbors below thesweep line; insert the intersection points asevent points, if any.

! An point of intersection is reached: Swap theline segments that intersect and test them forintersections below the sweep line with theirnew neighbors. If they intersect, insert thepoints as new event points.

! A lower end point is reached: Delete the linesegment and test whether the two newneighbors intersect. If they intersect, insert thepoints as new event points.

Report an

intersection!

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 12

Data structures used

• Event queue! A balanced binary search tree

• AVL-trees, red-black trees, etc thatsupport insertion, deletion, neighbor"successor and predecessor#, andlookup in O!log m" time when the treecontains m items.

! Define an order between the event point"highest first, leftmost to break ties#

• Status! A balanced binary search tree

• "One can store anything - not justnumbers - in such a tree as long as theitems are ordered.#

Page 4: Line segment intersection Determining whether at least one

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 13

A degenerate case

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 14

Analysis

• We inserted 2n endpoints into the eventqueue: O!n log n" time.

• Each intersection was inserted as an eventpoint: O!k log n" time.

• Total: O!n log n + k log n" = O!!n+k" log n" time.! Better analysis possible - see Lemma 2.3!

• This is worse than the brute-force solution"O!n2"# if k is more than O!n2/log n" but betterwhen k is less.! It is, however, output-sensitive.

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 15

Some basic definitions (Cont.)

• A polygonal chain is a finite concatenation ofline segments where consecutive segmentsshare one end point "$One ends where thenext starts$#.

• A simple polygon is a $closed polygonal chainthat doesn%t self-intersect$.! Simple polygons have vertices and edges that forms

their boundaries.

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 16

Doubly-Connected Edge Lists

• Suitable to keep track of subdivisions of theplane into different kinds of regions.

Page 5: Line segment intersection Determining whether at least one

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 17

DCEL:s

• Geometrical data: Coordinates.

• Topological data: Pointers to neighboring items.

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 18

The overlay of two subdivisions

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 19

Applying plane sweep

• Maintain a doubly-connected edge list duringthe sweep over the subdivisions.

torsdag 25 mars 2004 SMD156 Computational Geometry - Håkan Jonsson 20

An application: Boolean operations