1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations...

12
D7013E Computational Geometry - Håkan Jonsson 1 D7013E Lecture 2 Line segment intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest pairs D7013E Computational Geometry - Håkan Jonsson 2 1. Computing intersections among line segments 6 line segments, 15 intersections 7 line segments, 3 intersections Line segment intersection Compute and report all intersections among a set of n line segments Intersection means ”have at least one point in common” 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 • So, do we really need to spend O(n 2 ) time? – Yes, there are Ω(n 2 ) intersections in the worst case But there could also be just a few… • Interesting question: Is there a way to relate the running time to the number of intersections actually reported? D7013E Computational Geometry - Håkan Jonsson 3 D7013E Computational Geometry - Håkan Jonsson 4 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 As with Jarvis march for computing convex hulls Let k be the number of intersections found ”Number of pairs of intersecting line segments.” We will now learn about an output-sensitive algorithm that computes all intersections in O(n log n + k log n) time (There exists faster algorithms that run in O(n log n + k) time. These are optimal, but more complicated, and will not be covered.)

Transcript of 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations...

Page 1: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 1

D7013ELecture2Linesegment

intersection

Booleanoperationsonpolygons

Subdivisions

Mapoverlay

Doubly-ConnectedEdgeLists

Planesweep

Closestpairs

D7013E Computational Geometry - Håkan Jonsson 2

1. Computing intersections among line segments

6 line segments, 15 intersections 7 line segments, 3 intersections

Line segment intersection •  Computeandreportallintersectionsamongasetofnline

segments–  Intersectionmeans”haveatleastonepointincommon”

•  Atrivialsolutionbasedonbrute-force:–  Foreachpairoflinesegments:Computethepointofintersection

betweenthelinesthatcontainthelinesegments;ifthispointlieonbothlinesegments,wehavefoundanintersection

–  ThereareO(n2)pairssothistakesO(n2)time

•  So,dowereallyneedtospendO(n2)time?–  Yes,thereareΩ(n2)intersectionsintheworstcase–  Buttherecouldalsobejustafew…

•  Interestingquestion:Isthereawaytorelatetherunningtimetothenumberofintersectionsactuallyreported?

D7013E Computational Geometry - Håkan Jonsson 3 D7013E Computational Geometry - Håkan Jonsson 4

Output-sensitive algorithms •  Analgorithmforwhichthetime(orspace)complexity

dependsnotonlyonthesizeoftheinput(ninourcase)butalsothesizeoftheoutput–  AswithJarvismarchforcomputingconvexhulls

•  Letkbethenumberofintersectionsfound–  ”Numberofpairsofintersectinglinesegments.”

•  Wewillnowlearnaboutanoutput-sensitivealgorithmthatcomputesallintersectionsin

O(nlogn+klogn)time

•  (ThereexistsfasteralgorithmsthatruninO(nlogn+k)time.Theseareoptimal,butmorecomplicated,andwillnotbecovered.)

Page 2: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

Lower bound: Ω(nlogn+k) •  Thetermkisobvious;wehavetooutputtheresult•  Thetermnlogncanbeshownbyareductionfrom:•  ELEMENT-UNIQUENESS:

–  Givenasetofnnumbers,{x1,x2,…,xn},aretwoofthemequal?

–  RequiresΩ(nlogn)comparisons[Ben-Or1983]–  (Easytosolvebysorting)

•  Recallthatinourmodelallcommonbinaryoperatorslike<,>,=,AND,OR,NOT,+,-,*,/etccanbeperformedinunit(constant)time–  So,ELEMENT-UNIQUENESSrequiresΩ(nlogn)time

D7013E Computational Geometry - Håkan Jonsson 5

Lower bound reduction •  LetAbeanyalgorithmthatcomputesintersections

amonglinesegments•  WecanthensolveELEMENT-UNIQUENESSby

constructingshortlinesegmentsforthenumbersintheset,runA,andchecktheresult–  Mapxito,forinstance,thelinesegment[(xi,0),(xi,1)]–  Then,Awillreportintersectionsifftherearei≠j,xi=xj

•  SinceeverythingelsethanAtakesO(n)time,itmustbeAthattakesΩ(nlogn)time

