Segments(叉积)

时间:2023-03-08 16:05:45
Segments(叉积)

Segments

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

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18066   Accepted: 5680

Description

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 x1 y1 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

3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0

Sample Output

Yes!
Yes!
No! 枚举每两个线段的端点,判断这些端点形成的直线是否与线段都有交点
还有,题目说如果两个点的距离小于1e-8就看成一个点,所以要考虑重点
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std; struct Point{
double x,y;
}; struct Line{
Point s,e;
}line[]; double Cross(double x0,double y0,double x1,double y1,double x2,double y2){
return (x1-x0)*(y2-y0)-(y1-y0)*(x2-x0);
} double distance(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
} bool Check(double x1,double y1,double x2,double y2,int n){
if(distance(x1,y1,x2,y2)<0.00000001) return ;
for(int i=;i<=n;i++){
if(Cross(x1,y1,x2,y2,line[i].s.x,line[i].s.y)*Cross(x1,y1,x2,y2,line[i].e.x,line[i].e.y)>0.00000001){
return ;
}
}
return ;
} bool ac(int n){
Line tmp;
int co=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
tmp.s=line[i].s;
tmp.e=line[j].s;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].s;
tmp.e=line[j].e;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].e;
tmp.e=line[j].s;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].e;
tmp.e=line[j].e;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
} }
if(co)
return true;
return false;
} int main(){ int T;
cin>>T;
while(T--){
int n;
cin>>n;
Point s,e;
for(int i=;i<=n;i++){
cin>>s.x>>s.y>>e.x>>e.y;
line[i].s=s;
line[i].e=e;
}
if(ac(n)){
puts("Yes!");
}
else{
puts("No!");
}
} }