半平面求交算法_第1页
半平面求交算法_第2页
半平面求交算法_第3页
半平面求交算法_第4页
半平面求交算法_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/Step1:Separatetheh-planesintotwosets。Onehaspolaranglesof(-½π,½π],theotherhasthoseof(-π,—½π]∪(½π,π]。将半平面分成两部分,一部分极角范围(-½π,½π],另一部分范围(-π,-½π]∪(½π,π]Step2:Considerthesetofh-planesin(—½π,½π](theothersetshouldalsogothroughstep3and4similarly)。Sortthembythepolarangle.Fortheh—planeswiththesamepolarangle,wecankeeponlyonedown(deleteallothers)accordingtotheconstantoftheseh—planes考虑(-½π,½π]的半平面(另一个集合类似地做Step3/4),将他们极角排序。对极角相同的半平面,根据常数项保留其中之一。Step3。1:Startingfromtwoh-planeswiththeleastpolarangle,computetheirintersection.Pushthemtwointoastack.从排序后极角最小两个半平面开始,求出它们的交点并且将他们押入栈.Step3.2:Eachtime,addonemoreh-planebyincreasingorderofpolarangles,andcalculatetheintersectionofitandthetoph-planeinstack.每次按照极角从小到大顺序增加一个半平面,算出它与栈顶半平面的交点。Step3。3:Ifthisintersectionistotherightoftheintersectionoftoptwoh—planesinstack,wepopthestackonce.如果当前的交点在栈顶两个半平面交点的右边,出栈(pop)。upperupperhull上壳lowerhull下壳Step4.1:Intersectionsofadjacenth-planepairsinstackformhalfaconvexpolygon。Forthetwosets,wehavetwohalves–(-½π,½π]givesanupperhulland(—π,-½π]∪(½π,π]givesalowerhull.相邻半平面的交点组成半个凸多边形。我们有两个点集,(—½π,½π]给出上半个,(-π,—½π]∪(½π,π]给出下半个p4p4p3p1p2upperhull上壳lowerhull下壳Step4。2:Atthebeginning,fourpointersp1,p2,p3andp4indicateleftmost/rightmostedgesofbothupperandlowerhulls。p1andp3moverightwards,whilep2andp4walksleftwards.初始时候,四个指针p1,p2,p3andp4指向上/下凸壳的最左最右边p1,p3向右走,p2,p4向左走p3p3p4p1p2Step4.3:Atanytime,iftheleftmostintersectionisagainstthefeasibleregionprovidedbyp1orp3,wearesuretheleftmostoneistoberemoved.Naturally,p1orp3walksrightwardstoitsadjacentedge.任意时刻,如果最左边的交点不满足p1/p3所在半平面的限制,我们相信这个交点需要删除p1或p3走向它右边的相邻边p3p3p2p1p4p3p4p2p1Thejudgmentholdsanalogouslyforrightmostintersectionwithp2andp4。类似地我们处理最右边的交点p3p3p4p2p1Step4。4:Dotheaboveremovingrepeatedlyuntilthereisnomoreupdate.重复运作直到不再有更新出现—-迭代Everythingexceptsorting(Step2)inS&Ialgorithmremainlinear–O(n)runningtime。Usuallyweusequick—sort.ThetotalcomplexityisO(nlogn),withfairlysmallconstantfactorhidden.除了Step2中的排序以外,S&I算法的每一步都是线性的。通常我们用快速排序实现Step2,总的时间复杂度为O(nlogn),隐蔽其中的常数因子很小简单地说,我保存每一条直线的起点与终点,然后规定该(有向)直线的左手侧为半平面内部。这样,判断点是否在半平面内,还有极角序的工作变得相当简单,但是求交点就尴尬了。(求交点的精妙之处在于两个叉积的正负符号可以不同)ﻫ(这是极角序和判断点在平面内外的图示。被排序半平面平行的时候,比如图中的AD和BC,如果BD在AC的左手方向,那就应该保留AD,这个可以用叉积判断。判断点在平面内外时,点在半平面外<=〉点在半平面右手侧。)Code(POJ2451)://submititbyC++insteadofG++

