POJ Segments - 3304

http://poj.org/problem?id=3304

Use the cross product to determine which side of the line a point is on the line determined by the two line segments (u1 v1) (u2 v2)

Two lines intersect if u1 and v1 are on different sides of (u2 v2) or u2 and v2 are on different sides of (u1 v1)

For this question, is there a line such that the projections of all line segments on the line intersect at a point? This question is equivalent to whether there is a line that intersects all line segments because if there is such a line, the perpendicular of the line satisfies All projections intersect and the intersection of these two lines is the intersection of all projections

It should also be noted that when judging the intersection of a line segment and a line segment or a line segment and a straight line, only one cross product can be obtained (if it is a line segment and a straight line, it must be based on a straight line), but if it is a straight line and a straight line, it must be calculated twice.

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps=1e-8;
const int maxn=1e2+10;

struct node
{
    double x1,y1,x2,y2;
};

node seg[maxn];
double x[2*maxn],y[2*maxn];
int n;

double getval(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}

bool judge(double x1,double y1,double x2,double y2)
{
    double res1,res2;
    int i;
    if(fabs(x1-x2)<eps&&fabs(y1-y2)<eps) return 0;
    for(i=1;i<=n;i++){
        res1=getval(seg[i].x1-x1,seg[i].y1-y1,x2-x1,y2-y1);
        res2=getval(seg[i].x2-x1,seg[i].y2-y1,x2-x1,y2-y1);
        if(res1*res2>eps) return 0;
    }
    return 1;
}

int main()
{
    int t,i,j,flag;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&seg[i].x1,&seg[i].y1,&seg[i].x2,&seg[i].y2);
            x[2*i-1]=seg[i].x1,y[2*i-1]=seg[i].y1;
            x[2*i]=seg[i].x2,y[2*i]=seg[i].y2;
        }
        if(n<=2) printf("Yes!\n");
        else{
            flag=0;
            for(i=1;i<=2*n;i++){
                for(j=i+1;j<=2*n;j++){
                    if(judge(x[i],y[i],x[j],y[j])) flag=1;
                }
            }
            if(flag) printf("Yes!\n");
            else printf("No!\n");
        }
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325484476&siteId=291194637