You can Solve a Geometry Problem too
2 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.00 3 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.000 0.00 0.00 1.00 0.00 0
1 3
#include<iostream> #include<cstdio> #include<cmath> using namespace std; const int maxn = 110; struct ss { double x1, x2, y1, y2; double k; bool flag; }; bool check(ss a, ss b) { //无斜率平行 if(a.flag == b.flag && a.flag == false) return false; //有一直线无斜率 if(a.flag == false || b.flag == false) { //a无斜率 if(a.flag == false) { double y = b.k*a.x1 + b.y1 -b.k*b.x1; if(y < min(b.y1, b.y2) || y > max(b.y1, b.y2)) return false; else return true; } else//b无斜率 { double y = a.k*b.x1 + a.y1 -a.k*a.x1; if(y < min(a.y1, a.y2) || y > max(a.y1, a.y2)) return false; else return true; } } //有斜率平行 if(a.k == b.k) return false; else//不平行 { double x = (b.y1-b.k*b.x1-a.y1+a.k*a.x1)/(a.k-b.k); if(x < min(a.x1, a.x2) || x > max(a.x1, a.x2) || x < min(b.x1, b.x2) || x > max(b.x1, b.x2)) return false; else return true; } } int N, ans; int main() { while(scanf("%d", &N) != EOF && N) { ss f[maxn]; ans = 0; for(int i = 0;i < N; ++i) { scanf("%lf %lf %lf %lf", &f[i].x1, &f[i].y1, &f[i].x2, &f[i].y2); if(f[i].x1 > f[i].x2) { swap(f[i].x1, f[i].x2); swap(f[i].y1, f[i].y2); } if(f[i].x1 == f[i].x2) f[i].flag = false;//无斜率 else { f[i].flag = true;//有斜率 f[i].k = (f[i].y2 - f[i].y1)/(f[i].x2 - f[i].x1); } } for(int i = 1;i < N; ++i) for(int j = 0;j < i; ++j) { if(check(f[i], f[j])) ans ++; } cout << ans << endl; } return 0; }