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

Post on 11-Jul-2020

4 views 0 download

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

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.)

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

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

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

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

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

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!

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

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

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)

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

D7013E Computational Geometry - Håkan Jonsson 45

d

d

d

d

Max 6