51nod 判断线段是否相交 poj Segments直线与多条线段相交

来源:互联网 发布:淘宝经典差评 编辑:程序博客网 时间:2024/05/22 17:27

给出两条线段的端点,判断是否相交

包括端点处的判断

若不包括端点处的,就在下面判断相交函数中去掉等号



#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const double eps=1e-8;struct Point{    double x,y;    Point(){}    Point(double _X, double _Y){        x = _X; y = _Y;    }};double Cross(Point p1,Point p2,Point p3){    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);}Point operator - (Point A,Point B){    return Point(A.x-B.x, A.y-B.y);}Point operator + (Point A, Point B){    return Point(A.x+B.x, A.y+B.y);}Point operator * (Point A, double p){    return Point(A.x*p, A.y*p);}bool operator == (Point A, Point B){    return (A.x-B.x) == 0 && (A.y-B.y) == 0;}bool SegmentProperIntersection(Point a,Point b,Point c,Point d){    return (max(a.x,b.x)>=min(c.x,d.x))&&            (max(c.x,d.x)>=min(a.x,b.x))&&            (max(a.y,b.y)>=min(c.y,d.y))&&            (max(c.y,d.y)>=min(a.y,b.y))&&            (Cross(a,c,b)*Cross(a,b,d)>=0)&&            (Cross(c,a,d)*Cross(c,d,b)>=0);}int main(){    int T;    //freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--)    {        Point a,b,c,d;        scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);        scanf("%lf%lf%lf%lf",&c.x,&c.y,&d.x,&d.y);        int flag=SegmentProperIntersection(a,b,c,d);        puts(flag?"Yes":"No");    }}



题意:

要你求是否存在一条直线,使得所有给出的线段投影在上面的影子至少有一个公共交点

题解:

由这一个公共点,我们可以推知,垂直这条线的直线,一定和所有线段相交

易知:极限情况下,这条直线一定过平面中某两个点

所以我们枚举两个点得到的直线是否和其他所有的线段都相交即可



#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 110const double eps=1e-8;struct Point{    double x,y;    Point(){}    Point(double _X,double _Y){        x = _X; y = _Y;    }};Point P[MAXN*2];double Cross(Point p1,Point p2,Point p3){    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);}double Dis(Point p1,Point p2){    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.x-p2.x)*(p1.x-p2.x));}Point operator - (Point A,Point B){    return Point(A.x-B.x, A.y-B.y);}Point operator + (Point A, Point B){    return Point(A.x+B.x, A.y+B.y);}Point operator * (Point A, double p){    return Point(A.x*p, A.y*p);}bool operator == (Point A, Point B){    return (A.x-B.x) == 0 && (A.y-B.y) == 0;}bool deal(int n){    int flag;    for(int i=1;i<=n*2;i++){        for(int j=i+1;j<=n*2;j++){            flag=1;            if(Dis(P[i],P[j])<eps)                continue;            for(int k=1;k<=n*2;k+=2){                if(Cross(P[i],P[j],P[k])*Cross(P[i],P[j],P[k+1])>0){                    flag=0;                    break;                }            }            if(flag)                return 1;        }    }    return 0;}int main(){    int n,T;    //freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=1;i<=n*2;i+=2)            scanf("%lf%lf%lf%lf",&P[i].x,&P[i].y,&P[i+1].x,&P[i+1].y);        if(deal(n))            puts("Yes!");        else            puts("No!");    }    return 0;}


阅读全文
0 0