D7013E Computational Geometry - Håkan Jonsson 6

{x1,x2,…,xn}=>

x1 x5 x4 x3 x2 xn … …

D7013E Computational Geometry - Håkan Jonsson 7

Observation 1 about our intersections

•  Onlylinesegmentsthatoverlapifprojectedontothey-axismightintersect!

Algorithmic technique: Plane sweep •  Imagineahorizontallinesweepingdownwardsoverthe

plane,startingabovealllinesegments–  Alsocalledlinesweep(orjustsweep)

•  Asthissweeplineismoved,wekeeptrackofthesetoflinesegmentsthatitintersects-thestatus–  Thestatuscontainstheintersectedlinesegmentsorderedin

thex-directionbyhowtheyintersectthesweepline•  Ingeneral,thestatusisinvariant.Itonlychangeswhen

thesweeplineverticallyreachesaneventpoint•  Aneventpointiseither

–  anendpointofalinesegment,or–  apointofintersectionbetweentwolinesegments

D7013E Computational Geometry - Håkan Jonsson 8

Page 3: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 9

Example

s3

s7

s1

s5 s6

s2

s4

Start.

D7013E Computational Geometry - Håkan Jonsson 10

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 11

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 12

Example

s3

s7

s1

s5 s6

s2

s4

Page 4: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 13

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 14

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 15

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 16

Example

s3

s7

s1

s5 s6

s2

s4

Page 5: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 17

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 18

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 19

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 20

Example

s3

s7

s1

s5 s6

s2

s4

Page 6: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 21

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 22

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 23

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 24

Example

s3

s7

s1

s5 s6

s2

s4

Page 7: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 25

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 26

Example

s3

s7

s1

s5 s6

s2

s4

D7013E Computational Geometry - Håkan Jonsson 27

Example

s3

s7

s1

s5 s6

s2

s4

Done.

D7013E Computational Geometry - Håkan Jonsson 28

Observation 2 about our intersections

•  Iftwolinesegmentsintersect,theymustatsometimeduringthesweeplienexttoeachotherinthestatus!

Page 8: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 29

Algorithm •  Useaneventqueue(apriorityqueue)tostore

eventpointsorderedbytheiry-coordinate•  Insertall2nendpointsoflinesegmentsinthe

beginning•  Startwithanemptystatus,andgo(”sweep”)from

oneeventpointtothenextwhilekeepinginvariantthat:

”Abovethesweeplineallpointsofinter-sectionhavebeencomputed.”

•  Important:

–  Thestatusdoesnotcontainthecoordinateswherethesweeplineintersects.Itholdstheorderinwhichlinesegmentsintersectthesweepline

D7013E Computational Geometry - Håkan Jonsson 30

Algorithm (cont.) •  Thishappensataneventpointwhen…

–  Anupperendpointisreached:Insertthenewlinesegment(intothestatus)andtestitforintersectionswithitsneighborsbelowthesweepline;inserttheintersectionpointsaseventpoints,ifany

–  Apointofintersectionisreached:Swapthelinesegmentsthatintersectandtestthemforintersectionsbelowthesweeplinewiththeirnewneighbors.Iftheyintersect,insertthepointsasneweventpoints

–  Alowerendpointisreached:Deletethelinesegmentandtestwhetherthetwonewneighborsintersectbelow.Iftheyintersect,insertthepointasaneweventpoint

Report intersections!

Data structures used •  Eventqueue

–  Abalancedbinarysearchtree•  AVL-trees,red-blacktrees,etcthatsupport

insertion,deletion,neighbor(successorandpredecessor),andlookupinO(logm)timewhenthetreecontainsmitems

–  Defineanorderbetweentheeventpoints(highestfirst,leftmosttobreakties)

•  Status–  Abalancedbinarysearchtree

•  (Onecanstoreanything-notjustnumbers-insuchatreeaslongastheitemsarecomparable)

•  NB!Comparisonsbetweenlinesegmentsisdonebycomputingpointsofintersectionwiththesweeplinewhenneededonly

D7013E Computational Geometry - Håkan Jonsson 31 D7013E Computational Geometry - Håkan Jonsson 32

A degenerate case

Special delicate care must be taken if line segments share end points

Page 9: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 33

