判断线段相交
#include <iostream> #include <cmath> #include <cstdio> using namespace std; struct line{ double x1,y1; double x2,y2; bool operator < (const line &a) const { return x1 < a.x1; } }arr[110]; bool cross(line a,line b) { double fa=((a.x2-a.x1)*(b.y1-a.y1)-(a.y2-a.y1)*(b.x1-a.x1))* ((a.x2-a.x1)*(b.y2-a.y1)-(a.y2-a.y1)*(b.x2-a.x1)); if(fa>0) return false; double fb=((b.x2-b.x1)*(a.y1-b.y1)-(b.y2-b.y1)*(a.x1-b.x1))* ((b.x2-b.x1)*(a.y2-b.y1)-(b.y2-b.y1)*(a.x2-b.x1)); if(fb>0) return false; if(fa==0&&fb==0) if( (a.x1-b.x1)*(a.x2-b.x1)+(a.y1-b.y1)*(a.y2-b.y1)>0 && (a.x1-b.x2)*(a.x2-b.x2)+(a.y1-b.y2)*(a.y2-b.y2)>0 ) return false; return true; } int main() { int n; while(cin>>n,n) { for(int i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&arr[i].x1,&arr[i].y1,&arr[i].x2,&arr[i].y2); } int ans=0; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(cross(arr[i],arr[j])) ans++; cout<<ans<<endl; } return 0; } /* 2 0 0 0 2 0 3 0 5 2 0 0 0 2 0 2 4 0 */