1086 You can Solve a Geometry Problem too

来源:互联网 发布:非农数据分析软件 编辑:程序博客网 时间:2024/06/07 20:13
 #include<iostream>
#include<math.h>
using namespace std;
struct point
{
    double x;
    double y;
};
struct point p[100];
struct point q[100];
double cross(point w1,point w2,point w3)
{
   return (w2.x-w1.x)*(w3.y-w1.y)-(w2.y-w1.y)*(w3.x-w1.x);
}
int on(point w1,point w2,point w3)
{
    return (w1.x-w2.x)*(w1.x-w3.x)+(w1.y-w2.y)*(w1.y-w3.y);
}
int three(double d)
{
    if(fabs(d)<1e-6)return 0;
    else if(d>0)return 1;
    else return -1;
}
int findcross(point x1,point x2,point y1,point y2)
{
 int a,b,c,d;
 a=three(cross(x1,x2,y1));
 b=three(cross(x1,x2,y2));
 c=three(cross(y1,y2,x1));
 d=three(cross(y1,y2,x2));
 if(a*b<0&&c*d<0)return 1;
 if(a==0&&on(x2,x1,y1)<=0) return 2;
 if(b==0&&on(y2,x1,y1)<=0) return 2; 
 if(c==0&&on(x1,y1,y2)<=0) return 2;
 if(d==0&&on(x2,y1,y2)<=0) return 2;
 return 0;
}
void main()
{
 int sum,i,j,n;
 while(cin>>n&&n!=0)
 {
  for(i=1,sum=0;i<=n;i++)cin>>p[i].x>>p[i].y>>q[i].x>>q[i].y;
  for(i=1;i<=n;i++)
   for(j=i+1;j<=n;j++)
    if(findcross(p[i],q[i],p[j],q[j]))sum++;
  cout<<sum<<endl;
 }
}