51Nod 1264 線段相交

來源:互聯網
上載者:User

標籤:--   iostream   map   main   關注   algo   cpp   cout   時間   

1264 線段相交基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題 收藏 關注描述

給出平面上兩條線段的兩個端點,判斷這兩條線段是否相交(有一個公用點或有部分重合認為相交)。 如果相交,輸出"Yes",否則輸出"No"。

Input

第1行:一個數T,表示輸入的測試數量(1 <= T <= 1000)
第2 - T + 1行:每行8個數,x1,y1,x2,y2,x3,y3,x4,y4。(-10^8 <= xi, yi <= 10^8)
(直線1的兩個端點為x1,y1 | x2, y2,直線2的兩個端點為x3,y3 | x4, y4)

Output

輸出共T行,如果相交輸出"Yes",否則輸出"No"。

Input樣本

2
1 2 2 1 0 0 2 2
-1 1 1 1 0 0 1 -1

Output樣本

Yes
No

題解

通過判斷叉積來判斷線段是否相交。代碼如下:

#include <cstdio>#include <iostream>#include <algorithm>#include <string>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <map>#include <set>#include <queue>#include <utility>#define ll long longusing namespace std ;struct point {double x , y ;} ;double xmul ( point a , point b , point c ){    return ( b.x - a.x ) * ( c.y - a.y ) - ( b.y - a.y ) * ( c.x - a.x ) ;}int main(){    int t ;    cin >> t ;    while ( t -- ){        point a , b , c , d ;        cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y >> d.x >> d.y ;        if ( xmul(a , b , c) * xmul(a , b , d) <= 0 && xmul(c , d , a) * xmul(c , d , b) <= 0 ){            cout << "Yes" << endl ;        }else{            cout << "No" << endl ;        }    }    return 0 ;}

51Nod 1264 線段相交

聯繫我們

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