Analysis

•  Weinserted2nendpointsintotheeventqueue:–  O(nlogn)time.

•  Eachintersectionwasinsertedasaneventpoint:–  O(klogn)time.

•  Total:–  O(nlogn+klogn)=O((n+k)logn)time.

D7013E Computational Geometry - Håkan Jonsson 34

Reflection •  O((n+k)logn)time…?Betteranalysispossible-seeLemma2.3!•  Thisisworsethanthebrute-forcesolution(O(n2))ifkismorethan

O(n2/logn)butbetterwhenkisless–  Itis,however,output-sensitive

•  ComparewiththeincrementalalgorithmthatcomputesconvexhullsfromLecture1–  Ouralgorithmisalsosort-ofincremental…

•  …makesmonotoneprogressandkeepsaninvariant–  butstilldifferent

•  …considersnotonlyinputpoints(=endpointsoflinesegments)butalsointersectionsthatarefoundduringthecourseofthealgorithm

•  Todetermineifthereareintersections,wejustneedtostopthelinesweepassoonasthefirstintersectionisfound,ifitexists–  So,this(simpler)problemcanbesolvedinO(nlogn)time

D7013E Computational Geometry - Håkan Jonsson 35

Doubly-Connected Edge Lists (DCEL)

•  Adatastructuresuitabletokeeptrackofsubdivisionsoftheplaneintodifferentkindsofregions.

D7013E Computational Geometry - Håkan Jonsson 36

Some basic definitions (Cont.)

•  Apolygonalchainisafiniteconcatenationoflinesegmentswhereconsecutivesegmentsshareoneendpoint(”Oneendswherethenextstarts”)

•  Asimplepolygonisa”closedpolygonalchainthatdoesn’tself-intersect”–  Simplepolygonshaveverticesandedgesthatformstheirboundaries

–  Aconvexhullisasimplepolygonvertex

edge

Page 10: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 37

DCEL:s •  Geometricaldata:

–  Coordinates•  Topologicaldata:

–  Pointerstoneighboringitems

D7013E Computational Geometry - Håkan Jonsson 38

DCEL:s •  Storeddata:

Lists/arrays

D7013E Computational Geometry - Håkan Jonsson 39

The overlay of two subdivisions Euler’sformula

•  Foranembeddingofaplanargraphwithvvertices,eedges,andffaces(regionsboundedbyedges,includingtheouter,infinitely-large,region)withoutcrossingedges,

v−e+f=2

D7013E Computational Geometry - Håkan Jonsson 40

(v=7, e=8, f=3)

Page 11: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 41

Applying plane sweep

•  Maintainadoubly-connectededgelistduringthesweepoverthesubdivisions.

D7013E Computational Geometry - Håkan Jonsson 42

An application: Boolean operations

Application:ComputingClosestPairsusingPlaneSweep•  Problem:

–  Givenasetofpoints,findapairofpointswiththesmallestdistancebetweenthem

•  RequiresΩ(nlogn)time–  Simplereduction

•  SolvedinO(nlogn)timebymanyalgorithms,forinstanceplanesweep

D7013E Computational Geometry - Håkan Jonsson 43

LowerBoundonClosestPairComputations•  ReductiontoElementUniqueness:•  Givenasetofnumbers{xi}(and

facedwithhavingtodecideifallareunique),we–  1)constructpointspi=(xi,0),–  2)runanyClosestPointalgorithm,and–  3)checkifthedistancebetweenthe

closestpointsreturnedis0ornot•  Ifnot0,allareuniqueandotherwise

thereareduplicates•  WehavesolvedElementUniqueness

thatrequiresΩ(nlogn)time,andapartfromtheClosestPointalgorithm,everythingwedotakesO(n)time

•  So,itmustbetheClosestPointalgorithmthattakesΩ(nlogn)time.

D7013E Computational Geometry - Håkan Jonsson 44

p2 p1p3 p4p5 p0

Page 12: 1. Computing intersections among line segmentsLine segment Lecture 2 intersection Boolean operations on polygons Subdivisions Map overlay Doubly-Connected Edge Lists Plane sweep Closest

D7013E Computational Geometry - Håkan Jonsson 45

d

d

d

d

Max 6