/
fRectangleminRect;minRect.area=maxReal;//largestfinitefloating�pointnu fRectangleminRect;minRect.area=maxReal;//largestfinitefloating�pointnu

fRectangleminRect;minRect.area=maxReal;//largestfinitefloatingpointnu - PDF document

trish-goza
trish-goza . @trish-goza
Follow
368 views
Uploaded On 2017-11-23

fRectangleminRect;minRect.area=maxReal;//largestfinitefloatingpointnu - PPT Presentation

ti0polygonsize1i10i1polygonsizei0i1fVector2RealxTJxF30 x697x38 Txf 6x065 x0 Tdx 00originpolygoni0Vector2RealxTJxF30 x697x38 Txf 6x ID: 608080

ti0=polygon.size()1 i1=0;i1polygon.size();i0=i1++)fVector2Real&#x]TJ/;༰ ;.97;8 T; 6.;e ;�

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "fRectangleminRect;minRect.area=maxReal;/..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Minimum-AreaRectangleContainingaSetofPointsDavidEberly,GeometricTools,RedmondWA98052https://www.geometrictools.com/ThisworkislicensedundertheCreativeCommonsAttribution4.0InternationalLicense.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/4.0/orsendalettertoCreativeCommons,POBox1866,MountainView,CA94042,USA.Created:May17,2015LastModi ed:September11,2020Contents1Introduction22TheExhaustiveSearchAlgorithm23TheRotatingCalipersAlgorithm33.1ComputingtheInitialRectangle..................................53.2UpdatingtheRectangle.......................................73.2.1DistinctSupportingVertices................................73.2.2DuplicateSupportingVertices................................73.2.3MultiplePolygonEdgesAttainMinimumAngle.....................83.2.4TheGeneralUpdateStep..................................104ARobustImplementation114.1AvoidingNormalization.......................................114.2IndirectComparisonsofAngles...................................124.3UpdatingtheSupportInformation.................................124.4ConversiontoaFloating-PointRectangle.............................131 Thepreviousversionofthisdocument(February9,2008)discussedthesimpleO(n2)algorithmforcomputingtheminimum-arearectanglethatcontainsaconvexpolygonofnvertices.Thecurrentversionincludesadiscussionoftherotatingcalipersalgorithm,analgorithmthatcanbeusedtocomputetheminimum-arearectangleinO(n)time.Likemostalgorithmsincomputationalgeometry,arobustimplementationusing oating-pointarithmeticisdiculttoachieve.DetailsoftheGTEimplementationareprovidedhere,whereweuseexactrationalarithmetictoguaranteecorrectsignclassi cationsforsubproblemsthatariseinthealgorithm.1IntroductionGivena nitesetof2Dpoints,wewanttocomputetheminimum-arearectanglethatcontainsthepoints.Therectangleisnotrequiredtobeaxisaligned,andgenerallyitwillnotbe.Itisintuitivethattheminimum-arearectangleforthepointsissupportedbytheconvexhullofthepoints.Thehullisaconvexpolygon,andanypointsinteriortothepolygonhavenoin uenceontheboundingrectangle.Lettheconvexpolygonhavecounterclockwise-orderedverticesVifor0in.Itisessentialtorequirethatthepolygonhavenotripleofcollinearvertices.Theimplementationissimplerwiththisconstraint.Anedgeoftheminimum-areaboundingrectanglemustbecoincidentwithsomeedgeofthepolygon[1];insomecases,multipleedgesoftherectanglecancoincidewithpolygonedges.Inthediscussionweusetheperpoperator:Perp(x;y)=(y;�x).If(x;y)isunitlength,thenthesetofvectorsf(x;y);�Perp(x;y)gisright-handedandorthonormal.2TheExhaustiveSearchAlgorithmTheinputisasetofmpoints.Theconvexhullmustbecomputed rst,andtheoutputisasetofnpoints.Theasymptoticbehaviorofthehullalgorithmdependsonm,wherepotentiallymismuchlargerthann.Oncewehavethehull,wecanthenconstructtheminimum-arearectangle.Thesimplestalgorithmtoimplementinvolvesiteratingovertheedgesoftheconvexpolygon.Foreachedge,computethesmallestboundingrectanglewithanedgecoincidentwiththepolygonedge.Ofallnrectangles,choosetheonewiththeminimumarea.Tocomputetheboundingrectangleforanedge,projectthepolygonverticesontothelineoftheedge.Themaximumdistancebetweentheprojectedverticesisthewidthoftherectangle.Nowprojectthepolygonverticesontothelineperpendiculartothepolygonedge.Themaximumdistancebetweentheprojectedverticesistheheightoftherectangle.Forthespeci edpolygonedge,wecancomputetherectangleaxisdirectionsandtheextentsalongthosedirections.PseudocodeforthealgorithmisprovidedinListing1. Listing1.TheO(n2)algorithmforcomputingtheminimum-arearectanglecontainingaconvexpolygon.structRectanglefVector2Real&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;center,axis[2];Realextent[2],area;//area=4*extent[0]*extent[1]g;RectangleMinAreaRectangleOfHull(Vector2Real&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;const&polygon)f2 RectangleminRect;minRect.area=maxReal;//largestfinitefloating=pointnumberfor(size ti0=polygon.size()=1,i1=0;i1polygon.size();i0=i1++)fVector2Real&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;origin=polygon[i0];Vector2Real&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;U0=polygon[i1]=origin;Normalize(U0);//lengthofU0is1Vector2Real&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;U1==Perp(U0);Realmin0=0,max0=0;//projectionontoU0=axisis[min0,max0]Realmax1=0;//projectionontoU1=axisis[0,max1],min1=0isguaranteedfor(size tj=0;jpolygon.size();++j)fVector2Real&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;D=polygon[j]=origin;Realdot=Dot(U0,D);if(dotmin0)min0=dot;elseif(dot&#x]TJ/;ག ;.97;8 T; 10;&#x.599;&#x 0 T; [0;max0)max0=dot;dot=Dot(U1,D);if(dot&#x]TJ/;ག ;.97;8 T; 10;&#x.599;&#x 0 T; [0;max1)max1=dot;gRealarea=(max0=min0)*max1;if(areaminRect.area)fminRect.center=origin+((min0+max0)/2)*U0+(max1/2)*U1;minRect.axis[0]=U0;minRect.axis[1]=U1;minRect.extent[0]=(max0=min0)/2;minRect.extent[1]=max1/2;minRect.area=area;ggreturnminRect;gRectangleMinAreaRectangleOfPoints(std::vectorVector2Real&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;const&points)f//Assumptionsforinput://1.points.size()&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;=3//2.points[]arenotcollinear//Assumptionsforoutput://1.polygon.size()&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;=3//2.polygon[]arecounterclockwiseordered//3.notripleofpolygon[]iscollinearstd::vectorVector2Real&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;polygon;ComputeConvexHull(points,polygon);returnMinAreaRectangleOfHull(polygon);g ThetwoloopsmakeitclearwhythealgorithmisO(n2).Ifthenumberofverticesnissmall,thisisaviablealgorithmtouseinpractice.However,thechallengingpartofimplementingthealgorithmusing oating-pointarithmeticistocomputetheconvexhullofthepointsrobustly.Theminimum-arearectangleconstructionforaconvexpolygonisasubproblemforcomputingtheminimum-volumeboxcontainingaconvexpolyhedron[2].Inthiscontext,theO(n2)behaviorcanbenoticeable,soitisworthwhiletohaveafasteralgorithmavailableforthe3Dsetting.3TheRotatingCalipersAlgorithmTherotatingcalipersalgorithmiscreditedto[4]|althoughthatnamewasnotused|forlocatingantipodalpointsofaconvexpolygonwhencomputingthediameter.Thealgorithmmaybefoundinasourcemorereadilyavailable[3,Theorem4.18].Thenameofthealgorithmisdueto[5],anarticlethatdescribesmanyusesfortherotatingcalipers.3 Appliedtotheminimum-arearectangleproblem,therotatingcalipersalgorithmstartswithaboundingrectanglehavinganedgecoincidentwithapolygonedgeandasupportingsetofpolygonverticesfortheotherpolygonedges.Therectangleaxesarerotatedcounterclockwisebythesmallestanglethatleadstotherectanglebeingcoincidentwithanotherpolygonedge.Thenewsupportingsetofverticesisbuiltfromtheprevioussetandfromthenewpolygonedgevertices.Intheremainderofthissection,apolygonedgehVi;Vi+1iwillbereferencedbyitsindicesinstead;thus,wewillrefertotheedgehi;i+1i.Theindexadditioniscomputedmodulon,thenumberofverticesofthepolygon.ThesupportingverticesforarectangleformamultisetS=fVi0;Vi1;Vi2;Vi3g,eachvertexsupportinganedgeoftherectangle.Theyarelistedintheorder:bottom,right,top,andleft.Onevertexcansupporttwoedges,whichiswhyScanbeamultiset(duplicateelements).ThesupportingindicesareI=fi0;i1;i2;i3g.Figure1showsthetypicalcon gurationforaboundingrectanglewithedgecoincidenttoapolygonedgeandwiththreeadditionalsupportingvertices. Figure1.Thetypicalcon gurationofaboundingrectanglecorrespondingtoapolygonedge.ThepolygonverticesareVifor0i8;theyarelabeledinthe gureonlywiththesubscript.Thecurrentrectangleisdrawninred,hasaxisdirectionsU0andU1,andissupportedbyedgeh0;1i.Theminimumrotationisdeterminedbyedgeh5;6i,whichmakesitthenextedgetoprocess.ThenextrectangleisdrawninblueandhasaxisdirectionsU00andU01. InFigure1,theinitialrectanglehasitsbottomedgesupportedbypolygonedgeh0;1i;considerthecounterclockwise-mostvertexV1tobethesupportingvertex.TherightedgeoftherectangleissupportedbyV3,thetopedgeoftherectangleissupportedbyV5,andtheleftedgeoftherectangleissupportedbyV7.ThesupportingindicesfortheaxisdirectionsfU0;U1gareI=f1;3;5;7g.Thecandidaterotationanglesarethoseformedbyh1;2iwiththebottomedge,h3;4iwiththerightedge,h5;6iwiththetopedge,andh7;8iwiththeleftedge.Theminimumanglebetweenthepolygonedgesandtherectangleedgesisattainedbyh5;6i.Rotatethecurrentrectangletothenextrectanglebytheanglebetweenthetopedgeofthecurrentrectangleandh5;6i.Figure1showsthatthenextrectanglehasedgecoincidentwithh5;6i,buttheotherpolygonedgeswerenotreachedbythisrotationbecausetheiranglesarelargerthantheminimumangle.Thenamingofthenextrectangleedgesisdesignedtoprovidethecon gurationwehadfortheinitialrectangle;thenewaxisdirectionsaredrawinblueandprovidetheorientationthat4 goeswiththenames.Thenewbottomedgeish5;6i,anditscounterclockwise-mostvertexV6ischosenasthesupportpointforthebottomedge.ThenewaxisdirectionsareU00=(V6�V5)=jV6�V5jandU01=�Perp(U00).ThesupportingindicesfortheaxisdirectionsfU00;U01gareI0=f6;7;1;3g.Asingleloopisusedtovisittheminimum-areacandidaterectangles.TheloopinvariantisthatyouhaveaxisdirectionsfU0;U1gandsupportingindicesI=fb;r;t;`g,orderedbytheedgestheysupport:bottom(b),right(r),top(t),andleft(`).Thepolygonedgehb�1;biiscoincidentwiththerectangle'sbottomedge.Vbisrequiredtobethecounterclockwise-mostvertexonthebottomedge.Inthepreviousexample,52Iisselectedbecauseoftheminimum-angleconstraint.TheremainingelementsofIsupporttherotatedrectangle,sotheybecomeelementsofI0.ThesuccessorofV5isV6,so6becomesanelementofI0.3.1ComputingtheInitialRectangleLetthepolygonverticesbeVifor0in.Theinitialrectanglehasitsbottomedgecoincidentwiththepolygonedgehn�1;0i.Bystartingwiththisedge,weavoidusingthemodulusargumentintheloopindexing.TheinitialaxisdirectionsareU0=(V0�Vn�1)=jV0�Vn�1jandU1=�Perp(U0).ThepolygonverticesareconvertedtothecoordinatesystemwithoriginV0andaxisdirectionsU0andU1.Wemustsearchfortheextremevaluesoftheconvertedpoints.Anextremevalueistypicallygeneratedbyonevertex,butitispossiblethatitcanbegeneratedbytwovertices.Inparticular,thishappenswhenapolygonedgeisparalleltooneoftheaxisdirections.Becausewehaveassumedthatnotripleofverticesiscollinear,itisnotpossibletohavethreeormoreverticesattaintheextremevalueinanaxisdirection.Whenanextremevalueoccurstwicebecauseapolygonedgeisparalleltoanaxisdirection,thesupportingpointforthecorrespondingedgeoftherectangleischosentobethecounterclockwise-mostvertexoftheedge.Thisisrequiredbecausetheupdatestepcomputesrotationanglesforedgesemanatingfromthesupportingvertex,andthoseedgesmusthavedirectionpointingtotheinterioroftherectangle.Forexample,Figure1showsthath0;1iisparalleltoU0.WechooseV1asthesupportvertex,andtheemanatingedgeusedindeterminingtheminimalrotationangleish1;2i.Listing2showshowtocomputethesmallestrectanglethathasanedgecoincidentwithaspeci edpolygonedge.TheRectanglestructurestorestheaxisdirectionsandthesupportingindices.Theareaoftherectangleisalsostoredforuseintherectangleupdatewhenapplyingtherotatingcalipersalgorithm.Thisfunctionisusedtocomputetheinitialrectanglefortherotatingcalipersmethod,butintheimplementationitisalsousedfortheexhaustivealgorithm. Listing2.Pseudocodeforcomputingthesmallestrectanglethathasanedgecoincidentwithaspeci edapolygonedgehi0;i1i.structRectanglefVector2Real&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;U[2];intindex[4];//order:bottom,right,top,leftRealarea;g;RectangleSmallestRectangle(intj0,intj1,std::vectorVector2Real&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;const&vertices)fRectanglerect;rect.U[0]=vertices[j1]=vertices[j0];Normalize(rect.U[0]);//lengthofrect.U[0]is1rect.U[1]==Perp(rect.U[0]);rect.index=fj1,j1,j1,j1g;5 Vector2Real&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;origin=vertices[j1];Vector2Real&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;zero(0,0);Vector2Real&#x]TJ/;ག ;.97;8 T; 6.;d ;� Td;&#x [00;support[4]=fzero,zero,zero,zerog;for(size ti=0;ivertices.size();++i)f//Convertvertices[i]tocoordinatesystemwithoriginvertices[j1]and//axisdirectionsrect.U[0]andrect.U[1].Theconvertedpointisv.Vector2Real&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;diff=vertices[i]=origin;Vector2Real&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;v=fDot(rect.U[0],diff),Dot(rect.U[1],diff)g;//Theright=mostvertexofthebottomedgeisvertices[i1].The//assumptionofnotripleofcollinearverticesguaranteesthat//rect.index[0]isi1,whichistheinitialvalueassignedatthe//beginningofthisfunction.Therefore,thereisnoneedtotest//forotherverticesfarthertotherightthanvertices[i1].if(v[0]&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;support[1][0]jj(v[0]==support[1][0]&&v[1]&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;support[1][1]))f//NewrightmaximumORsamerightmaximumbutclosertotop.rect.index[1]=i;support[1]=v;gif(v[1]&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;support[2][1]jj(v[1]==support[2][1]&&v[0]support[2][0]))f//NewtopmaximumORsametopmaximumbutclosertoleft.rect.index[2]=i;support[2]=v;gif(v[0]support[3][0]jj(v[0]==support[3][0]&&v[1]support[3][1]))f//NewleftminimumORsameleftminimumbutclosertobottom.rect.index[3]=i;support[3]=v;gg//Thecommentintheloophastheimplicationthatsupport[0]=zero,sothe//height(support[2][1]=support[0][1])issimplysupport[2][1].rect.area=(support[1][0]=support[3][0])*support[2][1];//width*heightg Figure2showsacon gurationwheretwopolygonedgesareparalleltorectangleedges,inwhichcasetwoextremevaluesoccurtwice. Figure2.Thesupportingverticesofthepolygonaredrawninred.h0;1isupportsthebottomedgeoftherectangleandV1isasupportingvertex.h3;4isupportstherightedgeoftherectangleandV4isasupportingvertex. 6 3.2UpdatingtheRectangleAftercomputingtheinitialrectangle,weneedtodeterminetherotationangletoobtainthenextpolygonedgeanditscorrespondingrectangle.Thetypicalcon gurationofonecoincidentedgeandthreeothersupportingverticeshastheessenceoftheideafortherectangleupdate,butothercon gurationscanarisethatrequirespecialhandling.Inthediscussion,arithmeticontheindicesintothepolygonverticesisperformedmodulothenumberofvertices:theindicesi+j,i+j+n,andi+j�nallrefertothesamevertex.3.2.1DistinctSupportingVerticesThetypicalcon gurationofFigure1isthesimplesttoupdate.Asinglepolygonedgeiscoincidentwiththerectangleandhasuniqueverticessupportingtheotherthreeedges.ThesupportingindicesareI=fi0;i1;i2;i3g.Lethij;ij+1ibetheuniqueedgethatformstheminimumanglewiththerectangleedgeitemanatesfrom,wherej2f0;1;2;3g.InFigure1,thevertexfromwhichtheedgeemanatesisV5(j=2).Thenewcoincidentedgeishij;ij+1i.ThenewsupportingindicesI0areobtainedfromIbyreplacingijwithij+1andbypermutingsothatijisthe rst(bottom-supporting)vertex:I0=fij+1;ij+1;ij+2;ij+3g.Theadditionsinthej-subscriptsarecomputedmodulo4.3.2.2DuplicateSupportingVerticesItispossiblethatonepolygonvertexsupportstworectangleedges,inwhichcasethevertexisacorneroftherectangle.Figure3showssuchacon guration.Thecurrentrectanglesaredrawninredandthenextrectanglesaredrawninblue. Figure3.(a)Acon gurationwhereapolygonvertexsupportstworectangleedges.V3supportstherightedgeandthetopedge.ThesupportingindicesforthecurrentrectangleareI=f1;3;3;5g.ThesupportingindicesforthenextrectangleareI0=f6;1;3;3g.(b)Theduplicate-vertexcon gurationcanbethoughtofasthelimitingcaseofadistinct-vertexcon guration,whereVT!V3.ThesupportingindicesforthecurrentrectangleareI=f1;3;T;5g.ThesupportingindicesforthenextrectangleareI0=f6;1;3;Tg.NowmoveVTtotherightsothatitcoincideswithV3.Thecurrentrectanglesdonotchangeduringthisprocess,butthetopedgeofthenextrectangleofthedistinct-vertexcasebecomesthetopedgeofthenextrectangleoftheduplicate-vertexcase. (a)(b) 7 Theselectionofthenewcoincidentedgeandtheupdateofthesupportingindicesareperformedjustasinthecaseofdistinctsupportingindices,exceptthereisnominimum-anglecandidateedgeemanatingfromthe rstoftheduplicatevertices.IntheexampleofFigure3(a),I=f1;3;3;5g.h1;2iisacandidateedge.Thereisnoedgeemanatingfromthe rstoccurrenceofV3.Oryoucanthinkoftheedgeash3;3i,whichisdegenerate.Ineithercase,thatsupportingvertexisskipped.TheedgeemanatingfromthesecondoccurrenceofV3,namely,h3;4i,isacandidateedge.Finally,h5;6iisacandidateedge.Asillustrated,theminimum-angleedgeish5;6i.I0isgeneratedfromIbyreplacingV5byV6,thecounterclockwise-mostvertexoftheedge,andtheverticesarecycledsothatV6occurs rst:I0=f6;1;3;3g.3.2.3MultiplePolygonEdgesAttainMinimumAngleItispossiblethattheminimumangleisattainedbytwoormorepolygonedges.Figure4illustratesseveralpossibilities.8 Figure4.(a),(b),and(c)arecon gurationswheretwopolygonedgesattaintheminimumangle.(d)isacon gurationwherethreepolygonedgesattaintheminimumangle.Thediscussionafterthis gureisaboutthevariouscases. (a)(b) (c)(d) InFigure4(a),thesupportingindicesforthecurrentrectangleareI=f1;3;4;6g.Twopolygonedgesattaintheminimumrotationangle,h1;2iandh4;5i.Choosingh1;2iasthenewbottomedge,thecorrespondingsupportisV2.h4;5isupportsthenewtopedgeandthesupportisV5.ThenewsupportingindicesareI0=f2;3;5;6g.ToobtainI0fromI,theoldsupportV1isreplacedbytheotherendpointV2andtheoldsupportV4isreplacedbytheotherendpointV5.Nocyclingisnecessary,becauseV2isthebottomsupport9 and2isalreadythe rstelementinI0.InFigure4(b),thesupportingindicesforthecurrentrectangleareI=f1;2;3;5g.Twopolygonedgesattaintheminimumotationangle,h1;2iandh3;4i.Choosingh1;2iasthenewbottomedge,thecorrespondingsupportisV2.h3;4isupportsthenewtopedgeandthesupportisV4.ThenewsupportingindicesareI0=f2;2;4;5g.ObservethatV2isaduplicatethatsupportstwoedgesoftherectangle.ToobtainI0fromI,theoldsupportV1isreplacedbytheotherendpointV2andtheoldsupportV3isreplacedbytheotherendpointV4.Nocyclingisnecessary,becauseV2isthebottomsupportand2isalreadythe rstelementinI0.InFigure4(c),thesupportingindicesforthecurrentrectangleareI=f1;2;3;4g.Twopolygonedgesattaintheminimumrotationangle,h1;2iandh3;4i.Choosingh1;2iasthenewbottomedge,thecorrespondingsupportisV2.h3;4isupportsthenewtopedgeandthesupportisV4.ThenewsupportingindicesareI0=f2;2;4;4g.ObservethatV2isaduplicatethatsupportstwoedgesoftherectangleandV4isaduplicatethatsupportstheothertwoedges.ToobtainI0fromI,theoldsupportV1isreplacedbytheotherendpointV2andtheoldsupportV3isreplacedbytheotherendpointV4.Nocyclingisnecessary,becauseV2isthebottomsupportand2isalreadythe rstelementinI0.InFigure4(d),thesupportingindicesforthecurrentrectangleareI=f1;3;4;6g.Threepolygonedgesattaintheminimumrotationangle;h1;2i,h4;5i,andh6;0i.Choosingh1;2iasthenewbottomedge,thecorrespondingsupportisV2.h4;5isupportsthenewtopedgeandthesupportisV5.h6;0isupportsthenewleftedgeandthesupportisV0.ThenewsupportingindicesareI0=f2;3;5;0g.ToobtainI0fromI,theoldsupportV1isreplacedbytheotherendpointV2,theoldsupportV4isreplacedbytheotherendpointV5,andtheoldsupportV6isreplacedbytheotherendpointV0.3.2.4TheGeneralUpdateStepThecurrentrectanglehasbottomedgecoincidentwiththepolygonedgehVi0�1;Vi0i.ThecorrespondingaxisdirectionisU0=(Vi0�Vi0�1)=jVi0�Vi0�1j.TheperpendicularaxishasdirectionU1=�Perp(U0).ThesupportingindicesareI=fi0;i1;i2;i3g.Thenextrectanglehasbottomedgecoincidentwiththepolygonedgehi00�1;i00i.ThecorrespondingaxisdirectionisU00=(Vi00�Vi00�1)=jVi00�Vi00�1j.TheperpendicularaxishasdirectionU01=�Perp(U00).ThesupportingindicesareI0=fi00;i01;i02;i03g.WeneedtodetermineI0fromI.De nethesetMf0;1;2;3gasfollows:ifm2M,thenim2Iandthepolygonedgehim;im+1iformstheminimumanglewiththerectangleedgesupportedbyVim.Theindexadditionim+1iscomputedmodulon.WhenMhasmultipleelements,theminimumangleisattainedmultipletimes.IntheexampleofFigure1,I=f1;3;5;7gandh5;6iattainstheminimumangle,soM=f2g.IntheexampleofFigure3(a),I=f1;3;3;5gandh5;6iattainstheminimumangle,soM=f3g.IntheexamplesofFigure4(a,b,c),M=f0;2g.IntheexampleofFigure4(d),M=f0;2;3g.StartwithanemptyM.Processeachik2Iforwhichik+16=ik,wheretheindexadditionk+1iscomputedmodulo4.Computetheanglekformedbyhik;ik+1iandthesupportedrectangleedge,wheretheindexadditionik+1iscomputedmodulon.Itisnecessarythatk0.Ifk�0,storethepair(k;k)inasetAsortedontheanglecomponent.AfterprocessingallelementsofI,ifAisnonempty,thenits rstelementis(kmin;kmin).IterateoverAandinsertalltheindexcomponentskintoMforwhichk=kmin.ItispossiblethatAisempty,inwhichcasetheoriginalpolygonmustalreadybearectangle.TogenerateI0fromI,foreachm2Mreplaceimbyim+1,wheretheindexadditioniscomputedmodulo10 n.TheresultingmultisetoffourelementsisJ.Letm0bethesmallestelementofM.Thenextrectangleisselectedsothatitsbottomedgeissupportedbyhim0�1;im0i;thatis,i00=im0.CycletheelementsofJsothatim0occurs rst,whichproducesthemultisetI0.4ARobustImplementationThecorrectnessofthealgorithmdepends rstontheinputpolygonbeingconvex,evenwhentheverticeshave oating-pointcomponents.Experimentshaveshownthatformoderatelylargen,generatingnrandomanglesin[0;2),sortingthem,andthengeneratingvertices(acos;bsin)onanellipse(x=a)2+(y=b)2=1canleadtoavertexsetforwhichnotallthegeneratedverticesarepartoftheconvexhull.Ofcourse,thisisaconsequenceofnumericalroundingerrorswhencomputingthesineandcosinefunctionsandtheproductsbytheellipseextents.Ifaconvexhullalgorithmisnotappliedtothesepoints,theexhaustivealgorithmandtherotatingcalipersalgorithmcanproduceslightlydi erentresults.Thecorrectnessofthealgorithmisalsothwartedbytheroundingerrorsthatoccurwhennormalizingtheedgestoobtainunit-lengthvectors.Theseerrorsarefurtherpropagatedinthevariousalgebraicoperationsusedtocomputetheanglesandareas.Toobtainacorrectresult,itissucienttouseexactrationalarithmeticintheimplementation.However,normalizingavectorgenerallycannotbedoneexactlyusingrationalarithmetic.Thenormalizationcanactuallybeomittedbymakingsomealgebraicobservations.Theminimum-areaboxcanbecomputedexactly.Ifyouneedanoriented-boxrepresentationwithunit-lengthaxisdirections,youcanachievethisattheveryendoftheprocess,incurring oating-pointroundingerrorsonlyintheconversionofrationalvaluesto oating-pointvalues.Theseoccurwhencomputingtheextentsintheaxisdirectionsandwhennormalizingvectorstoobtainunit-lengthdirections.4.1AvoidingNormalizationConsiderthecomputationofthesmallestrectangleforaspeci edpolygonedgehj0;j1i.LetW0=Vj1�Vj0andW1=�Perp(W0),whicharetheunnormalizedaxisdirectionswithjW1j=jW0j.Theunit-lengthaxisdirectionsareUi=Wi=jW0jfori=0;1.LetthesupportverticesbeVb,Vr,Vt,andV`whosesubscriptsindicatetheedgestheysupport:bottom(b),right(r),top(t),andleft(`).Thewidthwandheighthoftherectanglearew=U0(Vr�V`);h=U1(Vt�Vb)(1)andtheareaaisa=wh=(W0(Vr�V`))(W1(Vt�Vb)) jW0j2(2)Boththenumeratoranddenominatorofthefractionfortheareacanbecomputedexactlyusingrationalarithmeticwhentheinputverticeshaverationalcomponents.Theconstructionofthesupportverticesusescomparisonsofdotproducts.Forexample,Listing2hasaloopwithcomparisonsforthecurrentright-edgesupport.Thecomparisonv[0]�support[1][0]isanimplementationofU0(Vi�Vi1)�U0(Vr0�Vi1)(3)whereVi1istheoriginofthecoordinatesystem,r0istheindexintothevertexarraythatcorrespondstothecurrentsupportvertex,andiistheindexofthevertexunderconsideration.TheBooleanvalueofthe11 comparisonisequivalenttothatusingunnormalizedvectors,W0(Vi�Vi1)�W0(Vr0�Vi1)(4)Theexpressionsinthecomparisoncanbecomputedexactlyusingrationalarithmetic.4.2IndirectComparisonsofAnglesWemustlocatetheedgeemanatingfromasupportvertexthatpointsinsidetherectangleand,ofallsuchedges,formstheminimumanglewiththerectangleedgecorrespondingtothesupportvertex.LetDbetheunit-lengthrectangleedgedirection(usingcounterclockwiseordering).ThatdirectionisU0forthebottomedge,U1fortherightedge,�U0forthetopedge,and�U1fortheleftedge.ForanemanatingedgeoriginatingatsupportvertexVsandterminatingatVfinsidetherectangle,thevectorformedbytheverticesisE=Vf�Vs.TheanglebetweenDandEisdeterminedbycos=DE jEj(5)Wecanapplytheinversecosinefunction(acos)toextracttheangleitself.DisobtainedbynormalizingoneofU0orU1,whichwealreadyknowcannotgenerallybedoneexactlywithrationalarithmetic.Similarly,wemustcomputethelengthofE,e ectivelyanothernormalization.Andtheinversecosinefunctioncannotbecomputedexactly.Observethat2(0;=2].Theanglemustbepositivebecausetheedgeisdirectedtowardtherectangleinterior.Iftheangleattainsthemaximum=2,thenthesupportvertexmustbeacorneroftherectangle.Unlessthepolygonisalreadyarectangle,theremustbeatleastoneotherboundingrectangleedgethathasanemanatingedgewithanglesmallerthan=2.Toavoidtheinexactcomputationstoobtain,wecaninsteadcomputethequantitiesjU0j2sin2,whichisapositiveandincreasingfunctionof.SortingofandjU0j2sin2leadtothesameordering.UsingsomealgebraandtrigonometrywemayshowthatjU0j2sin2=(WiPerp(E))2 jEj2(6)whereD=Uiforsomechoiceofsignandofindexi.Thefractionontheright-handsideoftheequationcanbecomputedexactlyusingrationalarithmetic.Thequantitiesofequation(6)arecomputedforallemanatingedgesandstoredinanarrayastheyareencountered.Weneedtosortthevaluestoidentifythosethatattaintheminimumvalue.Althoughanycomparison-basedsortwillwork,swappingofarrayelementshasanassociatedcostwhenthevaluesarearbitraryprecisionrationalnumbers.Ourimplementationusesanindirectsortbymaintaininganarrayofintegerindicesintothearrayofvalues,andtheintegerindicesareswappedasneededtoproducea nalintegerarraythatrepresentsthesortedvalues.4.3UpdatingtheSupportInformationThisisastraightforwardimplementationoftheideasofSection3.2.4.Theonlyissuenotmentionedyetisthatastherotatingcalipersalgorithmprogresses,thepolygonedgesarenotvisitedsequentiallyin-order.12 Moreover,ifthepolygonhaspairsofparalleledge,itispossiblethattheminimum-arearectangleiscomputedwithoutvisitingallpolygonedges.Inourimplementation,wemaintainanarrayofnBooleanvalues,oneforeachcounterclockwise-mostvertexofapolygonedge.TheBooleanvaluesareallinitiallyfalse.Afterthesmallestrectangleiscomputedforapolygonedgehi0;i1i,theBooleanvalueatindexi1issettotrue.Whenthesupportinformationisupdatedduringeachpassofthealgorithm,theBooleanvalueistestedtoseewhethertheedgehasbeenvisitedasecondtime.Ifithasbeenvisitedbefore,thealgorithmterminates(otherwise,wewillhaveanin niteloop).4.4ConversiontoaFloating-PointRectangleOurimplementationoftherotatingcalipersalgorithmtrackstheminimum-arearectangleviathedatastructurestructRectanglefVector2ComputeType&#x]TJ/;ག ;.97;8 T; 5.;݉ ;� Td;&#x [00;U[2];std::arrayint,4�index;//order:bottom,right,top,leftRealsqrLenU0;//squaredlengthofU[0]Realarea;g;Thisisslightlymodi edfromthatofListing2;thesquaredlengthofU0isstoredforusethroughoutthecode.TheComputeTypeisarationaltype.Therectanglecenterandsquaredextentscanbecomputedexactlyfromtheminimum-arearectangleminRect,Vector2ComputeType&#x]TJ/;ག ;.97;8 T; 5.;݉ ;� Td;&#x [00;sum[2]=frvertices[minRect.index[1]]+rvertices[minRect.index[3]],rvertices[minRect.index[2]]+rvertices[minRect.index[0]]g;Vector2ComputeType&#x]TJ/;ག ;.97;8 T; 5.;݉ ;� Td;&#x [00;difference[2]=frvertices[minRect.index[1]]=rvertices[minRect.index[3]],rvertices[minRect.index[2]]=rvertices[minRect.index[0]]g;Vector2ComputeType&#x]TJ/;ག ;.97;8 T; 5.;݉ ;� Td;&#x [00;center=mHalf*(Dot(minRect.U[0],sum[0])*minRect.U[0]+Dot(minRect.U[1],sum[1])*minRect.U[1])/minRect.sqrLenU0;Vector2ComputeType&#x]TJ/;ག ;.97;8 T; 5.;݉ ;� Td;&#x [00;sqrExtent;for(inti=0;i2;++i)fsqrExtent[i]=mHalf*Dot(minRect.U[i],difference[i]);sqrExtent[i]*=sqrExtent[i];sqrExtent[i]/=minRect.sqrLenU0;gThearraynamerverticesstorestherationalrepresentationofthe oating-pointverticesreferenceinpreviouspartsofthisdocument.Theconversiontothe oating-pointrepresentationoftheboxhassomelossofprecision,butthisoccursattheveryend.OrientedBox2Real&#x]TJ/;ག ;.97;8 T; 6.;e ;� Td;&#x [00;itMinRect;//input=typeminimum=arearectangle;for(inti=0;i2;++i)fitMinRect.center[i]=(Real)center[i];13 itMinRect.extent[i]=sqrt((Real)sqrExtent[i]);//Beforeconvertingtofloating=point,factoroutthemaximum//componentusingComputeTypetogeneraterationalnumbersina//rangethatavoidslossofprecisionduringtheconversionand//normalization.Vector2ComputeType&#x]TJ/;ག ;.97;8 T; 5.;݉ ;� Td;&#x [00;axis=minRect.U[i];ComputeTypecmax=max(abs(axis[0]),abs(axis[1]));ComputeTypeinvCMax=1/cmax;for(intj=0;j2;++j)fitMinRect.axis[i][j]=(Real)(axis[j]*invCMax);gNormalize(itMinRect.axis[i]);g//Quantitiesaccessiblethroughget=accessorsinthepublicinterface.supportIndices=minRect.index;area=(Real)minRect.area;Ifyouneedtocontinueworkingwiththeexactrationaldatageneratedbytherotatingcalipersalgorithmratherthanthe oating-pointrectangle,ourimplementationallowsyouaccesstoit.Thesourcecodefortheimplementationisinthe leMinimumAreaBox2.h.References[1]H.FreemanandR.Shapira.Determiningtheminimum-areaencasingrectangleforanarbitraryclosedcurve.InCommunicationsoftheACM,volume18,pages409{413,NewYork,NY,July1975.[2]JosephO'Rourke.Findingminimalenclosingboxes.InternationalJournalofComputer&InformationSciences,14(3):183{199,1985.[3]FrancoP.PreparataandMichaelIanShamos.ComputationalGeometry:AnIntroduction.Springer-Verlag,NewYork,1985.[4]MichaelIanShamos.Computationalgeometry.PhDthesis,YaleUniversity,1978.[5]GodfriedToussaint.Solvinggeometricproblemswiththerotatingcalipers.InProceedingsoftheIEEE,Athens,Greece,1983.14