题目大意:给出n及n条线段的2个端点坐标,求是否有线段相交(不考虑端点相交)
解题思路:用差积判断线段的起点与另一线段的2个端点分别构成的向量是否分别在该线段向量的2边
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <list>
using namespace std;
const int maxn = 2000+10;
const double eps = 1e-10;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
struct Point
{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
bool operator == (const Point& A)const
{
return dcmp(x-A.x)==0 && dcmp(y-A.y)==0;
}
bool operator < (const Point& A)const
{
return dcmp(x-A.x)<0 || dcmp(x-A.x)==0;
}
}p1[maxn],p2[maxn];
typedef Point Vector;
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
bool isInter(Point A1,Point A2,Point B1,Point B2)
{
double c1 = Cross(A2-A1,B1-A1);
double c2 = Cross(A2-A1,B2-A1);
double c3 = Cross(B2-B1,A1-B1);
double c4 = Cross(B2-B1,A2-A1);
return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%lf%lf%lf%lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
}
int flag = 0;
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(isInter(p1[i],p2[i],p1[j],p2[j]))
{
printf("burned!\n");
flag = 1;
break;
}
}
if(flag) break;
}
if(!flag) printf("ok!\n");
}
return 0;
}