Post on 17-Mar-2020
transcript
6.006IntroductiontoAlgorithms
Lecture24:GeometryProf.ErikDemaine
Today• Computationalgeometry• Line‐segmentintersection
– Sweep‐linetechnique• Closestpairofpoints
– Divide&conquerstrikesback!
Motivation:CollisionDetectionphotobyfotios lindiakos
http://www.flickr.com/photos/fotios_lindiakos/342596118/
Motivation:CollisionDetectionphotobyfotios lindiakos
http://www.flickr.com/photos/fotios_lindiakos/342596118/
“GTA4Carmageddon!”bydot12321http://www.youtube.com/watch?v=4-620xx7yTo
Line‐SegmentIntersection• Input: linesegmentsin2D• Goal: Findthe intersections
ObviousAlgorithmforeverypair oflinesegments:checkforintersection
Sweep‐LineTechnique• Idea: Sweepaverticallinefromlefttoright• Maintainintersectionoflinewithinput
Sweep‐LineIntersections
Sweep‐LineEvents• Discretizecontinuousmotionofsweepline• Eventswhenintersectionchanges
– Segmentendpoints – Intersections
Sweep‐LineAlgorithmSketch
• Maintainsweep‐lineintersection• Maintainpriorityqueue of(possible)eventtimes ( coordinatesofsweepline)
• Untilqueueisempty:– Deleteminimumeventtime frompriorityqueue– Updatesweep‐lineintersectionfrom to– Updatepossibleeventtimesinpriorityqueue
“Discrete‐eventsimulation”
IntersectionDataStructure• Balancedbinarysearchtree(e.g.,AVLtree)tostoresorted ( ) orderofintersections
EndpointEvents• Foreachlinesegment :
– At :insert (binarysearchforneighborsthen)– At :delete
IntersectionEvents?• Howtoknowwhenhaveintersectionevents?• Thisisthewholeproblem!
IntersectionEvents• Computewhenneighboringsegmentswouldintersect,ifnothingchangesmeanwhile
• Ifsuchaneventisnext,thenitreallyhappens
while isnotempty:
if is :insert into (binarysearchingwith )
elif is :delete from
elif is :reportintersectionbetween andswapcontentsofnodesfor and in
foreachnode changedorneighboringchangedin :deleteall eventsinvolving fromadd & to
Sweep‐LineAlgorithm
Sweep‐LineAnalysis• Runningtime=• Numberofendpointevents=• Numberofmeetevents=number ofintersections=sizeofoutput
• Runningtime=• Outputsensitivealgorithm:runningtimedependsonsizeofoutput
• Bestalgorithmrunsin time
ClosestPairofPoints• Input: pointsin2D• Goal: findtwoclosestpoints
ObviousAlgorithmmin(distance
foreverypair ofpoints)
Divide‐and‐ConquerIdea• Initiallysortpointsby coordinate• Splitpointsintolefthalfandrighthalf• Recurseoneachhalf:findclosestpair• returnmin{closestpairineachhalf,
closestpairbetweentwohalves}
ClosestPairBetweenTwoHalves?
• Let =min{closestpairineachhalf}• Onlyinterestedinpairswithdistance• Restricttowindowofwidth aroundmiddle
ClosestPairBetweenTwoHalves
• Foreachleftpoint,interestedinpointsonrightwithindistance
• Pointsonrightsidecan’tbewithin ofeachother
• Soatmostthreerightpointstoconsiderforeachleftpoint– Dittoforeachrightpoint
• Cancomputein timebymergingtwosortedarrays
Divide‐and‐Conquerpresortpointsbydef :
sort bysort bymergeandfindclosestpairbetweentwolistsreturnmin{ ,closestdistancefrommerge}
FasterDivide‐and‐Conquer[Shamos &Hoey 1975]
presortpointsby and ,andcrosslinkpointsdef :
maptoypoints &findclosestpairbetweenlistsreturnmin{ ,closestdistancefrommerge}
OtherComputationalGeometryProblems
• Convexhull• Voronoi diagram• Triangulation• Pointlocation• Rangesearching• Motionplanning• …
6.850:GeometricComputing