cf#13F - Letter A-計算幾何

來源:互聯網
上載者:User

http://codeforces.com/contest/13/problem/B

給出 三個線段的起點,終點

讓你判斷是否組成一個A字母


條件如下:

1 有兩線段同端點,並且第三條線段經過了前兩條線段上的點,串連他們

2、有共同端點的兩線段夾角大於零小於等於90度

3。 第三條線段分別把前兩條線段截成兩部分,要求被截出來的部分必須大於等於所線上段長度1/5,且小於等於4/5


沒什麼坑。。。老老實實做就是了。。注意一些細節就好


思路: 符合要求的六個點,必然是2個相同,四個分別不同

1、那麼重複次數為2的點個數為1,重複次數為1的點個數為4;(共六個點)

2、找到重複的那個點後,從而確定哪兩條是有公用端點的線段,然後求出他們的夾角(注意是>0&&《90)開始寫成<90

3、判斷第三條線段的端點S是否在line2,是求出其到line2一端的長度是否滿足要求,同理判斷另一側


如果以上有任一不滿足,say no。

都滿足了。。say yes


參考code:

#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <cmath>#include <iostream>using namespace std;const double pi=acos(-1.0);struct POINT{double x;double y; POINT(__int64 a=0, __int64 b=0) { x=a; y=b;}bool operator==(POINT &B) const{return x==B.x&&y==B.y;}bool operator<(const POINT &b) const{if (x!=b.x)return x<b.x;elsereturn y<b.y;}};struct LINESEG{POINT s;POINT e;LINESEG(POINT a, POINT b) { s=a; e=b;}LINESEG() { }};double angle(POINT o,POINT e,POINT s){double cosfi,fi,norm;double dsx = s.x - o.x;double dsy = s.y - o.y;double dex = e.x - o.x;double dey = e.y - o.y;cosfi=dsx*dex+dsy*dey;norm=(dsx*dsx+dey*dey)*(dex*dex+dey*dey);cosfi /= sqrt( norm );if (cosfi >= 1.0 ) return 0;if (cosfi <= -1.0 ) return -3.1415926;fi=acos(cosfi);if (dsx*dey-dsy*dex>0) return fi;// 說明向量os 在向量 oe的順時針方向return -fi;}double multiply(POINT sp,POINT ep,POINT op){return((sp.x-op.x)*(ep.y-op.y) - (ep.x-op.x)*(sp.y-op.y));}bool online(LINESEG l,POINT p){return ((multiply(l.e,p,l.s)==0)&& ( ( (p.x-l.s.x) * (p.x-l.e.x) <=0 ) && ( (p.y-l.s.y)*(p.y-l.e.y) <=0 ) ) );}double dist(POINT p1,POINT p2){return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );}int main(){int t;__int64 i ;cin>>t;__int64 x,y,x1,y1;LINESEG ll[5];while(t--){__int64 vis[5];memset(vis,0,sizeof(vis));map<POINT,__int64 > sb;map<POINT,__int64 >::iterator it;sb.clear();__int64 tmp=1;while(tmp<=3){scanf("%I64d%I64d%I64d%I64d",&x,&y,&x1,&y1);ll[tmp++]=LINESEG(POINT(x,y),POINT(x1,y1));sb[POINT(x,y)]++;sb[POINT(x1,y1)]++;}__int64 flag=0;__int64 cun=0;POINT who;who.x=who.y=-1;for (it=sb.begin();it!=sb.end();it++){if (it->second==2){flag=1;who=it->first;}elseif (it->second==1)cun++;}__int64 ret=0;if (cun==4&&flag) ret=1;if (!ret) {printf("NO\n"); continue;}for (i=1;i<=3;i++){if (ll[i].s==who||ll[i].e==who) vis[i]=1;}for (i=1;i<=3;i++){if (vis[i]==0) break;}__int64 line2,line3,line1;if (i==1){line1=1;line2=2;line3=3;}if (i==2){line1=2;line2=1;line3=3;}if (i==3){line1=3;line2=2;line3=1;}POINT P1,P2;if (ll[line2].s==who) P1=ll[line2].e;if (ll[line2].e==who) P1=ll[line2].s;if (ll[line3].s==who) P2=ll[line3].e;if (ll[line3].e==who) P2=ll[line3].s;__int64 flag_angle=0;double ret_angel=angle(who,P1,P2); if (ret_angel<0) ret_angel=-ret_angel;  if (ret_angel>0&&ret_angel<=pi/2) flag_angle=1;ret_angel=angle(who,P2,P1); if (ret_angel<0) ret_angel=-ret_angel;  if (ret_angel>0&&ret_angel<=pi/2) flag_angle=1; if (flag_angle==0) {printf("NO\n"); continue;}__int64 fla=0;double dis3=0;double dis2=0;if (online(ll[line2],ll[line1].s)){double line_dis=dist(ll[line2].s,ll[line2].e);dis2=dist(ll[line1].s,ll[line2].s);if (dis2*5>=line_dis && dis2*5<=line_dis*4)fla=1;}if (online(ll[line2],ll[line1].e)){double line_dis=dist(ll[line2].s,ll[line2].e);dis2=dist(ll[line1].e,ll[line2].s);if (dis2*5>=line_dis && dis2*5<=line_dis*4)fla=1;}if (fla==0) {printf("NO\n"); continue;} fla=0;if (online(ll[line3],ll[line1].s)){double line_dis=dist(ll[line3].s,ll[line3].e);dis2=dist(ll[line1].s,ll[line3].s);if (dis2*5>=line_dis && dis2*5<=line_dis*4)fla=1;}if (online(ll[line3],ll[line1].e)){double line_dis=dist(ll[line3].s,ll[line3].e);dis2=dist(ll[line1].e,ll[line3].s);if (dis2*5>=line_dis && dis2*5<=line_dis*4)fla=1;}if (fla==0) {printf("NO\n"); continue;}elseprintf("YES\n");}   return 0;}//57283


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.