POJ 3130 半麵相交
題意:很明顯判斷多邊形是否存在內核。
將每個邊看做一個半麵,判斷版麵是否大於2即可。
#include<cmath> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int mm=111; typedef double DIY; struct point { DIY x,y; point() {} point(DIY _x,DIY _y):x(_x),y(_y) {} } g[mm]; point MakeVector(point &P,point &Q) { return point(Q.x-P.x,Q.y-P.y); } DIY CrossProduct(point P,point Q) { return P.x*Q.y-P.y*Q.x; } DIY MultiCross(point P,point Q,point R) { return CrossProduct(MakeVector(Q,P),MakeVector(Q,R)); } struct halfPlane { point s,t; double angle; halfPlane() {} halfPlane(point _s,point _t):s(_s),t(_t) {} halfPlane(DIY sx,DIY sy,DIY tx,DIY ty):s(sx,sy),t(tx,ty) {} void GetAngle() { angle=atan2(t.y-s.y,t.x-s.x); } } hp[mm],q[mm]; point IntersectPoint(halfPlane P,halfPlane Q) { DIY a1=CrossProduct(MakeVector(P.s,Q.t),MakeVector(P.s,Q.s)); DIY a2=CrossProduct(MakeVector(P.t,Q.s),MakeVector(P.t,Q.t)); return point((P.s.x*a2+P.t.x*a1)/(a2+a1),(P.s.y*a2+P.t.y*a1)/(a2+a1)); } bool cmp(halfPlane P,halfPlane Q) { if(fabs(P.angle-Q.angle)<1e-8) return MultiCross(P.s,P.t,Q.s)>0; return P.angle<Q.angle; } bool IsParallel(halfPlane P,halfPlane Q) { return fabs(CrossProduct(MakeVector(P.s,P.t),MakeVector(Q.s,Q.t)))<1e-8; } void HalfPlaneIntersect(int n,int &m) { sort(hp,hp+n,cmp); int i,l=0,r=1; for(m=i=1; i<n; ++i) if(hp[i].angle-hp[i-1].angle>1e-8)hp[m++]=hp[i]; n=m; m=0; q[0]=hp[0],q[1]=hp[1]; for(i=2; i<n; ++i) { if(IsParallel(q[r],q[r-1])||IsParallel(q[l],q[l+1]))return; while(l<r&&MultiCross(hp[i].s,hp[i].t,IntersectPoint(q[r],q[r-1]))>0)--r; while(l<r&&MultiCross(hp[i].s,hp[i].t,IntersectPoint(q[l],q[l+1]))>0)++l; q[++r]=hp[i]; } while(l<r&&MultiCross(q[l].s,q[l].t,IntersectPoint(q[r],q[r-1]))>0)--r; while(l<r&&MultiCross(q[r].s,q[r].t,IntersectPoint(q[l],q[l+1]))>0)++l; q[++r]=q[l]; for(i=l; i<r; ++i) g[m++]=IntersectPoint(q[i],q[i+1]); } int main() { int n,m; while(~scanf("%d",&n),n) { for(int i=0; i<n; i++) scanf("%lf%lf",&g[i].x,&g[i].y); for(int i=0; i<n; i++) { hp[i]=halfPlane(g[i],g[(i+1)%n]); hp[i].GetAngle(); } HalfPlaneIntersect(n,m); puts(m>2?"1":"0"); } return 0; }
最後更新:2017-04-03 21:30:13
上一篇:
Hadoop 真特麼難用
下一篇:
暴雪娛樂推首個免費遊戲 支持iPad平板電腦
阿裏雲支撐馬來西亞數字自由貿易區落地 幫助馬來西亞中小企業參與全球貿易
停止去人性化吧 SOC應找回人的元素
阿裏雲ECS部署Grafana接入zabbix
《TensorFlow技術解析與實戰》——第2章 TensorFlow環境的準備 2.1下載TensorFlow 1.1.0
http://www.blogjava.net/killme2008/archive/2007/07/23/131788_zh.html
智能家居未來已來,可沒做到這點便是“雞肋”!
虛擬無線接入網:行業的演進方向
JVM實用參數(四)內存調優
如果沒有Visual Studio 2015,我們如何創建.NET Core項目 ?
Spring 設計理念探討係列一