計算幾何習題集

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   

poj 1269 (直線交點):

  這道題是給兩條直線(輸入直線上的兩個點),然後問你兩條直線是相交、重合還是平行。

  很簡單一道題。不過用g++測試的話double要用%f輸出。不知道為什麼。

  (求交點的模板感覺很科學)

 1 #include <cstdio> 2 #include <iostream> 3 #define FOR(i,l,r) for(int i=(l);i<=(r);i++) 4 #define FE(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++) 5 #define rep(i,n) for(int i=0;i<(n);i++) 6 #define zero(x) ((x>=0?x:-x)<1e-10) 7 #define debug(x) cout<<#x<<" = "<<x<<endl 8 using namespace std; 9 10 struct pot{11     double x,y;12     pot() { x=0; y=0; }13     pot(double _x, double _y):x(_x),y(_y){};14 };15 double cross(pot a,pot b) { return a.x*b.y-a.y*b.x; }16 pot to_v(pot a,pot b) { pot c=pot(b.x-a.x,b.y-a.y); return c; }17 pot intersection_line(pot u1,pot u2,pot v1,pot v2)18 {19     pot ret = u1;20     double t = ((u1.x-v1.x)*(v1.y-v2.y) - (u1.y-v1.y)*(v1.x-v2.x))21              / ((u1.x-u2.x)*(v1.y-v2.y) - (u1.y-u2.y)*(v1.x-v2.x));22     ret.x += (u2.x-u1.x) * t;23     ret.y += (u2.y-u1.y) * t;24     return ret;25 }26 27 pot a[5];28 int main()29 {30     puts("INTERSECTING LINES OUTPUT");31     int n; scanf("%d",&n);32     for (pot ans;n--;)33     {34         FOR (i,1,4) scanf("%lf%lf",&a[i].x,&a[i].y);35         if (zero(cross(to_v(a[1],a[2]),to_v(a[3],a[4]))))36             if (zero(cross(to_v(a[1],a[2]),to_v(a[1],a[3])))) puts("LINE");37             else puts("NONE");38         else ans=intersection_line(a[1],a[2],a[3],a[4]),printf("POINT %.2f %.2f\n",ans.x,ans.y);39     }40     puts("END OF OUTPUT");41     return 0;42 }43 /*pot intersection_line(pot u1,pot u2,pot v1,pot v2)44 {45     pot ret;46     double a1=u1.y-u2.y,b1=u2.x-u1.x,c1=u1.y*(u1.x-u2.x)-u1.x*(u1.y-u2.y);47     double a2=v1.y-v2.y,b2=v2.x-v1.x,c2=v1.y*(v1.x-v2.x)-v1.x*(v1.y-v2.y);48     ret.y=(a1*c2-a2*c1)/(a2*b1-a1*b2);49     ret.x=(b1*c2-b2*c1)/(a1*b2-a2*b1);50     return ret;51 }*/
萌萌的代碼~

 

聯繫我們

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