hdu-You can Solve a Geometry Problem too
生活随笔
收集整理的這篇文章主要介紹了
hdu-You can Solve a Geometry Problem too
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://acm.hdu.edu.cn/showproblem.php?pid=1086? ? ?
題意:判斷兩兩直線相交的個數(shù)
code:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cmath> using namespace std; //點 struct POINT {double x, y;POINT(){ }POINT(double a, double b){x = a;y = b;} }p[105]; //線段 struct Seg {POINT a, b;Seg() { }Seg(POINT x, POINT y){a = x;b = y;} }; //叉乘 double cross(POINT o, POINT a, POINT b) {return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y); } //判斷點在線段上 bool On_Seg(POINT a, Seg s) {double maxx = max(s.a.x, s.b.x), minx = min(s.a.x, s.b.x);double maxy = max(s.a.y, s.b.y), miny = min(s.a.y, s.b.y);if(a.x >= minx && a.x <= maxx && a.y >= miny && a.y <= maxy) return true;return false; } //判斷線段相交 bool Seg_cross(Seg s1, Seg s2) {double cs1 = cross(s1.a, s2.a, s2.b);double cs2 = cross(s1.b, s2.a, s2.b);double cs3 = cross(s2.a, s1.a, s1.b);double cs4 = cross(s2.b, s1.a, s1.b);// 互相跨立if(cs1 * cs2 < 0 && cs3 * cs4 < 0) return true;if(cs1 == 0 && On_Seg(s1.a, s2)) return true;if(cs2 == 0 && On_Seg(s1.b, s2)) return true;if(cs3 == 0 && On_Seg(s2.a, s1)) return true;if(cs4 == 0 && On_Seg(s2.b, s1)) return true;return false; }int main(){int n;while(cin >> n, n){Seg p[105];for(int i = 0;i < n;i++)cin >> p[i].a.x >> p[i].a.y >> p[i].b.x >> p[i].b.y;int cnt = 0;for(int i = 0;i < n;i++){for(int j = i+1;j < n;j++){if(Seg_cross(p[i],p[j])) cnt ++;}}cout << cnt << endl;} }
總結(jié)
以上是生活随笔為你收集整理的hdu-You can Solve a Geometry Problem too的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu-1392 Surround th
- 下一篇: poj 2187 Beauty Cont