判断线段与直线是否相交,注意精度问题。
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
#define eps 0.00000001
#define zero(x) ((fabs(x)<eps)?0:x)
struct segn
{
double x1,y1;
double x2,y2;
}seg[110];
int n;
struct line
{
int leap;
double k;
double b;
}ll;
int dos(line a,segn b)
{
if(a.leap==1)
{
if((a.b<=max(b.x1,b.x2)&&a.b>=min(b.x1,b.x2))||(!zero(a.b-b.x1)||!zero(a.b-b.x2)))return 1;
else return 0;
}
double b1,b2,bb;
b1=b.y1-a.k*b.x1;
b2=b.y2-a.k*b.x2;
bb=a.b;
if((bb<=max(b1,b2)&&bb>=min(b1,b2))||(!zero(bb-b1)||!zero(bb-b2)))return 1;
return 0;
}
int pan(double x1,double y1,double x2,double y2)
{
if(zero(x1-x2)==0)
{
ll.leap=1;
ll.b=x1;
}
else
{
ll.leap=0;
ll.b=(y1*x2/x1-y2)/(x2/x1-1);
ll.k=(y1-ll.b)/x1;
}
for(int i=1;i<=n;i++)
{
if(!dos(ll,seg[i]))return 0;
}
return 1;
}
int leap;
int main()
{
int T,i,j;
cin>>T;
while(T--)
{
leap=0;
cin>>n;
for(i=1; i<=n; i++)
{
cin>>seg[i].x1>>seg[i].y1>>seg[i].x2>>seg[i].y2;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(pan(seg[i].x1,seg[i].y1,seg[i].x2,seg[i].y2))leap=1;
if(pan(seg[i].x1,seg[i].y1,seg[j].x2,seg[j].y2))leap=1;
if(pan(seg[i].x1,seg[i].y1,seg[j].x1,seg[j].y1))leap=1;
if(pan(seg[j].x1,seg[j].y1,seg[i].x2,seg[i].y2))leap=1;
if(pan(seg[j].x1,seg[j].y1,seg[j].x2,seg[j].y2))leap=1;
if(pan(seg[j].x2,seg[j].y2,seg[i].x2,seg[i].y2))leap=1;
if(leap)break;
}
if(leap)break;
}
if(leap==1)cout<<"Yes!"<<endl;
else cout<<"No!"<<endl;
}
return 0;
}