poj 2653二維vector線段相交

來源:互聯網
上載者:User
#include <iostream>#include <cstring>#include<cmath>#include<algorithm>#include<vector>const double N=-1e-10;using namespace std;int bz[1005];struct point{double x,y;};vector<point> a[100005];int xj(point x1,point x2,point x3,point x4)//相交為1,不交為0 {if(min(x1.x,x2.x)>max(x3.x,x4.x)||min(x1.y,x2.y)>max(x3.y,x4.y)   ||min(x3.x,x4.x)>max(x1.x,x2.x)||min(x3.y,x4.y)>max(x1.y,x2.y)  )  return 0;//不交:矩形排斥實驗 ,最小的>最大的 肯定不交   double a,b,c,d;  a=(x1.x-x2.x)*(x3.y-x1.y)-(x1.y-x2.y)*(x3.x-x1.x);//跨立實驗   b=(x1.x-x2.x)*(x4.y-x1.y)-(x1.y-x2.y)*(x4.x-x1.x);  c=(x3.x-x4.x)*(x1.y-x3.y)-(x3.y-x4.y)*(x1.x-x3.x);  d=(x3.x-x4.x)*(x2.y-x3.y)-(x3.y-x4.y)*(x2.x-x3.x);  return a*b<=N&&c*d<=N;}int main(void){      int t,i,j,s;   point s1,s2;   while(cin>>t&&t)   {  memset(bz,0,sizeof(bz));     for(i=0;i<t;i++)     {          cin>>s1.x>>s1.y>>s2.x>>s2.y; a[i].push_back(s1); a[i].push_back(s2);      }      for(s=i=0;i<t;i++)        { for(j=i+1;j<t;j++)            if(xj(a[i][0],a[i][1],a[j][0],a[j][1]))   break;  if(j==t) bz[s++]=i+1;//沒有提前退出說明沒有被覆蓋        }  cout<<"Top sticks:";      for(i=0;i<s-1;i++)  cout<<' '<<bz[i]<<',';      cout<<' '<<t<<'.'<<endl;  for(i=0;i<t;i++)  a[i].clear();   }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.