poj 3304 Segments 直线与线段

来源:互联网 发布:汽车电气设计软件 编辑:程序博客网 时间:2024/06/05 16:15

Segments


Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

Input

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1y1 x2 y2 follow, in which (x1y1) and (x2y2) are the coordinates of the two endpoints for one of the segments.

Output

For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

Sample Input
321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0
Sample Output
Yes!Yes!No!

题目大意:可以理解为:给你n条线段,判断是否存在一条直线与所有的直线相交。
题目分析:看了别人的博客才知道,解法就是枚举两条线段的端点,构成一条直线,判断是否存在这样一条直线与所有线段相交。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<string>#include<stdlib.h>using namespace std;typedef long long ll;const double eps=1e-8;struct point{double xx,yy;point(double x=0,double y=0):xx(x),yy(y) {};};double Cross(point A,point B){    return A.xx*B.yy-A.yy*B.xx;}int dcmp(double x){    if(fabs(x)<eps)return 0;    return x<0?-1:1;}point operator +(point A,point B) {return point(A.xx+B.xx,A.yy+B.yy); }point operator -(point A,point B) {return point(A.xx-B.xx,A.yy-B.yy); }point operator *(point A,double p) {return point(A.xx*p,A.yy*p); }point operator /(point A,double p) {return point(A.xx/p,A.yy/p); }bool operator ==(point A,point B) {return dcmp(A.xx-B.xx)==0&&dcmp(A.yy-B.yy)==0;}  //-------------------------------------------------------------------------struct Rectangle{point p1;point p2;}a[105];int n;int judge(Rectangle t){if(t.p1==t.p2)  return 0;//必须要判断两点是否重合。 for(int i=0;i<n;i++){point v1,v2,v3,v4;v1=a[i].p1-t.p1;v2=a[i].p1-t.p2;v3=a[i].p2-t.p1;v4=a[i].p2-t.p2;if(dcmp(Cross(v1,v2)*Cross(v3,v4))>0)//如果线段的两点在分别直线的两边,则线段与直线一定相交。 return 0;}return 1;}int main(){int T,flag;Rectangle t1,t2,t3,t4;scanf("%d",&T);while(T--){flag=0;scanf("%d",&n);for(int i=0;i<n;i++)scanf("%lf%lf%lf%lf",&a[i].p1.xx,&a[i].p1.yy,&a[i].p2.xx,&a[i].p2.yy);if(n==1)  {puts("Yes!");continue;}//必须加 for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){t1.p1=a[i].p1;t1.p2=a[j].p1;t2.p1=a[i].p1;t2.p2=a[j].p2;t3.p1=a[i].p2;t3.p2=a[j].p1;t4.p1=a[i].p2;t4.p2=a[j].p2;//t5=a[i]   不需要加  if( judge(t1) || judge(t2) || judge(t3) || judge(t4)  )flag=1;}}if(flag)printf("Yes!\n");elseprintf("No!\n");}return 0;}