#include<cstdio〉

#include<cmath〉

#include<algorithm〉ﻫusingnamespacestd;ﻫﻫ#defineMXN20050

constdoubleeps=1e-7;ﻫinlineintsgn(doublea){

if(fabs(a)<eps)return0;ﻫreturn(a〈0)?-1:1;ﻫ}ﻫ#defineZERO(X)(sgn(X)==0)

#defineNEGA(X)(sgn(X)==-1)

#definePOSI(X)(sgn(X)==1)ﻫ

structpoint{

doublex,y;ﻫpoint():x(0),y(0){}ﻫpoint(doublea,doubleb):x(a),y(b){}

pointoperator—(constpoint&b)const{

returnpoint(x-b.x,y—b.y);ﻫ}

doubleoperator*(constpoint&b)const{ﻫreturnx*b.y—y*b.x;

booloperator==(constpoint&b)const{ﻫreturn(ZERO(y—b.y)&&ZERO(x—b。x));

}ﻫ}P[MXN];

ﻫstructhvec{ﻫpoints,t;ﻫdoubleang;

pointoperator*(consthvec&b)const{ﻫdoubles1=(b.t-s)*(b.s-s),

s2=(b。s—t)*(b。t—t);

returnpoint((s。x*s2+t.x*s1)/(s2+s1),ﻫ(s.y*s2+t。y*s1)/(s2+s1));

}ﻫbooloperator<(consthvec&b)const{

if(ZERO(b.ang-ang))

returnsgn((b.t-b.s)*(t-b.s))>=0;ﻫelsereturnang〈b.ang;

}

}H[MXN],Q[MXN];

intN;ﻫﻫinlineboolisout(consthvec&h,constpoint&p){

returnPOSI((p—h.s)*(h.t-h.s));ﻫ}

inlineboolparralel(consthvec&a,consthvec&b){ﻫreturnZERO((a.t-a。s)*(b.t-b.s));ﻫ}

pointinte(hveca,hvecb){returna*b;}

boollesstahn(hveca,hvecb){returna<b;}

ﻫinlineinthvec_int(){

intM=0,C=1,l=0,r=1;

sort(H+1,H+N+1);

for(inti=2;i〈=N;++i)

if(!ZERO(H[i].ang-H[i—1].ang))

H[++C]=H[i];ﻫQ[0]=H[1],Q[1]=H[2];

for(inti=3;i〈=C;++i){

while(l〈r&&isout(H[i],Q[r]*Q[r—1]))-—r;ﻫwhile(l<r&&isout(H[i],Q[l]*Q[l+1]))++l;ﻫQ[++r]=H[i];

}

while(l<r&&isout(Q[l],Q[r]*Q[r-1]))-—r;ﻫwhile(l〈r&&isout(Q[r],Q[l]*Q[l+1]))++l;ﻫif(r<=l+1)return0;

for(inti=l;i〈r;++i)P[++M]=Q[i]*Q[i+1];

P[++M]=Q[l]*Q[r];ﻫM=unique(P+1,P+M+1)—P—1;ﻫreturnM;ﻫ}

doublearea(intn){ﻫif(n<3)return0;

doubles=0;ﻫfor(inti=1;i<=n;++i)ﻫs+=P[i]*P[i%n+1];

returnfabs(s/2);ﻫ}

voidadd_hvec(points,pointt){

H[++N].s=s,H[N]。t=t;

pointc=t-s;

H[N].ang=atan2(c.y,c。x);

}ﻫﻫintmain(){ﻫintm;

#ifdefLOCALﻫfreopen("uyuw.in”,"r",stdin);

#endifﻫN=0;ﻫadd_hvec(point(0,0),point(10000,0)),

add_hvec(point(10000,0),point(10000,10000)),

add_hvec(point(10000,10000),point(0,10000)),ﻫadd_hvec(point(0,10000),point(0,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论