题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086
分析:判断AB和CD两线段是否有交点:
同时满足两个条件,1.C点D点分别在AB的两侧.(向量(ABxAC)*(ABxAD)<=0)
2.A点和B点分别在CD两侧.(向量(CDxCA)*(CDxCB)<=0)
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
const int maxn=100000;
struct point{
double x,y;
void read(){
cin>>x>>y;
}
};
struct line{
point st, en;
void read(){
st.read();
en.read();
}
}f[maxn];
double cross(point p0,point p1,point p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int work(line h,line g){
double x = cross(h.st,h.en,g.st);
double y = cross(h.st,h.en,g.en);
if(x*y<=0) return 1;
return 0;
}
int main(){
int n;
while(cin>>n,n){
int sum=0;
for(int i=0;i<n;++i){
f[i].read();
for(int j=0;j<i;++j)
sum += ( work(f[i],f[j])&& work(f[j],f[i]) );
}
cout<<sum<<endl;
}
return 0;
}