Post on 17-Mar-2020
transcript
Outline
• 3-Dvectorgeometry• High-Dhyperplaneintersections• Convexhull&itsextensionto3dimensions• Pointinsidepolyhedron• Voronoi diagrams
CS491WF– CompetitiveAlgorithmicProgramming 2
3-DVectorGeometry• Recall:
• Vectorshavemagnitudeanddirection• Canberepresentedbyacoordinatetriple𝐚 = 𝑎$, 𝑎', 𝑎(
• Dotproduct:• In2-D,𝐚 · 𝐛 = 𝑎$𝑏$ + 𝑎'𝑏' = ‖𝐚‖‖𝐛‖cos 𝜃• In3-D,𝐚 · 𝐛 = 𝑎$𝑏$ + 𝑎'𝑏' + 𝑎(𝑏( = ‖𝐚‖‖𝐛‖cos 𝜙,where𝜙istheanglebetween𝐚and𝐛intheplaneformedby𝐚and𝐛
• Directioncosines:• 𝐮 = ‖𝐮‖ cos 𝛼𝐢 + cos 𝛽𝐣 + cos 𝛾𝐤
CS491WF– CompetitiveAlgorithmicProgramming 3
3-DVectorGeometry
• Crossproduct:• In2-D, ‖𝐚×𝐛‖ istheareaoftheparallelogramformedby𝐚and𝐛• In3-D,𝐚×𝐛 describesnormalvectorfortwovectors
• Right-handrule
• 𝐚×𝐛 = 𝑎'𝑏( − 𝑎(𝑏' 𝐢 + 𝑎(𝑏$ − 𝑎$𝑏( 𝐣 + 𝑎$𝑏' − 𝑎'𝑏$ 𝐤
CS491WF– CompetitiveAlgorithmicProgramming 4
High-DHyperplanes
• Whatisahyperplane?• “Anysubspaceofonedimensionlessthanitsambientspace”• In1-D,apoint 𝑥 = 𝑎• In2-D,aline 𝑎𝑥 + 𝑏𝑦 = 𝑐• In3-D,aplane 𝑎𝑥 + 𝑏𝑦 + 𝑐𝑧 = 𝑑• etc.• Canalsobedescribedbyapointandanormalvector(rememberthecross-product?)
• Howdotheyintersect?• In2-D,atapoint• In3-D,ataline• Ingeneral,intheformofahyperplanewithonefewerdimension
CS491WF– CompetitiveAlgorithmicProgramming 5
3-DLinesandPlanes
• Whataboutlinesin3-D?• Lineshave2fewerdimensionsthantheirambientspace• Usuallydescribedusingparametricequations:
• 𝑥, 𝑦, 𝑧 = 𝑥B + 𝑎𝑡, 𝑦B + 𝑏𝑡, 𝑧B + 𝑐𝑡• Canalsobedescribedastheintersectionof2planes• Itispossiblefortwolinestonotbeparallelbutnotintersect!
• Linesandplanes• Ifalineisnotparalleltoaplane,theywillintersectinapoint
CS491WF– CompetitiveAlgorithmicProgramming 6
ConvexHullProblem
• Convexhull:“findthesmallestconvexpolygoncompletelyenclosingasetofpoints”
• GrahamScanusespolarlinesweep1. Sortpointsinangleorder2. Appendpointsinincreasingordertochain3. Ifpointcausesconcavecorner,removepreviouspointsuntilchainbecomes
convexTimecomplexity:𝑂 𝑛log𝑛
CS491WF– CompetitiveAlgorithmicProgramming 7
ConvexHullin3-D
• HowdoweperformGrahamScanin3dimensions?• Howdowedefinea2-dimensionalpolarangle?
• Weneedabetterway• Thereisadivide-and-conqueralgorithm…
CS491WF– CompetitiveAlgorithmicProgramming 8
Quickhull
1. Findthemostextremepointsineachdimension(minandmaxx- andy-values)2. Drawlinesbetweenthepoints;pointsinsidethisquadrilateralcannotbeonthe
convexhullandareremoved3. Splittingtheremainingpointsinto2setsbyalinethroughtheextremex-value
points4. Ineachset,findthepointthatisthefarthestfromtheline;pointsinsidethe
triangleformedbythispointandthelinecanberemoved5. Recursivelyrepeatthisprocessforthe2newlinescreatedbythetriangle6. Oncetherearenomorepointslefttoprocess,thepointsselectedconstitute
theconvexhull
Timecomplexity:𝑂 𝑛log𝑛
CS491WF– CompetitiveAlgorithmicProgramming 9
ConvexHullin3-D
• PerformQuickhull in3-D:1. Findallextremepointsineachdimension(x,y,andz)2. Usetheextremepointstoformatetrahedron(simplex)3. Foreachface,determinewhichpointsareoutsidethetetrahedronand
discardallothers4. Findpointwithlargestdistancefromface5. Findallfacesvisiblefromthatpoint;createnewfacewitheachhorizonof
thefaceandthepoint6. Removeallpointsnotoutsidethenewfaces7. Repeatuntilallpointshavebeenremoved;chosensetisconvexhull
Goodexplanation:http://thomasdiewald.com/blog/?p=1888CS491WF– CompetitiveAlgorithmicProgramming 11
PointInsidePolyhedron
• For2-D,canuseray-casting(precisionproblems)orwindingnumberalgorithm(seehttp://geomalgorithms.com/a03-_inclusion.html)• For3-D:• Ifpolyhedronisplanarpolygon,canprojectdowninto2-Dandsolve• Otherwise,needtouseray-castingforeachface
CS491WF– CompetitiveAlgorithmicProgramming 12
Voronoi Diagrams
• Forasetofpointsinaplane,Voronoi diagramdescribesthenearestneighbor pointforanygivenpointintheplane• Canbeusedtosolve“largestemptycircle”and“nearestneighborlookup”problems
CS491WF– CompetitiveAlgorithmicProgramming 13
Fortune’sAlgorithm
• Isasweeplinealgorithm• Keeptrackofsweeplineandbeachline• Pointstotheleftofthesweeplinehavebeenconsidered• Foreachpointtotheleftofthesweepline,beachlineistheboundaryoftheunionofthesetofparabolasequidistantfromsweeplineandthepoints• IntersectionsbetweenparabolasbecomeedgesoftheVoronoi diagram• Intersectionsbetween3parabolasbecomeverticesoftheVoronoi diagram• KeeptrackofparabolasusingBSTandpriorityqueuefornextpointsandintersectionstoconsider
Timecomplexity:𝑂 𝑛log𝑛
CS491WF– CompetitiveAlgorithmicProgramming 14
Fortune’sAlgorithm
Illustration:
Reference:http://blog.ivank.net/fortunes-algorithm-and-implementation.html
CS491WF– CompetitiveAlgorithmicProgramming 15
DelaunayTriangulation
• Triangulationofasetofpointsthatmaximizestheminimumangleofalltriangles• Delaunaytriangulationalsoensuresthatnopointp isinthecircumcircleofanytriangleinthetriangulation• CentersofcircumcirclesformedbyDTaretheverticesintheVoronoidiagram:
CS491WF– CompetitiveAlgorithmicProgramming 16
Delaunay triangulation Voronoi diagram
Additionaltopics
• Rotatingcalipers– canbeusedtofindthediameterandwidthofapolygon,buthasmanyadditionalapplications(boundingboxes,convexhullofconvexpolygons,etc.)• http://www.tcs.fudan.edu.cn/rudolf/Courses/Algorithms/Alg_ss_07w/Webprojects/Qinbo_diameter/2d_alg.htm
CS491WF– CompetitiveAlgorithmicProgramming 17
ExampleAlgorithmImplementations
• O’Rourke’sComputationalGeometryinC (link)• Convexhull(2-Dand3-D)• Delaunaytriangulation• Segment-segmentintersection• Pointinpolygon• Pointinpolyhedron
• Rotatingcalipers(link)
• Theseimplementationsarelongandincludeerrorchecking,sotheyarenotsuitedforcontest• Practiceimplementingyourown,anduseawell-debuggedandshortversionincontests!
CS491WF– CompetitiveAlgorithmicProgramming 18
ExampleProblem
• Giventwo3Dconvexhulls,judgewhethertheyhaveanyintersection.• Thenumberofvertices<=100
CS491WF– CompetitiveAlgorithmicProgramming 19
Resources
• Differenttypesofconvexhullalgorithms:http://jeffe.cs.illinois.edu/teaching/compgeom/notes/01-convexhull.pdf• Fortune’salgorithmforVoronoidiagrams:http://blog.ivank.net/fortunes-algorithm-and-implementation.html• Rotatingcalipersdescription:• https://en.wikipedia.org/wiki/Rotating_calipers• http://www.tcs.fudan.edu.cn/rudolf/Courses/Algorithms/Alg_ss_07w/Webprojects/Qinbo_diameter/2d_alg.htm
CS491WF– CompetitiveAlgorithmicProgramming 20