题目链接点击打开链接
题意:给你n条线段,求这些线段有几个交点,用模板就好了,哈哈
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=1e2+10;
struct Point//点 向量
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
//向量使用点作为表示方法 结构相同 为了代码清晰定义宏加以区别
const double eps = 1e-8;
int dcmp(double x) //三态函数 处理与double零有关的精度问题
{
if(fabs(x) < eps) return 0;
return x<0 ? -1 : 1;
}
//向量运算
Vector operator + (Vector A, Vector B)
{
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Vector A, Vector B)
{
return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (Vector A, double p)
{
return Vector(A.x*p, A.y*p);
}
Vector operator / (Vector A, double p)
{
return Vector(A.x/p, A.y/p);
}
bool operator == (const Vector& A, const Vector& B)
{
return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;
}
bool operator < (const Point&a,const Point &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
double Dot(Vector A, Vector B) //向量点积
{
return A.x * B.x + A.y * B.y;
}
double Cross(Vector A, Vector B) //向量叉积
{
return A.x * B.y - A.y * B.x;
}
//判断线段相交(每条线段的两个端点都在另一条线段的两侧)
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<=0&&dcmp(c3)*dcmp(c4)<=0;
}
int main()
{
int n;
Point p[maxn][2];
while(cin>>n&&n)
{
int cunt=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<2; j++)
cin>>p[i][j].x>>p[i][j].y;
}
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
if(SegmentProperIntersection(p[i][0],p[i][1],p[j][0],p[j][1]))
cunt++;
cout<<cunt<<endl;
}
return 0;
}