您的位置:首页 > 其它

Segments--poj3304(判断直线与线段之间的关系)

2016-04-24 17:59 489 查看
http://poj.org/problem?id=3304

给你几条线段 然后 让你找到一条直线让他在这条直线上的映射有一个重合点

如果有这条直线的话 这个重合的部分的两个端点一定是某两条线段的端点

所以只需要枚举每个点连成的直线能不能跟所有的线段相交就行了

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#define N 200
const double ESP = 1e-8;
struct Point
{
double x, y;

Point(double x=0,double y=0):x(x),y(y) {}
Point operator + (const Point &temp)const{
return Point(x+temp.x, y+temp.y);
}
Point operator - (const Point &temp)const{
return Point(x-temp.x, y-temp.y);
}
bool operator == (const Point &temp)const{
return (fabs(x-temp.x) < ESP && fabs(y-temp.y) < ESP);
}
int operator * (const Point &temp)const{
double t=(x*temp.y)-(y*temp.x);
if(t > ESP)
return 1;
if(fabs(t) < ESP)
return 0;
return -1;
}
};

struct node
{
Point A,B;
node(Point A=0,Point B=0):A(A),B(B){}

};

int Find(node t,node a[],int n)
{
for(int i=0;i<n;i++)
{
int k=fabs((a[i].A-t.A)*(t.B-t.A)+(a[i].B-t.A)*(t.B-t.A));

if(k==2)
return false;
}
return true;
}

int main()
{
int n, T;
scanf("%d", &T);
while(T--)
{
Point p
;
node a
;
scanf("%d", &n);
double x1,x2,y2,y1;
int b=0;
for(int i=0;i<n;i++)
{
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
p[b++]=Point(x1,y1);
p[b++]=Point(x2,y2);
a[i]=node(p[b-2],p[b-1]);
}
int ok=0;
for(int i=0; i<b && !ok; i++)
{
for(int j=i+1; j<b && !ok; j++)
{
if(p[i] == p[j])
continue;
ok = Find(node(p[i],p[j]),a,n);
}
}
if(ok)
printf("Yes!\n");
else
printf("No!\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: